def create(req, sock, client, server, cfg): resp = Response(req, sock, cfg) # set initial environ environ = default_environ(req, sock, cfg) # default variables host = None url_scheme = "https" if cfg.is_ssl else "http" script_name = os.environ.get("SCRIPT_NAME", "") # set secure_headers secure_headers = cfg.secure_scheme_headers if client and not isinstance(client, string_types): if ('*' not in cfg.forwarded_allow_ips and client[0] not in cfg.forwarded_allow_ips): secure_headers = {} # add the headers tot the environ for hdr_name, hdr_value in req.headers: if hdr_name == "EXPECT": # handle expect if hdr_value.lower() == "100-continue": sock.send(b"HTTP/1.1 100 Continue\r\n\r\n") elif secure_headers and (hdr_name in secure_headers and hdr_value == secure_headers[hdr_name]): url_scheme = "https" elif hdr_name == 'HOST': host = hdr_value elif hdr_name == "SCRIPT_NAME": script_name = hdr_value elif hdr_name == "CONTENT-TYPE": environ['CONTENT_TYPE'] = hdr_value continue elif hdr_name == "CONTENT-LENGTH": environ['CONTENT_LENGTH'] = hdr_value continue key = 'HTTP_' + hdr_name.replace('-', '_') if key in environ: hdr_value = "%s,%s" % (environ[key], hdr_value) environ[key] = hdr_value # set the url schejeme environ['wsgi.url_scheme'] = url_scheme # set the REMOTE_* keys in environ # authors should be aware that REMOTE_HOST and REMOTE_ADDR # may not qualify the remote addr: # http://www.ietf.org/rfc/rfc3875 if isinstance(client, string_types): environ['REMOTE_ADDR'] = client elif isinstance(client, binary_type): environ['REMOTE_ADDR'] = str(client) else: environ['REMOTE_ADDR'] = client[0] environ['REMOTE_PORT'] = str(client[1]) # handle the SERVER_* # Normally only the application should use the Host header but since the # WSGI spec doesn't support unix sockets, we are using it to create # viable SERVER_* if possible. if isinstance(server, string_types): server = server.split(":") if len(server) == 1: # unix socket if host and host is not None: server = host.split(':') if len(server) == 1: if url_scheme == "http": server.append(80), elif url_scheme == "https": server.append(443) else: server.append('') else: # no host header given which means that we are not behind a # proxy, so append an empty port. server.append('') environ['SERVER_NAME'] = server[0] environ['SERVER_PORT'] = str(server[1]) # set the path and script name path_info = req.path if script_name: path_info = path_info.split(script_name, 1)[1] environ['PATH_INFO'] = unquote_to_wsgi_str(path_info) environ['SCRIPT_NAME'] = script_name # override the environ with the correct remote and server address if # we are behind a proxy using the proxy protocol. environ.update(proxy_environ(req)) return resp, environ
def create(req, sock, client, server, cfg): resp = Response(req, sock) environ = default_environ(req, sock, cfg) # authors should be aware that REMOTE_HOST and REMOTE_ADDR # may not qualify the remote addr: # http://www.ietf.org/rfc/rfc3875 forward = client or "127.0.0.1" url_scheme = "https" if cfg.is_ssl else "http" script_name = os.environ.get("SCRIPT_NAME", "") secure_headers = cfg.secure_scheme_headers x_forwarded_for_header = cfg.x_forwarded_for_header if '*' not in cfg.forwarded_allow_ips and client\ and client[0] not in cfg.forwarded_allow_ips: x_forwarded_for_header = None secure_headers = {} for hdr_name, hdr_value in req.headers: if hdr_name == "EXPECT": # handle expect if hdr_value.lower() == "100-continue": sock.send(b"HTTP/1.1 100 Continue\r\n\r\n") elif x_forwarded_for_header and hdr_name == x_forwarded_for_header: forward = hdr_value elif secure_headers and (hdr_name.upper() in secure_headers and hdr_value == secure_headers[hdr_name.upper()]): url_scheme = "https" elif hdr_name == "HOST": server = hdr_value elif hdr_name == "SCRIPT_NAME": script_name = hdr_value elif hdr_name == "CONTENT-TYPE": environ['CONTENT_TYPE'] = hdr_value continue elif hdr_name == "CONTENT-LENGTH": environ['CONTENT_LENGTH'] = hdr_value continue key = 'HTTP_' + hdr_name.replace('-', '_') if key in environ: hdr_value = "%s,%s" % (environ[key], hdr_value) environ[key] = hdr_value environ['wsgi.url_scheme'] = url_scheme if isinstance(forward, string_types): # we only took the last one # http://en.wikipedia.org/wiki/X-Forwarded-For if forward.find(",") >= 0: forward = forward.rsplit(",", 1)[1].strip() # find host and port on ipv6 address if '[' in forward and ']' in forward: host = forward.split(']')[0][1:].lower() elif ":" in forward and forward.count(":") == 1: host = forward.split(":")[0].lower() else: host = forward forward = forward.split(']')[-1] if ":" in forward and forward.count(":") == 1: port = forward.split(':', 1)[1] else: port = 80 remote = (host, port) else: remote = forward environ['REMOTE_ADDR'] = remote[0] environ['REMOTE_PORT'] = str(remote[1]) if isinstance(server, string_types): server = server.split(":") if len(server) == 1: if url_scheme == "http": server.append("80") elif url_scheme == "https": server.append("443") else: server.append('') environ['SERVER_NAME'] = server[0] environ['SERVER_PORT'] = str(server[1]) path_info = req.path if script_name: path_info = path_info.split(script_name, 1)[1] environ['PATH_INFO'] = unquote_to_wsgi_str(path_info) environ['SCRIPT_NAME'] = script_name environ.update(proxy_environ(req)) return resp, environ
def create(req, sock, client, server, cfg): resp = Response(req, sock, cfg) environ = default_environ(req, sock, cfg) # authors should be aware that REMOTE_HOST and REMOTE_ADDR # may not qualify the remote addr: # http://www.ietf.org/rfc/rfc3875 forward = client or "127.0.0.1" url_scheme = "https" if cfg.is_ssl else "http" script_name = os.environ.get("SCRIPT_NAME", "") secure_headers = cfg.secure_scheme_headers x_forwarded_for_header = cfg.x_forwarded_for_header if '*' not in cfg.forwarded_allow_ips and client\ and client[0] not in cfg.forwarded_allow_ips: x_forwarded_for_header = None secure_headers = {} for hdr_name, hdr_value in req.headers: if hdr_name == "EXPECT": # handle expect if hdr_value.lower() == "100-continue": sock.send(b"HTTP/1.1 100 Continue\r\n\r\n") elif x_forwarded_for_header and hdr_name == x_forwarded_for_header: forward = hdr_value elif secure_headers and (hdr_name.upper() in secure_headers and hdr_value == secure_headers[hdr_name.upper()]): url_scheme = "https" elif hdr_name == "HOST": server = hdr_value elif hdr_name == "SCRIPT_NAME": script_name = hdr_value elif hdr_name == "CONTENT-TYPE": environ['CONTENT_TYPE'] = hdr_value continue elif hdr_name == "CONTENT-LENGTH": environ['CONTENT_LENGTH'] = hdr_value continue key = 'HTTP_' + hdr_name.replace('-', '_') if key in environ: hdr_value = "%s,%s" % (environ[key], hdr_value) environ[key] = hdr_value environ['wsgi.url_scheme'] = url_scheme if isinstance(forward, string_types): # we only took the last one # http://en.wikipedia.org/wiki/X-Forwarded-For if forward.find(",") >= 0: forward = forward.rsplit(",", 1)[1].strip() # find host and port on ipv6 address if '[' in forward and ']' in forward: host = forward.split(']')[0][1:].lower() elif ":" in forward and forward.count(":") == 1: host = forward.split(":")[0].lower() else: host = forward forward = forward.split(']')[-1] if ":" in forward and forward.count(":") == 1: port = forward.split(':', 1)[1] else: port = 80 remote = (host, port) else: remote = forward environ['REMOTE_ADDR'] = remote[0] environ['REMOTE_PORT'] = str(remote[1]) if isinstance(server, string_types): server = server.split(":") if len(server) == 1: if url_scheme == "http": server.append("80") elif url_scheme == "https": server.append("443") else: server.append('') environ['SERVER_NAME'] = server[0] environ['SERVER_PORT'] = str(server[1]) path_info = req.path if script_name: path_info = path_info.split(script_name, 1)[1] environ['PATH_INFO'] = unquote_to_wsgi_str(path_info) environ['SCRIPT_NAME'] = script_name environ.update(proxy_environ(req)) return resp, environ