Repository: abhinavsingh/proxy.py
Pull request #1062 (2022-01-26 08:51:23)

[Fix] Using `okResponse()` without content hangs the connection
- It hangs because of no content-length or connection close header
- Fixes https://github.com/abhinavsingh/proxy.py/issues/1028

codecov[bot]: # [Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh) Report
> Merging [#1062](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh) (f82882b) into [develop](https://codecov.io/gh/abhinavsingh/proxy.py/commit/6c21e343c7fa08bb2740d89e747e931fb7e4e8d6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh) (6c21e34) will **decrease** coverage by `0.22%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/graphs/tree.svg?width=650&height=150&src=pr&token=Zh9J7b4la2&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh)](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh)

```diff
@@             Coverage Diff             @@
##           develop    #1062      +/-   ##
===========================================
- Coverage    87.81%   87.59%   -0.23%     
===========================================
  Files          165      165              
  Lines         6779     6777       -2     
  Branches       680      679       -1     
===========================================
- Hits          5953     5936      -17     
- Misses         714      722       +8     
- Partials       112      119       +7     
```

| Flag | Coverage Δ | |
|---|---|---|
| pytest | `87.53% <100.00%> (-0.11%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh) | Coverage Δ | |
|---|---|---|
| [...ests/http/exceptions/test\_http\_request\_rejected.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-dGVzdHMvaHR0cC9leGNlcHRpb25zL3Rlc3RfaHR0cF9yZXF1ZXN0X3JlamVjdGVkLnB5) | `100.00% <ø> (ø)` | |
| [tests/http/parser/test\_http\_parser.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-dGVzdHMvaHR0cC9wYXJzZXIvdGVzdF9odHRwX3BhcnNlci5weQ==) | `100.00% <ø> (ø)` | |
| [proxy/common/utils.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-cHJveHkvY29tbW9uL3V0aWxzLnB5) | `98.29% <100.00%> (-0.03%)` | :arrow_down: |
| [tests/integration/test\_integration.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-dGVzdHMvaW50ZWdyYXRpb24vdGVzdF9pbnRlZ3JhdGlvbi5weQ==) | `94.31% <0.00%> (-5.69%)` | :arrow_down: |
| [tests/test\_set\_open\_file\_limit.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-dGVzdHMvdGVzdF9zZXRfb3Blbl9maWxlX2xpbWl0LnB5) | `96.42% <0.00%> (-3.58%)` | :arrow_down: |
| [proxy/common/flag.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-cHJveHkvY29tbW9uL2ZsYWcucHk=) | `91.48% <0.00%> (-1.42%)` | :arrow_down: |
| [proxy/http/handler.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-cHJveHkvaHR0cC9oYW5kbGVyLnB5) | `76.10% <0.00%> (-1.33%)` | :arrow_down: |
| [proxy/core/base/tcp\_server.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-cHJveHkvY29yZS9iYXNlL3RjcF9zZXJ2ZXIucHk=) | `88.88% <0.00%> (-1.24%)` | :arrow_down: |
| [proxy/http/parser/parser.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-cHJveHkvaHR0cC9wYXJzZXIvcGFyc2VyLnB5) | `95.81% <0.00%> (-0.94%)` | :arrow_down: |
| [proxy/proxy.py](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh#diff-cHJveHkvcHJveHkucHk=) | `98.34% <0.00%> (-0.83%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh). Last update [6c21e34...f82882b](https://codecov.io/gh/abhinavsingh/proxy.py/pull/1062?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Abhinav+Singh).


Patch:
diff --git a/proxy/common/utils.py b/proxy/common/utils.py
index 324dc16..cb2cb1d 100644
--- a/proxy/common/utils.py
+++ b/proxy/common/utils.py
@@ -111,17 +111,13 @@ def build_http_response(
         line.append(reason)
     if headers is None:
         headers = {}
-    has_content_length = False
     has_transfer_encoding = False
     for k, _ in headers.items():
-        if k.lower() == b'content-length':
-            has_content_length = True
         if k.lower() == b'transfer-encoding':
             has_transfer_encoding = True
-    if body is not None and \
-            not has_transfer_encoding and \
-            not has_content_length:
-        headers[b'Content-Length'] = bytes_(len(body))
+            break
+    if not has_transfer_encoding:
+        headers[b'Content-Length'] = bytes_(len(body)) if body else b'0'
     return build_http_pkt(line, headers, body, conn_close)
 
 


Test patch:
diff --git a/tests/http/exceptions/test_http_request_rejected.py b/tests/http/exceptions/test_http_request_rejected.py
index 9a6652d..f50cee7 100644
--- a/tests/http/exceptions/test_http_request_rejected.py
+++ b/tests/http/exceptions/test_http_request_rejected.py
@@ -31,6 +31,7 @@ class TestHttpRequestRejected(unittest.TestCase):
         self.assertEqual(
             e.response(self.request), CRLF.join([
                 b'HTTP/1.1 200 OK',
+                b'Content-Length: 0',
                 b'Connection: close',
                 CRLF,
             ]),
diff --git a/tests/http/parser/test_http_parser.py b/tests/http/parser/test_http_parser.py
index 6851041..a8192c8 100644
--- a/tests/http/parser/test_http_parser.py
+++ b/tests/http/parser/test_http_parser.py
@@ -146,6 +146,7 @@ class TestHttpParser(unittest.TestCase):
             okResponse(protocol_version=b'HTTP/1.1'),
             CRLF.join([
                 b'HTTP/1.1 200 OK',
+                b'Content-Length: 0',
                 CRLF,
             ]),
         )
@@ -157,6 +158,7 @@ class TestHttpParser(unittest.TestCase):
             CRLF.join([
                 b'HTTP/1.1 200 OK',
                 b'key: value',
+                b'Content-Length: 0',
                 CRLF,
             ]),
         )