Skip to content

Commit

Permalink
Add get/read/write descriptor API for HttpWebServerBasePlugin too
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavsingh committed Oct 29, 2021
1 parent a2f7fc4 commit ddc9439
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion proxy/http/proxy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def name(self) -> str:

# TODO(abhinavsingh): get_descriptors, write_to_descriptors, read_from_descriptors
# can be placed into their own abstract class which can then be shared by
# HttpProxyBasePlugin and HttpProtocolHandlerPlugin class.
# HttpProxyBasePlugin, HttpWebServerBasePlugin and HttpProtocolHandlerPlugin class.
#
# Currently code has been shamelessly copied. Also these methods are not
# marked as abstract to avoid breaking custom plugins written by users for
Expand Down
36 changes: 35 additions & 1 deletion proxy/http/server/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
"""
from abc import ABC, abstractmethod
import socket
import argparse

from abc import ABC, abstractmethod
from typing import List, Tuple
from uuid import UUID

from ..websocket import WebsocketFrame
from ..parser import HttpParser

from ...common.types import Readables, Writables
from ...core.connection import TcpClientConnection
from ...core.event import EventQueue

Expand All @@ -33,6 +37,36 @@ def __init__(
self.client = client
self.event_queue = event_queue

# TODO(abhinavsingh): get_descriptors, write_to_descriptors, read_from_descriptors
# can be placed into their own abstract class which can then be shared by
# HttpProxyBasePlugin, HttpWebServerBasePlugin and HttpProtocolHandlerPlugin class.
#
# Currently code has been shamelessly copied. Also these methods are not
# marked as abstract to avoid breaking custom plugins written by users for
# previous versions of proxy.py
#
# Since 3.4.0
#
# @abstractmethod
def get_descriptors(
self) -> Tuple[List[socket.socket], List[socket.socket]]:
return [], [] # pragma: no cover

# @abstractmethod
def write_to_descriptors(self, w: Writables) -> bool:
"""Implementations must now write/flush data over the socket.
Note that buffer management is in-build into the connection classes.
Hence implementations MUST call `flush` here, to send any buffered data
over the socket.
"""
return False # pragma: no cover

# @abstractmethod
def read_from_descriptors(self, r: Readables) -> bool:
"""Implementations must now read data over the socket."""
return False # pragma: no cover

@abstractmethod
def routes(self) -> List[Tuple[int, str]]:
"""Return List(protocol, path) that this plugin handles."""
Expand Down
15 changes: 8 additions & 7 deletions proxy/http/server/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(

if b'HttpWebServerBasePlugin' in self.flags.plugins:
for klass in self.flags.plugins[b'HttpWebServerBasePlugin']:
instance = klass(
instance: HttpWebServerBasePlugin = klass(
self.uid,
self.flags,
self.client,
Expand Down Expand Up @@ -181,11 +181,16 @@ def on_request_complete(self) -> Union[socket.socket, bool]:
self.client.queue(self.DEFAULT_404_RESPONSE)
return True

# TODO(abhinavsingh): Call plugin get/read/write descriptor callbacks
def get_descriptors(
self) -> Tuple[List[socket.socket], List[socket.socket]]:
return [], []

def write_to_descriptors(self, w: Writables) -> bool:
pass
return False

def read_from_descriptors(self, r: Readables) -> bool:
pass
return False

def on_client_data(self, raw: memoryview) -> Optional[memoryview]:
if self.switched_protocol == httpProtocolTypes.WEBSOCKET:
Expand Down Expand Up @@ -245,7 +250,3 @@ def access_log(self) -> None:
text_(self.request.method),
text_(self.request.path),
(time.time() - self.start_time) * 1000))

def get_descriptors(
self) -> Tuple[List[socket.socket], List[socket.socket]]:
return [], []

0 comments on commit ddc9439

Please sign in to comment.