Skip to content

Commit

Permalink
merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoye committed Nov 1, 2022
2 parents 779e2df + afdc7f5 commit 8e75eb5
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 25 deletions.
1 change: 0 additions & 1 deletion frontend/src/views/inspector/FlowList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ export default {
.flow-list-item-url {
display: inline-block;
word-break: keep-all;
max-width: 840px;
width: calc(100% - 50px);
white-space: nowrap;
overflow: hidden;
Expand Down
20 changes: 12 additions & 8 deletions lyrebird/mock/handlers/handler_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __init__(self, request):
'headers': utils.CaseInsensitiveDict({})
})
})
self.request_chain = []
self.response_chain = []
self.client_address = None
self.is_request_edited = False
self.is_response_edited = False
Expand Down Expand Up @@ -83,7 +85,7 @@ def _parse_request(self):

# handle request data
if self.request.method in ['POST', 'PUT']:
DataHelper.origin2flow(self.request, output=_request)
DataHelper.origin2flow(self.request, output=_request, chain=self.request_chain)

if self.request.headers.get('Lyrebird-Client-Address'):
self.client_address = self.request.headers.get('Lyrebird-Client-Address')
Expand Down Expand Up @@ -164,15 +166,17 @@ def set_response_source_proxy(self):

def get_request_body(self):
if self.is_request_edited:
self.flow['request']['headers'] = HeadersHelper.flow2origin(self.flow['request'])
_data = DataHelper.flow2origin(self.flow['request'])
# TODO Repeated calls, remove it
self.flow['request']['headers'] = HeadersHelper.flow2origin(self.flow['request'], chain=self.request_chain)

_data = DataHelper.flow2origin(self.flow['request'], chain=self.request_chain)
else:
_data = self.request.data or self.request.form or None
return _data

def get_request_headers(self):
if self.is_request_edited:
self.flow['request']['headers'] = HeadersHelper.flow2origin(self.flow['request'])
self.flow['request']['headers'] = HeadersHelper.flow2origin(self.flow['request'], chain=self.request_chain)

headers = {}
unproxy_headers = application.config.get('proxy.ignored_headers', {})
Expand All @@ -186,7 +190,7 @@ def get_request_headers(self):

def get_response_generator(self):
if self.is_response_edited:
self.flow['response']['headers'] = HeadersHelper.flow2origin(self.flow['response'])
self.flow['response']['headers'] = HeadersHelper.flow2origin(self.flow['response'], chain=self.response_chain)
_generator = self._generator_bytes()
else:
_generator = self._generator_stream()
Expand All @@ -195,7 +199,7 @@ def get_response_generator(self):
def _generator_bytes(self):
def generator():
try:
_resp_data = DataHelper.flow2origin(self.flow['response']) or ''
_resp_data = DataHelper.flow2origin(self.flow['response'], chain=self.response_chain) or ''
length = len(_resp_data)
size = self.response_chunk_size
bandwidth = config.bandwidth
Expand Down Expand Up @@ -228,7 +232,7 @@ def generator():
yield item
finally:
self.response.data = b''.join(buffer)
DataHelper.origin2flow(self.response, output=self.flow['response'])
DataHelper.origin2flow(self.response, output=self.flow['response'], chain=self.response_chain)

self.update_client_resp_time()
upstream.close()
Expand All @@ -242,7 +246,7 @@ def update_response_headers_code2flow(self, output_key='response'):
HeadersHelper.origin2flow(self.response, output=self.flow[output_key])

def update_response_data2flow(self, output_key='response'):
DataHelper.origin2flow(self.response, output=self.flow[output_key])
DataHelper.origin2flow(self.response, output=self.flow[output_key], chain=self.response_chain)

def update_client_req_time(self):
self.client_req_time = time.time()
Expand Down
18 changes: 11 additions & 7 deletions lyrebird/mock/handlers/http_data_helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class DataHelper:

@staticmethod
def origin2flow(origin_obj, output=None):
def origin2flow(origin_obj, output=None, chain=None):
if not origin_obj:
return

Expand All @@ -34,22 +34,26 @@ def origin2flow(origin_obj, output=None):

for headers_key, func in origin2flow_handlers.items():
headers_val = raw_headers.get(headers_key, '')
_data = func.origin2flow(headers_val, _data)
_data = func.origin2flow(headers_val, _data, chain=chain)

if output:
output['data'] = _data
else:
return _data

@staticmethod
def flow2origin(flow_obj, output=None):
def flow2origin(flow_obj, output=None, chain=None):
_data = flow_obj.get('data')
if not _data:
if _data is None:
return

for headers_key, func in flow2origin_handlers.items():
headers_val = flow_obj['headers'].get(headers_key, '')
_data = func.flow2origin(headers_val, _data)
if chain:
for func in chain[::-1]:
_data = func.flow2origin(_data)
else:
for headers_key, func in flow2origin_handlers.items():
headers_val = flow_obj['headers'].get(headers_key, '')
_data = func.flow2origin(headers_val, _data)

if output:
output.data = _data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ def _get_matched_action(content_encoding_name):
return content_encoding_handlers[content_encoding_name]
return DefaultHandler

def origin2flow(content_encoding, request_data):
def origin2flow(content_encoding, request_data, chain=None):
func = _get_matched_action(content_encoding)
try:
_data = func.origin2flow(request_data)
except Exception as e:
_data = DefaultHandler.origin2flow(request_data)
func = DefaultHandler
_data = func.origin2flow(request_data)
logger.warning(f'Convert Content-Encoding: {content_encoding} data origin2flow failed! {e}')
finally:
chain.append(func)

return _data

def flow2origin(content_encoding, flow_data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ def _get_matched_action(content_type):
return func
return DefaultHandler

def origin2flow(content_type, request_data):
def origin2flow(content_type, request_data, chain=None):
func = _get_matched_action(content_type)
try:
_data = func.origin2flow(request_data)
except Exception as e:
_data = DefaultHandler.origin2flow(request_data)
func = DefaultHandler
_data = func.origin2flow(request_data)
logger.warning(f'Convert Content-Type: {content_type} data origin2flow failed! {e}')
finally:
chain.append(func)

return _data

def flow2origin(content_type, flow_data):
Expand Down
4 changes: 2 additions & 2 deletions lyrebird/mock/handlers/http_header_helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def origin2flow(origin_obj, output=None):
return _headers

@staticmethod
def flow2origin(flow_obj, output=None):
def flow2origin(flow_obj, output=None, chain=None):
_headers = flow_obj.get('headers')
if not _headers:
return

for headers_key, func in flow2origin_handlers.items():
_headers[headers_key] = func.flow2origin(flow_obj)
_headers[headers_key] = func.flow2origin(flow_obj, chain=chain)

if output:
output.headers = _headers
Expand Down
4 changes: 2 additions & 2 deletions lyrebird/mock/handlers/http_header_helper/content_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def origin2flow(origin_obj):
return str(len(_resp_data))

@staticmethod
def flow2origin(flow_obj):
_resp_data = DataHelper.flow2origin(flow_obj)
def flow2origin(flow_obj, chain=None):
_resp_data = DataHelper.flow2origin(flow_obj, chain=chain)
if not _resp_data:
return '0'
return str(len(_resp_data))
2 changes: 1 addition & 1 deletion lyrebird/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def render(data):
}

try:
template_data = Template(data)
template_data = Template(data, keep_trailing_newline=True)
return template_data.render(params)
except Exception:
logger.error(f'Format error!\n {traceback.format_exc()}')
Expand Down

0 comments on commit 8e75eb5

Please sign in to comment.