Tomcat 9 and response.setTrailerFields

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Tomcat 9 and response.setTrailerFields

Julian Reschke

Hi,

I just tried to figure out whether Tomcat 9 will let be send trailer
fields in a chunked HTTP/1.1 response, using

https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/http/HttpServletResponse.html#setTrailerFields-java.util.function.Supplier-

I couldn't get it to work yet, so below some questions:

1) Is this actually supported for HTTP/1.1?

2) Will it automatically switch to chunked encoding (leaving out
content-length), or does the application code need to take care of that?

3) I understand that I need to send "Trailer" upfront, but what about
"TE" and "Connection"?

(pointer to working example would be cool as well)

Best regards, Julian


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Tomcat 9 and response.setTrailerFields

markt
On 24/06/2020 15:59, Julian Reschke wrote:

>
> Hi,
>
> I just tried to figure out whether Tomcat 9 will let be send trailer
> fields in a chunked HTTP/1.1 response, using
>
> https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/http/HttpServletResponse.html#setTrailerFields-java.util.function.Supplier-
>
>
> I couldn't get it to work yet, so below some questions:
>
> 1) Is this actually supported for HTTP/1.1?

Yes.

> 2) Will it automatically switch to chunked encoding (leaving out
> content-length), or does the application code need to take care of that?

Tomcat will handle all of that but you need to make sure you set the
trailer fields before the response is committed so Tomcat can set the
appropriate headers.

> 3) I understand that I need to send "Trailer" upfront, but what about
> "TE" and "Connection"?

Just the Trailer header. Tomcat will look after the rest.

Mark


>
> (pointer to working example would be cool as well)
>
> Best regards, Julian
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Tomcat 9 and response.setTrailerFields

Julian Reschke
On 24.06.2020 17:13, Mark Thomas wrote:

> On 24/06/2020 15:59, Julian Reschke wrote:
>>
>> Hi,
>>
>> I just tried to figure out whether Tomcat 9 will let be send trailer
>> fields in a chunked HTTP/1.1 response, using
>>
>> https://tomcat.apache.org/tomcat-9.0-doc/servletapi/javax/servlet/http/HttpServletResponse.html#setTrailerFields-java.util.function.Supplier-
>>
>>
>> I couldn't get it to work yet, so below some questions:
>>
>> 1) Is this actually supported for HTTP/1.1?
>
> Yes.
>
>> 2) Will it automatically switch to chunked encoding (leaving out
>> content-length), or does the application code need to take care of that?
>
> Tomcat will handle all of that but you need to make sure you set the
> trailer fields before the response is committed so Tomcat can set the
> appropriate headers.
>
>> 3) I understand that I need to send "Trailer" upfront, but what about
>> "TE" and "Connection"?
>
> Just the Trailer header. Tomcat will look after the rest.
>
> Mark
> ...

Weird.

I'm trying this with Apache Jackrabbit trunk and the minimal patch below:

> Index: jackrabbit-parent/pom.xml
> ===================================================================
> --- jackrabbit-parent/pom.xml   (Revision 1879148)
> +++ jackrabbit-parent/pom.xml   (Arbeitskopie)
> @@ -554,7 +554,7 @@
>        <dependency>
>          <groupId>javax.servlet</groupId>
>          <artifactId>javax.servlet-api</artifactId>
> -        <version>3.1.0</version>
> +        <version>4.0.1</version>
>          <scope>provided</scope>
>        </dependency>
>        <dependency>
> Index: jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/server/AbstractWebdavServlet.java
> ===================================================================
> --- jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/server/AbstractWebdavServlet.java      (Revision 1879148)
> +++ jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/server/AbstractWebdavServlet.java      (Arbeitskopie)
> @@ -95,8 +95,10 @@
>  import java.util.ArrayList;
>  import java.util.Collections;
>  import java.util.Enumeration;
> +import java.util.HashMap;
>  import java.util.List;
>  import java.util.Locale;
> +import java.util.Map;
>
>  /**
>   * <code>AbstractWebdavServlet</code>
> @@ -602,6 +604,11 @@
>              }
>          }
>
> +        response.setHeader("Trailer", "foo");
> +        Map<String, String> trailers = new HashMap<>();
> +        trailers.put("foo", "bar");
> +        response.setTrailerFields(() -> trailers);
> +
>          // spool resource properties and eventually resource content.
>          OutputStream out = (sendContent) ? response.getOutputStream() : null;
>          resource.spool(getOutputContext(response, out));

When I deploy jackrabbit-webapp in Tomcat 9.0.36, and curl a resource, I
see:

> < HTTP/1.1 200
> < Trailer: foo
> < ETag: "2692247-1592985065092"
> < Last-Modified: Wed, 24 Jun 2020 07:51:05 GMT
> < Content-Type: image/jpeg
> < Content-Length: 2692247
> < Date: Wed, 24 Jun 2020 15:33:11 GMT
> <
> { [7996 bytes data]

So it does set "Trailer" (so the response was not committed yet), but it
doesn't switch to chunked encoding.

There must be something that I'm doing wrong...

Best regards, Julian

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Tomcat 9 and response.setTrailerFields

Julian Reschke
On 24.06.2020 17:35, Julian Reschke wrote:
> ... > So it does set "Trailer" (so the response was not committed yet), but it
> doesn't switch to chunked encoding.
>
> There must be something that I'm doing wrong...
> ...

Found the issue.

I was using a HttpServletResponse object that *delegates* to the real
one, and as the trailer field related methods have default
implementations, I actually executed the default "no op" implementation.

Best regards, Julian

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Tomcat 9 and response.setTrailerFields

markt
On 25/06/2020 07:44, Julian Reschke wrote:

> On 24.06.2020 17:35, Julian Reschke wrote:
>> ... > So it does set "Trailer" (so the response was not committed
>> yet), but it
>> doesn't switch to chunked encoding.
>>
>> There must be something that I'm doing wrong...
>> ...
>
> Found the issue.
>
> I was using a HttpServletResponse object that *delegates* to the real
> one, and as the trailer field related methods have default
> implementations, I actually executed the default "no op" implementation.

Glad you found it and thanks for reporting back with the root cause.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]