aboutsummaryrefslogtreecommitdiffstats
path: root/netlib
diff options
context:
space:
mode:
Diffstat (limited to 'netlib')
-rw-r--r--netlib/certutils.py2
-rw-r--r--netlib/http.py95
-rw-r--r--netlib/test.py2
-rw-r--r--netlib/wsgi.py7
4 files changed, 45 insertions, 61 deletions
diff --git a/netlib/certutils.py b/netlib/certutils.py
index d9b8ce57..0349bec7 100644
--- a/netlib/certutils.py
+++ b/netlib/certutils.py
@@ -96,7 +96,7 @@ def dummy_cert(ca, commonname, sans):
key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, raw)
cert = OpenSSL.crypto.X509()
- cert.gmtime_adj_notBefore(-3600)
+ cert.gmtime_adj_notBefore(-3600*48)
cert.gmtime_adj_notAfter(60 * 60 * 24 * 30)
cert.set_issuer(ca.get_subject())
cert.get_subject().CN = commonname
diff --git a/netlib/http.py b/netlib/http.py
index e160bd79..454edb3a 100644
--- a/netlib/http.py
+++ b/netlib/http.py
@@ -95,14 +95,17 @@ def read_headers(fp):
return odict.ODictCaseless(ret)
-def read_chunked(code, fp, limit):
+def read_chunked(fp, headers, limit, is_request):
"""
Read a chunked HTTP body.
May raise HttpError.
"""
+ # FIXME: Should check if chunked is the final encoding in the headers
+ # http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-16#section-3.3 3.3 2.
content = ""
total = 0
+ code = 400 if is_request else 502
while 1:
line = fp.readline(128)
if line == "":
@@ -151,35 +154,6 @@ def has_chunked_encoding(headers):
return "chunked" in [i.lower() for i in get_header_tokens(headers, "transfer-encoding")]
-def read_http_body(code, rfile, headers, all, limit):
- """
- Read an HTTP body:
-
- code: The HTTP error code to be used when raising HttpError
- rfile: A file descriptor to read from
- headers: An ODictCaseless object
- all: Should we read all data?
- limit: Size limit.
- """
- if has_chunked_encoding(headers):
- content = read_chunked(code, rfile, limit)
- elif "content-length" in headers:
- try:
- l = int(headers["content-length"][0])
- except ValueError:
- # FIXME: Not strictly correct - this could be from the server, in which
- # case we should send a 502.
- raise HttpError(code, "Invalid content-length header: %s"%headers["content-length"])
- if limit is not None and l > limit:
- raise HttpError(code, "HTTP Body too large. Limit is %s, content-length was %s"%(limit, l))
- content = rfile.read(l)
- elif all:
- content = rfile.read(limit if limit else -1)
- else:
- content = ""
- return content
-
-
def parse_http_protocol(s):
"""
Parse an HTTP protocol declaration. Returns a (major, minor) tuple, or
@@ -304,28 +278,6 @@ def connection_close(httpversion, headers):
return True
-
-def read_http_body_request(rfile, wfile, headers, httpversion, limit):
- """
- Read the HTTP body from a client request.
- """
- if "expect" in headers:
- # FIXME: Should be forwarded upstream
- if "100-continue" in headers['expect'] and httpversion >= (1, 1):
- wfile.write('HTTP/1.1 100 Continue\r\n')
- wfile.write('\r\n')
- del headers['expect']
- return read_http_body(400, rfile, headers, False, limit)
-
-
-def read_http_body_response(rfile, headers, limit):
- """
- Read the HTTP body from a server response.
- """
- all = "close" in get_header_tokens(headers, "connection")
- return read_http_body(500, rfile, headers, all, limit)
-
-
def parse_response_line(line):
parts = line.strip().split(" ", 2)
if len(parts) == 2: # handle missing message gracefully
@@ -359,10 +311,41 @@ def read_response(rfile, method, body_size_limit):
headers = read_headers(rfile)
if headers is None:
raise HttpError(502, "Invalid headers.")
- if code >= 100 and code <= 199:
- return read_response(rfile, method, body_size_limit)
- if method == "HEAD" or code == 204 or code == 304:
+
+ # Parse response body according to http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-16#section-3.3
+ if method == "HEAD" or (code in [204, 304]) or 100 <= code <= 199:
content = ""
else:
- content = read_http_body_response(rfile, headers, body_size_limit)
+ content = read_http_body(rfile, headers, body_size_limit, False)
return httpversion, code, msg, headers, content
+
+
+def read_http_body(rfile, headers, limit, is_request):
+ """
+ Read an HTTP message body:
+
+ rfile: A file descriptor to read from
+ headers: An ODictCaseless object
+ limit: Size limit.
+ is_request: True if the body to read belongs to a request, False otherwise
+ """
+ if has_chunked_encoding(headers):
+ content = read_chunked(rfile, headers, limit, is_request)
+ elif "content-length" in headers:
+ try:
+ l = int(headers["content-length"][0])
+ if l < 0:
+ raise ValueError()
+ except ValueError:
+ raise HttpError(400 if is_request else 502, "Invalid content-length header: %s"%headers["content-length"])
+ if limit is not None and l > limit:
+ raise HttpError(400 if is_request else 509, "HTTP Body too large. Limit is %s, content-length was %s"%(limit, l))
+ content = rfile.read(l)
+ elif is_request:
+ content = ""
+ else:
+ content = rfile.read(limit if limit else -1)
+ not_done = rfile.read(1)
+ if not_done:
+ raise HttpError(400 if is_request else 509, "HTTP Body too large. Limit is %s," % limit)
+ return content \ No newline at end of file
diff --git a/netlib/test.py b/netlib/test.py
index 0c36da6a..2209ebc3 100644
--- a/netlib/test.py
+++ b/netlib/test.py
@@ -18,7 +18,7 @@ class ServerTestBase:
handler = None
addr = ("localhost", 0)
use_ipv6 = False
-
+
@classmethod
def setupAll(cls):
cls.q = Queue.Queue()
diff --git a/netlib/wsgi.py b/netlib/wsgi.py
index dffc2ace..647cb899 100644
--- a/netlib/wsgi.py
+++ b/netlib/wsgi.py
@@ -33,7 +33,7 @@ class WSGIAdaptor:
def __init__(self, app, domain, port, sversion):
self.app, self.domain, self.port, self.sversion = app, domain, port, sversion
- def make_environ(self, request, errsoc):
+ def make_environ(self, request, errsoc, **extra):
if '?' in request.path:
path_info, query = request.path.split('?', 1)
else:
@@ -59,6 +59,7 @@ class WSGIAdaptor:
# FIXME: We need to pick up the protocol read from the request.
'SERVER_PROTOCOL': "HTTP/1.1",
}
+ environ.update(extra)
if request.client_conn.address:
environ["REMOTE_ADDR"], environ["REMOTE_PORT"] = request.client_conn.address
@@ -86,7 +87,7 @@ class WSGIAdaptor:
soc.write("\r\n")
soc.write(c)
- def serve(self, request, soc):
+ def serve(self, request, soc, **env):
state = dict(
response_started = False,
headers_sent = False,
@@ -123,7 +124,7 @@ class WSGIAdaptor:
errs = cStringIO.StringIO()
try:
- dataiter = self.app(self.make_environ(request, errs), start_response)
+ dataiter = self.app(self.make_environ(request, errs, **env), start_response)
for i in dataiter:
write(i)
if not state["headers_sent"]: