Skip to content

Commit

Permalink
Merge pull request #3897 from mhils/sans-io-adjustments
Browse files Browse the repository at this point in the history
Sync sans-io adjustments
  • Loading branch information
mhils committed Apr 4, 2020
2 parents 6acabbb + 9897ca7 commit 9cc5d93
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
30 changes: 16 additions & 14 deletions mitmproxy/net/http/http1/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys
import re

import typing

from mitmproxy.net.http import request
from mitmproxy.net.http import response
from mitmproxy.net.http import headers
Expand Down Expand Up @@ -171,8 +173,15 @@ def connection_close(http_version, headers):
return http_version != "HTTP/1.1" and http_version != b"HTTP/1.1"


def expected_http_body_size(request, response=None):
def expected_http_body_size(
request: request.Request,
response: typing.Optional[response.Response] = None,
expect_continue_as_0: bool = True
):
"""
Args:
- expect_continue_as_0: If true, incorrectly predict a body size of 0 for requests which are waiting
for a 100 Continue response.
Returns:
The expected body length:
- a positive integer, if the size is known in advance
Expand All @@ -186,24 +195,17 @@ def expected_http_body_size(request, response=None):
# http://tools.ietf.org/html/rfc7230#section-3.3
if not response:
headers = request.headers
response_code = None
is_request = True
else:
headers = response.headers
response_code = response.status_code
is_request = False

if is_request:
if headers.get("expect", "").lower() == "100-continue":
if expect_continue_as_0 and headers.get("expect", "").lower() == "100-continue":
return 0
else:
headers = response.headers
if request.method.upper() == "HEAD":
return 0
if 100 <= response_code <= 199:
if 100 <= response.status_code <= 199:
return 0
if response_code == 200 and request.method.upper() == "CONNECT":
if response.status_code == 200 and request.method.upper() == "CONNECT":
return 0
if response_code in (204, 304):
if response.status_code in (204, 304):
return 0

if "chunked" in headers.get("transfer-encoding", "").lower():
Expand All @@ -220,7 +222,7 @@ def expected_http_body_size(request, response=None):
return size
except ValueError:
raise exceptions.HttpSyntaxException("Unparseable Content Length")
if is_request:
if not response:
return 0
return -1

Expand Down
5 changes: 5 additions & 0 deletions test/mitmproxy/net/http/http1/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ def test_expected_http_body_size():
assert expected_http_body_size(
treq(headers=Headers(expect="100-continue", content_length="42"))
) == 0
# Expect: 100-continue
assert expected_http_body_size(
treq(headers=Headers(expect="100-continue", content_length="42")),
expect_continue_as_0=False
) == 42

# http://tools.ietf.org/html/rfc7230#section-3.3
assert expected_http_body_size(
Expand Down

0 comments on commit 9cc5d93

Please sign in to comment.