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 e427fc56a0
commit 1ed8a120c1

View File

@ -70,7 +70,7 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
buf = ctx.alloc().buffer();
// Encode the message.
encodeInitialLine(buf, m);
m.headers().forEachEntry(new HttpHeadersEncoder(buf));
encodeHeaders(m.headers(), buf);
buf.writeBytes(CRLF);
state = HttpHeaderUtil.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK;
}
@ -133,6 +133,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) throws Exception {
headers.forEachEntry(new HttpHeadersEncoder(buf));
}
private void encodeChunkedContent(ChannelHandlerContext ctx, Object msg, long contentLength, List<Object> out) {
if (contentLength > 0) {
byte[] length = Long.toHexString(contentLength).getBytes(CharsetUtil.US_ASCII);
@ -152,7 +159,7 @@ public abstract class HttpObjectEncoder<H extends HttpMessage> extends MessageTo
ByteBuf buf = ctx.alloc().buffer();
buf.writeBytes(ZERO_CRLF);
try {
headers.forEachEntry(new HttpHeadersEncoder(buf));
encodeHeaders(headers, buf);
} catch (Exception ex) {
buf.release();
PlatformDependent.throwException(ex);