Ginés Martínez Sánchez avatar Ginés Martínez Sánchez committed b756d98 Draft

developing http server

Comments (0)

Files changed (4)

ginsfsm/examples/http/http_server_echo.py

-"""
-GObj :class:`OnHttpServer`
-==========================
-
-Http serve sample.
-
-The server echo the data.
-
-It uses :class:`ginsfsm.protocols.http.server.c_http_server.GHttpServer`.
-
-.. autoclass:: OnHttpServer
-    :members:
-
-"""
-
-import logging
-logging.basicConfig(level=logging.DEBUG)
-
-from ginsfsm.smachine import SMachine
-from ginsfsm.c_sock import GSock
-from ginsfsm.gobj import GObj
-from ginsfsm.gaplic import GAplic
-from ginsfsm.protocols.http.server.c_http_server import GHttpServer
-from ginsfsm.protocols.http.common.response import HttpSimpleOkResponse
-
-
-def ac_channel_opened(self, event):
-    print "======> channel opened"
-
-
-def ac_channel_closed(self, event):
-    print "======> channel closed"
-
-
-def ac_request(self, event):
-    print "======> channel request"
-    channel = event.source[-1]
-    request = event.request
-    response = HttpSimpleOkResponse(
-        request,
-        'Caraculo\r\nCaraculo\r\nCaraculo\r\n'
-    )
-    self.send_event(channel, 'EV_HTTP_RESPONSE', response=response)
-
-
-HTTP_ECHO_FSM = {
-    'event_list': (
-        'EV_HTTP_CHANNEL_OPENED: bottom input',
-        'EV_HTTP_CHANNEL_CLOSED: bottom input',
-        'EV_HTTP_REQUEST: bottom input',
-    ),
-    'state_list': ('ST_IDLE',),
-    'machine': {
-        'ST_IDLE':
-        (
-            ('EV_HTTP_CHANNEL_OPENED',  ac_channel_opened,  'ST_IDLE'),
-            ('EV_HTTP_CHANNEL_CLOSED',  ac_channel_closed,  'ST_IDLE'),
-            ('EV_HTTP_REQUEST',         ac_request,         'ST_IDLE'),
-        ),
-    }
-}
-
-
-HTTP_ECHO_GCONFIG = {  # type, default_value, flag, validate_function, desc
-    'verbose': [int, 1, 0, None, "Increase output verbosity. Values [0,1,2]"],
-}
-
-
-class OnHttpServer(GObj):
-    """  Server GObj.
-
-    .. ginsfsm::
-       :fsm: HTTP_ECHO_FSM
-       :gconfig: HTTP_ECHO_GCONFIG
-
-    *Input-Events:*
-
-
-    *Output-Events:*
-
-
-    """
-    def __init__(self):
-        GObj.__init__(self, HTTP_ECHO_FSM, HTTP_ECHO_GCONFIG)
-
-    def start_up(self):
-        self.server = self.create_gobj(
-            None,
-            GHttpServer,
-            self,
-            host='0.0.0.0',
-            port=8000,
-            subscriber=self,
-            inactivity_timeout=10,
-            responseless_timeout=2,
-            maximum_simultaneous_requests=0,
-        )
-
-        if self.verbose > 0:
-            settings = {
-                'GObj.trace_mach': True,
-                'GObj.logger': logging,
-            }
-            self.overwrite_parameters(-1, **settings)
-            SMachine._global_trace_mach = True
-            GSock._global_trace_dump = True
-
-
-#===============================================================
-#                   Main
-#===============================================================
-if __name__ == "__main__":
-    ga_srv = GAplic('Server')
-    ga_srv.create_gobj(
-        'server',
-        OnHttpServer,
-        None,
-    )
-
-    try:
-        ga_srv.mt_process()
-    except (KeyboardInterrupt, SystemExit):
-        print('Program stopped')

ginsfsm/examples/http/simple_http_server.py

