Skip to content

Commit

Permalink
Add function to ProxyListener to be called before request body chunks…
Browse files Browse the repository at this point in the history
… are sent to target (#8)
  • Loading branch information
Rhiannon Lolley Neville authored and hsbc-github committed Dec 4, 2024
1 parent f72755d commit f8b7ae8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/hsbc/cranker/mucranker/ProxyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/hsbc/cranker/mucranker/RouterSocketV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f8b7ae8

Please sign in to comment.