Skip to content

Commit 1c0a51a

Browse files
authored
Merge pull request #496 from lostsnow/fix/form-urlencoded-post-body-character-encoding-error
fixes character encoding error when collect form urlencoded post body
2 parents b2b84e3 + 0a92b3d commit 1c0a51a

File tree

1 file changed

+33
-28
lines changed
  • dongtai-core/src/main/java/io/dongtai/iast/core/handler/hookpoint/controller/impl

1 file changed

+33
-28
lines changed

dongtai-core/src/main/java/io/dongtai/iast/core/handler/hookpoint/controller/impl/HttpImpl.java

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,6 @@ public static void solveHttpRequest(Object obj, Object req, Object resp, Map<Str
109109
} catch (Throwable ignore) {
110110
}
111111

112-
try {
113-
String contentType = ((Map<String, String>) requestMeta.get("headers")).get("Content-Type");
114-
String method = (String) requestMeta.get("method");
115-
if (("POST".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method))
116-
&& !isRawBody(contentType)) {
117-
Method getParameterNamesMethod = ReflectUtils.getDeclaredMethodFromSuperClass(req.getClass(),
118-
"getParameterNames", null);
119-
Method getParameterMethod = ReflectUtils.getDeclaredMethodFromSuperClass(req.getClass(),
120-
"getParameter", new Class[]{String.class});
121-
Enumeration<?> parameterNames = (Enumeration<?>) getParameterNamesMethod.invoke(req);
122-
StringBuilder postBody = new StringBuilder();
123-
boolean first = true;
124-
while (parameterNames.hasMoreElements()) {
125-
String key = (String) parameterNames.nextElement();
126-
if (first) {
127-
first = false;
128-
postBody.append(key).append("=").append((String) getParameterMethod.invoke(req, key));
129-
} else {
130-
postBody.append("&").append(key).append("=").append((String) getParameterMethod.invoke(req, key));
131-
}
132-
}
133-
if (postBody.length() > 0) {
134-
requestMeta.put("body", postBody.toString());
135-
}
136-
}
137-
} catch (Throwable ignore) {
138-
}
139-
140112
EngineManager.enterHttpEntry(requestMeta);
141113
DongTaiLog.debug("HTTP Request:{} {} from: {}", requestMeta.get("method"), requestMeta.get("requestURI"),
142114
obj.getClass().getName());
@@ -205,6 +177,39 @@ public static void solveHttpResponse(Object obj, Object req, Object resp, Collec
205177
if (EngineManager.REQUEST_CONTEXT.get() == null) {
206178
return;
207179
}
180+
181+
Map<String, Object> requestMeta = EngineManager.REQUEST_CONTEXT.get();
182+
try {
183+
// Collecting the form urlencoded POST body must be done at the exit of the http method
184+
// Otherwise, it may cause character encoding errors
185+
String contentType = ((Map<String, String>) requestMeta.get("headers")).get("Content-Type");
186+
String method = (String) requestMeta.get("method");
187+
if (("POST".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method))
188+
&& (requestMeta.get("body") == null || requestMeta.get("body") == "")
189+
&& !isRawBody(contentType)) {
190+
Method getParameterNamesMethod = ReflectUtils.getDeclaredMethodFromSuperClass(req.getClass(),
191+
"getParameterNames", null);
192+
Method getParameterMethod = ReflectUtils.getDeclaredMethodFromSuperClass(req.getClass(),
193+
"getParameter", new Class[]{String.class});
194+
Enumeration<?> parameterNames = (Enumeration<?>) getParameterNamesMethod.invoke(req);
195+
StringBuilder postBody = new StringBuilder();
196+
boolean first = true;
197+
while (parameterNames.hasMoreElements()) {
198+
String key = (String) parameterNames.nextElement();
199+
if (first) {
200+
first = false;
201+
postBody.append(key).append("=").append((String) getParameterMethod.invoke(req, key));
202+
} else {
203+
postBody.append("&").append(key).append("=").append((String) getParameterMethod.invoke(req, key));
204+
}
205+
}
206+
if (postBody.length() > 0) {
207+
requestMeta.put("body", postBody.toString());
208+
}
209+
}
210+
} catch (Throwable ignore) {
211+
}
212+
208213
Map<String, Collection<String>> headers = parseResponseHeaders(resp, headerNames);
209214
EngineManager.REQUEST_CONTEXT.get().put("responseStatus",
210215
(String) EngineManager.REQUEST_CONTEXT.get().get("protocol") + " " + status);

0 commit comments

Comments
 (0)