@@ -428,6 +428,83 @@ private void doTestChunkSize(boolean expectPass,
428428 }
429429 }
430430
431+
432+ @ Test
433+ public void testTrailerHeaderNameNotTokenThrowException () throws Exception {
434+ doTestTrailerHeaderNameNotToken (false );
435+ }
436+
437+ @ Test
438+ public void testTrailerHeaderNameNotTokenSwallowException () throws Exception {
439+ doTestTrailerHeaderNameNotToken (true );
440+ }
441+
442+ private void doTestTrailerHeaderNameNotToken (boolean swallowException ) throws Exception {
443+
444+ // Setup Tomcat instance
445+ Tomcat tomcat = getTomcatInstance ();
446+
447+ // No file system docBase required
448+ Context ctx = tomcat .addContext ("" , null );
449+
450+ Tomcat .addServlet (ctx , "servlet" , new SwallowBodyServlet (swallowException ));
451+ ctx .addServletMappingDecoded ("/" , "servlet" );
452+
453+ tomcat .start ();
454+
455+ String [] request = new String []{
456+ "POST / HTTP/1.1" + SimpleHttpClient .CRLF +
457+ "Host: localhost" + SimpleHttpClient .CRLF +
458+ "Transfer-encoding: chunked" + SimpleHttpClient .CRLF +
459+ "Content-Type: application/x-www-form-urlencoded" + SimpleHttpClient .CRLF +
460+ "Connection: close" + SimpleHttpClient .CRLF +
461+ SimpleHttpClient .CRLF +
462+ "3" + SimpleHttpClient .CRLF +
463+ "a=0" + SimpleHttpClient .CRLF +
464+ "4" + SimpleHttpClient .CRLF +
465+ "&b=1" + SimpleHttpClient .CRLF +
466+ "0" + SimpleHttpClient .CRLF +
467+ "x@trailer: Test" + SimpleHttpClient .CRLF +
468+ SimpleHttpClient .CRLF };
469+
470+ TrailerClient client = new TrailerClient (tomcat .getConnector ().getLocalPort ());
471+ client .setRequest (request );
472+
473+ client .connect ();
474+ client .processRequest ();
475+ // Expected to fail because of invalid trailer header name
476+ Assert .assertTrue (client .getResponseLine (), client .isResponse400 ());
477+ }
478+
479+ private static class SwallowBodyServlet extends HttpServlet {
480+ private static final long serialVersionUID = 1L ;
481+
482+ private final boolean swallowException ;
483+
484+ SwallowBodyServlet (boolean swallowException ) {
485+ this .swallowException = swallowException ;
486+ }
487+
488+ @ Override
489+ protected void doPost (HttpServletRequest req , HttpServletResponse resp )
490+ throws ServletException , IOException {
491+ resp .setContentType ("text/plain" );
492+ PrintWriter pw = resp .getWriter ();
493+
494+ // Read the body
495+ InputStream is = req .getInputStream ();
496+ try {
497+ while (is .read () > -1 ) {
498+ }
499+ pw .write ("OK" );
500+ } catch (IOException ioe ) {
501+ if (!swallowException ) {
502+ throw ioe ;
503+ }
504+ }
505+ }
506+ }
507+
431508 private static class EchoHeaderServlet extends HttpServlet {
432509 private static final long serialVersionUID = 1L ;
433510
0 commit comments