mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-30 03:14:22 +00:00
http2: make proper use of odict
This commit is contained in:
parent
ecc7ffe928
commit
faf17d3d60
@ -2,7 +2,7 @@ from __future__ import (absolute_import, print_function, division)
|
||||
import itertools
|
||||
|
||||
from hpack.hpack import Encoder, Decoder
|
||||
from netlib import http, utils
|
||||
from netlib import http, utils, odict
|
||||
from . import frame
|
||||
|
||||
|
||||
@ -189,7 +189,8 @@ class HTTP2Protocol(object):
|
||||
def read_response(self, *args):
|
||||
stream_id, headers, body = self._receive_transmission()
|
||||
|
||||
response = http.Response("HTTP/2", headers[':status'], "", headers, body)
|
||||
status = headers[':status'][0]
|
||||
response = http.Response("HTTP/2", status, "", headers, body)
|
||||
response.stream_id = stream_id
|
||||
return response
|
||||
|
||||
@ -197,11 +198,11 @@ class HTTP2Protocol(object):
|
||||
stream_id, headers, body = self._receive_transmission()
|
||||
|
||||
form_in = ""
|
||||
method = headers.get(':method', '')
|
||||
scheme = headers.get(':scheme', '')
|
||||
host = headers.get(':host', '')
|
||||
method = headers.get(':method', [''])[0]
|
||||
scheme = headers.get(':scheme', [''])[0]
|
||||
host = headers.get(':host', [''])[0]
|
||||
port = '' # TODO: parse port number?
|
||||
path = headers.get(':path', '')
|
||||
path = headers.get(':path', [''])[0]
|
||||
|
||||
request = http.Request(form_in, method, scheme, host, port, path, "HTTP/2", headers, body)
|
||||
request.stream_id = stream_id
|
||||
@ -233,15 +234,17 @@ class HTTP2Protocol(object):
|
||||
break
|
||||
# TODO: implement window update & flow
|
||||
|
||||
headers = {}
|
||||
headers = odict.ODictCaseless()
|
||||
for header, value in self.decoder.decode(header_block_fragment):
|
||||
headers[header] = value
|
||||
headers.add(header, value)
|
||||
|
||||
return stream_id, headers, body
|
||||
|
||||
def create_response(self, code, stream_id=None, headers=None, body=None):
|
||||
if headers is None:
|
||||
headers = []
|
||||
if isinstance(headers, odict.ODict):
|
||||
headers = headers.items()
|
||||
|
||||
headers = [(b':status', bytes(str(code)))] + headers
|
||||
|
||||
|
@ -20,8 +20,6 @@ class ODict(object):
|
||||
"""
|
||||
|
||||
def __init__(self, lst=None):
|
||||
if isinstance(lst, ODict):
|
||||
lst = lst.items()
|
||||
self.lst = lst or []
|
||||
|
||||
def _kconv(self, s):
|
||||
|
@ -1,6 +1,6 @@
|
||||
import OpenSSL
|
||||
|
||||
from netlib import tcp
|
||||
from netlib import tcp, odict
|
||||
from netlib.http import http2
|
||||
from netlib.http.http2.frame import *
|
||||
from ... import tutils, tservers
|
||||
@ -256,7 +256,7 @@ class TestReadResponse(tservers.ServerTestBase):
|
||||
assert resp.httpversion == "HTTP/2"
|
||||
assert resp.status_code == "200"
|
||||
assert resp.msg == ""
|
||||
assert resp.headers == {':status': '200', 'etag': 'foobar'}
|
||||
assert resp.headers.lst == [[':status', '200'], ['etag', 'foobar']]
|
||||
assert resp.body == b'foobar'
|
||||
|
||||
|
||||
@ -282,7 +282,7 @@ class TestReadEmptyResponse(tservers.ServerTestBase):
|
||||
assert resp.httpversion == "HTTP/2"
|
||||
assert resp.status_code == "200"
|
||||
assert resp.msg == ""
|
||||
assert resp.headers == {':status': '200', 'etag': 'foobar'}
|
||||
assert resp.headers.lst == [[':status', '200'], ['etag', 'foobar']]
|
||||
assert resp.body == b''
|
||||
|
||||
|
||||
@ -307,7 +307,7 @@ class TestReadRequest(tservers.ServerTestBase):
|
||||
resp = protocol.read_request()
|
||||
|
||||
assert resp.stream_id
|
||||
assert resp.headers == {':method': 'GET', ':path': '/', ':scheme': 'https'}
|
||||
assert resp.headers.lst == [[u':method', u'GET'], [u':path', u'/'], [u':scheme', u'https']]
|
||||
assert resp.body == b'foobar'
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user