mitmproxy/netlib/http/http2/connections.py

427 lines
14 KiB
Python
Raw Normal View History

from __future__ import (absolute_import, print_function, division)
import itertools
2015-07-27 07:36:50 +00:00
import time
from hpack.hpack import Encoder, Decoder
2015-09-16 16:43:24 +00:00
from ... import utils
2015-09-26 15:39:50 +00:00
from .. import Headers, Response, Request
2016-01-31 11:15:44 +00:00
from hyperframe import frame
2015-07-27 07:36:50 +00:00
class TCPHandler(object):
2015-08-10 18:44:36 +00:00
2015-07-27 07:36:50 +00:00
def __init__(self, rfile, wfile=None):
self.rfile = rfile
self.wfile = wfile
2015-09-15 22:04:23 +00:00
class HTTP2Protocol(object):
ERROR_CODES = utils.BiDi(
NO_ERROR=0x0,
PROTOCOL_ERROR=0x1,
INTERNAL_ERROR=0x2,
FLOW_CONTROL_ERROR=0x3,
SETTINGS_TIMEOUT=0x4,
STREAM_CLOSED=0x5,
FRAME_SIZE_ERROR=0x6,
REFUSED_STREAM=0x7,
CANCEL=0x8,
COMPRESSION_ERROR=0x9,
CONNECT_ERROR=0xa,
ENHANCE_YOUR_CALM=0xb,
INADEQUATE_SECURITY=0xc,
HTTP_1_1_REQUIRED=0xd
)
2016-01-31 13:16:03 +00:00
CLIENT_CONNECTION_PREFACE = b'PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'
2016-01-31 11:15:44 +00:00
HTTP2_DEFAULT_SETTINGS = {
frame.SettingsFrame.HEADER_TABLE_SIZE: 4096,
frame.SettingsFrame.ENABLE_PUSH: 1,
frame.SettingsFrame.MAX_CONCURRENT_STREAMS: None,
frame.SettingsFrame.INITIAL_WINDOW_SIZE: 2 ** 16 - 1,
frame.SettingsFrame.MAX_FRAME_SIZE: 2 ** 14,
frame.SettingsFrame.MAX_HEADER_LIST_SIZE: None,
}
2015-07-27 07:36:50 +00:00
def __init__(
self,
tcp_handler=None,
rfile=None,
wfile=None,
is_server=False,
dump_frames=False,
encoder=None,
decoder=None,
unhandled_frame_cb=None,
2015-07-27 07:36:50 +00:00
):
self.tcp_handler = tcp_handler or TCPHandler(rfile, wfile)
2015-06-11 13:38:32 +00:00
self.is_server = is_server
2015-07-27 07:36:50 +00:00
self.dump_frames = dump_frames
self.encoder = encoder or Encoder()
self.decoder = decoder or Decoder()
self.unhandled_frame_cb = unhandled_frame_cb
2016-01-31 11:15:44 +00:00
self.http2_settings = self.HTTP2_DEFAULT_SETTINGS.copy()
self.current_stream_id = None
2015-06-12 12:41:54 +00:00
self.connection_preface_performed = False
2015-08-10 18:44:36 +00:00
def read_request(
self,
2015-09-16 16:43:24 +00:00
__rfile,
2015-08-10 18:44:36 +00:00
include_body=True,
body_size_limit=None,
allow_empty=False,
):
2015-08-24 16:16:34 +00:00
if body_size_limit is not None:
raise NotImplementedError()
2015-07-30 11:52:13 +00:00
self.perform_connection_preface()
2015-07-29 09:27:43 +00:00
timestamp_start = time.time()
if hasattr(self.tcp_handler.rfile, "reset_timestamps"):
self.tcp_handler.rfile.reset_timestamps()
stream_id, headers, body = self._receive_transmission(
include_body=include_body,
)
2015-07-29 09:27:43 +00:00
if hasattr(self.tcp_handler.rfile, "first_byte_timestamp"):
# more accurate timestamp_start
timestamp_start = self.tcp_handler.rfile.first_byte_timestamp
timestamp_end = time.time()
2016-01-31 13:16:03 +00:00
authority = headers.get(':authority', b'')
2015-09-05 16:15:47 +00:00
method = headers.get(':method', 'GET')
scheme = headers.get(':scheme', 'https')
path = headers.get(':path', '/')
2015-08-16 18:02:18 +00:00
host = None
port = None
if path == '*' or path.startswith("/"):
2016-04-02 19:57:35 +00:00
first_line_format = "relative"
2015-08-16 18:02:18 +00:00
elif method == 'CONNECT':
2016-04-02 19:57:35 +00:00
first_line_format = "authority"
2015-08-16 18:02:18 +00:00
if ":" in authority:
host, port = authority.split(":", 1)
else:
host = authority
else:
2016-04-02 19:57:35 +00:00
first_line_format = "absolute"
2015-08-16 18:02:18 +00:00
# FIXME: verify if path or :host contains what we need
scheme, host, port, _ = utils.parse_url(path)
2016-01-31 13:16:03 +00:00
scheme = scheme.decode('ascii')
host = host.decode('ascii')
2015-08-16 18:02:18 +00:00
if host is None:
host = 'localhost'
if port is None:
port = 80 if scheme == 'http' else 443
port = int(port)
2015-09-16 16:43:24 +00:00
request = Request(
2016-04-02 19:57:35 +00:00
first_line_format,
2016-01-31 13:16:03 +00:00
method.encode('ascii'),
scheme.encode('ascii'),
host.encode('ascii'),
2015-08-16 18:02:18 +00:00
port,
2016-01-31 13:16:03 +00:00
path.encode('ascii'),
2016-02-08 14:09:25 +00:00
b"HTTP/2.0",
2015-07-29 09:27:43 +00:00
headers,
body,
timestamp_start,
timestamp_end,
)
request.stream_id = stream_id
return request
2015-08-10 18:44:36 +00:00
def read_response(
self,
2015-09-16 16:43:24 +00:00
__rfile,
2016-01-31 13:16:03 +00:00
request_method=b'',
2015-08-10 18:44:36 +00:00
body_size_limit=None,
include_body=True,
2015-08-24 16:16:34 +00:00
stream_id=None,
2015-08-10 18:44:36 +00:00
):
2015-08-24 16:16:34 +00:00
if body_size_limit is not None:
raise NotImplementedError()
2015-07-30 11:52:13 +00:00
self.perform_connection_preface()
2015-07-29 09:27:43 +00:00
timestamp_start = time.time()
if hasattr(self.tcp_handler.rfile, "reset_timestamps"):
self.tcp_handler.rfile.reset_timestamps()
stream_id, headers, body = self._receive_transmission(
2015-08-24 16:16:34 +00:00
stream_id=stream_id,
include_body=include_body,
)
2015-07-29 09:27:43 +00:00
if hasattr(self.tcp_handler.rfile, "first_byte_timestamp"):
# more accurate timestamp_start
timestamp_start = self.tcp_handler.rfile.first_byte_timestamp
if include_body:
timestamp_end = time.time()
2015-08-24 14:52:32 +00:00
else:
2015-07-29 09:27:43 +00:00
timestamp_end = None
2015-09-16 16:43:24 +00:00
response = Response(
2016-02-08 14:09:25 +00:00
b"HTTP/2.0",
2015-09-05 16:15:47 +00:00
int(headers.get(':status', 502)),
2016-01-31 13:16:03 +00:00
b'',
2015-07-29 09:27:43 +00:00
headers,
body,
timestamp_start=timestamp_start,
timestamp_end=timestamp_end,
)
response.stream_id = stream_id
return response
2015-09-16 16:43:24 +00:00
def assemble(self, message):
if isinstance(message, Request):
return self.assemble_request(message)
elif isinstance(message, Response):
return self.assemble_response(message)
else:
raise ValueError("HTTP message not supported.")
2015-07-29 09:27:43 +00:00
def assemble_request(self, request):
2015-09-16 16:43:24 +00:00
assert isinstance(request, Request)
2015-07-29 09:27:43 +00:00
authority = self.tcp_handler.sni if self.tcp_handler.sni else self.tcp_handler.address.host
if self.tcp_handler.address.port != 443:
authority += ":%d" % self.tcp_handler.address.port
2015-07-30 11:52:13 +00:00
headers = request.headers.copy()
2015-09-05 16:15:47 +00:00
if ':authority' not in headers:
headers.insert(0, b':authority', authority.encode('ascii'))
2015-09-05 16:15:47 +00:00
if ':scheme' not in headers:
headers.insert(0, b':scheme', request.scheme.encode('ascii'))
2015-09-05 16:15:47 +00:00
if ':path' not in headers:
headers.insert(0, b':path', request.path.encode('ascii'))
2015-09-05 16:15:47 +00:00
if ':method' not in headers:
headers.insert(0, b':method', request.method.encode('ascii'))
2015-07-29 09:27:43 +00:00
if hasattr(request, 'stream_id'):
stream_id = request.stream_id
else:
stream_id = self._next_stream_id()
return list(itertools.chain(
2015-07-30 11:52:13 +00:00
self._create_headers(headers, stream_id, end_stream=(request.body is None or len(request.body) == 0)),
2015-07-29 09:27:43 +00:00
self._create_body(request.body, stream_id)))
def assemble_response(self, response):
2015-09-16 16:43:24 +00:00
assert isinstance(response, Response)
2015-07-29 09:27:43 +00:00
2015-07-30 11:52:13 +00:00
headers = response.headers.copy()
2015-09-05 16:15:47 +00:00
if ':status' not in headers:
headers.insert(0, b':status', str(response.status_code).encode('ascii'))
2015-07-29 09:27:43 +00:00
if hasattr(response, 'stream_id'):
stream_id = response.stream_id
else:
stream_id = self._next_stream_id()
return list(itertools.chain(
2015-07-30 11:52:13 +00:00
self._create_headers(headers, stream_id, end_stream=(response.body is None or len(response.body) == 0)),
2015-07-29 09:27:43 +00:00
self._create_body(response.body, stream_id),
))
2015-07-30 11:52:13 +00:00
def perform_connection_preface(self, force=False):
if force or not self.connection_preface_performed:
if self.is_server:
self.perform_server_connection_preface(force)
else:
self.perform_client_connection_preface(force)
2015-06-12 12:41:54 +00:00
def perform_server_connection_preface(self, force=False):
if force or not self.connection_preface_performed:
self.connection_preface_performed = True
2015-06-11 13:38:32 +00:00
2015-06-12 12:41:54 +00:00
magic_length = len(self.CLIENT_CONNECTION_PREFACE)
magic = self.tcp_handler.rfile.safe_read(magic_length)
assert magic == self.CLIENT_CONNECTION_PREFACE
2015-06-11 13:38:32 +00:00
2016-01-31 11:15:44 +00:00
frm = frame.SettingsFrame(settings={
frame.SettingsFrame.ENABLE_PUSH: 0,
frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 1,
})
self.send_frame(frm, hide=True)
2015-06-15 11:15:06 +00:00
self._receive_settings(hide=True)
2015-06-11 13:38:32 +00:00
2015-06-12 12:41:54 +00:00
def perform_client_connection_preface(self, force=False):
if force or not self.connection_preface_performed:
self.connection_preface_performed = True
self.tcp_handler.wfile.write(self.CLIENT_CONNECTION_PREFACE)
2016-01-31 11:15:44 +00:00
self.send_frame(frame.SettingsFrame(), hide=True)
2015-08-20 18:36:51 +00:00
self._receive_settings(hide=True) # server announces own settings
self._receive_settings(hide=True) # server acks my settings
2015-06-11 13:38:32 +00:00
2015-06-15 11:15:06 +00:00
def send_frame(self, frm, hide=False):
2016-01-31 11:15:44 +00:00
raw_bytes = frm.serialize()
2015-06-11 13:38:32 +00:00
self.tcp_handler.wfile.write(raw_bytes)
self.tcp_handler.wfile.flush()
2015-06-15 15:31:08 +00:00
if not hide and self.dump_frames: # pragma no cover
2015-06-15 11:15:06 +00:00
print(frm.human_readable(">>"))
2015-06-15 11:15:06 +00:00
def read_frame(self, hide=False):
2015-09-03 19:22:40 +00:00
while True:
2016-01-31 11:15:44 +00:00
frm = utils.http2_read_frame(self.tcp_handler.rfile)
2015-09-03 19:22:40 +00:00
if not hide and self.dump_frames: # pragma no cover
print(frm.human_readable("<<"))
if isinstance(frm, frame.PingFrame):
2016-01-31 11:15:44 +00:00
raw_bytes = frame.PingFrame(flags=['ACK'], payload=frm.payload).serialize()
2015-09-03 19:22:40 +00:00
self.tcp_handler.wfile.write(raw_bytes)
self.tcp_handler.wfile.flush()
continue
2016-01-31 11:15:44 +00:00
if isinstance(frm, frame.SettingsFrame) and 'ACK' not in frm.flags:
2015-09-03 19:22:40 +00:00
self._apply_settings(frm.settings, hide)
2016-01-31 11:15:44 +00:00
if isinstance(frm, frame.DataFrame) and frm.flow_controlled_length > 0:
self._update_flow_control_window(frm.stream_id, frm.flow_controlled_length)
2015-09-03 19:22:40 +00:00
return frm
2015-07-29 09:27:43 +00:00
def check_alpn(self):
alp = self.tcp_handler.get_alpn_proto_negotiated()
2015-09-26 15:39:50 +00:00
if alp != b'h2':
2015-07-29 09:27:43 +00:00
raise NotImplementedError(
"HTTP2Protocol can not handle unknown ALP: %s" % alp)
return True
def _handle_unexpected_frame(self, frm):
if isinstance(frm, frame.SettingsFrame):
return
2015-08-24 14:52:32 +00:00
if self.unhandled_frame_cb:
self.unhandled_frame_cb(frm)
2015-07-29 09:27:43 +00:00
def _receive_settings(self, hide=False):
while True:
frm = self.read_frame(hide)
if isinstance(frm, frame.SettingsFrame):
break
else:
self._handle_unexpected_frame(frm)
2015-07-29 09:27:43 +00:00
def _next_stream_id(self):
if self.current_stream_id is None:
if self.is_server:
# servers must use even stream ids
self.current_stream_id = 2
else:
# clients must use odd stream ids
self.current_stream_id = 1
else:
self.current_stream_id += 2
return self.current_stream_id
2015-06-15 11:15:06 +00:00
def _apply_settings(self, settings, hide=False):
for setting, value in settings.items():
old_value = self.http2_settings[setting]
if not old_value:
old_value = '-'
self.http2_settings[setting] = value
2016-01-31 11:15:44 +00:00
frm = frame.SettingsFrame(flags=['ACK'])
2015-06-18 13:32:52 +00:00
self.send_frame(frm, hide)
2015-06-15 15:31:08 +00:00
def _update_flow_control_window(self, stream_id, increment):
2016-01-31 11:15:44 +00:00
frm = frame.WindowUpdateFrame(stream_id=0, window_increment=increment)
self.send_frame(frm)
2016-01-31 11:15:44 +00:00
frm = frame.WindowUpdateFrame(stream_id=stream_id, window_increment=increment)
self.send_frame(frm)
def _create_headers(self, headers, stream_id, end_stream=True):
def frame_cls(chunks):
for i in chunks:
if i == 0:
yield frame.HeadersFrame, i
else:
yield frame.ContinuationFrame, i
2015-09-05 16:15:47 +00:00
header_block_fragment = self.encoder.encode(headers.fields)
2016-01-31 11:15:44 +00:00
chunk_size = self.http2_settings[frame.SettingsFrame.MAX_FRAME_SIZE]
chunks = range(0, len(header_block_fragment), chunk_size)
frms = [frm_cls(
2016-01-31 11:15:44 +00:00
flags=[],
stream_id=stream_id,
2016-01-31 11:15:44 +00:00
data=header_block_fragment[i:i+chunk_size]) for frm_cls, i in frame_cls(chunks)]
2016-01-31 11:15:44 +00:00
frms[-1].flags.add('END_HEADERS')
if end_stream:
2016-01-31 11:15:44 +00:00
frms[0].flags.add('END_STREAM')
2015-06-15 11:15:06 +00:00
2015-06-15 15:31:08 +00:00
if self.dump_frames: # pragma no cover
for frm in frms:
print(frm.human_readable(">>"))
2015-06-15 11:15:06 +00:00
2016-01-31 11:15:44 +00:00
return [frm.serialize() for frm in frms]
def _create_body(self, body, stream_id):
if body is None or len(body) == 0:
return b''
2016-01-31 11:15:44 +00:00
chunk_size = self.http2_settings[frame.SettingsFrame.MAX_FRAME_SIZE]
chunks = range(0, len(body), chunk_size)
frms = [frame.DataFrame(
2016-01-31 11:15:44 +00:00
flags=[],
stream_id=stream_id,
2016-01-31 11:15:44 +00:00
data=body[i:i+chunk_size]) for i in chunks]
frms[-1].flags.add('END_STREAM')
2015-06-15 15:31:08 +00:00
if self.dump_frames: # pragma no cover
for frm in frms:
print(frm.human_readable(">>"))
2015-06-15 11:15:06 +00:00
2016-01-31 11:15:44 +00:00
return [frm.serialize() for frm in frms]
2015-06-15 11:15:06 +00:00
def _receive_transmission(self, stream_id=None, include_body=True):
2015-08-24 14:52:32 +00:00
if not include_body:
raise NotImplementedError()
2015-06-11 13:38:32 +00:00
body_expected = True
2016-01-31 11:15:44 +00:00
header_blocks = b''
body = b''
while True:
frm = self.read_frame()
if (
(isinstance(frm, frame.HeadersFrame) or isinstance(frm, frame.ContinuationFrame)) and
(stream_id is None or frm.stream_id == stream_id)
):
stream_id = frm.stream_id
2016-01-31 11:15:44 +00:00
header_blocks += frm.data
if 'END_STREAM' in frm.flags:
body_expected = False
2016-01-31 11:15:44 +00:00
if 'END_HEADERS' in frm.flags:
break
else:
self._handle_unexpected_frame(frm)
2015-06-11 13:38:32 +00:00
while body_expected:
frm = self.read_frame()
if isinstance(frm, frame.DataFrame) and frm.stream_id == stream_id:
2016-01-31 11:15:44 +00:00
body += frm.data
if 'END_STREAM' in frm.flags:
break
else:
self._handle_unexpected_frame(frm)
2015-09-16 16:43:24 +00:00
headers = Headers(
(k.encode('ascii'), v.encode('ascii')) for k, v in self.decoder.decode(header_blocks)
2015-09-05 16:15:47 +00:00
)
return stream_id, headers, body