Commits

Ginés Martínez Sánchez  committed 347d41d Draft

SSL integrated in http server, but without test it

  • Participants
  • Parent commits 7f2c7ff

Comments (0)

Files changed (3)

File ginsfsm/c_sock.py

     'ip': [str, '', 0, None, ""],
     'host': [str, '', 0, None, "server or client host (ip or name)"],
     'port': [int, 0, 0, None, "server or client port"],
-    'ssl_options': [dict, None, 0, None, "ssl options"],
+    'ssl_options': [dict, {}, 0, None, "ssl options"],
     'tx_buffer_size': [int, 4096, 0, None, ""],
     'connected_event_name': [str, 'EV_CONNECTED', 0, None,
         "Must be empty if you don't want receive this event"],
         # connect. This doesn't verify that the keys are legitimate, but
         # the SSL module doesn't do that until there is a connected socket
         # which seems like too much work
-        if self.ssl_options is not None:
+        if self.ssl_options:
             # Only certfile is required: it can contain both keys
             if 'certfile' not in self.ssl_options:
                 raise KeyError('missing key "certfile" in ssl_options')
         # followed by _handle_write we need this to be synchronous.
         self.socket = ssl.wrap_socket(self.socket,
                                       do_handshake_on_connect=False,
-                                      **self._ssl_options)
+                                      **self.ssl_options)
         super(GSSLSock, self).handle_connect()
 
     # TODO para revisar
 
     def recv(self):
+        """ TODO: pending to include
         while True:
             # Read from the socket until we get EWOULDBLOCK or equivalent.
             # SSL sockets do some internal buffering, and if the data is
             # try to read it.
             if not super(GSSLSock, self).recv():
                 break
-
-
-
+        """
 
         if self._ssl_accepting:
             # If the handshake hasn't finished yet, there can't be anything
             # to read (attempting to read may or may not raise an exception
             # depending on the SSL version)
-            return None
+            return b''
+
         try:
             # SSLSocket objects have both a read() and recv() method,
             # while regular sockets only have recv().
             # The recv() method blocks (at least in python 2.6) if it is
             # called when there is nothing to read, so we have to use
             # read() instead.
-            chunk = self.socket.read(self.read_chunk_size)
-        except ssl.SSLError, e:
+            data = self.socket.read(8192)
+            if not data:
+                # a closed connection is indicated by signaling
+                # a read condition, and having recv() return 0.
+                self.handle_close()
+                return b''
+            else:
+                return data
+        except ssl.SSLError as why:
             # SSLError is a subclass of socket.error, so this except
             # block must come first.
-            if e.args[0] == ssl.SSL_ERROR_WANT_READ:
-                return None
+            if why.args[0] == ssl.SSL_ERROR_WANT_READ:
+                return b''
             else:
                 raise
-        except socket.error, e:
-            if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
-                return None
+        except socket.error as why:
+            # winsock sometimes throws ENOTCONN
+            if why.args[0] in _DISCONNECTED:
+                self.handle_close()
+                return b''
+            elif why.args[0] in (EWOULDBLOCK, EAGAIN):
+                return b''
             else:
                 raise
-        if not chunk:
-            self.close()
-            return None
-        return chunk
 
 
 # ---------------------------------------------------------------------------

File ginsfsm/c_srv_sock.py

         ],
     'host': [str, '', 0, None, "server host (ip or name)"],
     'port': [int, 0, 0, None, "server port"],
-    'ssl_options': [dict, None, 0, None, "ssl options"],
+    'ssl_options': [dict, {}, 0, None, "ssl options"],
 }
 
 
             sock.setsockopt(level, optname, value)
 
         # copied from tornado.netutil.py
-        if self.ssl_options is not None:
+        if self.ssl_options:
             assert ssl, "Python 2.6+ and OpenSSL required for SSL"
             try:
                 sock = ssl.wrap_socket(
             prefix_name = None
         channel = '.clisrv-gsock-%x' % self._n_clisrv
 
-        if self.ssl_options is not None:
+        if self.ssl_options:
             gsock_class = GSSLSock
         else:
             gsock_class = GSock

File ginsfsm/protocols/http/common/parser.py

     ChunkedReceiver,
     )
 
-from ginsfsm.wsgi.utilities import (
+from ginsfsm.protocols.http.common.utilities import (
     find_double_newline,
     RequestEntityTooLarge,
     RequestHeaderFieldsTooLarge,