request -> request_method

This commit is contained in:
Maximilian Hils 2015-08-24 18:17:04 +02:00
parent f1f34e7713
commit 56a4bc381e
5 changed files with 24 additions and 31 deletions

View File

@ -41,7 +41,7 @@ def send_connect_request(conn, host, port, update_state=True):
protocol = http1.HTTP1Protocol(conn) protocol = http1.HTTP1Protocol(conn)
conn.send(protocol.assemble(upstream_request)) conn.send(protocol.assemble(upstream_request))
resp = HTTPResponse.from_protocol(protocol, upstream_request) resp = HTTPResponse.from_protocol(protocol, upstream_request.method)
if resp.status_code != 200: if resp.status_code != 200:
raise proxy.ProxyError(resp.status_code, raise proxy.ProxyError(resp.status_code,
"Cannot establish SSL " + "Cannot establish SSL " +
@ -177,7 +177,7 @@ class HTTPHandler(ProtocolHandler):
# Only get the headers at first... # Only get the headers at first...
flow.response = HTTPResponse.from_protocol( flow.response = HTTPResponse.from_protocol(
self.c.server_conn.protocol, self.c.server_conn.protocol,
flow.request, flow.request.method,
body_size_limit=self.c.config.body_size_limit, body_size_limit=self.c.config.body_size_limit,
include_body=False, include_body=False,
) )
@ -760,7 +760,7 @@ class RequestReplayThread(threading.Thread):
self.flow.server_conn.protocol = http1.HTTP1Protocol(self.flow.server_conn) self.flow.server_conn.protocol = http1.HTTP1Protocol(self.flow.server_conn)
self.flow.response = HTTPResponse.from_protocol( self.flow.response = HTTPResponse.from_protocol(
self.flow.server_conn.protocol, self.flow.server_conn.protocol,
r, r.method,
body_size_limit=self.config.body_size_limit, body_size_limit=self.config.body_size_limit,
) )
if self.channel: if self.channel:

View File

@ -240,13 +240,10 @@ class HTTPRequest(MessageMixin, semantics.Request):
def from_protocol( def from_protocol(
self, self,
protocol, protocol,
include_body=True, *args,
body_size_limit=None, **kwargs
): ):
req = protocol.read_request( req = protocol.read_request(*args, **kwargs)
include_body = include_body,
body_size_limit = body_size_limit,
)
return self.wrap(req) return self.wrap(req)
@classmethod @classmethod
@ -352,15 +349,10 @@ class HTTPResponse(MessageMixin, semantics.Response):
def from_protocol( def from_protocol(
self, self,
protocol, protocol,
request, *args,
include_body=True, **kwargs
body_size_limit=None
): ):
resp = protocol.read_response( resp = protocol.read_response(*args, **kwargs)
request,
body_size_limit,
include_body=include_body
)
return self.wrap(resp) return self.wrap(resp)
@classmethod @classmethod

View File

@ -34,10 +34,10 @@ class Http1Layer(Layer):
body_size_limit=self.config.body_size_limit body_size_limit=self.config.body_size_limit
) )
def read_from_server(self, request): def read_from_server(self, request_method):
return HTTPResponse.from_protocol( return HTTPResponse.from_protocol(
self.server_protocol, self.server_protocol,
request, request_method,
body_size_limit=self.config.body_size_limit, body_size_limit=self.config.body_size_limit,
include_body=False, include_body=False,
) )
@ -64,6 +64,7 @@ class Http1Layer(Layer):
layer = HttpLayer(self, self.mode) layer = HttpLayer(self, self.mode)
layer() layer()
class Http2Layer(Layer): class Http2Layer(Layer):
def __init__(self, ctx, mode): def __init__(self, ctx, mode):
super(Http2Layer, self).__init__(ctx) super(Http2Layer, self).__init__(ctx)
@ -72,20 +73,20 @@ class Http2Layer(Layer):
self.server_protocol = HTTP2Protocol(self.server_conn, is_server=False, unhandled_frame_cb=self.handle_unexpected_frame) self.server_protocol = HTTP2Protocol(self.server_conn, is_server=False, unhandled_frame_cb=self.handle_unexpected_frame)
def read_from_client(self): def read_from_client(self):
return HTTPRequest.from_protocol( request = HTTPRequest.from_protocol(
self.client_protocol, self.client_protocol,
body_size_limit=self.config.body_size_limit body_size_limit=self.config.body_size_limit
) )
self._stream_id = request.stream_id
def read_from_server(self, request): def read_from_server(self, request_method):
response = HTTPResponse.from_protocol( return HTTPResponse.from_protocol(
self.server_protocol, self.server_protocol,
request, request_method,
body_size_limit=self.config.body_size_limit, body_size_limit=self.config.body_size_limit,
include_body=False, include_body=False,
stream_id=self._stream_id
) )
response.stream_id = request.stream_id
return response
def send_to_client(self, message): def send_to_client(self, message):
# TODO: implement flow control and WINDOW_UPDATE frames # TODO: implement flow control and WINDOW_UPDATE frames
@ -356,7 +357,7 @@ class HttpLayer(Layer):
def get_response_from_server(self, flow): def get_response_from_server(self, flow):
def get_response(): def get_response():
self.send_to_server(flow.request) self.send_to_server(flow.request)
flow.response = self.read_from_server(flow.request) flow.response = self.read_from_server(flow.request.method)
try: try:
get_response() get_response()

View File

@ -28,19 +28,19 @@ class TestHTTPResponse:
"\r\n" "\r\n"
protocol = mock_protocol(s) protocol = mock_protocol(s)
r = HTTPResponse.from_protocol(protocol, netlib.http.EmptyRequest(method="GET")) r = HTTPResponse.from_protocol(protocol, "GET")
assert r.status_code == 200 assert r.status_code == 200
assert r.content == "content" assert r.content == "content"
assert HTTPResponse.from_protocol(protocol, netlib.http.EmptyRequest(method="GET")).status_code == 204 assert HTTPResponse.from_protocol(protocol, "GET").status_code == 204
protocol = mock_protocol(s) protocol = mock_protocol(s)
# HEAD must not have content by spec. We should leave it on the pipe. # HEAD must not have content by spec. We should leave it on the pipe.
r = HTTPResponse.from_protocol(protocol, netlib.http.EmptyRequest(method="HEAD")) r = HTTPResponse.from_protocol(protocol, "HEAD")
assert r.status_code == 200 assert r.status_code == 200
assert r.content == "" assert r.content == ""
tutils.raises( tutils.raises(
"Invalid server response: 'content", "Invalid server response: 'content",
HTTPResponse.from_protocol, protocol, netlib.http.EmptyRequest(method="GET") HTTPResponse.from_protocol, protocol, "GET"
) )

View File

@ -36,7 +36,7 @@ class TestServerConnection:
sc.send(protocol.assemble(f.request)) sc.send(protocol.assemble(f.request))
protocol = http.http1.HTTP1Protocol(rfile=sc.rfile) protocol = http.http1.HTTP1Protocol(rfile=sc.rfile)
assert protocol.read_response(f.request, 1000) assert protocol.read_response(f.request.method, 1000)
assert self.d.last_log() assert self.d.last_log()
sc.finish() sc.finish()