text stringlengths 1 93.6k |
|---|
# as heartbeat is not concerned with responses, it is
|
# not a problem
|
# set the socket as non-blocking with a timeout
|
# NOTE this is for *initial* timeouts
|
websock.settimeout(INITIAL_SOCKET_TIMEOUT)
|
# this is used in case we have a 'partial' read
|
# but we use a timeout, cuz we don't want this to
|
# be stuck forever if something broke on the server
|
# side
|
_data = read_until(websock, b'\r\n', _data)
|
# re-set the socket back where it was
|
websock.settimeout(None)
|
except socket.timeout:
|
log.debug("No data received within {}s, moving on".format(
|
INITIAL_SOCKET_TIMEOUT))
|
raise
|
except socket.error:
|
print(
|
"[ ] Socket error when fetching data from remote server. "
|
"Timeout set to: {} Data so far: {}".format(
|
INITIAL_SOCKET_TIMEOUT,
|
repr(_data)))
|
raise
|
finally:
|
# always executed IIRC
|
websock.settimeout(None)
|
# at this point either the socket was broken, or we read all data.
|
# So we can continue.
|
# XXX what about a partial read from the socket? Will it result
|
# in some stuff in _data?
|
# hack: peek into the beginning of the line;
|
# (it may be a new HTTP response)
|
if is_async and b'HTTP/1' in _data[:10]:
|
log.debug("New HTTP response!")
|
# if it's an HTTP response, get rid of the
|
# headers and keep the body
|
_headers, _body, _cookie = split_headers(_data)
|
_data = _body
|
# at this point we have a chunked body
|
#
|
# split around \r\n to get the chunk length
|
_split = _data.split(b'\r\n', 1)
|
if len(_split) != 2:
|
log.debug("No more data to read")
|
log.debug(repr(_data))
|
raise TransmissionError("Incomplete data received")
|
_len, _data = _split
|
#
|
# determine chunk length
|
try:
|
chunk_len = int(_len, base=16)
|
except (TypeError, ValueError):
|
print(("[ ] Received chunk length is not "
|
"a valid hex value: {}").format(repr(_len)))
|
raise TransmissionError("Invalid chunk length")
|
if chunk_len == 0:
|
# end of data
|
log.debug("Received chunk of length 0. End of data")
|
if len(_data) > 0:
|
# there shouldn't be any data. That is, if the remote server
|
# adheres to the spec....
|
log.debug(
|
"Left-over data in the pipeline:\n{}".format(repr(_data)))
|
# XXX FIXME -- this was yield _data for async; but it works
|
# anyway now. I can't tell why the generator should stay
|
# alive.
|
if is_async:
|
# flush and yield the last bits
|
log.debug("Async end of data. Carry on...")
|
# note, sync (e.g. JSP) don't return a double \r\n after the 0
|
# yield _data
|
return
|
else:
|
# this raises a StopIteration (it's a generator)
|
# TODO see help(next) and try the 'default' value
|
# instead of StopIteration?
|
return
|
log.debug("1. This chunk length: {}. Data: {} [data len: {}]".format(
|
chunk_len, repr(_data), len(_data)))
|
#
|
# keep reading until all 'chunk_len'
|
# if not all data has been pulled from the socket, catch up! (chuck_len
|
# doesn't account for the final \r\n)
|
if chunk_len + 2 > len(_data):
|
# but we know how much is left, so
|
need_to_read = chunk_len + 2 - len(_data)
|
log.debug("need to read an extra {} bytes".format(need_to_read))
|
_data = b''.join([_data, read_more(websock, need_to_read)])
|
# now we know _data is at least equal to chuck_len, so
|
#
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.