Skip to content

Commit

Permalink
Partial fix for Supervisor#664. Input buffer still treated as text.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsajip committed Jan 3, 2017
1 parent 9935e6f commit 5369ff0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 deletions.
16 changes: 2 additions & 14 deletions supervisor/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,6 @@ def writable(self, now=None):
else:
return False

# It is possible that self.ac_out_buffer is equal b''
# and in Python3 b'' is not equal ''. This cause
# http_server.http_channel.writable(self) is always True.
# To avoid this case, we need to force self.ac_out_buffer = ''
if len(self.ac_out_buffer) == 0:
self.ac_out_buffer = ''

return http_server.http_channel.writable(self)

def refill_buffer (self):
Expand All @@ -371,8 +364,7 @@ def refill_buffer (self):
self.producer_fifo.pop()
self.close()
return
else:
assert isinstance(p, bytes)
elif isinstance(p, bytes):
self.producer_fifo.pop()
self.ac_out_buffer += p
return
Expand All @@ -384,11 +376,7 @@ def refill_buffer (self):
return

elif data:
try:
self.ac_out_buffer = self.ac_out_buffer + data
except TypeError:
self.ac_out_buffer = as_bytes(self.ac_out_buffer) + as_bytes(data)

self.ac_out_buffer = self.ac_out_buffer + data
self.delay = False
return
else:
Expand Down
11 changes: 5 additions & 6 deletions supervisor/medusa/asynchat_25.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class async_chat (asyncore.dispatcher):

def __init__ (self, conn=None, map=None):
self.ac_in_buffer = ''
self.ac_out_buffer = ''
self.ac_out_buffer = b''
self.producer_fifo = fifo()
asyncore.dispatcher.__init__ (self, conn, map)

Expand Down Expand Up @@ -112,7 +112,7 @@ def handle_read (self):
n = terminator
if lb < n:
self.collect_incoming_data (self.ac_in_buffer)
self.ac_in_buffer = ''
self.ac_in_buffer = b''
self.terminator -= lb
else:
self.collect_incoming_data (self.ac_in_buffer[:n])
Expand Down Expand Up @@ -174,7 +174,7 @@ def writable (self):
# return len(self.ac_out_buffer) or len(self.producer_fifo) or (not self.connected)
# this is about twice as fast, though not as clear.
return not (
(self.ac_out_buffer == '') and
(self.ac_out_buffer == b'') and
self.producer_fifo.is_empty() and
self.connected
)
Expand All @@ -196,8 +196,7 @@ def refill_buffer (self):
self.producer_fifo.pop()
self.close()
return
else:
assert isinstance(p, bytes)
elif isinstance(p, bytes):
self.producer_fifo.pop()
self.ac_out_buffer += p
return
Expand Down Expand Up @@ -231,7 +230,7 @@ def initiate_send (self):
def discard_buffers (self):
# Emergencies only!
self.ac_in_buffer = ''
self.ac_out_buffer = ''
self.ac_out_buffer = b''
while self.producer_fifo:
self.producer_fifo.pop()

Expand Down
9 changes: 6 additions & 3 deletions supervisor/medusa/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,10 @@ def found_terminator (self):
)

def push (self, thing):
assert isinstance(thing, bytes)
thing = producers.simple_producer(thing, buffer_size=len(thing))
if isinstance(thing, str):
import pdb; pdb.set_trace()
if isinstance(thing, bytes):
thing = producers.simple_producer(thing, buffer_size=len(thing))
self.outgoing.append(thing)

def response (self, code=200):
Expand All @@ -278,10 +280,11 @@ def error (self, code):
'code': code,
'message': message,
}
s = as_bytes(s)
self['Content-Length'] = len(s)
self['Content-Type'] = 'text/html'
# make an error reply
self.push (s)
self.push(s)
self.done()

# can also be used for empty replies
Expand Down
4 changes: 2 additions & 2 deletions supervisor/tests/test_xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def getconn():
self.assertEqual(dummy_conn.closed, True)
self.assertEqual(dummy_conn.requestargs[0], 'POST')
self.assertEqual(dummy_conn.requestargs[1], '/')
self.assertEqual(dummy_conn.requestargs[2], '')
self.assertEqual(dummy_conn.requestargs[2], b'')
self.assertEqual(dummy_conn.requestargs[3]['Content-Length'], '0')
self.assertEqual(dummy_conn.requestargs[3]['Content-Type'], 'text/xml')
self.assertEqual(dummy_conn.requestargs[3]['Authorization'],
Expand All @@ -439,7 +439,7 @@ def getconn():
self.assertEqual(dummy_conn.closed, False)
self.assertEqual(dummy_conn.requestargs[0], 'POST')
self.assertEqual(dummy_conn.requestargs[1], '/')
self.assertEqual(dummy_conn.requestargs[2], '')
self.assertEqual(dummy_conn.requestargs[2], b'')
self.assertEqual(dummy_conn.requestargs[3]['Content-Length'], '0')
self.assertEqual(dummy_conn.requestargs[3]['Content-Type'], 'text/xml')
self.assertEqual(dummy_conn.requestargs[3]['Authorization'],
Expand Down
3 changes: 2 additions & 1 deletion supervisor/xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def continue_request(self, data, request):
# if we get anything but a function, it implies that this
# response doesn't need to be deferred, we can service it
# right away.
body = xmlrpc_marshal(value)
body = as_bytes(xmlrpc_marshal(value))
request['Content-Type'] = 'text/xml'
request['Content-Length'] = len(body)
request.push(body)
Expand Down Expand Up @@ -499,6 +499,7 @@ def get_connection(serverurl=serverurl):
raise ValueError('Unknown protocol for serverurl %s' % serverurl)

def request(self, host, handler, request_body, verbose=0):
request_body = as_bytes(request_body)
if not self.connection:
self.connection = self._get_connection()
self.headers = {
Expand Down

0 comments on commit 5369ff0

Please sign in to comment.