Skip to content

Commit 32a0759

Browse files
authored
[Fix] Using okResponse() without content hangs the connection (#1062)
* It hangs because of no content-length or connection close header * Fix tests
1 parent 6c21e34 commit 32a0759

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

proxy/common/utils.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,13 @@ def build_http_response(
111111
line.append(reason)
112112
if headers is None:
113113
headers = {}
114-
has_content_length = False
115114
has_transfer_encoding = False
116115
for k, _ in headers.items():
117-
if k.lower() == b'content-length':
118-
has_content_length = True
119116
if k.lower() == b'transfer-encoding':
120117
has_transfer_encoding = True
121-
if body is not None and \
122-
not has_transfer_encoding and \
123-
not has_content_length:
124-
headers[b'Content-Length'] = bytes_(len(body))
118+
break
119+
if not has_transfer_encoding:
120+
headers[b'Content-Length'] = bytes_(len(body)) if body else b'0'
125121
return build_http_pkt(line, headers, body, conn_close)
126122

127123

tests/http/exceptions/test_http_request_rejected.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def test_status_code_response(self) -> None:
3131
self.assertEqual(
3232
e.response(self.request), CRLF.join([
3333
b'HTTP/1.1 200 OK',
34+
b'Content-Length: 0',
3435
b'Connection: close',
3536
CRLF,
3637
]),

tests/http/parser/test_http_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ def test_build_response(self) -> None:
146146
okResponse(protocol_version=b'HTTP/1.1'),
147147
CRLF.join([
148148
b'HTTP/1.1 200 OK',
149+
b'Content-Length: 0',
149150
CRLF,
150151
]),
151152
)
@@ -157,6 +158,7 @@ def test_build_response(self) -> None:
157158
CRLF.join([
158159
b'HTTP/1.1 200 OK',
159160
b'key: value',
161+
b'Content-Length: 0',
160162
CRLF,
161163
]),
162164
)

0 commit comments

Comments
 (0)