Skip to content

Commit

Permalink
improve plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sivel committed Apr 22, 2024
1 parent 1ad9e88 commit 8b4c2a9
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions test/integration/targets/setup_proxy/files/hamsandwich.py
@@ -1,12 +1,31 @@
from __future__ import annotations

from proxy.http.proxy import HttpProxyBasePlugin
from proxy.http.parser import HttpParser, httpParserStates


class HamSandwichPlugin(HttpProxyBasePlugin):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parser = None
self.parsable = True

def handle_upstream_chunk(self, chunk):
headers, sep, body = chunk.tobytes().partition(b'\r\n\r\n')
if not sep:
if not self.parsable:
return chunk

if not self.parser:
self.parser = HttpParser.response(chunk)
else:
self.parser.parse(chunk)

if self.parser.state == httpParserStates.INITIALIZED:
self.parsable = False
return chunk
return memoryview(bytearray(headers + b'\r\nX-Sandwich: ham' + sep + body))

if not self.parser.is_complete:
return None

self.parser.add_header(b'X-Sandwich', b'ham')
return self.parser.build_response()

0 comments on commit 8b4c2a9

Please sign in to comment.