use new HTTP/1 protocol

This commit is contained in:
Thomas Kriechbaumer 2015-07-18 15:54:29 +02:00
parent caaac5cd5a
commit 6d5a3da929
5 changed files with 21 additions and 19 deletions

View File

@ -228,12 +228,12 @@ class Pathoc(tcp.TCPClient):
l = self.rfile.readline() l = self.rfile.readline()
if not l: if not l:
raise PathocError("Proxy CONNECT failed") raise PathocError("Proxy CONNECT failed")
parsed = http.http1.parse_response_line(l) parsed = self.protocol.parse_response_line(l)
if not parsed[1] == 200: if not parsed[1] == 200:
raise PathocError( raise PathocError(
"Proxy CONNECT failed: %s - %s" % (parsed[1], parsed[2]) "Proxy CONNECT failed: %s - %s" % (parsed[1], parsed[2])
) )
http.http1.read_headers(self.rfile) self.protocol.read_headers()
def socks_connect(self, connect_to): def socks_connect(self, connect_to):
try: try:

View File

@ -123,11 +123,12 @@ class PathodHandler(tcp.BaseHandler):
""" """
with logger.ctx() as lg: with logger.ctx() as lg:
if self.use_http2: if self.use_http2:
stream_id, headers, body = self.protocol.read_request() req = self.protocol.read_request()
method = headers[':method'] method = req.method
path = headers[':path'] path = req.path
headers = odict.ODict(headers) headers = odict.ODictCaseless(req.headers)
httpversion = "" httpversion = req.httpversion
stream_id = req.stream_id
else: else:
req = self.protocol.read_request(lg) req = self.protocol.read_request(lg)
if 'next_handle' in req: if 'next_handle' in req:

View File

@ -6,6 +6,9 @@ class HTTPProtocol:
def __init__(self, pathod_handler): def __init__(self, pathod_handler):
self.pathod_handler = pathod_handler self.pathod_handler = pathod_handler
self.wire_protocol = http1.HTTP1Protocol(
self.pathod_handler
)
def make_error_response(self, reason, body): def make_error_response(self, reason, body):
return language.http.make_error_response(reason, body) return language.http.make_error_response(reason, body)
@ -38,7 +41,7 @@ class HTTPProtocol:
""" """
Handle a CONNECT request. Handle a CONNECT request.
""" """
http1.read_headers(self.pathod_handler.rfile) self.wire_protocol.read_headers()
self.pathod_handler.wfile.write( self.pathod_handler.wfile.write(
'HTTP/1.1 200 Connection established\r\n' + 'HTTP/1.1 200 Connection established\r\n' +
('Proxy-agent: %s\r\n' % version.NAMEVERSION) + ('Proxy-agent: %s\r\n' % version.NAMEVERSION) +
@ -66,32 +69,31 @@ class HTTPProtocol:
return self.pathod_handler.handle_http_request, None return self.pathod_handler.handle_http_request, None
def read_request(self, lg): def read_request(self, lg):
line = http1.get_request_line(self.pathod_handler.rfile) line = self.wire_protocol.get_request_line()
if not line: if not line:
# Normal termination # Normal termination
return dict() return dict()
m = utils.MemBool() m = utils.MemBool()
if m(http1.parse_init_connect(line)): if m(self.wire_protocol.parse_init_connect(line)):
return dict(next_handle=self.handle_http_connect(m.v, lg)) return dict(next_handle=self.handle_http_connect(m.v, lg))
elif m(http1.parse_init_proxy(line)): elif m(self.wire_protocol.parse_init_proxy(line)):
method, _, _, _, path, httpversion = m.v method, _, _, _, path, httpversion = m.v
elif m(http1.parse_init_http(line)): elif m(self.wire_protocol.parse_init_http(line)):
method, path, httpversion = m.v method, path, httpversion = m.v
else: else:
s = "Invalid first line: %s" % repr(line) s = "Invalid first line: %s" % repr(line)
lg(s) lg(s)
return dict(errors=dict(type="error", msg=s)) return dict(errors=dict(type="error", msg=s))
headers = http1.read_headers(self.pathod_handler.rfile) headers = self.wire_protocol.read_headers()
if headers is None: if headers is None:
s = "Invalid headers" s = "Invalid headers"
lg(s) lg(s)
return dict(errors=dict(type="error", msg=s)) return dict(errors=dict(type="error", msg=s))
try: try:
body = http1.read_http_body( body = self.wire_protocol.read_http_body(
self.pathod_handler.rfile,
headers, headers,
None, None,
method, method,

View File

@ -12,7 +12,7 @@ class HTTP2Protocol:
def make_error_response(self, reason, body): def make_error_response(self, reason, body):
return language.http2.make_error_response(reason, body) return language.http2.make_error_response(reason, body)
def read_request(self): def read_request(self, lg=None):
self.wire_protocol.perform_server_connection_preface() self.wire_protocol.perform_server_connection_preface()
return self.wire_protocol.read_request() return self.wire_protocol.read_request()

View File

@ -5,7 +5,7 @@ import OpenSSL
from mock import Mock from mock import Mock
from netlib import tcp, http, socks from netlib import tcp, http, socks
from netlib.http import http2 from netlib.http import http1, http2
from libpathod import pathoc, test, version, pathod, language from libpathod import pathoc, test, version, pathod, language
import tutils import tutils
@ -272,8 +272,7 @@ class TestDaemonHTTP2(_TestDaemon):
c = pathoc.Pathoc( c = pathoc.Pathoc(
("127.0.0.1", self.d.port), ("127.0.0.1", self.d.port),
) )
# TODO: change if other protocols get implemented assert isinstance(c.protocol, http1.HTTP1Protocol)
assert c.protocol is None
def test_http2_alpn(self): def test_http2_alpn(self):
c = pathoc.Pathoc( c = pathoc.Pathoc(