Exemplo n.º 1
0
    def test_parse_invalid_uri(self):
        host, port, resource = http_header_util.parse_uri('ws:///')
        self.assertEqual(None, resource)

        host, port, resource = http_header_util.parse_uri('ws://localhost:')
        self.assertEqual(None, resource)

        host, port, resource = http_header_util.parse_uri('ws://localhost:/ws')
        self.assertEqual(None, resource)
Exemplo n.º 2
0
    def test_parse_invalid_uri(self):
        host, port, resource = http_header_util.parse_uri("ws:///")
        self.assertEqual(None, resource)

        host, port, resource = http_header_util.parse_uri("ws://localhost:")
        self.assertEqual(None, resource)

        host, port, resource = http_header_util.parse_uri("ws://localhost:/ws")
        self.assertEqual(None, resource)
Exemplo n.º 3
0
 def parse_request(self):
     if not httpserver.SimpleHTTPRequestHandler.parse_request(self):
         return False
     host, port, resource = http_header_util.parse_uri(self.path)
     if resource is None:
         self._logger.info('WS Invalid URI: %r', self.path)
         self._logger.info('WS Fallback to normal HTTP.')
         return True
     if (not self.permissive and (host is None or self.origin is None or 
                                 host.startswith('file://') or
                                 host != self.origin)) or \
                  (self.permissive and host is not None and
                   self.origin is not None and
                   not host.startswith('file://') and host != self.origin):
         self._logger.info('WS Invalid host: %r (expected: %r)',
                           host, self.origin)
         self._logger.info('WS Fallback to normal HTTP.')
         return True
     if port is not None and self.port is not None and port != self.port:
         self._logger.info('WS Invalid port: %r (expected: %r)',
                           port, self.port)
         self._logger.info('WS Fallback to normal HTTP.')
         return True
     
     self.path = resource
     return self.handle_ws(resource)
Exemplo n.º 4
0
    def test_parse_invalid_uri(self):
        host, port, resource = http_header_util.parse_uri('ws:///')
        self.assertEqual(None, resource)

        host, port, resource = http_header_util.parse_uri(
            'ws://localhost:INVALID_PORT')
        self.assertEqual(None, resource)

        host, port, resource = http_header_util.parse_uri(
            'ws://localhost:-1/ws')
        if sys.version >= '3.6':
            self.assertEqual(None, resource)
        else:
            self.assertEqual('localhost', host)
            self.assertEqual(80, port)
            self.assertEqual('/ws', resource)
Exemplo n.º 5
0
    def parse_request(self):
        """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled.
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling. See _StandaloneRequest.get_request, etc.
        cgi_req = CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self)
        if not cgi_req:
            return False
        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            logger.warning('invalid uri %r' % self.path)
            return True
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                logger.warning('invalid host %r '
                             '(expected: %r)' % (host, validation_host))
                return True
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                logger.warning('invalid port %r '
                             '(expected: %r)' % (port, validation_port))
                return True
        self.path = resource

        try:
            # Fallback to default http handler for request paths for which
            # we don't have request handlers.
            if not self._options.dispatcher.get_handler_suite(self.path):
                self.path = "/"
                 # we fall back to our default method if nothing higher is around...
            try:
                handshake.do_handshake(
                    self._request,
                    self._options.dispatcher,
                    allowDraft75=self._options.allow_draft75,
                    strict=self._options.strict)
            except handshake.AbortedByUserException, e:
                logger.warning('%s' % e)
                return False
            try:
                self._request._dispatcher = self._options.dispatcher
                self._options.dispatcher.transfer_data(self._request)
            except dispatch.DispatchException, e:
                logger.warning('%s' % e)
                return False
Exemplo n.º 6
0
    def parse_request(self):
        """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled.
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling (self.path, self.command, self.requestline, etc. See also
        # how _StandaloneRequest's members are implemented using these
        # attributes).
        if not CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self):
            return False

        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            self._logger.info('Invalid URI: %r', self.path)
            self._logger.info('Fallback to CGIHTTPRequestHandler')
            return True
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                self._logger.info('Invalid host: %r (expected: %r)',
                                  host,
                                  validation_host)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                self._logger.info('Invalid port: %r (expected: %r)',
                                  port,
                                  validation_port)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        self.path = resource

        request = standalone._StandaloneRequest(self, self._options.use_tls)

        try:
            # Fallback to default http handler for request paths for which
            # we don't have request handlers.
            #TODO fill in path determination for static files and this
            #if not self._options.dispatcher.get_handler_suite(self.path):
            self._logger.debug("Path : %r", self.path)
            if self.path != "/data" and self.path != "/shell":
                return True
        except dispatch.DispatchException, e:
            self._logger.info('%s', e)
            self.send_error(e.status)
            return False
