Skip to content

fix: inconsistent behavior when buffer http entity content with org.a… #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public static void bufferRequestEntity(HttpEntityEnclosingRequest enclosingReque
return;
}
try {
enclosingRequest.setEntity(new BufferedHttpEntity(enclosingRequest.getEntity()));
enclosingRequest.setEntity(new ArexBufferedHttpEntity(enclosingRequest.getEntity()));
} catch (Exception ignore) {
// ignore exception
// ignore exception, fallback to original entity and ignore recording
}
}

Expand All @@ -151,14 +151,14 @@ public static void bufferResponseEntity(HttpResponse response) {
return;
}
try {
EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity()));
EntityUtils.updateEntity(response, new ArexBufferedHttpEntity(response.getEntity()));
} catch (Exception e) {
// ignore exception
// ignore exception, fallback to original entity and ignore recording
}
}

private byte[] getEntityBytes(HttpEntity entity) {
if (!(entity instanceof BufferedHttpEntity)) {
if (!(entity instanceof ArexBufferedHttpEntity)) {
return ZERO_BYTE;
}
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.arex.inst.httpclient.apache.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.entity.HttpEntityWrapper;

/**
* This class only buffer the original HttpEntity content into a byte array, tt does not modify any
* behavior of the original HttpEntity.
* This class is known to have performance issues comparing to the original BufferedHttpEntity, but
* it provides consistent behavior with the original HttpEntity.
* @see org.apache.http.entity.BufferedHttpEntity
* @author: QizhengMo
* @date: 2025/3/12 15:35
*/
public class ArexBufferedHttpEntity extends HttpEntityWrapper {
private final byte[] buffer;

public ArexBufferedHttpEntity(HttpEntity wrappedEntity) throws IOException {
super(wrappedEntity);
final ByteArrayOutputStream out = new ByteArrayOutputStream();

// This class is only used in Arex Agent, so we are almost always the first to consume the content.
wrappedEntity.writeTo(out);
out.flush();
this.buffer = out.toByteArray();
}

/**
* Return a copy of the original content of the wrapped HttpEntity.
*/
@Override
public InputStream getContent() throws IOException {
return new ByteArrayInputStream(buffer);
}

@Override
public void writeTo(final OutputStream outStream) throws IOException {
if (this.buffer != null) {
outStream.write(this.buffer);
} else {
super.writeTo(outStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.*;

import io.arex.inst.httpclient.apache.common.ArexBufferedHttpEntity;
import io.arex.inst.runtime.context.ContextManager;
import java.io.IOException;
import net.bytebuddy.description.method.MethodDescription;
Expand Down Expand Up @@ -54,7 +55,7 @@ void testFutureAdvice() throws IOException {
((FutureCallbackWrapper<?>) wrapper).setNeedRecord(true);
BasicFutureInstrumentation.FutureAdvice.completed(httpResponse, wrapper);

assertInstanceOf(BufferedHttpEntity.class, httpResponse.getEntity());
assertInstanceOf(ArexBufferedHttpEntity.class, httpResponse.getEntity());

BasicFutureInstrumentation.FutureAdvice.completed(httpResponse, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.Mockito.mockStatic;

import io.arex.inst.httpclient.apache.async.RequestProducerInstrumentation.ConstructorAdvice;
import io.arex.inst.httpclient.apache.common.ArexBufferedHttpEntity;
import io.arex.inst.runtime.context.ContextManager;
import java.lang.reflect.Constructor;
import net.bytebuddy.description.method.MethodDescription.ForLoadedConstructor;
Expand Down Expand Up @@ -58,7 +59,7 @@ void onEnter() {
HttpPost request = new HttpPost();
request.setEntity(new ByteArrayEntity("test".getBytes()));
ConstructorAdvice.onEnter(request);
assertInstanceOf(BufferedHttpEntity.class, request.getEntity());
assertInstanceOf(ArexBufferedHttpEntity.class, request.getEntity());
}
}
}