add Request class and unify read_request interface

This commit is contained in:
Thomas Kriechbaumer 2015-07-17 09:37:57 +02:00
parent 808b294865
commit 4617ab8a3a
5 changed files with 59 additions and 24 deletions

View File

@ -1,2 +1,3 @@
from . import *
from exceptions import *
from semantics import *

View File

@ -11,25 +11,10 @@ from ..exceptions import *
class HTTP1Protocol(object):
# TODO: make this a regular class - just like Response
Request = collections.namedtuple(
"Request",
[
"form_in",
"method",
"scheme",
"host",
"port",
"path",
"httpversion",
"headers",
"content"
]
)
def __init__(self, tcp_handler):
self.tcp_handler = tcp_handler
def get_request_line(self):
"""
Get a line, possibly preceded by a blank.
@ -40,6 +25,7 @@ class HTTP1Protocol(object):
line = self.tcp_handler.rfile.readline()
return line
def read_headers(self):
"""
Read a set of headers.
@ -175,6 +161,7 @@ class HTTP1Protocol(object):
return None
return host, port, httpversion
@classmethod
def parse_init_proxy(self, line):
v = self.parse_init(line)
@ -188,6 +175,7 @@ class HTTP1Protocol(object):
scheme, host, port, path = parts
return method, scheme, host, port, path, httpversion
@classmethod
def parse_init_http(self, line):
"""
@ -425,7 +413,7 @@ class HTTP1Protocol(object):
True
)
return self.Request(
return http.Request(
form_in,
method,
scheme,

View File

@ -187,11 +187,25 @@ class HTTP2Protocol(object):
self._create_body(body, stream_id)))
def read_response(self, *args):
stream_id_, headers, body = self._receive_transmission()
return http.Response("HTTP/2", headers[':status'], "", headers, body)
stream_id, headers, body = self._receive_transmission()
response = http.Response("HTTP/2", headers[':status'], "", headers, body)
response.stream_id = stream_id
return response
def read_request(self):
return self._receive_transmission()
stream_id, headers, body = self._receive_transmission()
form_in = ""
method = headers.get(':method', '')
scheme = headers.get(':scheme', '')
host = headers.get(':host', '')
port = '' # TODO: parse port number?
path = headers.get(':path', '')
request = http.Request(form_in, method, scheme, host, port, path, "HTTP/2", headers, body)
request.stream_id = stream_id
return request
def _receive_transmission(self):
body_expected = True

View File

@ -7,6 +7,37 @@ import urlparse
from .. import utils
class Request(object):
def __init__(
self,
form_in,
method,
scheme,
host,
port,
path,
httpversion,
headers,
content,
):
self.form_in = form_in
self.method = method
self.scheme = scheme
self.host = host
self.port = port
self.path = path
self.httpversion = httpversion
self.headers = headers
self.content = content
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __repr__(self):
return "Request(%s - %s, %s)" % (self.method, self.host, self.path)
class Response(object):
def __init__(

View File

@ -278,6 +278,7 @@ class TestReadEmptyResponse(tservers.ServerTestBase):
resp = protocol.read_response()
assert resp.stream_id
assert resp.httpversion == "HTTP/2"
assert resp.status_code == "200"
assert resp.msg == ""
@ -303,11 +304,11 @@ class TestReadRequest(tservers.ServerTestBase):
c.convert_to_ssl()
protocol = http2.HTTP2Protocol(c, is_server=True)
stream_id, headers, body = protocol.read_request()
resp = protocol.read_request()
assert stream_id
assert headers == {':method': 'GET', ':path': '/', ':scheme': 'https'}
assert body == b'foobar'
assert resp.stream_id
assert resp.headers == {':method': 'GET', ':path': '/', ':scheme': 'https'}
assert resp.content == b'foobar'
class TestCreateResponse():