This commit is contained in:
Thomas Kriechbaumer 2015-07-22 13:01:24 +02:00
parent faf17d3d60
commit 657973eca3
3 changed files with 31 additions and 32 deletions

View File

@ -9,10 +9,18 @@ from netlib import odict, utils, tcp, http
from .. import status_codes
from ..exceptions import *
class TCPHandler(object):
def __init__(self, rfile, wfile=None):
self.rfile = rfile
self.wfile = wfile
class HTTP1Protocol(object):
def __init__(self, tcp_handler):
self.tcp_handler = tcp_handler
def __init__(self, tcp_handler=None, rfile=None, wfile=None):
if tcp_handler:
self.tcp_handler = tcp_handler
else:
self.tcp_handler = TCPHandler(rfile, wfile)
def read_request(self, include_body=True, body_size_limit=None, allow_empty=False):
@ -31,7 +39,7 @@ class HTTP1Protocol(object):
Raises:
HttpError: If the input is invalid.
"""
httpversion, host, port, scheme, method, path, headers, content = (
httpversion, host, port, scheme, method, path, headers, body = (
None, None, None, None, None, None, None, None)
request_line = self._get_request_line()
@ -56,7 +64,7 @@ class HTTP1Protocol(object):
400,
"Bad HTTP request line: %s" % repr(request_line)
)
elif method.upper() == 'CONNECT':
elif method == 'CONNECT':
form_in = "authority"
r = self._parse_init_connect(request_line)
if not r:
@ -64,8 +72,8 @@ class HTTP1Protocol(object):
400,
"Bad HTTP request line: %s" % repr(request_line)
)
host, port, _ = r
return http.ConnectRequest(host, port)
host, port, httpversion = r
path = None
else:
form_in = "absolute"
r = self._parse_init_proxy(request_line)
@ -81,7 +89,7 @@ class HTTP1Protocol(object):
raise HttpError(400, "Invalid headers")
expect_header = headers.get_first("expect", "").lower()
if expect_header == "100-continue" and httpversion >= (1, 1):
if expect_header == "100-continue" and httpversion == (1, 1):
self.tcp_handler.wfile.write(
'HTTP/1.1 100 Continue\r\n'
'\r\n'
@ -90,7 +98,7 @@ class HTTP1Protocol(object):
del headers['expect']
if include_body:
content = self.read_http_body(
body = self.read_http_body(
headers,
body_size_limit,
method,
@ -107,7 +115,7 @@ class HTTP1Protocol(object):
path,
httpversion,
headers,
content
body
)

View File

@ -5,7 +5,7 @@ import string
import sys
import urlparse
from .. import utils
from .. import utils, odict
class Request(object):
@ -37,6 +37,10 @@ class Request(object):
def __repr__(self):
return "Request(%s - %s, %s)" % (self.method, self.host, self.path)
@property
def content(self):
return self.body
class EmptyRequest(Request):
def __init__(self):
@ -47,22 +51,8 @@ class EmptyRequest(Request):
host="",
port="",
path="",
httpversion="",
headers="",
body="",
)
class ConnectRequest(Request):
def __init__(self, host, port):
super(ConnectRequest, self).__init__(
form_in="authority",
method="CONNECT",
scheme="",
host=host,
port=port,
path="",
httpversion="",
headers="",
httpversion=(0, 0),
headers=odict.ODictCaseless(),
body="",
)
@ -91,6 +81,10 @@ class Response(object):
def __repr__(self):
return "Response(%s - %s)" % (self.status_code, self.msg)
@property
def content(self):
return self.body
def is_valid_port(port):
if not 0 <= port <= 65535:

View File

@ -8,12 +8,9 @@ from ... import tutils, tservers
def mock_protocol(data='', chunked=False):
class TCPHandlerMock(object):
pass
tcp_handler = TCPHandlerMock()
tcp_handler.rfile = cStringIO.StringIO(data)
tcp_handler.wfile = cStringIO.StringIO()
return HTTP1Protocol(tcp_handler)
rfile = cStringIO.StringIO(data)
wfile = cStringIO.StringIO()
return HTTP1Protocol(rfile=rfile, wfile=wfile)