def __call__(self, env, start_response, app_instance = None): app_instance = app_instance or self http_request = HTTPRequest(env) http_request._parse_body() from gale import web handler = Application.__call__(app_instance, http_request, is_wsgi = True) if hasattr(handler, '_new_cookie'): for cookie in handler._new_cookie.values(): handler.add_header('Set-Cookie', cookie.OutputString()) status = "%s %s" % (handler._status_code, handler._status_mess) _body = b''.join(handler._push_buffer) if http_request.method == 'HEAD': # 如果是HEAD请求的话则不返回主体内容 _body = b'' if handler._status_code != 304: handler.set_header('Content-Length', len(_body)) headers = [(native_str(_k), str(native_str(_v))) for _k, _v in handler._headers.get_headers_items()] # 在python2.7 中,不允许headers是unicode write = start_response(native_str(status), headers) write(_body) handler.log_print() handler.on_finish() return []
def __init__(self, template_string, name="<string>", loader=None, compress_whitespace=None, autoescape=_UNSET): self.name = name if compress_whitespace is None: compress_whitespace = name.endswith(".html") or \ name.endswith(".js") if autoescape is not _UNSET: self.autoescape = autoescape elif loader: self.autoescape = loader.autoescape else: self.autoescape = _DEFAULT_AUTOESCAPE self.namespace = loader.namespace if loader else {} reader = _TemplateReader(name, escape.native_str(template_string)) self.file = _File(self, _parse(reader, self)) self.code = self._generate_python(loader, compress_whitespace) self.loader = loader try: # Under python2.5, the fake filename used here must match # the module name used in __name__ below. # The dont_inherit flag prevents template.py's future imports # from being applied to the generated code. self.compiled = compile( escape.to_unicode(self.code), "%s.generated.py" % self.name.replace('.', '_'), "exec", dont_inherit=True) except Exception: formatted_code = _format_code(self.code).rstrip() gen_log.error("%s code:\n%s", self.name, formatted_code) raise
def __init__(self, method, uri, version, headers, body, connection, real_ip = True): self.method = native_str(method) self.uri = native_str(uri) self.version = native_str(version) self.version_num = self.__version_num() self.headers = headers self.body = body _urlparse = urlsplit(self.uri) self.path = _urlparse.path self.host = headers.get('Host', '').strip() self.host, self.port = (self.host.split(':') + ['80'])[:2] self.query = _urlparse.query self.connection = connection self._start_time = time() self.real_ip = real_ip self.cookies = self.__get_cookies() self.files = {} self._body_arguments = {} self.client_ip = self.__remote_ip()
def _parse_headers(headers): """把http头信息组织到一个字典中去""" _headers = {} _last_key = None headers = native_str(headers) assert not isinstance(headers, dict) for header_line in headers.split('\n'): header_line = header_line.rstrip('\r') if header_line.startswith('\x20') or header_line.startswith('\t') : # 因为http首部是允许换行的, 所以如果这一行是以空格或制表符开头的,需要将信息加到之前那行 _headers[_last_key] += ' ' + header_line.lstrip() continue else: _header_name, _header_value = header_line.split(':', 1) _headers[_header_name] = _header_value.strip() _last_key = _header_name return _headers
def urlunquote(param): if param == None: return param param = unquote_plus(escape.native_str(param)) return escape.param_decode(param)