Skip to content
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

Rewrite WebRequest.getParameters (#844) #851

Merged
Merged
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
91 changes: 23 additions & 68 deletions src/main/java/org/htmlunit/WebRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* @author Lai Quang Duong
* @author Kristof Neirynck
*/
public class WebRequest implements Serializable {

Check failure on line 57 in src/main/java/org/htmlunit/WebRequest.java

View workflow job for this annotation

GitHub Actions / PMD

[PMD] reported by reviewdog 🐶 Too many fields Raw Output: {"locations":[{"physicalLocation":{"artifactLocation":{"uri":"file:///home/runner/work/htmlunit/htmlunit/src/main/java/org/htmlunit/WebRequest.java"},"region":{"endColumn":2,"endLine":702,"startColumn":49,"startLine":57}}}],"message":{"text":"Too many fields"},"ruleId":"TooManyFields","ruleIndex":0}

public enum HttpHint {
/** Force to include the charset. */
Expand Down Expand Up @@ -363,85 +363,40 @@
// the spring org.springframework.test.web.servlet.htmlunitHtmlUnitRequestBuilder uses
// this method and is sensitive to all the details of the current implementation.

// POST, PUT, PATCH, DELETE, OPTIONS
final List<NameValuePair> allParameters = new ArrayList<>(
HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// the servlet api ignores these parameters but to make spring happy we include them
final HttpMethod httpMethod = getHttpMethod();
if (httpMethod == HttpMethod.POST
|| httpMethod == HttpMethod.PUT
|| httpMethod == HttpMethod.PATCH
|| httpMethod == HttpMethod.DELETE
|| httpMethod == HttpMethod.OPTIONS) {

if (FormEncodingType.URL_ENCODED == getEncodingType()) {
|| httpMethod == HttpMethod.PUT
|| httpMethod == HttpMethod.PATCH
|| httpMethod == HttpMethod.DELETE
|| httpMethod == HttpMethod.OPTIONS) {
if (FormEncodingType.URL_ENCODED == getEncodingType()
&& httpMethod != HttpMethod.OPTIONS) {
// spring ignores URL_ENCODED parameters for OPTIONS requests
// getRequestParameters and getRequestBody are mutually exclusive
if (getRequestBody() == null) {
final List<NameValuePair> allParameters =
new ArrayList<>(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// for PATCH/DELETE/OPTIONS request browsers are sending the body
// but the servlet API does not get it
if (httpMethod != HttpMethod.PATCH
&& httpMethod != HttpMethod.DELETE
&& httpMethod != HttpMethod.OPTIONS) {
allParameters.addAll(getRequestParameters());
}

return normalize(allParameters);
allParameters.addAll(getRequestParameters());
}

// getRequestParameters and getRequestBody are mutually exclusive
final List<NameValuePair> allParameters =
new ArrayList<>(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// for PATCH/DELETE/OPTIONS request browsers are sending the body
// but the servlet API does not get it
if (httpMethod != HttpMethod.PATCH
&& httpMethod != HttpMethod.DELETE
&& httpMethod != HttpMethod.OPTIONS) {
else {
allParameters.addAll(HttpUtils.parseUrlQuery(getRequestBody(), getCharset()));
}

return normalize(allParameters);
}

if (FormEncodingType.TEXT_PLAIN == getEncodingType()) {
if (getRequestBody() == null) {
final List<NameValuePair> allParameters =
new ArrayList<>(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// the servlet api ignores the parameters
// allParameters.addAll(getRequestParameters());

return normalize(allParameters);
else if (FormEncodingType.MULTIPART == getEncodingType()) {
if (httpMethod == HttpMethod.POST) {
allParameters.addAll(getRequestParameters());
}
else {
// for PUT, PATCH, DELETE and OPTIONS spring moves the parameters up to the query
// it doesn't replace the query
allParameters.addAll(0, getRequestParameters());
}

return normalize(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
}

if (FormEncodingType.MULTIPART == getEncodingType()) {
final List<NameValuePair> allParameters = new ArrayList<>();

allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));

// the servlet api ignores these parameters but to make spring happy we include them
allParameters.addAll(getRequestParameters());

return normalize(allParameters);
}

return Collections.emptyList();
}

// GET, TRACE, HEAD

// a bit strange but
// HttpWebConnection.makeHttpMethod() moves the parameters up to the query
// (in fact replaces the query with the parameters)
// to reflect this we have to take the parameters into account even if this
// looks wrong for GET requests
if (!getRequestParameters().isEmpty()) {
return normalize(getRequestParameters());
}

return normalize(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
return normalize(allParameters);
}

private static List<NameValuePair> normalize(final List<NameValuePair> pairs) {
Expand Down