HTTP TRACE&TRACK漏洞修复

pull/64/head
chaos 11 months ago
parent e5d26ba277
commit 2102c9e7a5

@ -4,54 +4,64 @@ import java.io.Serializable;
/** /**
* common return * common return
* @author xuxueli 2015-12-4 16:32:31 *
* @param <T> * @param <T>
* @author xuxueli 2015-12-4 16:32:31
*/ */
public class ReturnT<T> implements Serializable { public class ReturnT<T> implements Serializable {
public static final long serialVersionUID = 42L; public static final long serialVersionUID = 42L;
public static final int SUCCESS_CODE = 200; public static final int SUCCESS_CODE = 200;
public static final int FAIL_CODE = 500; public static final int FAIL_CODE = 500;
public static final int METHOD_NOT_ALLOWED = 405;
public static final ReturnT<String> SUCCESS = new ReturnT<String>(null);
public static final ReturnT<String> FAIL = new ReturnT<String>(FAIL_CODE, null); public static final ReturnT<String> SUCCESS = new ReturnT<String>(null);
public static final ReturnT<String> FAIL = new ReturnT<String>(FAIL_CODE, null);
private int code;
private String msg; private int code;
private T content; private String msg;
private T content;
public ReturnT(){}
public ReturnT(int code, String msg) { public ReturnT() {
this.code = code; }
this.msg = msg;
} public ReturnT(int code, String msg) {
public ReturnT(T content) { this.code = code;
this.code = SUCCESS_CODE; this.msg = msg;
this.content = content; }
}
public ReturnT(T content) {
public int getCode() { this.code = SUCCESS_CODE;
return code; this.content = content;
} }
public void setCode(int code) {
this.code = code; public int getCode() {
} return code;
public String getMsg() { }
return msg;
} public void setCode(int code) {
public void setMsg(String msg) { this.code = code;
this.msg = msg; }
}
public T getContent() { public String getMsg() {
return content; return msg;
} }
public void setContent(T content) {
this.content = content; public void setMsg(String msg) {
} this.msg = msg;
}
@Override
public String toString() { public T getContent() {
return "ReturnT [code=" + code + ", msg=" + msg + ", content=" + content + "]"; return content;
} }
public void setContent(T content) {
this.content = content;
}
@Override
public String toString() {
return "ReturnT [code=" + code + ", msg=" + msg + ", content=" + content + "]";
}
} }

@ -156,16 +156,24 @@ public class EmbedServer {
public void run() { public void run() {
// do invoke // do invoke
Object responseObj = process(httpMethod, uri, requestData, accessTokenReq); Object responseObj = process(httpMethod, uri, requestData, accessTokenReq);
HttpResponseStatus status = resolveHttpStatus(responseObj);
// to json // to json
String responseJson = GsonTool.toJson(responseObj); String responseJson = GsonTool.toJson(responseObj);
// write response // write response
writeResponse(ctx, keepAlive, responseJson); writeResponse(ctx, keepAlive, status, responseJson);
} }
}); });
} }
private HttpResponseStatus resolveHttpStatus(Object responseObj) {
if (responseObj instanceof ReturnT) {
ReturnT returnT = (ReturnT) responseObj;
return returnT.getCode() == ReturnT.METHOD_NOT_ALLOWED ? HttpResponseStatus.METHOD_NOT_ALLOWED : HttpResponseStatus.OK;
} else {
return HttpResponseStatus.OK;
}
}
private Object process(HttpMethod httpMethod, String uri, String requestData, String accessTokenReq) { private Object process(HttpMethod httpMethod, String uri, String requestData, String accessTokenReq) {
// valid // valid
if (HttpMethod.POST != httpMethod) { if (HttpMethod.POST != httpMethod) {
@ -209,9 +217,9 @@ public class EmbedServer {
/** /**
* write response * write response
*/ */
private void writeResponse(ChannelHandlerContext ctx, boolean keepAlive, String responseJson) { private void writeResponse(ChannelHandlerContext ctx, boolean keepAlive, HttpResponseStatus status, String responseJson) {
// write response // write response
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(responseJson, CharsetUtil.UTF_8)); // Unpooled.wrappedBuffer(responseJson) FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer(responseJson, CharsetUtil.UTF_8)); // Unpooled.wrappedBuffer(responseJson)
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8"); // HttpHeaderValues.TEXT_PLAIN.toString() response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8"); // HttpHeaderValues.TEXT_PLAIN.toString()
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes()); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
if (keepAlive) { if (keepAlive) {

Loading…
Cancel
Save