1 # Copyright 2009, Google Inc.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
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
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.
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.
31 """PythonHeaderParserHandler for mod_pywebsocket.
33 Apache HTTP Server and mod_python must be configured such that this
34 function is called to handle Web Socket request.
38 from mod_python import apache
45 # PythonOption to specify the handler root directory.
46 _PYOPT_HANDLER_ROOT = 'mod_pywebsocket.handler_root'
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'
54 def _create_dispatcher():
55 _HANDLER_ROOT = apache.main_server.get_options().get(
56 _PYOPT_HANDLER_ROOT, None)
58 raise Exception('PythonOption %s is not defined' % _PYOPT_HANDLER_ROOT,
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)
69 _dispatcher = _create_dispatcher()
72 def headerparserhandler(request):
76 request: mod_python request.
78 This function is named headerparserhandler because it is the default name
79 for a PythonHeaderParserHandler.
83 handshaker = handshake.Handshaker(request, _dispatcher)
84 handshaker.do_handshake()
85 request.log_error('mod_pywebsocket: resource: %r' % request.ws_resource,
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.