Source

ginsfsm / ginsfsm / protocols / wsgi / server / c_wsgi_server.py

The default branch has multiple heads

# -*- encoding: utf-8 -*-
"""
GObj :class:`GWsgiServer`
=========================

WSGI server.

It uses :class:`ginsfsm.protocols.http.server.c_http_server.GHttpServer`.

.. autoclass:: GWsgiServer
    :members:

"""

from ginsfsm.gobj import GObj
from ginsfsm.compat import string_types
from ginsfsm.globals import get_global_app
from ginsfsm.protocols.http.server.c_http_server import GHttpServer
from ginsfsm.protocols.wsgi.common.wsgi_response import WsgiResponse
from ginsfsm.protocols.http.server.c_http_server import GHTTPSERVER_GCONFIG


def ac_channel_opened(self, event):
    """ New client opened.
    """


def ac_channel_closed(self, event):
    """ Client closed.
    """


def ac_request(self, event):
    channel = event.source[-1]
    request = event.request
    application = self.select_app(request)
    response = WsgiResponse(request, self, application)
    self.send_event(channel, 'EV_HTTP_RESPONSE', response=response)


GWSGISERVER_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,  None),
            ('EV_HTTP_CHANNEL_CLOSED',  ac_channel_closed,  None),
            ('EV_HTTP_REQUEST',         ac_request,         None),
        ),
    }
}

GWSGISERVER_GCONFIG = GHTTPSERVER_GCONFIG.copy()
GWSGISERVER_GCONFIG.update({
    'application': [None, None, 0, None, "wsgi application"],
})


class GWsgiServer(GObj):
    """  WSGI Server gobj.

    .. ginsfsm::
       :fsm: GWSGISERVER_FSM
       :gconfig: GWSGISERVER_GCONFIG

    *Input-Events:*

        * :attr:`'EV_HTTP_CHANNEL_OPENED'`: new http client.

        * :attr:`'EV_HTTP_CHANNEL_CLOSED'`: http client closed.

        * :attr:`'EV_HTTP_REQUEST'`: new http request.

          Event attributes:

            * ``request``: http request.

        See :class:`ginsfsm.protocols.http.server.c_http_clisrv.GHttpCliSrv`.

    """

    def __init__(self):
        GObj.__init__(self, GWSGISERVER_FSM, GWSGISERVER_GCONFIG)
        self._n_connected_clisrv = 0

    def start_up(self):
        if self.name and len(self.name):
            prefix_name = self.name
        else:
            prefix_name = None

        self.ghttpserver = self.create_gobj(
            prefix_name + '.http-server' if prefix_name else None,
            GHttpServer,
            self,
            subscriber=self,
            host=self.host,
            port=self.port,
            origins=self.origins,
            inactivity_timeout=10,
            responseless_timeout=2,
            maximum_simultaneous_requests=0,
        )
        self.serversock = self.ghttpserver.gserversock.socket

        # Used in environ
        self.effective_host, self.effective_port = self.getsockname()
        self.server_name = self._get_server_name(self.host)

    def select_app(self, request):
        # Starting from ini file, the application references is a string
        # because the wsgi-apps are loaded later than main gaplic.
        if isinstance(self.application, string_types):
            self.application = get_global_app(self.application)
        return self.application

    def _get_server_name(self, ip):
        """Given an IP or hostname, try to determine the server name."""
        if ip:
            srv_name = str(ip)
        else:
            srv_name = str(self.serversock.socketmod.gethostname())
        # Convert to a host name if necessary.
        for c in srv_name:
            if c != '.' and not c.isdigit():
                return srv_name
        try:
            if srv_name == '0.0.0.0':
                return 'localhost'
            srv_name = self.serversock.socketmod.gethostbyaddr(srv_name)[0]
        except:
            pass
        return srv_name

    def getsockname(self):
        return self.serversock.getsockname()