netty5/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageEncoder.java

163 lines
6.3 KiB
Java
Raw Normal View History

2008-11-19 08:22:15 +01:00
/*
2012-06-04 22:31:44 +02:00
* Copyright 2012 The Netty Project
2009-06-19 19:48:17 +02:00
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
2008-11-19 08:22:15 +01:00
*
2012-06-04 22:31:44 +02:00
* http://www.apache.org/licenses/LICENSE-2.0
2008-11-19 08:22:15 +01:00
*
2009-08-28 09:15:49 +02:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
2009-08-28 09:15:49 +02:00
* License for the specific language governing permissions and limitations
* under the License.
2008-11-19 08:22:15 +01:00
*/
2011-12-09 04:38:59 +01:00
package io.netty.handler.codec.http;
2008-11-19 08:22:15 +01:00
2011-12-09 04:38:59 +01:00
import static io.netty.buffer.ChannelBuffers.*;
import static io.netty.handler.codec.http.HttpConstants.*;
2011-12-09 04:38:59 +01:00
import io.netty.buffer.ChannelBuffer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.UnsupportedMessageTypeException;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpHeaders.Values;
2011-12-09 04:38:59 +01:00
import io.netty.util.CharsetUtil;
2008-11-19 08:22:15 +01:00
import java.io.UnsupportedEncodingException;
import java.util.Map;
2008-11-19 08:22:15 +01:00
/**
2009-06-19 17:35:19 +02:00
* Encodes an {@link HttpMessage} or an {@link HttpChunk} into
* a {@link ChannelBuffer}.
*
* <h3>Extensibility</h3>
*
* Please note that this encoder is designed to be extended to implement
* a protocol derived from HTTP, such as
* <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
* <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
* To implement the encoder of such a derived protocol, extend this class and
* implement all abstract methods properly.
* @apiviz.landmark
2008-11-19 08:22:15 +01:00
*/
public abstract class HttpMessageEncoder extends MessageToByteEncoder<Object> {
private static final ChannelBuffer LAST_CHUNK =
copiedBuffer("0\r\n\r\n", CharsetUtil.US_ASCII);
private volatile boolean chunked;
2009-06-19 17:35:19 +02:00
/**
* Creates a new instance.
*/
protected HttpMessageEncoder() {
}
@Override
public boolean isEncodable(Object msg) throws Exception {
return msg instanceof HttpMessage || msg instanceof HttpChunk;
}
2008-11-19 08:22:15 +01:00
@Override
public void encode(ChannelHandlerContext ctx, Object msg, ChannelBuffer out) throws Exception {
if (msg instanceof HttpMessage) {
HttpMessage m = (HttpMessage) msg;
boolean chunked;
if (m.isChunked()) {
// check if the Transfer-Encoding is set to chunked already.
// if not add the header to the message
if (!HttpCodecUtil.isTransferEncodingChunked(m)) {
m.addHeader(Names.TRANSFER_ENCODING, Values.CHUNKED);
}
chunked = this.chunked = true;
} else {
chunked = this.chunked = HttpCodecUtil.isTransferEncodingChunked(m);
}
out.markWriterIndex();
encodeInitialLine(out, m);
encodeHeaders(out, m);
out.writeByte(CR);
out.writeByte(LF);
ChannelBuffer content = m.getContent();
if (content.readable()) {
if (chunked) {
out.resetWriterIndex();
throw new IllegalArgumentException(
"HttpMessage.content must be empty " +
"if Transfer-Encoding is chunked.");
} else {
out.writeBytes(content, content.readerIndex(), content.readableBytes());
}
}
} else if (msg instanceof HttpChunk) {
HttpChunk chunk = (HttpChunk) msg;
if (chunked) {
2010-01-07 10:19:19 +01:00
if (chunk.isLast()) {
chunked = false;
2010-01-07 10:19:19 +01:00
if (chunk instanceof HttpChunkTrailer) {
out.writeByte((byte) '0');
out.writeByte(CR);
out.writeByte(LF);
encodeTrailingHeaders(out, (HttpChunkTrailer) chunk);
out.writeByte(CR);
out.writeByte(LF);
2010-01-07 10:19:19 +01:00
} else {
out.writeBytes(LAST_CHUNK, LAST_CHUNK.readerIndex(), LAST_CHUNK.readableBytes());
2010-01-07 10:19:19 +01:00
}
} else {
ChannelBuffer content = chunk.getContent();
int contentLength = content.readableBytes();
out.writeBytes(copiedBuffer(Integer.toHexString(contentLength), CharsetUtil.US_ASCII));
out.writeByte(CR);
out.writeByte(LF);
out.writeBytes(content, content.readerIndex(), contentLength);
out.writeByte(CR);
out.writeByte(LF);
}
} else {
if (!chunk.isLast()) {
ChannelBuffer chunkContent = chunk.getContent();
out.writeBytes(chunkContent, chunkContent.readerIndex(), chunkContent.readableBytes());
}
}
} else {
throw new UnsupportedMessageTypeException(msg, HttpMessage.class, HttpChunk.class);
2008-11-19 08:22:15 +01:00
}
}
private static void encodeHeaders(ChannelBuffer buf, HttpMessage message) {
try {
for (Map.Entry<String, String> h: message.getHeaders()) {
encodeHeader(buf, h.getKey(), h.getValue());
}
} catch (UnsupportedEncodingException e) {
throw (Error) new Error().initCause(e);
}
}
2008-11-19 08:22:15 +01:00
private static void encodeTrailingHeaders(ChannelBuffer buf, HttpChunkTrailer trailer) {
try {
for (Map.Entry<String, String> h: trailer.getHeaders()) {
encodeHeader(buf, h.getKey(), h.getValue());
2008-11-19 08:22:15 +01:00
}
} catch (UnsupportedEncodingException e) {
throw (Error) new Error().initCause(e);
2008-11-19 08:22:15 +01:00
}
}
private static void encodeHeader(ChannelBuffer buf, String header, String value)
throws UnsupportedEncodingException {
buf.writeBytes(header.getBytes("ASCII"));
buf.writeByte(COLON);
buf.writeByte(SP);
buf.writeBytes(value.getBytes("ASCII"));
2010-02-01 09:32:18 +01:00
buf.writeByte(CR);
buf.writeByte(LF);
}
2008-11-19 08:22:15 +01:00
protected abstract void encodeInitialLine(ChannelBuffer buf, HttpMessage message) throws Exception;
}