Skip to content

Commit fa85fb8

Browse files
committed
fix: inconsistent behavior when buffer http entity content with org.apache.http.entity.BufferedHttpEntity
1 parent 9d6488f commit fa85fb8

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

arex-instrumentation/httpclient/arex-httpclient-apache-v4/src/main/java/io/arex/inst/httpclient/apache/common/ApacheHttpClientAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static void bufferRequestEntity(HttpEntityEnclosingRequest enclosingReque
140140
return;
141141
}
142142
try {
143-
enclosingRequest.setEntity(new BufferedHttpEntity(enclosingRequest.getEntity()));
143+
enclosingRequest.setEntity(new ArexBufferedHttpEntity(enclosingRequest.getEntity()));
144144
} catch (Exception ignore) {
145145
// ignore exception
146146
}
@@ -151,7 +151,7 @@ public static void bufferResponseEntity(HttpResponse response) {
151151
return;
152152
}
153153
try {
154-
EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity()));
154+
EntityUtils.updateEntity(response, new ArexBufferedHttpEntity(response.getEntity()));
155155
} catch (Exception e) {
156156
// ignore exception
157157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.arex.inst.httpclient.apache.common;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import org.apache.http.HttpEntity;
8+
import org.apache.http.entity.HttpEntityWrapper;
9+
10+
/**
11+
* This class only buffer the original HttpEntity content into a byte array, tt does not modify any
12+
* behavior of the original HttpEntity.
13+
* This class is known to have performance issues comparing to the original BufferedHttpEntity, but
14+
* it provides consistent behavior with the original HttpEntity.
15+
* @see org.apache.http.entity.BufferedHttpEntity
16+
* @author: QizhengMo
17+
* @date: 2025/3/12 15:35
18+
*/
19+
public class ArexBufferedHttpEntity extends HttpEntityWrapper {
20+
private final byte[] buffer;
21+
22+
public ArexBufferedHttpEntity(HttpEntity wrappedEntity) throws IOException {
23+
super(wrappedEntity);
24+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
25+
26+
// This class is only used in Arex Agent, so we are almost always the first to consume the content.
27+
wrappedEntity.writeTo(out);
28+
out.flush();
29+
this.buffer = out.toByteArray();
30+
}
31+
32+
/**
33+
* Return a copy of the original content of the wrapped HttpEntity.
34+
*/
35+
@Override
36+
public InputStream getContent() throws IOException {
37+
return new ByteArrayInputStream(buffer);
38+
}
39+
}

0 commit comments

Comments
 (0)