Skip to content

Commit 7115231

Browse files
authored
fix: request params are recorded in the form of request body (#342)
1 parent 8747e60 commit 7115231

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed

arex-instrumentation/servlet/arex-httpservlet/src/main/java/io/arex/inst/httpservlet/ServletUtil.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package io.arex.inst.httpservlet;
22

3+
import io.arex.agent.bootstrap.util.MapUtils;
4+
import io.arex.agent.bootstrap.util.CollectionUtil;
35
import io.arex.agent.bootstrap.util.StringUtil;
46
import java.net.URI;
57
import java.net.URISyntaxException;
8+
import java.util.ArrayList;
9+
import java.util.Collections;
10+
import java.util.HashMap;
11+
import java.util.Iterator;
12+
import java.util.List;
13+
import java.util.Map;
14+
import org.springframework.util.MultiValueMap;
15+
import org.springframework.web.util.UriComponentsBuilder;
616

717
public class ServletUtil {
818

@@ -40,4 +50,50 @@ public static String getRequestPath(String uri) {
4050
return uri;
4151
}
4252
}
53+
54+
/**
55+
* match requset params
56+
*
57+
* @param requestParams
58+
* @param name
59+
* @param value
60+
* @return
61+
*/
62+
public static boolean matchAndRemoveRequestParams(Map<String, List<String>> requestParams, String name, String value) {
63+
if (MapUtils.isEmpty(requestParams)) {
64+
return false;
65+
}
66+
List<String> values = requestParams.get(name);
67+
if(CollectionUtil.isEmpty(values)){
68+
return false;
69+
}
70+
Iterator<String> iterator = values.iterator();
71+
while (iterator.hasNext()) {
72+
String next = iterator.next();
73+
if (next.equals(value)) {
74+
iterator.remove();
75+
return true;
76+
}
77+
}
78+
return false;
79+
}
80+
81+
/**
82+
* obtain requset params
83+
*
84+
* @param queryString
85+
* @return
86+
*/
87+
public static Map<String, List<String>> getRequestParams(String queryString) {
88+
if (StringUtil.isEmpty(queryString)) {
89+
return Collections.emptyMap();
90+
}
91+
MultiValueMap<String, String> paramsKeyValueMap = UriComponentsBuilder.fromUriString(
92+
"?" + queryString).build().getQueryParams();
93+
Map<String, List<String>> requestParamsMap = new HashMap<>();
94+
for (Map.Entry<String, List<String>> entry : paramsKeyValueMap.entrySet()) {
95+
requestParamsMap.put(entry.getKey(), new ArrayList<>(entry.getValue()));
96+
}
97+
return requestParamsMap;
98+
}
4399
}

arex-instrumentation/servlet/arex-httpservlet/src/main/java/io/arex/inst/httpservlet/wrapper/CachedBodyRequestWrapperV3.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.arex.inst.httpservlet.wrapper;
22

33

