text stringlengths 1 93.6k |
|---|
return b"".join([
|
# the trick below works for python > 3.5, if you get an error
|
# use this instead:
|
# get_bytes("{}".format(len(what), 'X')),
|
b"%X" % len(what),
|
b"\r\n",
|
what,
|
b"\r\n"])
|
def read_more(websock, howmuch):
|
"""Read a given quantity of bytes from a socket."""
|
log.debug("Reading {} bytes from socket".format(howmuch))
|
read_bytes = 0
|
read_data = list()
|
log.debug("Now break server socket")
|
while read_bytes < howmuch:
|
with websock_mutex:
|
_recv = websock.recv(BUFFER_SIZE)
|
if len(_recv) == 0:
|
log.debug("Remote server closed the connection.")
|
break
|
read_data.append(_recv)
|
read_bytes += len(_recv)
|
return b''.join(read_data)
|
def read_until(websock, what, store_in=None, buffer_size=BUFFER_SIZE):
|
"""Read from a socket until a delimiter is found, or the socket is closed.
|
The `store_in` parameter is used to submit data that has been already read
|
and that might contain a partial sequence. For example,
|
what = '\r\n'
|
store_in = '\r'
|
left in socket = '\nfoobar'
|
In this case it will continue reading from the socket until the '\r\n'
|
sequence is established, or the socket is closed.
|
"""
|
if store_in is None:
|
_received = list()
|
last_received_chunks = b''
|
else:
|
# we're appending data
|
_received = [store_in]
|
last_received_chunks = store_in
|
# read until the expected senquence is in the data (e.g. '\r\n\r\n' to
|
# indicate end of HTTP headers, etc.)
|
# TODO: set a maximum buffer length, or a timeout?
|
while what not in last_received_chunks:
|
with websock_mutex:
|
_recv = websock.recv(buffer_size)
|
# log.debug("received: {}".format(repr(_recv)))
|
if len(_recv) == 0:
|
raise BrokenSocketException(
|
'[*] Remote server closed the connection.')
|
_received.append(_recv)
|
# need to assemble at least the last 2 chunks to check
|
# whether we got '\r' and '\n\r\n' in separate chunks
|
# not terribly efficient, but good enough
|
last_received_chunks = b''.join(_received[-2:])
|
log.debug("Spotted sequence {} in last received chunks of len {}".format(
|
repr(what), len(last_received_chunks)))
|
response = b''.join(_received)
|
# log.debug("all received data: {}".format(repr(response)))
|
return response
|
def read_chunks(websock, is_async, _data=None):
|
"""
|
CORE METHOD: a generator that keeps reading chunks from a socket.
|
Each call to next() will return a chunk. Remaining chunks
|
are placed back into the queue to be returned next time this is
|
called.
|
"""
|
keep_reading = True
|
while keep_reading:
|
# Initialisation of the generator.
|
#
|
# Part 1: keep reading until hitting a \r\n
|
# This takes care of:
|
# An empty buffer
|
# Some data in the buffer, but not yet until the last \r\n
|
#
|
if _data is None or b'\r\n' not in _data:
|
# possible scenarios:
|
# 1. There's a '\r' in raw_data but '\n' is still in the socket
|
# because the buffer length falls between those two bytes
|
# 2. There's no more data to read
|
# In both cases we need to keep reading
|
if _data is None:
|
log.debug("No data in the pipeline before reading from socket")
|
if _data is not None and b"\r\n" not in _data:
|
log.debug("No {}, let's read a bit more".format(repr('\r\n')))
|
try:
|
# XXX: there's a chance that the heartbeat will
|
# send data with this socket set non-blocking but
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.