Exemplo n.º 7
0
    def parse_request(self):
        """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled.
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling (self.path, self.command, self.requestline, etc. See also
        # how _StandaloneRequest's members are implemented using these
        # attributes).
        if not CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self):
            return False

        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            self._logger.info('Invalid URI: %r', self.path)
            self._logger.info('Fallback to CGIHTTPRequestHandler')
            return True
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                self._logger.info('Invalid host: %r (expected: %r)', host,
                                  validation_host)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                self._logger.info('Invalid port: %r (expected: %r)', port,
                                  validation_port)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        self.path = resource

        request = standalone._StandaloneRequest(self, self._options.use_tls)

        try:
            # Fallback to default http handler for request paths for which
            # we don't have request handlers.
            #TODO fill in path determination for static files and this
            #if not self._options.dispatcher.get_handler_suite(self.path):
            self._logger.debug("Path : %r", self.path)
            if self.path != "/data" and self.path != "/shell":
                return True
        except dispatch.DispatchException, e:
            self._logger.info('%s', e)
            self.send_error(e.status)
            return False
Exemplo n.º 8
0
    def test_parse_absolute_uri(self):
        host, port, resource = http_header_util.parse_uri("ws://localhost:10080/ws/test")
        self.assertEqual("localhost", host)
        self.assertEqual(10080, port)
        self.assertEqual("/ws/test", resource)

        host, port, resource = http_header_util.parse_uri("ws://example.com/ws/test")
        self.assertEqual("example.com", host)
        self.assertEqual(80, port)
        self.assertEqual("/ws/test", resource)

        host, port, resource = http_header_util.parse_uri("wss://example.com/")
        self.assertEqual("example.com", host)
        self.assertEqual(443, port)
        self.assertEqual("/", resource)

        host, port, resource = http_header_util.parse_uri("ws://example.com:8080")
        self.assertEqual("example.com", host)
        self.assertEqual(8080, port)
        self.assertEqual("/", resource)
Exemplo n.º 9
0
    def test_parse_absolute_uri(self):
        host, port, resource = http_header_util.parse_uri(
            'ws://localhost:10080/ws/test')
        self.assertEqual('localhost', host)
        self.assertEqual(10080, port)
        self.assertEqual('/ws/test', resource)

        host, port, resource = http_header_util.parse_uri(
            'ws://example.com/ws/test')
        self.assertEqual('example.com', host)
        self.assertEqual(80, port)
        self.assertEqual('/ws/test', resource)

        host, port, resource = http_header_util.parse_uri('wss://example.com/')
        self.assertEqual('example.com', host)
        self.assertEqual(443, port)
        self.assertEqual('/', resource)

        host, port, resource = http_header_util.parse_uri(
            'ws://example.com:8080')
        self.assertEqual('example.com', host)
        self.assertEqual(8080, port)
        self.assertEqual('/', resource)
Exemplo n.º 10
0
    def parse_request(self):
        """
        Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling (self.path, self.command, self.requestline, etc. See also
        # how _StandaloneRequest's members are implemented using these
        # attributes).

        # Modified
        # Most True values converted into False
        if not http.server.CGIHTTPRequestHandler.parse_request(self):
            return False

        if self._options.use_basic_auth:
            auth = self.headers.getheader('Authorization')
            if auth != self._options.basic_auth_credential:
                self.send_response(401)
                self.send_header('WWW-Authenticate',
                                 'Basic realm="Pywebsocket"')
                self.end_headers()
                self._logger.info(_('Request basic authentication'))
                return True

        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            self._logger.info(_("Invalid URI: {0}").format(self.path))
            self._logger.info(_('Fallback to CGIHTTPRequestHandler'))
            return False
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                self._logger.info(
                    _('Invalid host: {0:d} (expected: {1})').format(
                        host, validation_host))
                self._logger.info(_('Fallback to CGIHTTPRequestHandler'))
                return False
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                self._logger.info(
                    _('Invalid port: {0:d} (expected: {1})').format(
                        port, validation_port))
                self._logger.info(_('Fallback to CGIHTTPRequestHandler'))
                return False
        self.path = resource

        request = _StandaloneRequest(self, self._options.use_tls)

        try:
            # Fallback to default http handler for request paths for which
            # we do not have request handlers.
            if not self._options.dispatcher.get_handler_suite(self.path):
                self._logger.info(
                    _("No handler for resource: {0}").format(self.path))
                self._logger.info(_('Fallback to CGIHTTPRequestHandler'))
                return False
        except dispatch.DispatchException as e:
            self._logger.info(
                _("Dispatch failed for error: {0}").format(e.message))
            self.send_error(e.status)
            return False

        # If any Exceptions without except clause setup (including
        # DispatchException) is raised below this point, it will be caught
        # and logged by WebSocketServer.

        try:
            try:
                handshake.do_handshake(
                    request,
                    self._options.dispatcher,
                    allowDraft75=self._options.allow_draft75,
                    strict=self._options.strict)
            except handshake.VersionException as e:
                self._logger.info(
                    _("Handshake failed for version error: {0}").format(
                        e.message))
                self.send_response(common.HTTP_STATUS_BAD_REQUEST)
                self.send_header(common.SEC_WEBSOCKET_VERSION_HEADER,
                                 e.supported_versions)
                self.end_headers()
                return False
            except handshake.HandshakeException as e:
                # Handshake for ws(s) failed.
                self._logger.info(
                    _("Handshake failed for error: {0}").format(e.message))
                self.send_error(e.status)
                return False

            request._dispatcher = self._options.dispatcher
            self._options.dispatcher.transfer_data(request)
        except handshake.AbortedByUserException as e:
            self._logger.info(_("Aborted: {0}").format(e.message))
        return False
Exemplo n.º 11
0
 def test_parse_relative_uri(self):
     host, port, resource = http_header_util.parse_uri('/ws/test')
     self.assertEqual(None, host)
     self.assertEqual(None, port)
     self.assertEqual('/ws/test', resource)
Exemplo n.º 12
0
    def parse_request(self):
        """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled.
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling (self.path, self.command, self.requestline, etc. See also
        # how _StandaloneRequest's members are implemented using these
        # attributes).
        if not CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self):
            return False

        if self.command == "CONNECT":
            self.send_response(200, "Connected")
            self.send_header("Connection", "keep-alive")
            self.end_headers()
            return False

        if self._options.use_basic_auth:
            auth = self.headers.getheader('Authorization')
            if auth != self._options.basic_auth_credential:
                self.send_response(401)
                self.send_header('WWW-Authenticate',
                                 'Basic realm="Pywebsocket"')
                self.end_headers()
                self._logger.info('Request basic authentication')
                return False

        # Special paths for XMLHttpRequest benchmark
        xhr_benchmark_helper_prefix = '/073be001e10950692ccbf3a2ad21c245'
        parsed_path = urllib.parse.urlsplit(self.path)
        if parsed_path.path == (xhr_benchmark_helper_prefix + '_send'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_send()
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix + '_receive'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive_and_parse()
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix +
                                '_receive_getnocache'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive(int(parsed_path.query), False,
                                             False)
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix +
                                '_receive_getcache'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive(int(parsed_path.query), False,
                                             True)
            return False

        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            self._logger.info('Invalid URI: %r', self.path)
            self._logger.info('Fallback to CGIHTTPRequestHandler')
            return True
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                self._logger.info('Invalid host: %r (expected: %r)', host,
                                  validation_host)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                self._logger.info('Invalid port: %r (expected: %r)', port,
                                  validation_port)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        self.path = resource

        request = _StandaloneRequest(self, self._options.use_tls)

        try:
            # Fallback to default http handler for request paths for which
            # we don't have request handlers.
            if not self._options.dispatcher.get_handler_suite(self.path):
                self._logger.info('No handler for resource: %r', self.path)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        except dispatch.DispatchException as e:
            self._logger.info('Dispatch failed for error: %s', e)
            self.send_error(e.status)
            return False

        # If any Exceptions without except clause setup (including
        # DispatchException) is raised below this point, it will be caught
        # and logged by WebSocketServer.

        try:
            try:
                handshake.do_handshake(
                    request,
                    self._options.dispatcher,
                    allowDraft75=self._options.allow_draft75,
                    strict=self._options.strict)
            except handshake.VersionException as e:
                self._logger.info('Handshake failed for version error: %s', e)
                self.send_response(common.HTTP_STATUS_BAD_REQUEST)
                self.send_header(common.SEC_WEBSOCKET_VERSION_HEADER,
                                 e.supported_versions)
                self.end_headers()
                return False
            except handshake.HandshakeException as e:
                # Handshake for ws(s) failed.
                self._logger.info('Handshake failed for error: %s', e)
                self.send_error(e.status)
                return False

            request._dispatcher = self._options.dispatcher
            self._options.dispatcher.transfer_data(request)
        except handshake.AbortedByUserException as e:
            self._logger.info('Aborted: %s', e)
        return False
Exemplo n.º 13
0
    def parse_request(self):
        """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled.
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling (self.path, self.command, self.requestline, etc. See also
        # how _StandaloneRequest's members are implemented using these
        # attributes).
        if not CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self):
            return False

        if self._options.use_basic_auth:
            auth = self.headers.getheader('Authorization')
            if auth != self._options.basic_auth_credential:
                self.send_response(401)
                self.send_header('WWW-Authenticate',
                                 'Basic realm="Pywebsocket"')
                self.end_headers()
                self._logger.info('Request basic authentication')
                return False

        # Special paths for XMLHttpRequest benchmark
        xhr_benchmark_helper_prefix = '/073be001e10950692ccbf3a2ad21c245'
        parsed_path = urlparse.urlsplit(self.path)
        if parsed_path.path == (xhr_benchmark_helper_prefix + '_send'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_send()
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix + '_receive'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive_and_parse()
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix +
                                '_receive_getnocache'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive(int(parsed_path.query), False,
                                             False)
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix +
                                '_receive_getcache'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive(int(parsed_path.query), False,
                                             True)
            return False

        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            self._logger.info('Invalid URI: %r', self.path)
            self._logger.info('Fallback to CGIHTTPRequestHandler')
            return True
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                self._logger.info('Invalid host: %r (expected: %r)', host,
                                  validation_host)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                self._logger.info('Invalid port: %r (expected: %r)', port,
                                  validation_port)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        self.path = resource

        request = _StandaloneRequest(self, self._options.use_tls)

        try:
            # Fallback to default http handler for request paths for which
            # we don't have request handlers.
            if not self._options.dispatcher.get_handler_suite(self.path):
                self._logger.info('No handler for resource: %r', self.path)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        except dispatch.DispatchException, e:
            self._logger.info('Dispatch failed for error: %s', e)
            self.send_error(e.status)
            return False
Exemplo n.º 14
0
 def test_parse_relative_uri(self):
     host, port, resource = http_header_util.parse_uri("/ws/test")
     self.assertEqual(None, host)
     self.assertEqual(None, port)
     self.assertEqual("/ws/test", resource)
Exemplo n.º 15
0
    def parse_request(self):
        """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled.
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling (self.path, self.command, self.requestline, etc. See also
        # how _StandaloneRequest's members are implemented using these
        # attributes).
        if not CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self):
            return False

        if self._options.use_basic_auth:
            auth = self.headers.getheader('Authorization')
            if auth != self._options.basic_auth_credential:
                self.send_response(401)
                self.send_header('WWW-Authenticate',
                                 'Basic realm="Pywebsocket"')
                self.end_headers()
                self._logger.info('Request basic authentication')
                return True

        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            self._logger.info('Invalid URI: %r', self.path)
            self._logger.info('Fallback to CGIHTTPRequestHandler')
            return True
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                self._logger.info('Invalid host: %r (expected: %r)',
                                  host,
                                  validation_host)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                self._logger.info('Invalid port: %r (expected: %r)',
                                  port,
                                  validation_port)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        self.path = resource

        request = _StandaloneRequest(self, self._options.use_tls)

        try:
            # Fallback to default http handler for request paths for which
            # we don't have request handlers.
            if not self._options.dispatcher.get_handler_suite(self.path):
                self._logger.info('No handler for resource: %r',
                                  self.path)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        except dispatch.DispatchException, e:
            self._logger.info('Dispatch failed for error: %s', e)
            self.send_error(e.status)
            return False
Exemplo n.º 16
0
    def parse_request(self):
        """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled.
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling (self.path, self.command, self.requestline, etc. See also
        # how _StandaloneRequest's members are implemented using these
        # attributes).

        ### Modified
        # Most True values converted into False
        if not CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self):
            return False

        if self._options.use_basic_auth:
            auth = self.headers.getheader('Authorization')
            if auth != self._options.basic_auth_credential:
                self.send_response(401)
                self.send_header('WWW-Authenticate',
                                 'Basic realm="Pywebsocket"')
                self.end_headers()
                self._logger.info('Request basic authentication')
                return True

        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            self._logger.info('Invalid URI: %r', self.path)
            self._logger.info('Fallback to CGIHTTPRequestHandler')
            return False
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                self._logger.info('Invalid host: %r (expected: %r)', host,
                                  validation_host)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return False
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                self._logger.info('Invalid port: %r (expected: %r)', port,
                                  validation_port)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return False
        self.path = resource

        request = _StandaloneRequest(self, self._options.use_tls)

        try:
            # Fallback to default http handler for request paths for which
            # we don't have request handlers.
            if not self._options.dispatcher.get_handler_suite(self.path):
                self._logger.info('No handler for resource: %r', self.path)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return False
        except dispatch.DispatchException, e:
            self._logger.info('%s', e)
            self.send_error(e.status)
            return False
Exemplo n.º 17
0
    def parse_request(self):
        """Override BaseHTTPServer.BaseHTTPRequestHandler.parse_request.

        Return True to continue processing for HTTP(S), False otherwise.

        See BaseHTTPRequestHandler.handle_one_request method which calls
        this method to understand how the return value will be handled.
        """

        # We hook parse_request method, but also call the original
        # CGIHTTPRequestHandler.parse_request since when we return False,
        # CGIHTTPRequestHandler.handle_one_request continues processing and
        # it needs variables set by CGIHTTPRequestHandler.parse_request.
        #
        # Variables set by this method will be also used by WebSocket request
        # handling (self.path, self.command, self.requestline, etc. See also
        # how _StandaloneRequest's members are implemented using these
        # attributes).
        if not CGIHTTPServer.CGIHTTPRequestHandler.parse_request(self):
            return False

        if self.command == "CONNECT":
            self.send_response(200, "Connected")
            self.send_header("Connection", "keep-alive")
            self.end_headers()
            return False

        if self._options.use_basic_auth:
            auth = self.headers.getheader('Authorization')
            if auth != self._options.basic_auth_credential:
                self.send_response(401)
                self.send_header('WWW-Authenticate',
                                 'Basic realm="Pywebsocket"')
                self.end_headers()
                self._logger.info('Request basic authentication')
                return False

        # Special paths for XMLHttpRequest benchmark
        xhr_benchmark_helper_prefix = '/073be001e10950692ccbf3a2ad21c245'
        parsed_path = urllib.parse.urlsplit(self.path)
        if parsed_path.path == (xhr_benchmark_helper_prefix + '_send'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_send()
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix + '_receive'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive_and_parse()
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix +
                                '_receive_getnocache'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive(
                int(parsed_path.query), False, False)
            return False
        if parsed_path.path == (xhr_benchmark_helper_prefix +
                                '_receive_getcache'):
            xhr_benchmark_handler = XHRBenchmarkHandler(
                self.headers, self.rfile, self.wfile)
            xhr_benchmark_handler.do_receive(
                int(parsed_path.query), False, True)
            return False

        host, port, resource = http_header_util.parse_uri(self.path)
        if resource is None:
            self._logger.info('Invalid URI: %r', self.path)
            self._logger.info('Fallback to CGIHTTPRequestHandler')
            return True
        server_options = self.server.websocket_server_options
        if host is not None:
            validation_host = server_options.validation_host
            if validation_host is not None and host != validation_host:
                self._logger.info('Invalid host: %r (expected: %r)',
                                  host,
                                  validation_host)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        if port is not None:
            validation_port = server_options.validation_port
            if validation_port is not None and port != validation_port:
                self._logger.info('Invalid port: %r (expected: %r)',
                                  port,
                                  validation_port)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        self.path = resource

        request = _StandaloneRequest(self, self._options.use_tls)

        try:
            # Fallback to default http handler for request paths for which
            # we don't have request handlers.
            if not self._options.dispatcher.get_handler_suite(self.path):
                self._logger.info('No handler for resource: %r',
                                  self.path)
                self._logger.info('Fallback to CGIHTTPRequestHandler')
                return True
        except dispatch.DispatchException as e:
            self._logger.info('Dispatch failed for error: %s', e)
            self.send_error(e.status)
            return False

        # If any Exceptions without except clause setup (including
        # DispatchException) is raised below this point, it will be caught
        # and logged by WebSocketServer.

        try:
            try:
                handshake.do_handshake(
                    request,
                    self._options.dispatcher,
                    allowDraft75=self._options.allow_draft75,
                    strict=self._options.strict)
            except handshake.VersionException as e:
                self._logger.info('Handshake failed for version error: %s', e)
                self.send_response(common.HTTP_STATUS_BAD_REQUEST)
                self.send_header(common.SEC_WEBSOCKET_VERSION_HEADER,
                                 e.supported_versions)
                self.end_headers()
                return False
            except handshake.HandshakeException as e:
                # Handshake for ws(s) failed.
                self._logger.info('Handshake failed for error: %s', e)
                self.send_error(e.status)
                return False

            request._dispatcher = self._options.dispatcher
            self._options.dispatcher.transfer_data(request)
        except handshake.AbortedByUserException as e:
            self._logger.info('Aborted: %s', e)
        return False