Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion proxy/http/exception/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

http
"""
from typing import Optional, TYPE_CHECKING
from typing import Any, Optional, TYPE_CHECKING

if TYPE_CHECKING:
from ..parser import HttpParser
Expand All @@ -26,5 +26,8 @@ class HttpProtocolException(Exception):
``response()`` method to optionally return custom response to client.
"""

def __init__(self, message: Optional[str] = None, **kwargs: Any) -> None:
super().__init__(message or 'Reason unknown')

def response(self, request: 'HttpParser') -> Optional[memoryview]:
return None # pragma: no cover
20 changes: 14 additions & 6 deletions proxy/http/exception/http_request_rejected.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
"""
from typing import Optional, Dict, TYPE_CHECKING
from typing import Any, Optional, Dict, TYPE_CHECKING

from .base import HttpProtocolException

Expand All @@ -25,16 +25,24 @@ class HttpRequestRejected(HttpProtocolException):
HTTP status code can be returned."""

def __init__(
self,
status_code: Optional[int] = None,
reason: Optional[bytes] = None,
headers: Optional[Dict[bytes, bytes]] = None,
body: Optional[bytes] = None,
self,
status_code: Optional[int] = None,
reason: Optional[bytes] = None,
headers: Optional[Dict[bytes, bytes]] = None,
body: Optional[bytes] = None,
**kwargs: Any,
):
self.status_code: Optional[int] = status_code
self.reason: Optional[bytes] = reason
self.headers: Optional[Dict[bytes, bytes]] = headers
self.body: Optional[bytes] = body
klass_name = self.__class__.__name__
super().__init__(
message='%s %r' % (klass_name, reason)
if reason
else klass_name,
**kwargs,
)

def response(self, _request: 'HttpParser') -> Optional[memoryview]:
if self.status_code:
Expand Down
5 changes: 4 additions & 1 deletion proxy/http/exception/proxy_auth_failed.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
auth
http
"""
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from .base import HttpProtocolException

Expand Down Expand Up @@ -43,5 +43,8 @@ class ProxyAuthenticationFailed(HttpProtocolException):
),
)

def __init__(self, **kwargs: Any) -> None:
super().__init__(self.__class__.__name__, **kwargs)

def response(self, _request: 'HttpParser') -> memoryview:
return self.RESPONSE_PKT
5 changes: 3 additions & 2 deletions proxy/http/exception/proxy_conn_failed.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

conn
"""
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from .base import HttpProtocolException

Expand Down Expand Up @@ -40,10 +40,11 @@ class ProxyConnectionFailed(HttpProtocolException):
),
)

def __init__(self, host: str, port: int, reason: str):
def __init__(self, host: str, port: int, reason: str, **kwargs: Any):
self.host: str = host
self.port: int = port
self.reason: str = reason
super().__init__('%s %s' % (self.__class__.__name__, reason), **kwargs)

def response(self, _request: 'HttpParser') -> memoryview:
return self.RESPONSE_PKT
2 changes: 1 addition & 1 deletion proxy/http/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def handle_data(self, data: memoryview) -> Optional[bool]:
break
data = optional_data
except HttpProtocolException as e:
logger.exception('HttpProtocolException raised', exc_info=e)
logger.info('HttpProtocolException: %s' % e)
response: Optional[memoryview] = e.response(self.request)
if response:
self.work.queue(response)
Expand Down
3 changes: 1 addition & 2 deletions proxy/http/proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,7 @@ def connect_upstream(self) -> None:
text_(host), port, repr(e),
) from e
else:
logger.exception('Both host and port must exist')
raise HttpProtocolException()
raise HttpProtocolException('Both host and port must exist')

#
# Interceptor related methods
Expand Down
6 changes: 2 additions & 4 deletions proxy/http/server/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,9 @@ def on_client_data(self, raw: memoryview) -> Optional[memoryview]:
# TODO: Tear down if invalid protocol exception
remaining = frame.parse(remaining)
if frame.opcode == websocketOpcodes.CONNECTION_CLOSE:
logger.warning(
raise HttpProtocolException(
'Client sent connection close packet',
)
raise HttpProtocolException()
else:
assert self.route
self.route.on_websocket_message(frame)
Expand All @@ -287,10 +286,9 @@ def on_client_data(self, raw: memoryview) -> Optional[memoryview]:
if self.pipeline_request.is_complete:
self.route.handle_request(self.pipeline_request)
if not self.pipeline_request.is_http_1_1_keep_alive:
logger.error(
raise HttpProtocolException(
'Pipelined request is not keep-alive, will tear down request...',
)
raise HttpProtocolException()
self.pipeline_request = None
return raw

Expand Down
6 changes: 2 additions & 4 deletions proxy/plugin/proxy_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,11 @@ def before_upstream_connection(
try:
self.upstream.connect()
except TimeoutError:
logger.info(
raise HttpProtocolException(
'Timed out connecting to upstream proxy {0}:{1}'.format(
*endpoint_tuple,
),
)
raise HttpProtocolException()
except ConnectionRefusedError:
# TODO(abhinavsingh): Try another choice, when all (or max configured) choices have
# exhausted, retry for configured number of times before giving up.
Expand All @@ -124,12 +123,11 @@ def before_upstream_connection(
# A periodic health check must put them back in the pool. This can be achieved
# using a datastructure without having to spawn separate thread/process for health
# check.
logger.info(
raise HttpProtocolException(
'Connection refused by upstream proxy {0}:{1}'.format(
*endpoint_tuple,
),
)
raise HttpProtocolException()
logger.debug(
'Established connection to upstream proxy {0}:{1}'.format(
*endpoint_tuple,
Expand Down
3 changes: 1 addition & 2 deletions proxy/plugin/reverse_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ def handle_request(self, request: HttpParser) -> None:
)
self.upstream.queue(memoryview(request.build()))
except ConnectionRefusedError:
logger.info(
raise HttpProtocolException(
'Connection refused by upstream server {0}:{1}'.format(
text_(self.choice.hostname), port,
),
)
raise HttpProtocolException()

def on_client_connection_close(self) -> None:
if self.upstream and not self.upstream.closed:
Expand Down