import io.netty.handler.codec.http.HttpHeaderUtil; //导入依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, FullHttpRequest request, List out) throws Exception {
// Handle a bad request.
if (!request.decoderResult().isSuccess()) {
sendHttpResponse(ctx, request, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
return;
}
if ("websocket".equalsIgnoreCase((String) request.headers().get("Upgrade"))) {
handleWebSocket(ctx, request);
return;
}
String queryStr = request.uri();
if ("/favicon.ico".equals(queryStr)) {
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
sendHttpResponse(ctx, request, res);
return;
}
if (queryStr != null) {
queryStr = queryStr.trim();
if (queryStr.length() > 0 && queryStr.charAt(0) == '/') {
if (queryStr.length() == 1) {
queryStr = "";
} else {
int wh = queryStr.indexOf('?');
if (wh > 0) {
queryStr = queryStr.substring(wh + 1);
} else {
queryStr = queryStr.substring(1);
}
}
}
} else {
queryStr = "";
}
HttpMethod method = request.method();
logger.debug("decode queryStr ={}, method={}, msg={}", queryStr, method, request);
if (directHandleMethod(ctx, request, method)) {
return;
}
ByteBuf content;
if (queryStr.length() == 0) {
if (HttpMethod.GET.equals(method)) {
handleHttpHomePage(ctx, request);
return;
}
content = request.content().retain();
} else {
queryStr = URLDecoder.decode(queryStr, "UTF-8");
int strLen = queryStr.length();
if (queryStr.charAt(0) != '[' && strLen > 5) {
boolean wordOrLetter = Character.isLetterOrDigit(queryStr.charAt(strLen - 1));
for (int i = 2, MAX = Math.min(strLen, 7); i < MAX; i++) {
char c = queryStr.charAt(strLen - i);
if (c == '.') {
if (wordOrLetter) {
serverDef.httpResourceHandler.process(ctx, request, queryStr, strLen - i);
return;
}
break;
} else if (wordOrLetter && !Character.isLetterOrDigit(c)) {
wordOrLetter = false;
}
}
}
byte[] bytes = queryStr.getBytes();
// System.err.println("URI: bytes[0] = "+bytes[0]+", len =
// "+bytes.length);
content = ctx.alloc().buffer(bytes.length);
content.writeBytes(bytes);
}
logger.debug("content.size = " + content.readableBytes());
out.add(content);
boolean alive = HttpHeaderUtil.isKeepAlive(request);
Object event = alive ? thriftMessageWrapperKeepAlive : thriftMessageWrapperNormal;
ctx.fireUserEventTriggered(event);
}