diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/Fields.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/Fields.java index f0015c70d0a..50a2ce5fcaf 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/Fields.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/Fields.java @@ -14,6 +14,7 @@ package org.eclipse.jetty.util; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; @@ -329,7 +330,31 @@ public Iterator iterator() */ public Map toStringArrayMap() { - Map result = new LinkedHashMap<>(); + Map result = new LinkedHashMap<>() + { + @Override + public String toString() + { + if (fields.isEmpty()) + return "{}"; + + StringBuilder sb = new StringBuilder(); + sb.append('{'); + for (Iterator> i = entrySet().iterator(); i.hasNext();) + { + Map.Entry e = i.next(); + String key = e.getKey(); + String[] value = e.getValue(); + sb.append(key); + sb.append('='); + sb.append(Arrays.asList(value)); + if (i.hasNext()) + sb.append(','); + } + sb.append('}'); + return sb.toString(); + } + }; fields.forEach((k, f) -> result.put(f.getName(), f.getValues().toArray(new String[0]))); return result; } diff --git a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/RequestTest.java b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/RequestTest.java index 69d820b4d52..869717e2864 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/RequestTest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/RequestTest.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.Socket; @@ -626,4 +627,49 @@ protected void service(HttpServletRequest request, HttpServletResponse resp) thr assertThat(date, containsString(":")); } } + + @Test + public void testParameters() throws Exception + { + final AtomicReference parameterMap = new AtomicReference<>(); + + startServer(new HttpServlet() + { + @Override + protected void service(HttpServletRequest request, HttpServletResponse resp) throws IOException + { + parameterMap.set(request.getParameterMap().toString()); + PrintWriter out = resp.getWriter(); + out.println(request.getParameter("a")); + out.println(request.getParameterValues("a")[1]); + out.println(request.getParameterValues("a")[2]); + out.println(Arrays.asList(request.getParameterValues("b"))); + out.println(Arrays.asList(request.getParameterValues("c"))); + out.println(Arrays.asList(request.getParameterValues("d"))); + + } + }); + + String rawResponse = _connector.getResponse( + """ + POST /test/parameters?a=1&a=2&b=one&c= HTTP/1.1\r + Host: localhost\r + Connection: close\r + Content-Type: application/x-www-form-urlencoded\r + Content-Length: 23\r + \r + a=3&b=two&b=three&d=xyz\r + """); + HttpTester.Response response = HttpTester.parseResponse(rawResponse); + assertThat(response.getStatus(), is(HttpStatus.OK_200)); + assertThat(parameterMap.get(), is("{a=[1, 2, 3],b=[one, two, three],c=[],d=[xyz]}")); + assertThat(response.getContent().replaceAll("\r\n","\n"), is(""" + 1 + 2 + 3 + [one, two, three] + [] + [xyz] + """)); + } }