Home | History | Annotate | Download | only in mod_pywebsocket
      1 # Copyright 2009, Google Inc.
      2 # All rights reserved.
      3 #
      4 # Redistribution and use in source and binary forms, with or without
      5 # modification, are permitted provided that the following conditions are
      6 # met:
      7 #
      8 #     * Redistributions of source code must retain the above copyright
      9 # notice, this list of conditions and the following disclaimer.
     10 #     * Redistributions in binary form must reproduce the above
     11 # copyright notice, this list of conditions and the following disclaimer
     12 # in the documentation and/or other materials provided with the
     13 # distribution.
     14 #     * Neither the name of Google Inc. nor the names of its
     15 # contributors may be used to endorse or promote products derived from
     16 # this software without specific prior written permission.
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 
     31 """PythonHeaderParserHandler for mod_pywebsocket.
     32 
     33 Apache HTTP Server and mod_python must be configured such that this
     34 function is called to handle Web Socket request.
     35 """
     36 
     37 
     38 from mod_python import apache
     39 
     40 import dispatch
     41 import handshake
     42 import util
     43 
     44 
     45 # PythonOption to specify the handler root directory.
     46 _PYOPT_HANDLER_ROOT = 'mod_pywebsocket.handler_root'
     47 
     48 # PythonOption to specify the handler scan directory.
     49 # This must be a directory under the root directory.
     50 # The default is the root directory.
     51 _PYOPT_HANDLER_SCAN = 'mod_pywebsocket.handler_scan'
     52 
     53 
     54 def _create_dispatcher():
     55     _HANDLER_ROOT = apache.main_server.get_options().get(
     56             _PYOPT_HANDLER_ROOT, None)
     57     if not _HANDLER_ROOT:
     58         raise Exception('PythonOption %s is not defined' % _PYOPT_HANDLER_ROOT,
     59                         apache.APLOG_ERR)
     60     _HANDLER_SCAN = apache.main_server.get_options().get(
     61             _PYOPT_HANDLER_SCAN, _HANDLER_ROOT)
     62     dispatcher = dispatch.Dispatcher(_HANDLER_ROOT, _HANDLER_SCAN)
     63     for warning in dispatcher.source_warnings():
     64         apache.log_error('mod_pywebsocket: %s' % warning, apache.APLOG_WARNING)
     65     return dispatcher
     66 
     67 
     68 # Initialize
     69 _dispatcher = _create_dispatcher()
     70 
     71 
     72 def headerparserhandler(request):
     73     """Handle request.
     74 
     75     Args:
     76         request: mod_python request.
     77 
     78     This function is named headerparserhandler because it is the default name
     79     for a PythonHeaderParserHandler.
     80     """
     81 
     82     try:
     83         handshaker = handshake.Handshaker(request, _dispatcher)
     84         handshaker.do_handshake()
     85         request.log_error('mod_pywebsocket: resource: %r' % request.ws_resource,
     86                           apache.APLOG_DEBUG)
     87         _dispatcher.transfer_data(request)
     88     except handshake.HandshakeError, e:
     89         # Handshake for ws/wss failed.
     90         # But the request can be valid http/https request.
     91         request.log_error('mod_pywebsocket: %s' % e, apache.APLOG_INFO)
     92         return apache.DECLINED
     93     except dispatch.DispatchError, e:
     94         request.log_error('mod_pywebsocket: %s' % e, apache.APLOG_WARNING)
     95         return apache.DECLINED
     96     return apache.DONE  # Return DONE such that no other handlers are invoked.
     97 
     98 
     99 # vi:sts=4 sw=4 et
    100