From f8b7ae863c646eaaed431f5abb6fedf79b390465 Mon Sep 17 00:00:00 2001 From: Rhiannon Lolley Neville Date: Wed, 4 Dec 2024 01:50:18 +0000 Subject: [PATCH] Add function to ProxyListener to be called before request body chunks are sent to target (#8) --- .../com/hsbc/cranker/mucranker/CrankerMuHandler.java | 6 ++++++ .../com/hsbc/cranker/mucranker/ProxyListener.java | 11 +++++++++++ .../com/hsbc/cranker/mucranker/RouterSocketV3.java | 7 +++++++ 3 files changed, 24 insertions(+) diff --git a/src/main/java/com/hsbc/cranker/mucranker/CrankerMuHandler.java b/src/main/java/com/hsbc/cranker/mucranker/CrankerMuHandler.java index 48ae8f3..fd9cb5a 100644 --- a/src/main/java/com/hsbc/cranker/mucranker/CrankerMuHandler.java +++ b/src/main/java/com/hsbc/cranker/mucranker/CrankerMuHandler.java @@ -210,6 +210,12 @@ private void sendRequestOverWebSocket(MuRequest clientRequest, MuResponse client public void onDataReceived(ByteBuffer buffer, DoneCallback callback) { try { final int position = buffer.position(); + + for (ProxyListener proxyListener : proxyListeners) { + proxyListener.onBeforeRequestBodyChunkSentToTarget(crankedSocket, buffer.position(position)); + } + buffer.position(position); + final DoneCallback doneWrapper = error -> { if (error == null && !proxyListeners.isEmpty()) { for (ProxyListener proxyListener : proxyListeners) { diff --git a/src/main/java/com/hsbc/cranker/mucranker/ProxyListener.java b/src/main/java/com/hsbc/cranker/mucranker/ProxyListener.java index 98cafd7..3450de8 100644 --- a/src/main/java/com/hsbc/cranker/mucranker/ProxyListener.java +++ b/src/main/java/com/hsbc/cranker/mucranker/ProxyListener.java @@ -91,6 +91,17 @@ public interface ProxyListener { default void onAfterTargetToProxyHeadersReceived(ProxyInfo info, int status, Headers headers) throws WebApplicationException {}; + /** + * Called before a chunk of request body data is sent to the target + * This will be called many times if the body has been fragmented + * + * @param info Info about the request and response. + * @param chunk Request body data which is going to be sent to target. + * + */ + default void onBeforeRequestBodyChunkSentToTarget(ProxyInfo info, ByteBuffer chunk) {}; + + /** * Called when a chunk of request body data is sent to the target * This will be called many times if the body has been fragmented diff --git a/src/main/java/com/hsbc/cranker/mucranker/RouterSocketV3.java b/src/main/java/com/hsbc/cranker/mucranker/RouterSocketV3.java index c3be838..ecd1ae3 100644 --- a/src/main/java/com/hsbc/cranker/mucranker/RouterSocketV3.java +++ b/src/main/java/com/hsbc/cranker/mucranker/RouterSocketV3.java @@ -134,6 +134,13 @@ public void onDataReceived(ByteBuffer buffer, DoneCallback callback) { final int remaining = buffer.remaining(); final int position = buffer.position(); + if (!proxyListeners.isEmpty()) { + for (ProxyListener proxyListener : proxyListeners) { + proxyListener.onBeforeRequestBodyChunkSentToTarget(context, buffer.position(position)); + } + } + buffer.position(position); + DoneCallback wrapper = error -> { context.fromClientBytes.addAndGet(remaining);