Skip to content

Commit

Permalink
调试异步http server
Browse files Browse the repository at this point in the history
  • Loading branch information
virjar committed Oct 12, 2019
1 parent c8c0c5d commit 2dc2462
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 35 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ script/dist.zip

container-builder-repkg/src/main/resources/ratel_engine.properties

container-builder-repkg/src/main/resources/dex_maker_opt.zip
container-builder-repkg/src/main/resources/dex_maker_opt.zip

logs/
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import external.com.alibaba.fastjson.JSONException;
import external.com.alibaba.fastjson.JSONObject;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
Expand Down Expand Up @@ -103,12 +105,10 @@ public void onSekiroResponse(SekiroNatMessage sekiroNatMessage) {
}
}

DefaultHttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(data));
httpResponse.headers().set(HeaderNameValue.CONTENT_TYPE, responseContentType);
httpResponse.headers().set(HeaderNameValue.CONTENT_LENGTH, data.length);

channel.write(httpResponse);
channel.writeAndFlush(data).addListener(ChannelFutureListener.CLOSE);
channel.writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HttpRequestDispatcher extends SimpleChannelInboundHandler<DefaultFullHttpRequest> {
public class HttpRequestDispatcher extends SimpleChannelInboundHandler<FullHttpRequest> {


@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, DefaultFullHttpRequest request) throws Exception {
protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest request) throws Exception {
doHandle(channelHandlerContext, request);

}

private void doHandle(ChannelHandlerContext channelHandlerContext, FullHttpRequest request) {
String uri = request.getUri();
HttpMethod method = request.getMethod();

Expand All @@ -39,7 +46,8 @@ protected void channelRead0(ChannelHandlerContext channelHandlerContext, Default
if (uri.contains("?")) {
int index = uri.indexOf("?");
url = uri.substring(0, index);
query = uri.substring(index);
//排除?
query = uri.substring(index + 1);
}
if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
Expand All @@ -52,8 +60,7 @@ protected void channelRead0(ChannelHandlerContext channelHandlerContext, Default
if (!StringUtils.equalsAnyIgnoreCase(url, "/asyncInvoke")) {
//404
Channel channel = channelHandlerContext.channel();
channel.write(DefaultHtmlHttpResponse.notFound);
channel.writeAndFlush(DefaultHtmlHttpResponse.notFound.contentByteData).addListener(ChannelFutureListener.CLOSE);
channel.writeAndFlush(DefaultHtmlHttpResponse.notFound()).addListener(ChannelFutureListener.CLOSE);
return;
}

Expand All @@ -63,8 +70,7 @@ protected void channelRead0(ChannelHandlerContext channelHandlerContext, Default
if (contentType == null && !method.equals(HttpMethod.GET)) {
//不识别的请求类型
Channel channel = channelHandlerContext.channel();
channel.write(DefaultHtmlHttpResponse.badRequest);
channel.writeAndFlush(DefaultHtmlHttpResponse.badRequest.contentByteData).addListener(ChannelFutureListener.CLOSE);
channel.writeAndFlush(DefaultHtmlHttpResponse.badRequest()).addListener(ChannelFutureListener.CLOSE);
return;
}
if (contentType == null) {
Expand All @@ -80,8 +86,7 @@ protected void channelRead0(ChannelHandlerContext channelHandlerContext, Default
DefaultHtmlHttpResponse contentTypeNotSupportMessage = new DefaultHtmlHttpResponse(errorMessage);

Channel channel = channelHandlerContext.channel();
channel.write(contentTypeNotSupportMessage);
channel.writeAndFlush(contentTypeNotSupportMessage.contentByteData).addListener(ChannelFutureListener.CLOSE);
channel.writeAndFlush(contentTypeNotSupportMessage).addListener(ChannelFutureListener.CLOSE);
return;
// httpSekiroResponse.failed("sekiro framework only support contentType:application/x-www-form-urlencoded | application/json, now is: " + contentType.getMimeType());
// return;
Expand Down Expand Up @@ -111,25 +116,24 @@ protected void channelRead0(ChannelHandlerContext channelHandlerContext, Default
//TODO
log.warn("request body empty");
Channel channel = channelHandlerContext.channel();
channel.write(DefaultHtmlHttpResponse.badRequest);
channel.writeAndFlush(DefaultHtmlHttpResponse.badRequest.contentByteData).addListener(ChannelFutureListener.CLOSE);
channel.writeAndFlush(DefaultHtmlHttpResponse.badRequest()).addListener(ChannelFutureListener.CLOSE);
return;
}

String group;
String requestBody;
String bindClient;
if ("application/x-www-form-urlencoded".equalsIgnoreCase(contentType.getMimeType())) {
Multimap nameValuePairs = Multimap.parseQuery(query);
nameValuePairs.putAll(Multimap.parseQuery(postBody));
Multimap nameValuePairs = Multimap.parseUrlEncoded(query);
nameValuePairs.putAll(Multimap.parseUrlEncoded(postBody));
requestBody = CommonUtil.joinListParam(nameValuePairs);
group = nameValuePairs.getString("group");
bindClient = nameValuePairs.getString("bindClient");
} else {

try {
JSONObject jsonObject = JSONObject.parseObject(postBody);
jsonObject.putAll(Multimap.parseQuery(query));
jsonObject.putAll(Multimap.parseUrlEncoded(query));
group = jsonObject.getString("group");
requestBody = jsonObject.toJSONString();
bindClient = jsonObject.getString("bindClient");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.virjar.sekiro.server.netty.http;

import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequestDecoder;
Expand All @@ -16,6 +17,6 @@ protected HttpMessage createMessage(String[] initialLine) throws Exception {

String uri = initialLine[1];

return new RBHttpRequest(version, method, uri, validateHeaders);
return new DefaultHttpRequest(version, method, uri, validateHeaders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ private void startUp() {

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast("decoder", new RBHttpRequestDecoder())
.addLast("encoder", new HttpResponseEncoder())
socketChannel.pipeline().addLast(new RBHttpRequestDecoder())
.addLast(new HttpResponseEncoder())
//最大32M的报文
.addLast("aggregator", new HttpObjectAggregator(1 << 25))
.addLast("dispatcher", new HttpRequestDispatcher());
.addLast(new HttpObjectAggregator(1 << 25))
.addLast(new HttpRequestDispatcher());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
import com.google.common.base.Charsets;
import com.virjar.sekiro.server.netty.http.HeaderNameValue;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

public class DefaultHtmlHttpResponse extends io.netty.handler.codec.http.DefaultHttpResponse {
public final byte[] contentByteData;
public class DefaultHtmlHttpResponse extends DefaultFullHttpResponse {
// public final byte[] contentByteData;

public DefaultHtmlHttpResponse(String content) {
super(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
super(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(content.getBytes(Charsets.UTF_8)));

contentByteData = content.getBytes(Charsets.UTF_8);
//contentByteData = content.getBytes(Charsets.UTF_8);
headers().set(HeaderNameValue.CONTENT_TYPE, "text/html;charset=utf8;");
headers().set(HeaderNameValue.CONTENT_LENGTH, contentByteData.length);
// headers().set(HeaderNameValue.CONTENT_LENGTH, contentByteData.length);
}

private static final String badRequestContent = "<!DOCTYPE html>\n" +
Expand Down Expand Up @@ -47,9 +50,14 @@ public DefaultHtmlHttpResponse(String content) {
"</body>\n" +
"</html>";

public static DefaultHtmlHttpResponse badRequest() {
return new DefaultHtmlHttpResponse(badRequestContent);
}


public static DefaultHtmlHttpResponse badRequest = new DefaultHtmlHttpResponse(badRequestContent);
public static DefaultHtmlHttpResponse notFound() {
return new DefaultHtmlHttpResponse(notFoundContent);
}


public static DefaultHtmlHttpResponse notFound = new DefaultHtmlHttpResponse(notFoundContent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

import external.com.alibaba.fastjson.JSON;
import external.com.alibaba.fastjson.JSONObject;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
Expand Down Expand Up @@ -62,13 +64,11 @@ public static void writeRes(HttpServletResponse httpServletResponse, CommonRes<?
public static void writeRes(Channel channel, CommonRes<?> commonRes) {

byte[] bytes = JSON.toJSONString(commonRes).getBytes(StandardCharsets.UTF_8);
DefaultHttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(bytes));

httpResponse.headers().set(HeaderNameValue.CONTENT_TYPE, "application/json;charset=utf8;");
httpResponse.headers().set(HeaderNameValue.CONTENT_LENGTH, bytes.length);

channel.write(httpResponse);
channel.writeAndFlush(bytes).addListener(ChannelFutureListener.CLOSE);
channel.writeAndFlush(httpResponse).addListener(ChannelFutureListener.CLOSE);

}

Expand Down

0 comments on commit 2dc2462

Please sign in to comment.