Allow to override how headers are encoded

Motivation:

Even if its against the HTTP RFC there are situations where it may be useful to use other chars then US_ASCII in the headers. We should allow to make it possible by allow the user to override the how headers are encoded.

Modifications:

- Add encodeHeaders(...) method and so allow to override it.

Result:

It's now possible to encode headers with other charset then US_ASCII by just extend the encoder and override the encodeHeaders(...) method.
This commit is contained in:
Norman Maurer 2014-12-08 08:32:48 +01:00 committed by Norman Maurer
parent b77e338cb7
commit 9638f2e9d7

View File

@ -69,7 +69,7 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
buf = ctx.alloc().buffer(); buf = ctx.alloc().buffer();
// Encode the message. // Encode the message.
encodeInitialLine(buf, m); encodeInitialLine(buf, m);
HttpHeaders.encode(m.headers(), buf); encodeHeaders(m.headers(), buf);
buf.writeBytes(CRLF); buf.writeBytes(CRLF);
state = HttpHeaders.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK; state = HttpHeaders.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK;
} }
@ -132,6 +132,13 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
} }
} }
/**
* Encode the {@link HttpHeaders} into a {@link ByteBuf}.
*/
protected void encodeHeaders(HttpHeaders headers, ByteBuf buf) {
HttpHeaders.encode(headers, buf);
}
private void encodeChunkedContent(ChannelHandlerContext ctx, Object msg, long contentLength, List<Object> out) { private void encodeChunkedContent(ChannelHandlerContext ctx, Object msg, long contentLength, List<Object> out) {
if (contentLength > 0) { if (contentLength > 0) {
byte[] length = Long.toHexString(contentLength).getBytes(CharsetUtil.US_ASCII); byte[] length = Long.toHexString(contentLength).getBytes(CharsetUtil.US_ASCII);
@ -150,7 +157,7 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
} else { } else {
ByteBuf buf = ctx.alloc().buffer(); ByteBuf buf = ctx.alloc().buffer();
buf.writeBytes(ZERO_CRLF); buf.writeBytes(ZERO_CRLF);
HttpHeaders.encode(headers, buf); encodeHeaders(headers, buf);
buf.writeBytes(CRLF); buf.writeBytes(CRLF);
out.add(buf); out.add(buf);
} }