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 b8dd95b8ad
commit f0e306c2fd

View File

@ -70,7 +70,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 = HttpHeaderUtil.isTransferEncodingChunked(m) ? ST_CONTENT_CHUNK : ST_CONTENT_NON_CHUNK; 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 {
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);
@ -151,9 +158,8 @@ 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);
try { try {
HttpHeaders.encode(headers, buf); encodeHeaders(headers, buf);
} catch (Exception ex) { } catch (Exception ex) {
buf.release(); buf.release();
PlatformDependent.throwException(ex); PlatformDependent.throwException(ex);