+"""
+GObj :class:`SimpleHttpServer`
+==============================
+
+Http serve sample.
+
+The server echo the data.
+
+It uses :class:`ginsfsm.protocols.http.server.c_http_server.GHttpServer`.
+
+.. autoclass:: SimpleHttpServer
+    :members:
+
+"""
+
+import logging
+logging.basicConfig(level=logging.DEBUG)
+
+from ginsfsm.smachine import SMachine
+from ginsfsm.c_sock import GSock
+from ginsfsm.gobj import GObj
+from ginsfsm.gaplic import GAplic
+from ginsfsm.protocols.http.server.c_http_server import GHttpServer
+from ginsfsm.protocols.http.common.response import HttpSimpleOkResponse
+
+
+def ac_channel_opened(self, event):
+    print "======> channel opened"
+
+
+def ac_channel_closed(self, event):
+    print "======> channel closed"
+
+
+def ac_request(self, event):
+    print "======> channel request"
+    channel = event.source[-1]
+    request = event.request
+    response = HttpSimpleOkResponse(
+        request,
+        'Caraculo\r\nCaraculo\r\nCaraculo\r\n'
+    )
+    self.send_event(channel, 'EV_HTTP_RESPONSE', response=response)
+
+
+HTTP_ECHO_FSM = {
+    'event_list': (
+        'EV_HTTP_CHANNEL_OPENED: bottom input',
+        'EV_HTTP_CHANNEL_CLOSED: bottom input',
+        'EV_HTTP_REQUEST: bottom input',
+    ),
+    'state_list': ('ST_IDLE',),
+    'machine': {
+        'ST_IDLE':
+        (
+            ('EV_HTTP_CHANNEL_OPENED',  ac_channel_opened,  'ST_IDLE'),
+            ('EV_HTTP_CHANNEL_CLOSED',  ac_channel_closed,  'ST_IDLE'),
+            ('EV_HTTP_REQUEST',         ac_request,         'ST_IDLE'),
+        ),
+    }
+}
+
+
+HTTP_ECHO_GCONFIG = {  # type, default_value, flag, validate_function, desc
+    'verbose': [int, 1, 0, None, "Increase output verbosity. Values [0,1,2]"],
+}
+
+
+class SimpleHttpServer(GObj):
+    """  Server GObj.
+
+    .. ginsfsm::
+       :fsm: HTTP_ECHO_FSM
+       :gconfig: HTTP_ECHO_GCONFIG
+
+    *Input-Events:*
+
+
+    *Output-Events:*
+
+
+    """
+    def __init__(self):
+        GObj.__init__(self, HTTP_ECHO_FSM, HTTP_ECHO_GCONFIG)
+
+    def start_up(self):
+        self.server = self.create_gobj(
+            None,
+            GHttpServer,
+            self,
+            host='0.0.0.0',
+            port=8000,
+            subscriber=self,
+            inactivity_timeout=10,
+            responseless_timeout=2,
+            maximum_simultaneous_requests=0,
+        )
+
+        if self.verbose > 0:
+            settings = {
+                'GObj.trace_mach': True,
+                'GObj.logger': logging,
+            }
+            self.overwrite_parameters(-1, **settings)
+            SMachine._global_trace_mach = True
+            GSock._global_trace_dump = True
+
+
+#===============================================================
+#                   Main
+#===============================================================
+if __name__ == "__main__":
+    ga_srv = GAplic('Server')
+    ga_srv.create_gobj(
+        'server',
+        SimpleHttpServer,
+        None,
+    )
+
+    try:
+        ga_srv.mt_process()
+    except (KeyboardInterrupt, SystemExit):
+        print('Program stopped')

ginsfsm/protocols/http/server/c_http_clisrv.py

 class GHttpCliSrv(GObj):
     """  Http clisrv (client of server) class.
 
-    This class will process the events of the partner
+    This gobj is create by GHttpServer when it receives an EV_CONNECTED event
+    from a new gsock gobj.
+
+    This class will subscribe all the events of the partner
     :class:`ginsfsm.c_sock.GSock` gobj, to implement the http protocol.
 
     .. ginsfsm::

ginsfsm/protocols/http/server/c_http_server.py

 
         * :attr:`'EV_HTTP_CHANNEL_CLOSED'`: http channel disconnected.
 
-          The clisrv `gobj` will be destroyed.
-
-
-    *Output-Events:*
-        * :attr:`'EV_HTTP_CHANNEL_OPENED'`: new client *http channel*.
-          Event attributes:
-
-            * ``channel``: gsock attending the socket connection.
-
-        * :attr:`'EV_HTTP_CHANNEL_CLOSED'`: *http channel* closed.
-          Event attributes:
-
-            * ``channel``: gsock closing.
-
-        * :attr:`'EV_HTTP_REQUESTS'`: new *http request*.
-
-          Event attributes:
-
-            * ``channel``: gsock receiving requets.
-            * ``requests``: list of received requests. One or more than one.
-
-    *Other events:*
-
-        ``'EV_SET_TIMER'`` and  ``'EV_TIMEOUT'`` are of internal use.
-
+          The http server subcribes this event from clisrv gobj,
+          in order to destroy it when became disconnected.
     """
 
     def __init__(self):
     def getsockname(self):
         return self.serversock.socket.getsockname()
 
-    def get_ssl_certificate(self, binary_form=False):
+    def _get_ssl_certificate(self, binary_form=False):
         """Returns the client's SSL certificate, if any.
 
         To use client certificates, the HTTPServer must have been constructed
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.