Skip to content

Commit d7a568e

Browse files
[CacheByContentTypePlugin] Prepare for content type parsing (#1038)
* Move default cache directory within `proxy.py` instance data directory * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add `CacheByContentTypePlugin` skeleton and remove unnecessary mixin * Fix startup issues after removal of mixin * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * . * Fix broken mock * doc fix * doc Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 7845cbb commit d7a568e

File tree

13 files changed

+77
-120
lines changed

13 files changed

+77
-120
lines changed

helper/benchmark.sh

Lines changed: 0 additions & 70 deletions
This file was deleted.

proxy/common/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ def _env_threadless_compliant() -> bool:
145145
DEFAULT_DEVTOOLS_LOADER_ID = secrets.token_hex(8)
146146

147147
DEFAULT_DATA_DIRECTORY_PATH = os.path.join(str(pathlib.Path.home()), '.proxy')
148+
DEFAULT_CACHE_DIRECTORY_PATH = os.path.join(
149+
DEFAULT_DATA_DIRECTORY_PATH, 'cache',
150+
)
148151

149152
# Cor plugins enabled by default or via flags
150153
DEFAULT_ABC_PLUGINS = [

proxy/common/flag.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ def initialize(
395395
)
396396
os.makedirs(args.ca_cert_dir, exist_ok=True)
397397

398+
# FIXME: Necessary here until flags framework provides a way
399+
# for flag owners to initialize
400+
os.makedirs(args.cache_dir, exist_ok=True)
401+
os.makedirs(os.path.join(args.cache_dir, 'response'), exist_ok=True)
402+
398403
return args
399404

400405
@staticmethod

proxy/common/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import socket
1818
import logging
19+
import argparse
1920
import functools
2021
import ipaddress
2122
import contextlib
@@ -34,6 +35,13 @@
3435
logger = logging.getLogger(__name__)
3536

3637

38+
def tls_interception_enabled(flags: argparse.Namespace) -> bool:
39+
return flags.ca_key_file is not None and \
40+
flags.ca_cert_dir is not None and \
41+
flags.ca_signing_key_file is not None and \
42+
flags.ca_cert_file is not None
43+
44+
3745
def is_threadless(threadless: bool, threaded: bool) -> bool:
3846
# if default is threadless then return true unless
3947
# user has overridden mode using threaded flag.

proxy/http/descriptors.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
from typing import Any
12-
1311
from ..common.types import Readables, Writables, Descriptors
1412

1513

@@ -19,10 +17,6 @@ class DescriptorsHandlerMixin:
1917
include web and proxy plugins. By using DescriptorsHandlerMixin, class
2018
becomes complaint with core event loop."""
2119

22-
def __init__(self, *args: Any, **kwargs: Any) -> None:
23-
# FIXME: Required for multi-level inheritance to work
24-
super().__init__(*args, **kwargs) # type: ignore
25-
2620
# @abstractmethod
2721
async def get_descriptors(self) -> Descriptors:
2822
"""Implementations must return a list of descriptions that they wish to

proxy/http/mixins.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

proxy/http/plugin.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
from abc import ABC, abstractmethod
1414
from typing import TYPE_CHECKING, List, Union, Optional
1515

16-
from .mixins import TlsInterceptionPropertyMixin
1716
from .parser import HttpParser
1817
from .connection import HttpClientConnection
1918
from ..core.event import EventQueue
2019
from .descriptors import DescriptorsHandlerMixin
20+
from ..common.utils import tls_interception_enabled
2121

2222

2323
if TYPE_CHECKING: # pragma: no cover
@@ -26,7 +26,6 @@
2626

2727
class HttpProtocolHandlerPlugin(
2828
DescriptorsHandlerMixin,
29-
TlsInterceptionPropertyMixin,
3029
ABC,
3130
):
3231
"""Base HttpProtocolHandler Plugin class.
@@ -59,7 +58,6 @@ def __init__(
5958
event_queue: Optional[EventQueue] = None,
6059
upstream_conn_pool: Optional['UpstreamConnectionPool'] = None,
6160
):
62-
super().__init__(uid, flags, client, event_queue, upstream_conn_pool)
6361
self.uid: str = uid
6462
self.flags: argparse.Namespace = flags
6563
self.client: HttpClientConnection = client
@@ -95,3 +93,7 @@ def on_client_connection_close(self) -> None:
9593
perform any cleanup work here.
9694
"""
9795
pass # pragma: no cover
96+
97+
@property
98+
def tls_interception_enabled(self) -> bool:
99+
return tls_interception_enabled(self.flags)

proxy/http/proxy/plugin.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
from abc import ABC
1313
from typing import TYPE_CHECKING, Any, Dict, Tuple, Optional
1414

15-
from ..mixins import TlsInterceptionPropertyMixin
1615
from ..parser import HttpParser
1716
from ..connection import HttpClientConnection
1817
from ...core.event import EventQueue
1918
from ..descriptors import DescriptorsHandlerMixin
19+
from ...common.utils import tls_interception_enabled
2020

2121

2222
if TYPE_CHECKING: # pragma: no cover
@@ -25,7 +25,6 @@
2525

2626
class HttpProxyBasePlugin(
2727
DescriptorsHandlerMixin,
28-
TlsInterceptionPropertyMixin,
2928
ABC,
3029
):
3130
"""Base HttpProxyPlugin Plugin class.
@@ -40,7 +39,6 @@ def __init__(
4039
event_queue: EventQueue,
4140
upstream_conn_pool: Optional['UpstreamConnectionPool'] = None,
4241
) -> None:
43-
super().__init__(uid, flags, client, event_queue, upstream_conn_pool)
4442
self.uid = uid # pragma: no cover
4543
self.flags = flags # pragma: no cover
4644
self.client = client # pragma: no cover
@@ -170,4 +168,4 @@ def do_intercept(self, _request: HttpParser) -> bool:
170168
flags BUT only conditionally enable interception for
171169
certain requests.
172170
"""
173-
return self.tls_interception_enabled
171+
return tls_interception_enabled(self.flags)

proxy/plugin/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
from .custom_dns_resolver import CustomDnsResolverPlugin
2727
from .filter_by_client_ip import FilterByClientIpPlugin
2828
from .filter_by_url_regex import FilterByURLRegexPlugin
29+
from .cache_by_content_type import CacheByContentTypePlugin
2930
from .modify_chunk_response import ModifyChunkResponsePlugin
3031
from .redirect_to_custom_server import RedirectToCustomServerPlugin
3132

3233

3334
__all__ = [
3435
'CacheResponsesPlugin',
36+
'CacheByContentTypePlugin',
3537
'BaseCacheResponsesPlugin',
3638
'FilterByUpstreamHostPlugin',
3739
'ManInTheMiddlePlugin',

proxy/plugin/cache/cache_responses.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11+
import os
1112
import multiprocessing
12-
from typing import Any
13+
from typing import Any, Dict, Optional
1314

1415
from .base import BaseCacheResponsesPlugin
1516
from .store.disk import OnDiskCacheStore
@@ -24,6 +25,16 @@ class CacheResponsesPlugin(BaseCacheResponsesPlugin):
2425
def __init__(self, *args: Any, **kwargs: Any) -> None:
2526
super().__init__(*args, **kwargs)
2627
self.disk_store = OnDiskCacheStore(
27-
uid=self.uid, cache_dir=self.flags.cache_dir,
28+
uid=self.uid,
29+
cache_dir=os.path.join(
30+
self.flags.cache_dir,
31+
'responses',
32+
),
2833
)
2934
self.set_store(self.disk_store)
35+
36+
def on_access_log(self, context: Dict[str, Any]) -> Optional[Dict[str, Any]]:
37+
context.update({
38+
'cache_file_path': self.disk_store.cache_file_path,
39+
})
40+
return super().on_access_log(context)

0 commit comments

Comments
 (0)