Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When handling OPTION calls ignore both 'Date' and 'Allow' headers to … #206

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/java/org/jruby/rack/servlet/ResponseCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -24,6 +27,13 @@
* Response wrapper passed to filter chain.
*/
public class ResponseCapture extends HttpServletResponseWrapper {
/**
* In case of OPTION calls we check what headers are set by the servlet to decide if a request is
* already handled. Some container implementation of DefaultServlet already set 'Allow' and/or 'Date' fields which
* we need to ignore when working with this approach
*/
private static final Set<String> HEADERS_NOT_CONSIDERED_HANDLED_FOR_OPTIONS_CALL
= new HashSet<String>(Arrays.asList("Allow", "Date"));

private static final String STREAM = "stream";
private static final String WRITER = "writer";
Expand Down Expand Up @@ -230,9 +240,10 @@ public boolean isHandled(final HttpServletRequest request) {
// not to happen but there's all kind of beasts out there
return false;
}
// if any other headers occur beside 'Allow' and 'Date' we consider this request handled.
for ( final String headerName : headerNames ) {
if ( ! "Allow".equals( headerName ) ) {
return handled = true; // not just Allow header - consider handled
if ( !HEADERS_NOT_CONSIDERED_HANDLED_FOR_OPTIONS_CALL.contains(headerName) ) {
return handled = true;
}
}
return false; // OPTIONS with only Allow header set - unhandled
Expand Down
12 changes: 11 additions & 1 deletion src/spec/ruby/rack/servlet/response_capture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@
expect( response_capture.isHandled(servlet_request) ).to be false
end

it "is not considered handled when only Allow or Date header is added with OPTIONS" do
servlet_request.method = 'OPTIONS'

# NOTE: Jetty sets both Date and Allow in DefaultServlet#doOptions
response_capture.addHeader "Allow", "GET, POST, OPTIONS"
response_capture.addHeader "Date", Time.now.httpdate

expect( response_capture.isHandled(servlet_request) ).to be false
end

it "is considered handled when more than Allow header is added with OPTIONS" do
pending "need Servlet API 3.0" unless servlet_30?

Expand Down Expand Up @@ -100,4 +110,4 @@ def servlet_30?
Java::JavaClass.for_name('javax.servlet.AsyncContext') rescue nil
end

end
end