def _encode_data(self, data): body = None if self.method in ENCODE_URL_METHODS: self.files = None self._encode_url(data) elif isinstance(data, bytes): assert self.files is None, ('data cannot be bytes when files are ' 'present') body = data elif is_string(data): assert self.files is None, ('data cannot be string when files are ' 'present') body = to_bytes(data, self.charset) elif data or self.files: if self.files: body, content_type = self._encode_files(data) else: body, content_type = self._encode_params(data) # set files to None, Important! self.files = None self.headers['Content-Type'] = content_type if body: self.headers['content-length'] = str(len(body)) elif 'expect' not in self.headers: self.headers.pop('content-length', None) self.headers.pop('content-type', None) return body
def __call__(self, html, fields, context): html.data('fields', len(fields)) for name, value in iteritems(fields): if is_string(value): html.append(Html('div', value, field=name)) else: html.append(Html('div', field=name, value=value))
def __call__(self, html, fields, context): html.data("fields", len(fields)) for name, value in iteritems(fields): if is_string(value): html.append(Html("div", value, field=name)) else: html.append(Html("div", field=name, value=value))
def apply_content(self, elem, content): elem.data({'id': content.id, 'content_type': content.content_type}) for name, value in chain( (('title', content.title), ('keywords', content.keywords)), iteritems(content.data)): if not is_string(value): elem.data(name, value) elif value: elem.append(Html('div', value, field=name))
def apply_content(self, elem, content): elem.data({'id': content.id, 'content_type': content.content_type}) for name, value in chain((('title', content.title), ('keywords', content.keywords)), iteritems(content.data)): if not is_string(value): elem.data(name, value) elif value: elem.append(Html('div', value, field=name))
def append(self, child): if child: if is_string(child): path = self.absolute_path(child) script = Html('script', src=path, type='application/javascript') self.children[script] = script return script elif isinstance(child, Html) and child.tag == 'script': self.children[child] = child
def _encode_files(self): fields = [] for field, val in mapping_iterator(self.data or ()): if (is_string(val) or isinstance(val, bytes) or not hasattr(val, '__iter__')): val = [val] for v in val: if v is not None: if not isinstance(v, bytes): v = str(v) fields.append((field.decode('utf-8') if isinstance(field, bytes) else field, v.encode('utf-8') if isinstance(v, str) else v)) for (k, v) in mapping_iterator(self.files): # support for explicit filename ft = None if isinstance(v, (tuple, list)): if len(v) == 2: fn, fp = v else: fn, fp, ft = v else: fn = guess_filename(v) or k fp = v if isinstance(fp, bytes): fp = BytesIO(fp) elif is_string(fp): fp = StringIO(fp) if ft: new_v = (fn, fp.read(), ft) else: new_v = (fn, fp.read()) fields.append((k, new_v)) # return encode_multipart_formdata(fields, charset=self.charset)
def append(self, child): '''add a new link to the javascript links. :param child: a ``string`` representing an absolute path to the script or relative path (does not start with ``http`` or ``/``), in which case the :attr:`Media.media_path` attribute is prepended. ''' if child: if is_string(child): path = self.absolute_path(child) script = Html('script', src=path, type='application/javascript') self.children[script] = script return script elif isinstance(child, Html) and child.tag == 'script': self.children[child] = child
def encode_body(self): '''Encode body or url if the :attr:`method` does not have body. Called by the :meth:`encode` method. ''' body = None if self.method in ENCODE_URL_METHODS: self.files = None self._encode_url(self.data) elif isinstance(self.data, bytes): assert self.files is None, ('data cannot be bytes when files are ' 'present') body = self.data elif is_string(self.data): assert self.files is None, ('data cannot be string when files are ' 'present') body = to_bytes(self.data, self.charset) elif self.data or self.files: if self.files: body, content_type = self._encode_files() else: body, content_type = self._encode_params() self.headers['Content-Type'] = content_type return body
def wsgi_environ(stream, address, client_address, headers, server_software=None, https=False, extra=None): protocol = stream.protocol() parser = stream.parser request_headers = stream.headers raw_uri = parser.get_url() request_uri = urlparse(raw_uri) # # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2 # If Request-URI is an absoluteURI, the host is part of the Request-URI. # Any Host header field value in the request MUST be ignored if request_uri.scheme: url_scheme = request_uri.scheme host = request_uri.netloc else: url_scheme = 'https' if https else 'http' host = None # environ = { "wsgi.input": stream, "wsgi.errors": sys.stderr, "wsgi.version": (1, 0), "wsgi.run_once": False, "wsgi.multithread": False, "wsgi.multiprocess": False, "SERVER_SOFTWARE": server_software or pulsar.SERVER_SOFTWARE, "REQUEST_METHOD": native_str(parser.get_method()), "QUERY_STRING": parser.get_query_string(), "RAW_URI": raw_uri, "SERVER_PROTOCOL": protocol, "CONTENT_TYPE": '' } forward = client_address script_name = os.environ.get("SCRIPT_NAME", "") for header, value in request_headers: header = header.lower() if header in HOP_HEADERS: headers[header] = value if header == 'x-forwarded-for': forward = value elif header == "x-forwarded-protocol" and value == "ssl": url_scheme = "https" elif header == "x-forwarded-ssl" and value == "on": url_scheme = "https" elif header == "host" and not host: host = value elif header == "script_name": script_name = value elif header == "content-type": environ['CONTENT_TYPE'] = value continue elif header == "content-length": environ['CONTENT_LENGTH'] = value continue key = 'HTTP_' + header.upper().replace('-', '_') environ[key] = value environ['wsgi.url_scheme'] = url_scheme if url_scheme == 'https': environ['HTTPS'] = 'on' if is_string(forward): # we only took the last one # http://en.wikipedia.org/wiki/X-Forwarded-For if forward.find(",") >= 0: forward = forward.rsplit(",", 1)[1].strip() remote = forward.split(":") if len(remote) < 2: remote.append('80') else: remote = forward environ['REMOTE_ADDR'] = remote[0] environ['REMOTE_PORT'] = str(remote[1]) if not host and protocol == 'HTTP/1.0': host = format_address(address) if host: host = host_and_port_default(url_scheme, host) environ['SERVER_NAME'] = socket.getfqdn(host[0]) environ['SERVER_PORT'] = host[1] path_info = request_uri.path if path_info is not None: if script_name: path_info = path_info.split(script_name, 1)[1] environ['PATH_INFO'] = unquote(path_info) environ['SCRIPT_NAME'] = script_name if extra: environ.update(extra) return environ
def wsgi_environ(stream, address, client_address, headers, server_software=None, https=False, extra=None): protocol = stream.protocol() parser = stream.parser request_headers = stream.headers raw_uri = parser.get_url() request_uri = urlparse(raw_uri) # # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2 # If Request-URI is an absoluteURI, the host is part of the Request-URI. # Any Host header field value in the request MUST be ignored if request_uri.scheme: url_scheme = request_uri.scheme host = request_uri.netloc else: url_scheme = 'https' if https else 'http' host = None # environ = {"wsgi.input": stream, "wsgi.errors": sys.stderr, "wsgi.version": (1, 0), "wsgi.run_once": False, "wsgi.multithread": False, "wsgi.multiprocess": False, "SERVER_SOFTWARE": server_software or pulsar.SERVER_SOFTWARE, "REQUEST_METHOD": native_str(parser.get_method()), "QUERY_STRING": parser.get_query_string(), "RAW_URI": raw_uri, "SERVER_PROTOCOL": protocol, "CONTENT_TYPE": ''} forward = client_address script_name = os.environ.get("SCRIPT_NAME", "") for header, value in request_headers: header = header.lower() if header in HOP_HEADERS: headers[header] = value if header == 'x-forwarded-for': forward = value elif header == "x-forwarded-protocol" and value == "ssl": url_scheme = "https" elif header == "x-forwarded-ssl" and value == "on": url_scheme = "https" elif header == "host" and not host: host = value elif header == "script_name": script_name = value elif header == "content-type": environ['CONTENT_TYPE'] = value continue elif header == "content-length": environ['CONTENT_LENGTH'] = value continue key = 'HTTP_' + header.upper().replace('-', '_') environ[key] = value environ['wsgi.url_scheme'] = url_scheme if url_scheme == 'https': environ['HTTPS'] = 'on' if is_string(forward): # we only took the last one # http://en.wikipedia.org/wiki/X-Forwarded-For if forward.find(",") >= 0: forward = forward.rsplit(",", 1)[1].strip() remote = forward.split(":") if len(remote) < 2: remote.append('80') else: remote = forward environ['REMOTE_ADDR'] = remote[0] environ['REMOTE_PORT'] = str(remote[1]) if not host and protocol == 'HTTP/1.0': host = format_address(address) if host: host = host_and_port_default(url_scheme, host) environ['SERVER_NAME'] = socket.getfqdn(host[0]) environ['SERVER_PORT'] = host[1] path_info = parser.get_path() if path_info is not None: if script_name: path_info = path_info.split(script_name, 1)[1] environ['PATH_INFO'] = unquote(path_info) environ['SCRIPT_NAME'] = script_name if extra: environ.update(extra) return environ