Skip to content

Fix incorrect byte calculation in HTTP response handling #965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions restler/engine/transport_layer/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def _recvResponse(self, req_timeout_sec, method_name):

@param req_timeout_sec: The time, in seconds, to wait for request to complete
@type req_timeout_sec : Int
@param method_name: The HTTP method name from the request
@type method_name : Str

@return: Data received on current socket.
@rtype : Str
Expand All @@ -267,7 +269,7 @@ def _recvResponse(self, req_timeout_sec, method_name):
global TERMINATING_CHUNK_DELIM
data = ''

def decode_buf (buf):
def decode_buf(buf):
try:
return buf.decode(UTF8)
except Exception as ex:
Expand Down Expand Up @@ -332,21 +334,22 @@ def decode_buf (buf):
except Exception as error:
content_len = 2**20

bytes_remain = content_len - bytes_received + header_len

# Calculate how much data we've already received in the body
body_bytes_received = bytes_received - header_len
bytes_remain = max(0, content_len - body_bytes_received)

# get rest of socket data
while bytes_remain > 0:
try:
buf = self._sock.recv(2**20)
buf = self._sock.recv(min(bytes_remain, 2**20))
except Exception as error:
raise TransportLayerException(f"Exception: {error!s}")

if len(buf) == 0:
return data

bytes_remain -= len(buf)
data += decode_buf(buf)
bytes_remain -= len(buf)

return data

Expand All @@ -360,4 +363,4 @@ def _closeSocket(self):
try:
self._sock.close()
except Exception as error:
raise TransportLayerException(f"Exception: {error!s}")
raise TransportLayerException(f"Exception: {error!s}")