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 issue#1204 #1236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,23 @@
<!-- LOGGING DEPENDENCIES -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-simple</artifactId>-->
<!-- <version>1.7.25</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->

<!-- JETTY DEPENDENCIES -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/spark/embeddedserver/jetty/JettyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
Expand Down Expand Up @@ -41,12 +41,21 @@ public JettyHandler(Filter filter) {

@Override
public void doHandle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {

HttpRequestWrapper wrapper = new HttpRequestWrapper(request);
final String[] METHODS = {"GET", "POST", "HEAD", "PUT", "OPTIONS", "DELETE", "TRACE", "CONNECT "};
boolean isValid = false;
for (String METHOD : METHODS) {
if (request.getMethod().equalsIgnoreCase(METHOD)) {
isValid = true;
break;
}
}
if (!isValid) return;
filter.doFilter(wrapper, response, null);

if (wrapper.notConsumed()) {
Expand All @@ -57,4 +66,4 @@ public void doHandle(

}

}
}
61 changes: 61 additions & 0 deletions src/test/java/spark/InvalidRequestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package spark;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

import static spark.Spark.halt;

public class InvalidRequestTest {
@Test
public void invalidRequestTest(){
Service service = Service.ignite().port(4567);
service.staticFiles.externalLocation("/Users/");

service.get("/", (req, res) -> {
if (!req.requestMethod().equalsIgnoreCase("GET")) {
halt(401, "invalid Http method");
}
return null;
});

String result = "";
String url = "http://localhost:4567";
BufferedReader in = null;
try {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("Method", "XYZ");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
return;
}
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
assertEquals("", result);
}
}