python: wrap socket in overridden process_request.

This commit is contained in:
Tatsuhiro Tsujikawa 2012-08-24 23:21:36 +09:00
parent e208305505
commit 9d66fc4b88
1 changed files with 56 additions and 55 deletions

View File

@ -1235,7 +1235,7 @@ try:
def send_cb(self, session, data):
return self.sslsock.send(data)
return self.request.send(data)
def read_cb(self, session, stream_id, length, read_ctrl, source):
data = source.read(length)
@ -1277,25 +1277,11 @@ try:
def handle(self):
self.request.setsockopt(socket.IPPROTO_TCP,
socket.TCP_NODELAY, True)
# TODO We need to call handshake manually because 3.3.0b2
# crashes if do_handshake_on_connect=True
self.sslsock = self.server.ctx.wrap_socket(\
self.request,
server_side=True,
do_handshake_on_connect=False)
self.sslsock.setblocking(False)
while True:
try:
self.sslsock.do_handshake()
break
except ssl.SSLWantReadError as e:
select.select([self.sslsock], [], [])
except ssl.SSLWantWriteError as e:
select.select([], [self.sslsock], [])
self.request.do_handshake()
self.request.setblocking(False)
version = npn_get_version(self.sslsock.selected_npn_protocol())
version = npn_get_version(self.request.selected_npn_protocol())
if version == 0:
return
@ -1310,13 +1296,14 @@ try:
self.session.submit_settings(\
FLAG_SETTINGS_NONE,
[(SETTINGS_MAX_CONCURRENT_STREAMS, ID_FLAG_SETTINGS_NONE, 100)]
[(SETTINGS_MAX_CONCURRENT_STREAMS, ID_FLAG_SETTINGS_NONE,
100)]
)
while self.session.want_read() or self.session.want_write():
want_read = want_write = False
try:
data = self.sslsock.recv(4096)
data = self.request.recv(4096)
if data:
self.session.recv(data)
else:
@ -1333,9 +1320,11 @@ try:
want_write = True
if want_read or want_write:
select.select([self.sslsock] if want_read else [],
[self.sslsock] if want_write else [],
select.select([self.request] if want_read else [],
[self.request] if want_write else [],
[])
finally:
self.request.setblocking(True)
# The following methods and attributes are copied from
# Lib/http/server.py of cpython source code
@ -1463,6 +1452,18 @@ try:
server_thread.daemon = daemon
server_thread.start()
def process_request(self, request, client_address):
# ThreadingMixIn.process_request() dispatches request and
# client_address to separate thread. To cleanly shutdown
# SSL/TLS wrapped socket, we wrap socket here.
# SSL/TLS handshake is postponed to each thread.
request = self.ctx.wrap_socket(\
request, server_side=True, do_handshake_on_connect=False)
socketserver.ThreadingMixIn.process_request(self,
request, client_address)
except ImportError:
# No server for 2.x because they lack TLS NPN.
pass