From faf17d3d60e658d0cd1df30a10be4f11035502f8 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Mon, 20 Jul 2015 16:33:00 +0200 Subject: [PATCH] http2: make proper use of odict --- netlib/http/http2/protocol.py | 19 +++++++++++-------- netlib/odict.py | 2 -- test/http/http2/test_protocol.py | 8 ++++---- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/netlib/http/http2/protocol.py b/netlib/http/http2/protocol.py index 1dfdda216..55b5ca763 100644 --- a/netlib/http/http2/protocol.py +++ b/netlib/http/http2/protocol.py @@ -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 diff --git a/netlib/odict.py b/netlib/odict.py index ee1e69389..f52acd504 100644 --- a/netlib/odict.py +++ b/netlib/odict.py @@ -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): diff --git a/test/http/http2/test_protocol.py b/test/http/http2/test_protocol.py index 34e4ef500..d30402663 100644 --- a/test/http/http2/test_protocol.py +++ b/test/http/http2/test_protocol.py @@ -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'