Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Send the response data in block of given lenght instead of sending al…
Browse files Browse the repository at this point in the history
…l at once.

This avoids the timeout for long responses.
Use try/except sending the response. The ospd-server will not die in case
of timeout or broken pipe (e.g. the client does not responde or was killed).
  • Loading branch information
jjnicola committed Sep 6, 2018
1 parent 0dbbf76 commit 628d851
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions ospd/ospd.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,25 @@ def new_client_stream(self, sock):
return None
return ssl_socket

@staticmethod
def write_to_stream(stream, response, block_len=1024):
"""
Send the response in blocks of the given len using the
passed method dependending on the socket type.
"""
try:
i_start = 0
i_end = block_len
while True:
if i_end > len(response):
stream(response[i_start:])
break
stream(response[i_start:i_end])
i_start = i_end
i_end += block_len
except (socket.timeout, socket.error) as exception:
logger.debug('Error sending response to the client: {0}'.format(exception))

def handle_client_stream(self, stream, is_unix=False):
""" Handles stream of data received from client. """

Expand Down Expand Up @@ -732,9 +751,10 @@ def handle_client_stream(self, stream, is_unix=False):
exception = OSPDError('Fatal error', 'error')
response = exception.as_xml()
if is_unix:
stream.sendall(response)
send_method = stream.send
else:
stream.write(response)
send_method = stream.write
self.write_to_stream(send_method, response)

def start_scan(self, scan_id, targets):
""" Starts the scan with scan_id. """
Expand Down

0 comments on commit 628d851

Please sign in to comment.