fix chunked encoding

This commit is contained in:
Maximilian Hils 2015-09-11 01:18:17 +02:00
parent 30f0ee40c5
commit c159c8ca13
2 changed files with 16 additions and 10 deletions

View File

@ -1,7 +1,9 @@
from __future__ import (absolute_import, print_function, division) from __future__ import (absolute_import, print_function, division)
import six import itertools
import sys import sys
import six
from netlib import tcp from netlib import tcp
from netlib.http import http1, HttpErrorConnClosed, HttpError, Headers from netlib.http import http1, HttpErrorConnClosed, HttpError, Headers
from netlib.http.semantics import CONTENT_MISSING from netlib.http.semantics import CONTENT_MISSING
@ -9,14 +11,13 @@ from netlib.tcp import NetLibError, Address
from netlib.http.http1 import HTTP1Protocol from netlib.http.http1 import HTTP1Protocol
from netlib.http.http2 import HTTP2Protocol from netlib.http.http2 import HTTP2Protocol
from netlib.http.http2.frame import GoAwayFrame, PriorityFrame, WindowUpdateFrame from netlib.http.http2.frame import GoAwayFrame, PriorityFrame, WindowUpdateFrame
from .. import utils from .. import utils
from ..exceptions import InvalidCredentials, HttpException, ProtocolException from ..exceptions import InvalidCredentials, HttpException, ProtocolException
from ..models import ( from ..models import (
HTTPFlow, HTTPRequest, HTTPResponse, make_error_response, make_connect_response, Error HTTPFlow, HTTPRequest, HTTPResponse, make_error_response, make_connect_response, Error
) )
from .base import Layer, Kill from .base import Layer, Kill
from .rawtcp import RawTCPLayer
class _HttpLayer(Layer): class _HttpLayer(Layer):
supports_streaming = False supports_streaming = False
@ -108,16 +109,21 @@ class Http1Layer(_StreamingHttpLayer):
response, response,
preserve_transfer_encoding=True preserve_transfer_encoding=True
) )
self.client_conn.send(h + "\r\n") self.client_conn.wfile.write(h + "\r\n")
self.client_conn.wfile.flush()
def send_response_body(self, response, chunks): def send_response_body(self, response, chunks):
if self.client_protocol.has_chunked_encoding(response.headers): if self.client_protocol.has_chunked_encoding(response.headers):
chunks = ( chunks = itertools.chain(
"%d\r\n%s\r\n" % (len(chunk), chunk) (
for chunk in chunks "{:x}\r\n{}\r\n".format(len(chunk), chunk)
for chunk in chunks if chunk
),
("0\r\n\r\n",)
) )
for chunk in chunks: for chunk in chunks:
self.client_conn.send(chunk) self.client_conn.wfile.write(chunk)
self.client_conn.wfile.flush()
def check_close_connection(self, flow): def check_close_connection(self, flow):
close_connection = ( close_connection = (

View File

@ -711,7 +711,7 @@ class TestStreamRequest(tservers.HTTPProxTest):
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("127.0.0.1", self.proxy.port)) connection.connect(("127.0.0.1", self.proxy.port))
fconn = connection.makefile() fconn = connection.makefile()
spec = '200:h"Transfer-Encoding"="chunked":r:b"4\\r\\nthis\\r\\n7\\r\\nisatest\\r\\n0\\r\\n\\r\\n"' spec = '200:h"Transfer-Encoding"="chunked":r:b"4\\r\\nthis\\r\\n11\\r\\nisatest__reachhex\\r\\n0\\r\\n\\r\\n"'
connection.send( connection.send(
"GET %s/p/%s HTTP/1.1\r\n" % "GET %s/p/%s HTTP/1.1\r\n" %
(self.server.urlbase, spec)) (self.server.urlbase, spec))
@ -726,7 +726,7 @@ class TestStreamRequest(tservers.HTTPProxTest):
chunks = list(protocol.read_http_body_chunked( chunks = list(protocol.read_http_body_chunked(
resp.headers, None, "GET", 200, False resp.headers, None, "GET", 200, False
)) ))
assert chunks == ["this", "isatest", ""] assert chunks == ["this", "isatest__reachhex"]
connection.close() connection.close()