2014-08-16 13:53:07 +00:00
|
|
|
from __future__ import (absolute_import, print_function, division)
|
2016-05-28 20:17:02 +00:00
|
|
|
|
2015-04-14 22:28:17 +00:00
|
|
|
import time
|
|
|
|
import traceback
|
2015-09-20 16:12:55 +00:00
|
|
|
import six
|
2016-05-28 20:17:02 +00:00
|
|
|
from io import BytesIO
|
2015-09-20 22:44:17 +00:00
|
|
|
from six.moves import urllib
|
2015-09-20 16:12:55 +00:00
|
|
|
|
2016-05-31 23:12:10 +00:00
|
|
|
from netlib import http, tcp, utils
|
2012-06-18 22:42:25 +00:00
|
|
|
|
2016-05-28 20:17:02 +00:00
|
|
|
|
2015-04-09 00:09:33 +00:00
|
|
|
class ClientConn(object):
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2012-06-19 02:23:22 +00:00
|
|
|
def __init__(self, address):
|
2014-02-05 20:34:14 +00:00
|
|
|
self.address = tcp.Address.wrap(address)
|
|
|
|
|
|
|
|
|
2015-04-09 00:09:33 +00:00
|
|
|
class Flow(object):
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2014-09-03 15:15:50 +00:00
|
|
|
def __init__(self, address, request):
|
|
|
|
self.client_conn = ClientConn(address)
|
|
|
|
self.request = request
|
2012-06-19 02:23:22 +00:00
|
|
|
|
|
|
|
|
2015-04-09 00:09:33 +00:00
|
|
|
class Request(object):
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-09-26 15:39:50 +00:00
|
|
|
def __init__(self, scheme, method, path, http_version, headers, content):
|
2012-06-19 02:23:22 +00:00
|
|
|
self.scheme, self.method, self.path = scheme, method, path
|
2015-09-26 15:39:50 +00:00
|
|
|
self.headers, self.content = headers, content
|
2015-09-20 22:44:17 +00:00
|
|
|
self.http_version = http_version
|
2012-06-19 02:23:22 +00:00
|
|
|
|
|
|
|
|
2012-06-18 22:42:25 +00:00
|
|
|
def date_time_string():
|
|
|
|
"""Return the current date and time formatted for a message header."""
|
|
|
|
WEEKS = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
|
2015-04-14 22:28:17 +00:00
|
|
|
MONTHS = [
|
|
|
|
None,
|
|
|
|
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
|
|
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
|
|
|
|
]
|
2012-06-18 22:42:25 +00:00
|
|
|
now = time.time()
|
2015-06-18 13:32:52 +00:00
|
|
|
year, month, day, hh, mm, ss, wd, y_, z_ = time.gmtime(now)
|
2012-06-18 22:42:25 +00:00
|
|
|
s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
|
2015-04-14 22:28:17 +00:00
|
|
|
WEEKS[wd],
|
|
|
|
day, MONTHS[month], year,
|
|
|
|
hh, mm, ss
|
|
|
|
)
|
2012-06-18 22:42:25 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
|
2015-04-09 00:09:33 +00:00
|
|
|
class WSGIAdaptor(object):
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2012-06-18 22:42:25 +00:00
|
|
|
def __init__(self, app, domain, port, sversion):
|
|
|
|
self.app, self.domain, self.port, self.sversion = app, domain, port, sversion
|
|
|
|
|
2014-09-03 15:15:50 +00:00
|
|
|
def make_environ(self, flow, errsoc, **extra):
|
2016-05-31 23:12:10 +00:00
|
|
|
path = utils.native(flow.request.path, "latin-1")
|
2015-09-20 22:44:17 +00:00
|
|
|
if '?' in path:
|
2016-05-31 23:12:10 +00:00
|
|
|
path_info, query = utils.native(path, "latin-1").split('?', 1)
|
2012-06-18 22:42:25 +00:00
|
|
|
else:
|
2015-09-20 22:44:17 +00:00
|
|
|
path_info = path
|
2012-06-18 22:42:25 +00:00
|
|
|
query = ''
|
|
|
|
environ = {
|
2015-05-27 09:18:54 +00:00
|
|
|
'wsgi.version': (1, 0),
|
2016-05-31 23:12:10 +00:00
|
|
|
'wsgi.url_scheme': utils.native(flow.request.scheme, "latin-1"),
|
2015-09-26 15:39:50 +00:00
|
|
|
'wsgi.input': BytesIO(flow.request.content or b""),
|
2015-05-27 09:18:54 +00:00
|
|
|
'wsgi.errors': errsoc,
|
|
|
|
'wsgi.multithread': True,
|
|
|
|
'wsgi.multiprocess': False,
|
|
|
|
'wsgi.run_once': False,
|
|
|
|
'SERVER_SOFTWARE': self.sversion,
|
2016-05-31 23:12:10 +00:00
|
|
|
'REQUEST_METHOD': utils.native(flow.request.method, "latin-1"),
|
2015-05-27 09:18:54 +00:00
|
|
|
'SCRIPT_NAME': '',
|
2015-09-20 22:44:17 +00:00
|
|
|
'PATH_INFO': urllib.parse.unquote(path_info),
|
2015-05-27 09:18:54 +00:00
|
|
|
'QUERY_STRING': query,
|
2016-05-31 23:12:10 +00:00
|
|
|
'CONTENT_TYPE': utils.native(flow.request.headers.get('Content-Type', ''), "latin-1"),
|
|
|
|
'CONTENT_LENGTH': utils.native(flow.request.headers.get('Content-Length', ''), "latin-1"),
|
2015-05-27 09:18:54 +00:00
|
|
|
'SERVER_NAME': self.domain,
|
|
|
|
'SERVER_PORT': str(self.port),
|
2016-05-31 23:12:10 +00:00
|
|
|
'SERVER_PROTOCOL': utils.native(flow.request.http_version, "latin-1"),
|
2012-06-18 22:42:25 +00:00
|
|
|
}
|
2014-01-04 21:57:50 +00:00
|
|
|
environ.update(extra)
|
2014-09-03 15:15:50 +00:00
|
|
|
if flow.client_conn.address:
|
2016-05-31 23:12:10 +00:00
|
|
|
environ["REMOTE_ADDR"] = utils.native(flow.client_conn.address.host, "latin-1")
|
2015-09-20 22:44:17 +00:00
|
|
|
environ["REMOTE_PORT"] = flow.client_conn.address.port
|
2012-06-18 22:42:25 +00:00
|
|
|
|
2014-09-03 15:15:50 +00:00
|
|
|
for key, value in flow.request.headers.items():
|
2016-05-31 23:12:10 +00:00
|
|
|
key = 'HTTP_' + utils.native(key, "latin-1").upper().replace('-', '_')
|
2012-06-18 22:42:25 +00:00
|
|
|
if key not in ('HTTP_CONTENT_TYPE', 'HTTP_CONTENT_LENGTH'):
|
|
|
|
environ[key] = value
|
|
|
|
return environ
|
|
|
|
|
|
|
|
def error_page(self, soc, headers_sent, s):
|
|
|
|
"""
|
|
|
|
Make a best-effort attempt to write an error page. If headers are
|
|
|
|
already sent, we just bung the error into the page.
|
|
|
|
"""
|
2015-11-12 02:27:10 +00:00
|
|
|
c = """
|
2012-06-18 22:42:25 +00:00
|
|
|
<html>
|
|
|
|
<h1>Internal Server Error</h1>
|
2015-11-12 02:27:10 +00:00
|
|
|
<pre>{err}"</pre>
|
2012-06-18 22:42:25 +00:00
|
|
|
</html>
|
2015-11-12 02:27:10 +00:00
|
|
|
""".format(err=s).strip().encode()
|
|
|
|
|
2012-06-18 22:42:25 +00:00
|
|
|
if not headers_sent:
|
2015-09-20 16:12:55 +00:00
|
|
|
soc.write(b"HTTP/1.1 500 Internal Server Error\r\n")
|
|
|
|
soc.write(b"Content-Type: text/html\r\n")
|
2015-11-12 02:27:10 +00:00
|
|
|
soc.write("Content-Length: {length}\r\n".format(length=len(c)).encode())
|
2015-09-20 16:12:55 +00:00
|
|
|
soc.write(b"\r\n")
|
2012-06-18 22:42:25 +00:00
|
|
|
soc.write(c)
|
|
|
|
|
2014-01-04 21:57:50 +00:00
|
|
|
def serve(self, request, soc, **env):
|
2012-06-18 22:42:25 +00:00
|
|
|
state = dict(
|
2015-05-27 09:18:54 +00:00
|
|
|
response_started=False,
|
|
|
|
headers_sent=False,
|
|
|
|
status=None,
|
|
|
|
headers=None
|
2012-06-18 22:42:25 +00:00
|
|
|
)
|
2015-04-14 22:28:17 +00:00
|
|
|
|
2012-06-18 22:42:25 +00:00
|
|
|
def write(data):
|
|
|
|
if not state["headers_sent"]:
|
2015-11-12 02:27:10 +00:00
|
|
|
soc.write("HTTP/1.1 {status}\r\n".format(status=state["status"]).encode())
|
2015-09-05 16:15:47 +00:00
|
|
|
headers = state["headers"]
|
|
|
|
if 'server' not in headers:
|
|
|
|
headers["Server"] = self.sversion
|
|
|
|
if 'date' not in headers:
|
|
|
|
headers["Date"] = date_time_string()
|
2015-09-20 16:12:55 +00:00
|
|
|
soc.write(bytes(headers))
|
|
|
|
soc.write(b"\r\n")
|
2012-06-18 22:42:25 +00:00
|
|
|
state["headers_sent"] = True
|
2012-06-26 02:49:23 +00:00
|
|
|
if data:
|
|
|
|
soc.write(data)
|
2012-06-18 22:42:25 +00:00
|
|
|
soc.flush()
|
|
|
|
|
|
|
|
def start_response(status, headers, exc_info=None):
|
|
|
|
if exc_info:
|
2015-09-20 22:44:17 +00:00
|
|
|
if state["headers_sent"]:
|
|
|
|
six.reraise(*exc_info)
|
2012-06-18 22:42:25 +00:00
|
|
|
elif state["status"]:
|
|
|
|
raise AssertionError('Response already started')
|
|
|
|
state["status"] = status
|
2016-05-31 23:12:10 +00:00
|
|
|
state["headers"] = http.Headers([[utils.always_bytes(k), utils.always_bytes(v)] for k, v in headers])
|
2015-09-20 22:44:17 +00:00
|
|
|
if exc_info:
|
|
|
|
self.error_page(soc, state["headers_sent"], traceback.format_tb(exc_info[2]))
|
|
|
|
state["headers_sent"] = True
|
2012-06-18 22:42:25 +00:00
|
|
|
|
2015-09-20 22:44:17 +00:00
|
|
|
errs = six.BytesIO()
|
2012-06-18 22:42:25 +00:00
|
|
|
try:
|
2015-04-14 22:28:17 +00:00
|
|
|
dataiter = self.app(
|
|
|
|
self.make_environ(request, errs, **env), start_response
|
|
|
|
)
|
2012-06-18 22:42:25 +00:00
|
|
|
for i in dataiter:
|
|
|
|
write(i)
|
|
|
|
if not state["headers_sent"]:
|
2015-09-20 16:12:55 +00:00
|
|
|
write(b"")
|
2016-05-28 12:36:43 +00:00
|
|
|
except Exception:
|
2012-06-18 22:42:25 +00:00
|
|
|
try:
|
|
|
|
s = traceback.format_exc()
|
2015-09-20 22:44:17 +00:00
|
|
|
errs.write(s.encode("utf-8", "replace"))
|
2012-06-18 22:42:25 +00:00
|
|
|
self.error_page(soc, state["headers_sent"], s)
|
2013-02-23 22:08:43 +00:00
|
|
|
except Exception: # pragma: no cover
|
|
|
|
pass
|
2012-06-18 22:42:25 +00:00
|
|
|
return errs.getvalue()
|