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

Fix #1077 : Solve the bad route selection based on acceptType. #1223

Closed
wants to merge 3 commits 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
8 changes: 8 additions & 0 deletions src/main/java/spark/http/matching/MatcherFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package spark.http.matching;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -34,6 +35,7 @@
import spark.Response;
import spark.embeddedserver.jetty.HttpRequestWrapper;
import spark.route.HttpMethod;
import spark.routematch.RouteMatch;
import spark.serialization.SerializerChain;
import spark.staticfiles.StaticFilesConfiguration;

Expand Down Expand Up @@ -107,6 +109,12 @@ public void doFilter(ServletRequest servletRequest,
String uri = httpRequest.getRequestURI();
String acceptType = httpRequest.getHeader(ACCEPT_TYPE_REQUEST_MIME_HEADER);

List<RouteMatch> routes=routeMatcher.findAll();
String firstAcceptType=routes.get(0).getAcceptType();
if(acceptType.equals("*/*")){
acceptType=firstAcceptType;
}

Body body = Body.create();

RequestWrapper requestWrapper = RequestWrapper.create();
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/spark/Issue1077Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package spark;

import static spark.Spark.*;


// Try to fix issue 1026: https://github.com/perwendel/spark/issues/1077
// I am not sure whether it is a bug, because it is tagged as Bug ..?
// But I think it conflict with the documentation, so I try to fix it
// In short, we expect the input and output are:
// curl -i -H "Accept: application/json" http://localhost:4567/hello : Hello application json
// curl -i -H "Accept: text/html" http://localhost:4567/hello : Go Away!!!
// curl http://localhost:4567/hello : Hello application json
// The first and second are right, but now the last command will get output: Go Away!!!
// I think it is not reasonable because the empty acceptType should match every possibilities
// so with the earliest match, it should match the first possible acceptType
// Therefore, you can exchange the 20th and 22th line to check whether it match the first type

public class Issue1077Test {
public static void main(String[] args) {
get("/hello","application/json", (request, response) -> "{\"message\": \"Hello application json\"}");

get("/hello","text/json", (request, response) -> "{\"message\": \"Hello text json\"}");

get("/hello", (request, response) -> {
response.status(406);
return "Go Away!!!";
});
}
}