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
|
|
|
*
|
2011-12-09 06:18:34 +01: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
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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
|
|
|
|
2012-06-11 10:02:00 +02:00
|
|
|
import static io.netty.buffer.Unpooled.*;
|
2012-05-31 20:32:42 +02:00
|
|
|
import static io.netty.handler.codec.http.HttpConstants.*;
|
2012-06-10 04:08:43 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-06-07 07:52:33 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2012-06-09 14:05:59 +02:00
|
|
|
import io.netty.handler.codec.MessageToByteEncoder;
|
2012-05-20 07:19:11 +02:00
|
|
|
import io.netty.handler.codec.UnsupportedMessageTypeException;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.util.CharsetUtil;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2012-05-20 07:19:11 +02:00
|
|
|
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
|
2012-06-10 04:08:43 +02:00
|
|
|
* a {@link ByteBuf}.
|
2009-06-19 17:35:19 +02:00
|
|
|
*
|
|
|
|
* <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.
|
2009-07-20 05:37:35 +02:00
|
|
|
* @apiviz.landmark
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2012-06-09 14:05:59 +02:00
|
|
|
public abstract class HttpMessageEncoder extends MessageToByteEncoder<Object> {
|
2008-12-17 08:38:32 +01:00
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private static final ByteBuf LAST_CHUNK =
|
2009-12-29 06:52:00 +01:00
|
|
|
copiedBuffer("0\r\n\r\n", CharsetUtil.US_ASCII);
|
2009-02-12 08:01:26 +01:00
|
|
|
|
2012-08-19 12:06:47 +02:00
|
|
|
private HttpTransferEncoding lastTE;
|
2010-01-07 10:05:38 +01:00
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*/
|
|
|
|
protected HttpMessageEncoder() {
|
2012-09-28 07:00:07 +02:00
|
|
|
super(HttpObject.class);
|
2012-05-29 22:34:01 +02:00
|
|
|
}
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
@Override
|
2012-06-10 04:08:43 +02:00
|
|
|
public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
|
2009-02-12 07:37:22 +01:00
|
|
|
if (msg instanceof HttpMessage) {
|
2010-01-07 10:05:38 +01:00
|
|
|
HttpMessage m = (HttpMessage) msg;
|
2012-08-19 12:06:47 +02:00
|
|
|
HttpTransferEncoding te = m.getTransferEncoding();
|
|
|
|
lastTE = te;
|
|
|
|
// Calling setTransferEncoding() will sanitize the headers and the content.
|
|
|
|
// For example, it will remove the cases such as 'Transfer-Encoding' and 'Content-Length'
|
|
|
|
// coexist. It also removes the content if the transferEncoding is not SINGLE.
|
|
|
|
m.setTransferEncoding(te);
|
2012-08-17 04:30:08 +02:00
|
|
|
|
2012-08-19 12:06:47 +02:00
|
|
|
// Encode the message.
|
2012-05-20 07:19:11 +02:00
|
|
|
out.markWriterIndex();
|
|
|
|
encodeInitialLine(out, m);
|
|
|
|
encodeHeaders(out, m);
|
|
|
|
out.writeByte(CR);
|
|
|
|
out.writeByte(LF);
|
2009-02-12 07:37:22 +01:00
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
ByteBuf content = m.getContent();
|
2012-08-19 12:06:47 +02:00
|
|
|
out.writeBytes(content, content.readerIndex(), content.readableBytes());
|
2012-05-20 07:19:11 +02:00
|
|
|
} else if (msg instanceof HttpChunk) {
|
2009-02-12 07:37:22 +01:00
|
|
|
HttpChunk chunk = (HttpChunk) msg;
|
2012-08-19 12:06:47 +02:00
|
|
|
HttpTransferEncoding te = lastTE;
|
|
|
|
if (te == null) {
|
|
|
|
throw new IllegalArgumentException("HttpChunk must follow an HttpMessage.");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (te) {
|
|
|
|
case SINGLE:
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"The transfer encoding of the last encoded HttpMessage is SINGLE.");
|
|
|
|
case STREAMED: {
|
|
|
|
ByteBuf content = chunk.getContent();
|
|
|
|
out.writeBytes(content, content.readerIndex(), content.readableBytes());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CHUNKED:
|
2010-01-07 10:19:19 +01:00
|
|
|
if (chunk.isLast()) {
|
|
|
|
if (chunk instanceof HttpChunkTrailer) {
|
2012-05-20 07:19:11 +02:00
|
|
|
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 {
|
2012-05-20 07:19:11 +02:00
|
|
|
out.writeBytes(LAST_CHUNK, LAST_CHUNK.readerIndex(), LAST_CHUNK.readableBytes());
|
2010-01-07 10:19:19 +01:00
|
|
|
}
|
2010-01-07 10:05:38 +01:00
|
|
|
} else {
|
2012-06-10 04:08:43 +02:00
|
|
|
ByteBuf content = chunk.getContent();
|
2010-01-07 10:05:38 +01:00
|
|
|
int contentLength = content.readableBytes();
|
2012-05-20 07:19:11 +02:00
|
|
|
out.writeBytes(copiedBuffer(Integer.toHexString(contentLength), CharsetUtil.US_ASCII));
|
2012-05-31 20:32:42 +02:00
|
|
|
out.writeByte(CR);
|
|
|
|
out.writeByte(LF);
|
2012-05-20 07:19:11 +02:00
|
|
|
out.writeBytes(content, content.readerIndex(), contentLength);
|
2012-05-31 20:32:42 +02:00
|
|
|
out.writeByte(CR);
|
|
|
|
out.writeByte(LF);
|
2010-01-07 10:05:38 +01:00
|
|
|
}
|
2009-02-12 08:01:26 +01:00
|
|
|
}
|
2012-05-20 07:19:11 +02:00
|
|
|
} else {
|
|
|
|
throw new UnsupportedMessageTypeException(msg, HttpMessage.class, HttpChunk.class);
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private static void encodeHeaders(ByteBuf buf, HttpMessage message) {
|
2012-08-19 12:06:47 +02:00
|
|
|
for (Map.Entry<String, String> h: message.getHeaders()) {
|
|
|
|
encodeHeader(buf, h.getKey(), h.getValue());
|
2009-11-17 08:31:35 +01:00
|
|
|
}
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private static void encodeTrailingHeaders(ByteBuf buf, HttpChunkTrailer trailer) {
|
2012-08-19 12:06:47 +02:00
|
|
|
for (Map.Entry<String, String> h: trailer.getHeaders()) {
|
|
|
|
encodeHeader(buf, h.getKey(), h.getValue());
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 12:06:47 +02:00
|
|
|
private static void encodeHeader(ByteBuf buf, String header, String value) {
|
2012-08-17 04:43:59 +02:00
|
|
|
buf.writeBytes(header.getBytes(CharsetUtil.US_ASCII));
|
2009-11-17 08:31:35 +01:00
|
|
|
buf.writeByte(COLON);
|
|
|
|
buf.writeByte(SP);
|
2012-08-17 04:43:59 +02:00
|
|
|
buf.writeBytes(value.getBytes(CharsetUtil.US_ASCII));
|
2010-02-01 09:32:18 +01:00
|
|
|
buf.writeByte(CR);
|
|
|
|
buf.writeByte(LF);
|
2009-11-17 08:31:35 +01:00
|
|
|
}
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
protected abstract void encodeInitialLine(ByteBuf buf, HttpMessage message) throws Exception;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|