Skip to content

Commit c042de4

Browse files
committed
Rewrite WebRequest.getParameters (#844)
1 parent 93a526c commit c042de4

File tree

1 file changed

+23
-68
lines changed

1 file changed

+23
-68
lines changed

src/main/java/org/htmlunit/WebRequest.java

+23-68
Original file line numberDiff line numberDiff line change
@@ -363,85 +363,40 @@ public List<NameValuePair> getParameters() {
363363
// the spring org.springframework.test.web.servlet.htmlunitHtmlUnitRequestBuilder uses
364364
// this method and is sensitive to all the details of the current implementation.
365365

366-
// POST, PUT, PATCH, DELETE, OPTIONS
366+
final List<NameValuePair> allParameters = new ArrayList<>(
367+
HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
368+
369+
// the servlet api ignores these parameters but to make spring happy we include them
367370
final HttpMethod httpMethod = getHttpMethod();
368371
if (httpMethod == HttpMethod.POST
369-
|| httpMethod == HttpMethod.PUT
370-
|| httpMethod == HttpMethod.PATCH
371-
|| httpMethod == HttpMethod.DELETE
372-
|| httpMethod == HttpMethod.OPTIONS) {
373-
374-
if (FormEncodingType.URL_ENCODED == getEncodingType()) {
372+
|| httpMethod == HttpMethod.PUT
373+
|| httpMethod == HttpMethod.PATCH
374+
|| httpMethod == HttpMethod.DELETE
375+
|| httpMethod == HttpMethod.OPTIONS) {
376+
if (FormEncodingType.URL_ENCODED == getEncodingType()
377+
&& httpMethod != HttpMethod.OPTIONS) {
378+
// spring ignores URL_ENCODED parameters for OPTIONS requests
379+
// getRequestParameters and getRequestBody are mutually exclusive
375380
if (getRequestBody() == null) {
376-
final List<NameValuePair> allParameters =
377-
new ArrayList<>(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
378-
379-
// for PATCH/DELETE/OPTIONS request browsers are sending the body
380-
// but the servlet API does not get it
381-
if (httpMethod != HttpMethod.PATCH
382-
&& httpMethod != HttpMethod.DELETE
383-
&& httpMethod != HttpMethod.OPTIONS) {
384-
allParameters.addAll(getRequestParameters());
385-
}
386-
387-
return normalize(allParameters);
381+
allParameters.addAll(getRequestParameters());
388382
}
389-
390-
// getRequestParameters and getRequestBody are mutually exclusive
391-
final List<NameValuePair> allParameters =
392-
new ArrayList<>(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
393-
394-
// for PATCH/DELETE/OPTIONS request browsers are sending the body
395-
// but the servlet API does not get it
396-
if (httpMethod != HttpMethod.PATCH
397-
&& httpMethod != HttpMethod.DELETE
398-
&& httpMethod != HttpMethod.OPTIONS) {
383+
else {
399384
allParameters.addAll(HttpUtils.parseUrlQuery(getRequestBody(), getCharset()));
400385
}
401-
402-
return normalize(allParameters);
403386
}
404-
405-
if (FormEncodingType.TEXT_PLAIN == getEncodingType()) {
406-
if (getRequestBody() == null) {
407-
final List<NameValuePair> allParameters =
408-
new ArrayList<>(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
409-
410-
// the servlet api ignores the parameters
411-
// allParameters.addAll(getRequestParameters());
412-
413-
return normalize(allParameters);
387+
else if (FormEncodingType.MULTIPART == getEncodingType()) {
388+
if (httpMethod == HttpMethod.POST) {
389+
allParameters.addAll(getRequestParameters());
390+
}
391+
else {
392+
// for PUT, PATCH, DELETE and OPTIONS spring moves the parameters up to the query
393+
// it doesn't replace the query
394+
allParameters.addAll(0, getRequestParameters());
414395
}
415-
416-
return normalize(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
417-
}
418-
419-
if (FormEncodingType.MULTIPART == getEncodingType()) {
420-
final List<NameValuePair> allParameters = new ArrayList<>();
421-
422-
allParameters.addAll(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
423-
424-
// the servlet api ignores these parameters but to make spring happy we include them
425-
allParameters.addAll(getRequestParameters());
426-
427-
return normalize(allParameters);
428396
}
429-
430-
return Collections.emptyList();
431-
}
432-
433-
// GET, TRACE, HEAD
434-
435-
// a bit strange but
436-
// HttpWebConnection.makeHttpMethod() moves the parameters up to the query
437-
// (in fact replaces the query with the parameters)
438-
// to reflect this we have to take the parameters into account even if this
439-
// looks wrong for GET requests
440-
if (!getRequestParameters().isEmpty()) {
441-
return normalize(getRequestParameters());
442397
}
443398

444-
return normalize(HttpUtils.parseUrlQuery(getUrl().getQuery(), getCharset()));
399+
return normalize(allParameters);
445400
}
446401

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

0 commit comments

Comments
 (0)