From 2137bad98cd18898f6763b1d326e0115a172b3dc Mon Sep 17 00:00:00 2001 From: Chauncey-Xxy <11810113@mail.sustech.edu.cn> Date: Sat, 24 Apr 2021 09:16:10 +0800 Subject: [PATCH 1/3] fix issue 1077 --- .../spark/http/matching/MatcherFilter.java | 56 +++++++++++-------- src/test/java/spark/Issue1077Test.java | 29 ++++++++++ 2 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 src/test/java/spark/Issue1077Test.java diff --git a/src/main/java/spark/http/matching/MatcherFilter.java b/src/main/java/spark/http/matching/MatcherFilter.java index 3fa05bea0a..722d8b29d5 100644 --- a/src/main/java/spark/http/matching/MatcherFilter.java +++ b/src/main/java/spark/http/matching/MatcherFilter.java @@ -17,6 +17,7 @@ package spark.http.matching; import java.io.IOException; +import java.util.List; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -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; @@ -107,6 +109,12 @@ public void doFilter(ServletRequest servletRequest, String uri = httpRequest.getRequestURI(); String acceptType = httpRequest.getHeader(ACCEPT_TYPE_REQUEST_MIME_HEADER); + List routes=routeMatcher.findAll(); + String firstAcceptType=routes.get(0).getAcceptType(); + if(acceptType.equals("*/*")){ + acceptType=firstAcceptType; + } + Body body = Body.create(); RequestWrapper requestWrapper = RequestWrapper.create(); @@ -117,15 +125,15 @@ public void doFilter(ServletRequest servletRequest, HttpMethod httpMethod = HttpMethod.get(httpMethodStr); RouteContext context = RouteContext.create() - .withMatcher(routeMatcher) - .withHttpRequest(httpRequest) - .withUri(uri) - .withAcceptType(acceptType) - .withBody(body) - .withRequestWrapper(requestWrapper) - .withResponseWrapper(responseWrapper) - .withResponse(response) - .withHttpMethod(httpMethod); + .withMatcher(routeMatcher) + .withHttpRequest(httpRequest) + .withUri(uri) + .withAcceptType(acceptType) + .withBody(body) + .withRequestWrapper(requestWrapper) + .withResponseWrapper(responseWrapper) + .withResponse(response) + .withHttpMethod(httpMethod); try { try { @@ -141,13 +149,13 @@ public void doFilter(ServletRequest servletRequest, } catch (Exception generalException) { GeneralError.modify( - httpRequest, - httpResponse, - body, - requestWrapper, - responseWrapper, - exceptionMapper, - generalException); + httpRequest, + httpResponse, + body, + requestWrapper, + responseWrapper, + exceptionMapper, + generalException); } @@ -165,7 +173,7 @@ public void doFilter(ServletRequest servletRequest, if (body.notSet()) { LOG.info("The requested route [{}] has not been mapped in Spark for {}: [{}]", - uri, ACCEPT_TYPE_REQUEST_MIME_HEADER, acceptType); + uri, ACCEPT_TYPE_REQUEST_MIME_HEADER, acceptType); httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND); if (CustomErrorPages.existsFor(404)) { @@ -181,13 +189,13 @@ public void doFilter(ServletRequest servletRequest, AfterAfterFilters.execute(context); } catch (Exception generalException) { GeneralError.modify( - httpRequest, - httpResponse, - body, - requestWrapper, - responseWrapper, - exceptionMapper, - generalException); + httpRequest, + httpResponse, + body, + requestWrapper, + responseWrapper, + exceptionMapper, + generalException); } } diff --git a/src/test/java/spark/Issue1077Test.java b/src/test/java/spark/Issue1077Test.java new file mode 100644 index 0000000000..2489e22da9 --- /dev/null +++ b/src/test/java/spark/Issue1077Test.java @@ -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 acceptTyoe +// 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!!!"; + }); + } +} From d9bbaf42e4b892f628607f0c4a4c11771a50a2ff Mon Sep 17 00:00:00 2001 From: Chauncey-Xxy <11810113@mail.sustech.edu.cn> Date: Sat, 24 Apr 2021 09:18:10 +0800 Subject: [PATCH 2/3] fix --- .../spark/http/matching/MatcherFilter.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/spark/http/matching/MatcherFilter.java b/src/main/java/spark/http/matching/MatcherFilter.java index 722d8b29d5..5f03eafdb1 100644 --- a/src/main/java/spark/http/matching/MatcherFilter.java +++ b/src/main/java/spark/http/matching/MatcherFilter.java @@ -125,15 +125,15 @@ public void doFilter(ServletRequest servletRequest, HttpMethod httpMethod = HttpMethod.get(httpMethodStr); RouteContext context = RouteContext.create() - .withMatcher(routeMatcher) - .withHttpRequest(httpRequest) - .withUri(uri) - .withAcceptType(acceptType) - .withBody(body) - .withRequestWrapper(requestWrapper) - .withResponseWrapper(responseWrapper) - .withResponse(response) - .withHttpMethod(httpMethod); + .withMatcher(routeMatcher) + .withHttpRequest(httpRequest) + .withUri(uri) + .withAcceptType(acceptType) + .withBody(body) + .withRequestWrapper(requestWrapper) + .withResponseWrapper(responseWrapper) + .withResponse(response) + .withHttpMethod(httpMethod); try { try { @@ -149,13 +149,13 @@ public void doFilter(ServletRequest servletRequest, } catch (Exception generalException) { GeneralError.modify( - httpRequest, - httpResponse, - body, - requestWrapper, - responseWrapper, - exceptionMapper, - generalException); + httpRequest, + httpResponse, + body, + requestWrapper, + responseWrapper, + exceptionMapper, + generalException); } @@ -173,7 +173,7 @@ public void doFilter(ServletRequest servletRequest, if (body.notSet()) { LOG.info("The requested route [{}] has not been mapped in Spark for {}: [{}]", - uri, ACCEPT_TYPE_REQUEST_MIME_HEADER, acceptType); + uri, ACCEPT_TYPE_REQUEST_MIME_HEADER, acceptType); httpResponse.setStatus(HttpServletResponse.SC_NOT_FOUND); if (CustomErrorPages.existsFor(404)) { @@ -189,13 +189,13 @@ public void doFilter(ServletRequest servletRequest, AfterAfterFilters.execute(context); } catch (Exception generalException) { GeneralError.modify( - httpRequest, - httpResponse, - body, - requestWrapper, - responseWrapper, - exceptionMapper, - generalException); + httpRequest, + httpResponse, + body, + requestWrapper, + responseWrapper, + exceptionMapper, + generalException); } } From 601d0a9006e68f3c9ca7ea1a8d8cac66d757db64 Mon Sep 17 00:00:00 2001 From: Chauncey-Xxy <11810113@mail.sustech.edu.cn> Date: Sat, 24 Apr 2021 09:28:01 +0800 Subject: [PATCH 3/3] fix --- src/test/java/spark/Issue1077Test.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/spark/Issue1077Test.java b/src/test/java/spark/Issue1077Test.java index 2489e22da9..4222c4dc10 100644 --- a/src/test/java/spark/Issue1077Test.java +++ b/src/test/java/spark/Issue1077Test.java @@ -12,7 +12,7 @@ // 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 acceptTyoe +// 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 {