All,
I thought it might be "fun" to play around with 103 Early Hints, which
is a feature with support added in servlet.next which means Tomcat 12.
It can be used currently with Tomcat 9.0 and later, as long as you are
willing to downcast the HttpServletResponse to a Tomcat-private class
and you can make a call to Response.sendEarlyHints.
It occurs to me that we (Tomcat) could make it easier for applications
to use this feature by providing something like a Filter that would use
Tomcat internal libraries, but insulate the application from such ugliness.
I have two ideas for how this would be implemented:
1. A new Filter dedicated to (a) setting Link headers and (b) calling
sendEarlyHints
2. As an option for the RewriteValve value, e.g. as a flag
I wrote a quick implementation of (1) above and it occurred to me that
is was pretty trivial, so I came up with idea (2) above as maybe a way
to add a feature to an existing component rather than building a new one.
When I started looking at the changes required for (2), it seems to me
that the only thing that made any sense would be to do the following:
RewriteRule .*\.jsp $0 [SEH]
The "SEH" is a proposed "Send Early Hints" flag which would invoke the
response.sendEarlyHints method which .... sends a 103 Early Hints
response and then proceeds with the rewrite/request.
I've decided that I don't really like (2) because you have to configure
a useless rewrite operation just to add the SEH flag. Also, RewriteValve
doesn't help you with the Link headers so you are back to writing a
Filter and then arranging to have that Filter run *before* RewriteValve
which ... could be a problem for you.
So I think I'm going to pursue option (1) above, looking at doing
something like I have below. Comments welcome and encouraged.
-chris
=== CUT ===
package org.apache.catalina.filters;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletResponse;
/**
* A Filter that adds a series of
*/
public class EarlyHintsFilter
implements Filter
{
private final ArrayList<String> hints = new ArrayList<String>();
@Override
public void init(FilterConfig config) throws ServletException {
Enumeration<String> paramNames = config.getInitParameterNames();
while(paramNames.hasMoreElements()) {
String name = paramNames.nextElement();
hints.add(config.getInitParameter(name));
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse rsp = (HttpServletResponse)response;
if(!hints.isEmpty()) {
for(String hint : hints) {
rsp.addHeader("Link", hint);
}
rsp.sendEarlyHints();
// for Tomcat 11 and earlier, this is:
((org.apache.catalina.connector.Response)rsp).sendEarlyHints();
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org