Skip to content

Commit

Permalink
change unproxy header to constant
Browse files Browse the repository at this point in the history
  • Loading branch information
noO0ob committed Jan 21, 2025
1 parent 5163219 commit 2f41019
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
14 changes: 14 additions & 0 deletions lyrebird/mock/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ def get_and_update_selected_filter_by_name(self, filter_name):
EMIT_INTERVAL = 1
last_emit_time = {}


"""
request header may casuse exception:
- Lyrebird internal header. eg. Lyrebird-Raw-Header
- The header should change each forwarding. eg. Host
"""
LYREBIRD_UNPROXY_HEADERS = (
'cache-control',
'host',
'transfer-encoding',
'lyrebird-client-address'
)


application = Application()

def make_ok_response(**kwargs):
Expand Down
13 changes: 7 additions & 6 deletions lyrebird/mock/handlers/handler_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from lyrebird.log import get_logger
from lyrebird.utils import CaseInsensitiveDict
from lyrebird.mock.blueprints.apis.bandwidth import config
from lyrebird.mock.context import LYREBIRD_UNPROXY_HEADERS
from urllib.parse import urlparse, unquote
from .http_data_helper import DataHelper
from .http_header_helper import HeadersHelper
Expand Down Expand Up @@ -66,14 +67,14 @@ def _parse_request(self):

raw_headers = None
# Read raw headers
if '_raw_header' in self.request.environ:
# Proxy-Raw-Headers will be removed in future
if 'Proxy-Raw-Headers' in self.request.headers:
raw_headers = json.loads(self.request.headers['Proxy-Raw-Headers'])
elif '_raw_header' in self.request.environ:
raw_headers = CaseInsensitiveDict(self.request.environ['_raw_header'])
for key in ('cache-control', 'host', 'transfer-encoding', 'lyrebird-client-address'):
for key in LYREBIRD_UNPROXY_HEADERS:
if key in raw_headers:
del raw_headers[key]
# Proxy-Raw-Headers will be removed in future
elif 'Proxy-Raw-Headers' in self.request.headers:
raw_headers = json.loads(self.request.headers['Proxy-Raw-Headers'])

# parse path
request_info = self._read_origin_request_info_from_url()
Expand Down Expand Up @@ -214,7 +215,7 @@ def get_request_headers(self):
headers = {}
unproxy_headers = application.config.get('proxy.ignored_headers', {})
for name, value in self.flow['request']['headers'].items():
if not value or name in ['Cache-Control', 'Host', 'Transfer-Encoding']:
if not value or name.lower() in LYREBIRD_UNPROXY_HEADERS:
continue
if name in unproxy_headers and unproxy_headers[name] in value:
continue
Expand Down
21 changes: 11 additions & 10 deletions lyrebird/mock/handlers/http_data_helper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict
from . import content_encoding, content_type
from lyrebird.utils import CaseInsensitiveDict
from lyrebird.mock.context import LYREBIRD_UNPROXY_HEADERS
import json

origin2flow_handlers = OrderedDict({
Expand All @@ -25,15 +26,15 @@ def origin2flow(origin_obj, output=None, chain=None):
if not _data:
return

if 'Proxy-Raw-Headers' in origin_obj.headers:
_origin_headers = json.loads(origin_obj.headers['Proxy-Raw-Headers'])
raw_headers = CaseInsensitiveDict(_origin_headers)
# Read raw headers, support the request from extra mock 9999 port
if hasattr(origin_obj, 'environ') and '_raw_header' in origin_obj.environ:
elif hasattr(origin_obj, 'environ') and '_raw_header' in origin_obj.environ:
raw_headers = CaseInsensitiveDict(origin_obj.environ['_raw_header'])
for key in ('cache-control', 'host', 'transfer-encoding', 'lyrebird-client-address'):
for key in LYREBIRD_UNPROXY_HEADERS:
if key in raw_headers:
del raw_headers[key]
elif 'Proxy-Raw-Headers' in origin_obj.headers:
_origin_headers = json.loads(origin_obj.headers['Proxy-Raw-Headers'])
raw_headers = CaseInsensitiveDict(_origin_headers)
else:
raw_headers = origin_obj.headers

Expand Down Expand Up @@ -74,15 +75,15 @@ def origin2string(origin_obj, output=None):
if not _data:
return

if 'Proxy-Raw-Headers' in origin_obj.headers:
_origin_headers = json.loads(origin_obj.headers['Proxy-Raw-Headers'])
raw_headers = CaseInsensitiveDict(_origin_headers)
# Read raw headers, support the request from extra mock 9999 port
if hasattr(origin_obj, 'environ') and '_raw_header' in origin_obj.environ:
elif hasattr(origin_obj, 'environ') and '_raw_header' in origin_obj.environ:
raw_headers = CaseInsensitiveDict(origin_obj.environ['_raw_header'])
for key in ('cache-control', 'host', 'transfer-encoding', 'lyrebird-client-address'):
for key in LYREBIRD_UNPROXY_HEADERS:
if key in raw_headers:
del raw_headers[key]
elif 'Proxy-Raw-Headers' in origin_obj.headers:
_origin_headers = json.loads(origin_obj.headers['Proxy-Raw-Headers'])
raw_headers = CaseInsensitiveDict(_origin_headers)
else:
raw_headers = origin_obj.headers

Expand Down

0 comments on commit 2f41019

Please sign in to comment.