http协议请求不符合method要求时改成返回405

This commit is contained in:
Redkale
2022-09-06 17:39:23 +08:00
parent cd3e5bdd14
commit 86a7ffb642
2 changed files with 17 additions and 4 deletions

View File

@@ -42,6 +42,8 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
protected static final byte[] bytes404 = "HTTP/1.1 404 Not Found\r\nContent-Length:0\r\n\r\n".getBytes();
protected static final byte[] bytes405 = "HTTP/1.1 405 Method Not Allowed\r\nContent-Length:0\r\n\r\n".getBytes();
protected static final byte[] bytes500 = "HTTP/1.1 500 Internal Server Error\r\nContent-Length:0\r\n\r\n".getBytes();
protected static final byte[] bytes504 = "HTTP/1.1 504 Gateway Timeout\r\nContent-Length:0\r\n\r\n".getBytes();
@@ -983,6 +985,14 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
super.finish(false, bytes404);
}
/**
* 以405状态码输出
*/
public void finish405() {
skipHeader();
super.finish(false, bytes405);
}
/**
* 以500状态码输出
*/

View File

@@ -16,7 +16,6 @@ import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
import static org.redkale.asm.Opcodes.*;
import org.redkale.boot.Application;
import org.redkale.net.*;
import org.redkale.service.RetResult;
import org.redkale.util.*;
/**
@@ -29,8 +28,10 @@ import org.redkale.util.*;
*/
public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse> {
@Deprecated //@since 2.8.0
public static final int RET_SERVER_ERROR = 1200_0001;
@Deprecated //@since 2.8.0
public static final int RET_METHOD_ERROR = 1200_0002;
String _actionSimpleMappingUrl; //只给HttpActionServlet使用_actionSimpleMappingUrl不能包含正则表达式比如 /json /createRecord, 不能是 /user/**
@@ -82,7 +83,8 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
if (request.actionEntry != null) {
ActionEntry entry = request.actionEntry;
if (!entry.checkMethod(request.getMethod())) {
response.finishJson(new RetResult(RET_METHOD_ERROR, "Method(" + request.getMethod() + ") Error"));
//response.finishJson(new RetResult(RET_METHOD_ERROR, "Method(" + request.getMethod() + ") Error"));
response.finish405();
return;
}
request.moduleid = entry.moduleid;
@@ -100,7 +102,8 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
if (request.getRequestURI().startsWith(en.getKey())) {
ActionEntry entry = en.getValue();
if (!entry.checkMethod(request.getMethod())) {
response.finishJson(new RetResult(RET_METHOD_ERROR, "Method(" + request.getMethod() + ") Error"));
//response.finishJson(new RetResult(RET_METHOD_ERROR, "Method(" + request.getMethod() + ") Error"));
response.finish405();
return;
}
request.actionEntry = entry;
@@ -332,7 +335,7 @@ public class HttpServlet extends Servlet<HttpContext, HttpRequest, HttpResponse>
final String name;
final String[] methods;
final String[] methods; //不能为null长度为0表示容许所有method
final HttpServlet servlet;