fix http message parsing, it may contain \n\n!
This commit is contained in:
parent
88f3104c4e
commit
86b39a72c6
1 changed files with 11 additions and 7 deletions
|
@ -651,7 +651,7 @@ class NonBlockingHTTP(NonBlockingTCP):
|
||||||
self.recvbuff = '%s%s' % (self.recvbuff or '', data)
|
self.recvbuff = '%s%s' % (self.recvbuff or '', data)
|
||||||
statusline, headers, httpbody, buffer_rest = self.parse_http_message(
|
statusline, headers, httpbody, buffer_rest = self.parse_http_message(
|
||||||
self.recvbuff)
|
self.recvbuff)
|
||||||
|
|
||||||
if not (statusline and headers and httpbody):
|
if not (statusline and headers and httpbody):
|
||||||
log.debug('Received incomplete HTTP response')
|
log.debug('Received incomplete HTTP response')
|
||||||
return
|
return
|
||||||
|
@ -663,12 +663,12 @@ class NonBlockingHTTP(NonBlockingTCP):
|
||||||
self.expected_length = int(headers['Content-Length'])
|
self.expected_length = int(headers['Content-Length'])
|
||||||
if 'Connection' in headers and headers['Connection'].strip()=='close':
|
if 'Connection' in headers and headers['Connection'].strip()=='close':
|
||||||
self.close_current_connection = True
|
self.close_current_connection = True
|
||||||
|
|
||||||
if self.expected_length > len(httpbody):
|
if self.expected_length > len(httpbody):
|
||||||
# If we haven't received the whole HTTP mess yet, let's end the thread.
|
# If we haven't received the whole HTTP mess yet, let's end the thread.
|
||||||
# It will be finnished from one of following recvs on plugged socket.
|
# It will be finnished from one of following recvs on plugged socket.
|
||||||
log.info('not enough bytes in HTTP response - %d expected, %d got' %
|
log.info('not enough bytes in HTTP response - %d expected, got %d' %
|
||||||
(self.expected_length, len(self.recvbuff)))
|
(self.expected_length, len(httpbody)))
|
||||||
else:
|
else:
|
||||||
# First part of buffer has been extraced and is going to be handled,
|
# First part of buffer has been extraced and is going to be handled,
|
||||||
# remove it from buffer
|
# remove it from buffer
|
||||||
|
@ -720,6 +720,7 @@ class NonBlockingHTTP(NonBlockingTCP):
|
||||||
http_rest - what is left in the message after a full HTTP header + body
|
http_rest - what is left in the message after a full HTTP header + body
|
||||||
'''
|
'''
|
||||||
message = message.replace('\r','')
|
message = message.replace('\r','')
|
||||||
|
message = message.lstrip('\n')
|
||||||
splitted = message.split('\n\n')
|
splitted = message.split('\n\n')
|
||||||
if len(splitted) < 2:
|
if len(splitted) < 2:
|
||||||
# no complete http message. Keep filling the buffer until we find one
|
# no complete http message. Keep filling the buffer until we find one
|
||||||
|
@ -727,9 +728,6 @@ class NonBlockingHTTP(NonBlockingTCP):
|
||||||
return ('', '', '', buffer_rest)
|
return ('', '', '', buffer_rest)
|
||||||
else:
|
else:
|
||||||
(header, httpbody) = splitted[:2]
|
(header, httpbody) = splitted[:2]
|
||||||
if httpbody.endswith('\n'):
|
|
||||||
httpbody = httpbody[:-1]
|
|
||||||
buffer_rest = "\n\n".join(splitted[2:])
|
|
||||||
header = header.split('\n')
|
header = header.split('\n')
|
||||||
statusline = header[0].split(' ', 2)
|
statusline = header[0].split(' ', 2)
|
||||||
header = header[1:]
|
header = header[1:]
|
||||||
|
@ -737,6 +735,12 @@ class NonBlockingHTTP(NonBlockingTCP):
|
||||||
for dummy in header:
|
for dummy in header:
|
||||||
row = dummy.split(' ', 1)
|
row = dummy.split(' ', 1)
|
||||||
headers[row[0][:-1]] = row[1]
|
headers[row[0][:-1]] = row[1]
|
||||||
|
body_size = headers['Content-Length']
|
||||||
|
rest_splitted = splitted[2:]
|
||||||
|
while (len(httpbody) < body_size) and rest_splitted:
|
||||||
|
# Complete httpbody until it has the announced size
|
||||||
|
httpbody = '\n\n'.join([httpbody, rest_splitted.pop(0)])
|
||||||
|
buffer_rest = "\n\n".join(rest_splitted)
|
||||||
return (statusline, headers, httpbody, buffer_rest)
|
return (statusline, headers, httpbody, buffer_rest)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue