Issue2439

classification
Title: 'SSLSocket' object has no attribute 'accept'
Type: behaviour Severity: major
Components: Library Versions: Jython 2.7
Milestone: Jython 2.7.1
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: darjus, kaneg, zyasoft
Priority: Keywords:

Created on 2015-12-06.06:16:54 by kaneg, last changed 2015-12-21.18:09:38 by zyasoft.

Messages
msg10515 (view) Author: Kane Gong (kaneg) Date: 2015-12-06.06:16:53
When executing below code, there is one exception thrown when visiting below Https Server in web browser. The code can run on Python 2.7.
########## code start
import BaseHTTPServer, SimpleHTTPServer
import ssl

httpd = BaseHTTPServer.HTTPServer(('localhost', 4433), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
                               certfile='server.crt',
                               server_side=True,
                               keyfile='server.key'
                               )
httpd.serve_forever()

###########code end
The stack trace:

File "/Users/kaneg/jython2.7/Lib/SocketServer.py", line 238, in serve_forever
    self._handle_request_noblock()
  File "/Users/kaneg/jython2.7/Lib/SocketServer.py", line 290, in _handle_request_noblock
    request, client_address = self.get_request()
  File "/Users/kaneg/jython2.7/Lib/SocketServer.py", line 290, in _handle_request_noblock
    request, client_address = self.get_request()
  File "/Users/kaneg/jython2.7/Lib/SocketServer.py", line 471, in get_request
    return self.socket.accept()
AttributeError: 'SSLSocket' object has no attribute 'accept'

According to code in ssl.py, there is no accept method in SSLSocket of Jython2.7, but in Python2.7. 
Is it a limitation or a bug?
msg10530 (view) Author: Jim Baker (zyasoft) Date: 2015-12-11.00:10:59
Should be a straightforward fix. Let's do this after the SSL changes that Darjus is doing.
msg10533 (view) Author: Darjus Loktevic (darjus) Date: 2015-12-13.14:29:52
Accept should now be implemented, though the server test provided still does not work. It is hanging with no errors. Need to dig deeper.
msg10534 (view) Author: Darjus Loktevic (darjus) Date: 2015-12-14.13:56:01
Dug some more. It seems the whole listen/childsocket thing needs to be implemented as well?
Jim, please advise.
msg10535 (view) Author: Jim Baker (zyasoft) Date: 2015-12-14.16:14:31
Darjus, right. We need to start with these additions to SSLSocket class:

+    def listen(self, backlog):
+        self.sock.listen(backlog)
+
+    def accept(self):
+        """Accepts a new connection from a remote client, and returns
+        a tuple containing that new connection wrapped with a server-side
+        SSL channel, and the address of the remote client."""
+
+        newsock, addr = self.sock.accept()
+        newsock = self._context.wrap_socket(newsock,
+                    do_handshake_on_connect=self.do_handshake_on_connect,
+                    suppress_ragged_eofs=self.suppress_ragged_eofs,
+                    server_side=True)
+        return newsock, addr

(I got that from CPython's implementation, with some minor changes.)

But the key piece is to insert the SSL handler at the right time in the pipeline, by using the ChildSocketHandler, so the socket is actually wrapped. This intersects with the inbound handler - we have all this latch machinery to do late insertions with respect to send/recv - otherwise one will see a race where ciphertext is being interpreted as plaintext.

I think it would be a good idea if I documented the state transitions for socket, select, ssl - it's definitely a complicated state machine now!
msg10537 (view) Author: Jim Baker (zyasoft) Date: 2015-12-15.01:07:36
Fixed as of https://hg.python.org/jython/rev/06cfd92ad6a0
History
Date User Action Args
2015-12-21 18:09:38zyasoftsetstatus: pending -> closed
2015-12-15 01:07:37zyasoftsetstatus: open -> pending
assignee: zyasoft
resolution: fixed
messages: + msg10537
2015-12-14 16:14:32zyasoftsetmessages: + msg10535
2015-12-14 13:56:02darjussetmessages: + msg10534
2015-12-13 14:29:53darjussetmessages: + msg10533
2015-12-11 00:10:59zyasoftsetnosy: + darjus, zyasoft
messages: + msg10530
milestone: Jython 2.7.1
2015-12-06 06:16:54kanegcreate