4+
import io.arex.inst.httpservlet.ServletUtil;
45
import javax.servlet.ReadListener;
56
import javax.servlet.ServletInputStream;
67
import javax.servlet.http.HttpServletRequest;
@@ -124,24 +125,34 @@ private boolean isFormPost() {
124125

125126
private void writeRequestParametersToCachedContent() {
126127
try {
127-
if (this.cachedContent.size() == 0) {
128+
// requestBody is not empty
129+
if (this.cachedContent.size() == 0 && this.getContentLength() > 0) {
130+
Map<String, List<String>> requestParams = ServletUtil.getRequestParams(this.getQueryString());
128131
String requestEncoding = getCharacterEncoding();
129132
Map<String, String[]> form = super.getParameterMap();
130133
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext(); ) {
131134
String name = nameIterator.next();
135+
boolean valueIsEmpty = true;
132136
List<String> values = Arrays.asList(form.get(name));
133137
for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext(); ) {
134138
String value = valueIterator.next();
139+
if (ServletUtil.matchAndRemoveRequestParams(requestParams, name, value)) {
140+
continue;
141+
}
135142
this.cachedContent.write(URLEncoder.encode(name, requestEncoding).getBytes());
136143
if (value != null) {
137144
this.cachedContent.write('=');
138145
this.cachedContent.write(URLEncoder.encode(value, requestEncoding).getBytes());
139146
if (valueIterator.hasNext()) {
140147
this.cachedContent.write('&');
141148
}
149+
if (valueIsEmpty) {
150+
valueIsEmpty = false;
151+
}
142152
}
153+
143154
}
144-
if (nameIterator.hasNext()) {
155+
if (nameIterator.hasNext() && !valueIsEmpty) {
145156
this.cachedContent.write('&');
146157
}
147158
}

arex-instrumentation/servlet/arex-httpservlet/src/main/java/io/arex/inst/httpservlet/wrapper/CachedBodyRequestWrapperV5.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.arex.inst.httpservlet.wrapper;
22

3+
import io.arex.inst.httpservlet.ServletUtil;
34
import jakarta.servlet.ReadListener;
45
import jakarta.servlet.ServletInputStream;
56
import jakarta.servlet.http.HttpServletRequest;
@@ -122,14 +123,20 @@ private boolean isFormPost() {
122123

123124
private void writeRequestParametersToCachedContent() {
124125
try {
125-
if (this.cachedContent.size() == 0) {
126+
// requestBody is not empty
127+
if (this.cachedContent.size() == 0 && this.getContentLength() > 0) {
128+
Map<String, List<String>> requestParams = ServletUtil.getRequestParams(this.getQueryString());
126129
String requestEncoding = getCharacterEncoding();
127130
Map<String, String[]> form = super.getParameterMap();
128131
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext(); ) {
129132
String name = nameIterator.next();
133+
boolean valueIsEmpty = true;
130134
List<String> values = Arrays.asList(form.get(name));
131135
for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext(); ) {
132136
String value = valueIterator.next();
137+
if (ServletUtil.matchAndRemoveRequestParams(requestParams, name, value)) {
138+
continue;
139+
}
133140
this.cachedContent.write(URLEncoder.encode(name, requestEncoding).getBytes());
134141
if (value != null) {
135142
this.cachedContent.write('=');
@@ -138,8 +145,11 @@ private void writeRequestParametersToCachedContent() {
138145
this.cachedContent.write('&');
139146
}
140147
}
148+
if (valueIsEmpty) {
149+
valueIsEmpty = false;
150+
}
141151
}
142-
if (nameIterator.hasNext()) {
152+
if (nameIterator.hasNext() && !valueIsEmpty) {
143153
this.cachedContent.write('&');
144154
}
145155
}

arex-instrumentation/servlet/arex-httpservlet/src/test/java/io/arex/inst/httpservlet/ServletUtilTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

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

5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
511
import org.junit.jupiter.api.Test;
612

713
class ServletUtilTest {
@@ -23,4 +29,21 @@ public void getFullPath() {
2329
assertEquals("/servletpath/controll/action", ServletUtil.getRequestPath("http://arextest.com/servletpath/controll/action"));
2430
assertEquals("/servletpath/controll/action?k1=v1", ServletUtil.getRequestPath("http://arextest.com/servletpath/controll/action?k1=v1"));
2531
}
32+
33+
@Test
34+
public void matchRequestParams() {
35+
Map<String, List<String>> requestParams = new HashMap<>();
36+
requestParams.put("name", new ArrayList<>(Arrays.asList("kimi")));
37+
requestParams.put("age", new ArrayList<>(Arrays.asList("0")));
38+
assertFalse(ServletUtil.matchAndRemoveRequestParams(requestParams, "name", "lock"));
39+
assertTrue(ServletUtil.matchAndRemoveRequestParams(requestParams, "age", "0"));
40+
assertFalse(ServletUtil.matchAndRemoveRequestParams(Collections.emptyMap(), "name", "lock"));
41+
}
42+
43+
@Test
44+
public void getRequestParams() {
45+
String queryString = "name=kimi&age=0";
46+
Map<String, List<String>> requestParams = ServletUtil.getRequestParams(queryString);
47+
assertEquals(2, requestParams.size());
48+
}
2649
}

0 commit comments

Comments
 (0)