Пример #1
0
    def from_values(cls, path='/', base_url=None, query_string=None, **options):
        """Create a new request object based on the values provided.  If
        environ is given missing values are filled from there.  This method is
        useful for small scripts when you need to simulate a request from an URL.
        Do not use this method for unittesting, there is a full featured client
        object in `werkzeug.test` that allows to create multipart requests
        etc.

        This accepts the same options as the `create_environ` function from the
        utils module and additionally an `environ` parameter that can contain
        values which will override the values from dict returned by
        `create_environ`.

        Additionally a dict passed to `query_string` will be encoded in the
        request class charset.

        :return: request object
        """
        if isinstance(query_string, dict):
            query_string = url_encode(query_string, cls.charset)
        environ = options.pop('environ', None)
        new_env = create_environ(path, base_url, query_string, **options)
        result = {}
        if environ is not None:
            result.update(environ)
        result.update(new_env)
        return cls(result)
Пример #2
0
    def build(self, values):
        """Assembles the relative url for that rule and the subdomain.
        If building doesn't work for some reasons `None` is returned.

        :internal:
        """
        tmp = []
        add = tmp.append
        processed = set(self.arguments)
        for is_dynamic, data in self._trace:
            if is_dynamic:
                try:
                    add(self._converters[data].to_url(values[data]))
                except ValidationError:
                    return
                processed.add(data)
            else:
                add(data)
        subdomain, url = (u''.join(tmp)).split('|', 1)

        query_vars = {}
        for key in set(values) - processed:
            query_vars[key] = unicode(values[key])
        if query_vars:
            url += '?' + url_encode(query_vars, self.map.charset)

        return subdomain, url
Пример #3
0
    def get_environ(self):
        """Return the built environ."""
        input_stream = self.input_stream
        content_length = self.content_length
        content_type = self.content_type

        if input_stream is not None:
            start_pos = input_stream.tell()
            input_stream.seek(0, 2)
            end_pos = input_stream.tell()
            input_stream.seek(start_pos)
            content_length = end_pos - start_pos
        elif content_type == 'multipart/form-data':
            values = CombinedMultiDict([self.form, self.files])
            input_stream, content_length, boundary = \
                stream_encode_multipart(values, charset=self.charset)
            content_type += '; boundary="%s"' % boundary
        elif content_type == 'application/x-www-form-urlencoded':
            values = url_encode(self.form, charset=self.charset)
            content_length = len(values)
            input_stream = StringIO(values)
        else:
            input_stream = _empty_stream

        result = {}
        if self.environ_base:
            result.update(self.environ_base)

        def _encode(x):
            if isinstance(x, unicode):
                return x.encode(self.charset)
            return x

        result.update({
            'REQUEST_METHOD':       self.method,
            'SCRIPT_NAME':          _encode(self.script_root),
            'PATH_INFO':            _encode(self.path),
            'QUERY_STRING':         self.query_string,
            'SERVER_NAME':          self.server_name,
            'SERVER_PORT':          str(self.server_port),
            'HTTP_HOST':            self.host,
            'SERVER_PROTOCOL':      self.server_protocol,
            'CONTENT_TYPE':         content_type or '',
            'CONTENT_LENGTH':       str(content_length or '0'),
            'wsgi.version':         self.wsgi_version,
            'wsgi.url_scheme':      self.url_scheme,
            'wsgi.input':           input_stream,
            'wsgi.errors':          self.errors_stream,
            'wsgi.multithread':     self.multithread,
            'wsgi.multiprocess':    self.multiprocess,
            'wsgi.run_once':        self.run_once
        })
        for key, value in self.headers.to_list(self.charset):
            result['HTTP_%s' % key.upper().replace('-', '_')] = value
        if self.environ_overrides:
            result.update(self.environ_overrides)
        return result
Пример #4
0
 def from_values(cls, path='/', base_url=None, query_string=None, **options):
     """
     Create a new request object based on the values provided.  If
     environ is given missing values are filled from there.  This method
     is useful for small scripts when you need to simulate a request
     from an url.  Do not use this method for unittesting, there is a
     full featured client object in `werkzeug.test` that allows to create
     multipart requests etc.
     """
     if isinstance(query_string, dict):
         query_string = url_encode(query_string, cls.charset)
     environ = options.pop('environ', None)
     new_env = create_environ(path, base_url, query_string, **options)
     result = {}
     if environ is not None:
         result.update(environ)
     result.update(new_env)
     return cls(result)
Пример #5
0
 def _get_query_string(self):
     if self._query_string is None:
         if self._args is not None:
             return url_encode(self._args, charset=self.charset)
         return ''
     return self._query_string