Some optimizations to the http codec
This commit is contained in:
parent
a3b4cdd614
commit
0e8fb21554
@ -39,7 +39,9 @@ import static io.netty.handler.codec.http.HttpConstants.*;
|
|||||||
* implement all abstract methods properly.
|
* implement all abstract methods properly.
|
||||||
*/
|
*/
|
||||||
public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageToByteEncoder<HttpObject> {
|
public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageToByteEncoder<HttpObject> {
|
||||||
|
private static final byte[] CRLF = { CR, LF };
|
||||||
|
private static final byte[] CRLF_END = { CR, LF, 0 };
|
||||||
|
private static final byte[] HEADER_SEPARATOR = { COLON , SP };
|
||||||
private static final int ST_INIT = 0;
|
private static final int ST_INIT = 0;
|
||||||
private static final int ST_CONTENT_NON_CHUNK = 1;
|
private static final int ST_CONTENT_NON_CHUNK = 1;
|
||||||
private static final int ST_CONTENT_CHUNK = 2;
|
private static final int ST_CONTENT_CHUNK = 2;
|
||||||
@ -60,8 +62,7 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
|
|||||||
// Encode the message.
|
// Encode the message.
|
||||||
encodeInitialLine(out, m);
|
encodeInitialLine(out, m);
|
||||||
encodeHeaders(out, m);
|
encodeHeaders(out, m);
|
||||||
out.writeByte(CR);
|
out.writeBytes(CRLF);
|
||||||
out.writeByte(LF);
|
|
||||||
|
|
||||||
state = HttpHeaders.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK;
|
state = HttpHeaders.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK;
|
||||||
}
|
}
|
||||||
@ -86,20 +87,16 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
|
|||||||
} else if (state == ST_CONTENT_CHUNK) {
|
} else if (state == ST_CONTENT_CHUNK) {
|
||||||
if (contentLength > 0) {
|
if (contentLength > 0) {
|
||||||
out.writeBytes(copiedBuffer(Integer.toHexString(contentLength), CharsetUtil.US_ASCII));
|
out.writeBytes(copiedBuffer(Integer.toHexString(contentLength), CharsetUtil.US_ASCII));
|
||||||
out.writeByte(CR);
|
out.writeBytes(CRLF);
|
||||||
out.writeByte(LF);
|
|
||||||
out.writeBytes(content, content.readerIndex(), contentLength);
|
out.writeBytes(content, content.readerIndex(), contentLength);
|
||||||
out.writeByte(CR);
|
out.writeBytes(CRLF);
|
||||||
out.writeByte(LF);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chunk instanceof LastHttpContent) {
|
if (chunk instanceof LastHttpContent) {
|
||||||
out.writeByte((byte) '0');
|
out.writeBytes(CRLF_END);
|
||||||
out.writeByte(CR);
|
|
||||||
out.writeByte(LF);
|
|
||||||
encodeTrailingHeaders(out, (LastHttpContent) chunk);
|
encodeTrailingHeaders(out, (LastHttpContent) chunk);
|
||||||
out.writeByte(CR);
|
out.writeBytes(CRLF);
|
||||||
out.writeByte(LF);
|
|
||||||
state = ST_INIT;
|
state = ST_INIT;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -122,11 +119,9 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
|
|||||||
|
|
||||||
private static void encodeHeader(ByteBuf buf, String header, String value) {
|
private static void encodeHeader(ByteBuf buf, String header, String value) {
|
||||||
buf.writeBytes(header.getBytes(CharsetUtil.US_ASCII));
|
buf.writeBytes(header.getBytes(CharsetUtil.US_ASCII));
|
||||||
buf.writeByte(COLON);
|
buf.writeBytes(HEADER_SEPARATOR);
|
||||||
buf.writeByte(SP);
|
|
||||||
buf.writeBytes(value.getBytes(CharsetUtil.US_ASCII));
|
buf.writeBytes(value.getBytes(CharsetUtil.US_ASCII));
|
||||||
buf.writeByte(CR);
|
buf.writeBytes(CRLF);
|
||||||
buf.writeByte(LF);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void encodeInitialLine(ByteBuf buf, H message) throws Exception;
|
protected abstract void encodeInitialLine(ByteBuf buf, H message) throws Exception;
|
||||||
|
@ -26,6 +26,7 @@ import static io.netty.handler.codec.http.HttpConstants.*;
|
|||||||
*/
|
*/
|
||||||
public class HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> {
|
public class HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> {
|
||||||
private static final char SLASH = '/';
|
private static final char SLASH = '/';
|
||||||
|
private static final byte[] CRLF = { CR, LF };
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean acceptOutboundMessage(Object msg) throws Exception {
|
public boolean acceptOutboundMessage(Object msg) throws Exception {
|
||||||
@ -47,11 +48,10 @@ public class HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> {
|
|||||||
uri += SLASH;
|
uri += SLASH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf.writeBytes(uri.getBytes("UTF-8"));
|
buf.writeBytes(uri.getBytes(CharsetUtil.UTF_8));
|
||||||
|
|
||||||
buf.writeByte(SP);
|
buf.writeByte(SP);
|
||||||
buf.writeBytes(request.getProtocolVersion().toString().getBytes(CharsetUtil.US_ASCII));
|
buf.writeBytes(request.getProtocolVersion().toString().getBytes(CharsetUtil.US_ASCII));
|
||||||
buf.writeByte(CR);
|
buf.writeBytes(CRLF);
|
||||||
buf.writeByte(LF);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ import static io.netty.handler.codec.http.HttpConstants.*;
|
|||||||
* a {@link ByteBuf}.
|
* a {@link ByteBuf}.
|
||||||
*/
|
*/
|
||||||
public class HttpResponseEncoder extends HttpObjectEncoder<HttpResponse> {
|
public class HttpResponseEncoder extends HttpObjectEncoder<HttpResponse> {
|
||||||
|
private static final byte[] CRLF = { CR, LF };
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean acceptOutboundMessage(Object msg) throws Exception {
|
public boolean acceptOutboundMessage(Object msg) throws Exception {
|
||||||
@ -38,7 +39,6 @@ public class HttpResponseEncoder extends HttpObjectEncoder<HttpResponse> {
|
|||||||
buf.writeBytes(String.valueOf(response.getStatus().code()).getBytes(CharsetUtil.US_ASCII));
|
buf.writeBytes(String.valueOf(response.getStatus().code()).getBytes(CharsetUtil.US_ASCII));
|
||||||
buf.writeByte(SP);
|
buf.writeByte(SP);
|
||||||
buf.writeBytes(String.valueOf(response.getStatus().reasonPhrase()).getBytes(CharsetUtil.US_ASCII));
|
buf.writeBytes(String.valueOf(response.getStatus().reasonPhrase()).getBytes(CharsetUtil.US_ASCII));
|
||||||
buf.writeByte(CR);
|
buf.writeBytes(CRLF);
|
||||||
buf.writeByte(LF);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user