2008-11-19 08:22:15 +01:00
|
|
|
/*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Copyright 2009 Red Hat, Inc.
|
2009-06-19 19:48:17 +02:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Red Hat 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
|
|
|
*
|
2009-08-28 09:15:49 +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
|
|
|
|
* 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.HttpCodecUtil.*;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-06-15 09:50:53 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2010-01-08 09:29:37 +01:00
|
|
|
import java.util.Map;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.buffer.ChannelBuffer;
|
|
|
|
import io.netty.buffer.ChannelBuffers;
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.handler.codec.oneone.OneToOneEncoder;
|
|
|
|
import io.netty.util.CharsetUtil;
|
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.
|
2009-07-20 05:37:35 +02:00
|
|
|
* @apiviz.landmark
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2008-12-17 08:38:32 +01:00
|
|
|
public abstract class HttpMessageEncoder extends OneToOneEncoder {
|
|
|
|
|
2009-12-29 06:52:00 +01:00
|
|
|
private static final ChannelBuffer LAST_CHUNK =
|
|
|
|
copiedBuffer("0\r\n\r\n", CharsetUtil.US_ASCII);
|
2009-02-12 08:01:26 +01:00
|
|
|
|
2010-01-07 10:05:38 +01:00
|
|
|
private volatile boolean chunked;
|
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*/
|
|
|
|
protected HttpMessageEncoder() {
|
|
|
|
}
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
@Override
|
2008-12-17 08:38:32 +01:00
|
|
|
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) 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;
|
|
|
|
boolean chunked = this.chunked = HttpCodecUtil.isTransferEncodingChunked(m);
|
2009-02-12 07:37:22 +01:00
|
|
|
ChannelBuffer header = ChannelBuffers.dynamicBuffer(
|
|
|
|
channel.getConfig().getBufferFactory());
|
2010-01-07 10:05:38 +01:00
|
|
|
encodeInitialLine(header, m);
|
|
|
|
encodeHeaders(header, m);
|
2010-02-01 09:32:18 +01:00
|
|
|
header.writeByte(CR);
|
|
|
|
header.writeByte(LF);
|
2009-02-12 07:37:22 +01:00
|
|
|
|
2010-01-07 10:05:38 +01:00
|
|
|
ChannelBuffer content = m.getContent();
|
2009-04-02 11:10:57 +02:00
|
|
|
if (!content.readable()) {
|
2009-02-12 07:37:22 +01:00
|
|
|
return header; // no content
|
2010-01-07 10:05:38 +01:00
|
|
|
} else if (chunked) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"HttpMessage.content must be empty " +
|
|
|
|
"if Transfer-Encoding is chunked.");
|
2009-02-12 07:37:22 +01:00
|
|
|
} else {
|
|
|
|
return wrappedBuffer(header, content);
|
|
|
|
}
|
2009-03-04 14:45:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msg instanceof HttpChunk) {
|
2009-02-12 07:37:22 +01:00
|
|
|
HttpChunk chunk = (HttpChunk) msg;
|
2010-01-07 10:05:38 +01:00
|
|
|
if (chunked) {
|
2010-01-07 10:19:19 +01:00
|
|
|
if (chunk.isLast()) {
|
2010-01-07 10:05:38 +01:00
|
|
|
chunked = false;
|
2010-01-07 10:19:19 +01:00
|
|
|
if (chunk instanceof HttpChunkTrailer) {
|
|
|
|
ChannelBuffer trailer = ChannelBuffers.dynamicBuffer(
|
|
|
|
channel.getConfig().getBufferFactory());
|
|
|
|
trailer.writeByte((byte) '0');
|
2010-02-01 09:32:18 +01:00
|
|
|
trailer.writeByte(CR);
|
|
|
|
trailer.writeByte(LF);
|
2010-01-07 10:19:19 +01:00
|
|
|
encodeTrailingHeaders(trailer, (HttpChunkTrailer) chunk);
|
2010-02-01 09:32:18 +01:00
|
|
|
trailer.writeByte(CR);
|
|
|
|
trailer.writeByte(LF);
|
2010-01-07 10:19:19 +01:00
|
|
|
return trailer;
|
|
|
|
} else {
|
|
|
|
return LAST_CHUNK.duplicate();
|
|
|
|
}
|
2010-01-07 10:05:38 +01:00
|
|
|
} else {
|
|
|
|
ChannelBuffer content = chunk.getContent();
|
|
|
|
int contentLength = content.readableBytes();
|
2009-02-12 07:37:22 +01:00
|
|
|
|
2010-01-07 10:05:38 +01:00
|
|
|
return wrappedBuffer(
|
|
|
|
copiedBuffer(
|
|
|
|
Integer.toHexString(contentLength),
|
|
|
|
CharsetUtil.US_ASCII),
|
|
|
|
wrappedBuffer(CRLF),
|
|
|
|
content.slice(content.readerIndex(), contentLength),
|
|
|
|
wrappedBuffer(CRLF));
|
|
|
|
}
|
|
|
|
} else {
|
2010-01-07 10:19:19 +01:00
|
|
|
if (chunk.isLast()) {
|
2010-01-07 10:05:38 +01:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return chunk.getContent();
|
|
|
|
}
|
2009-02-12 08:01:26 +01:00
|
|
|
}
|
2010-01-07 10:05:38 +01:00
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-02-12 07:37:22 +01:00
|
|
|
|
|
|
|
// Unknown message type.
|
|
|
|
return msg;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
private void encodeHeaders(ChannelBuffer buf, HttpMessage message) {
|
2009-06-15 09:50:53 +02:00
|
|
|
try {
|
2010-01-08 09:29:37 +01:00
|
|
|
for (Map.Entry<String, String> h: message.getHeaders()) {
|
|
|
|
encodeHeader(buf, h.getKey(), h.getValue());
|
2009-11-17 08:31:35 +01:00
|
|
|
}
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw (Error) new Error().initCause(e);
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-11-17 08:31:35 +01:00
|
|
|
private void encodeTrailingHeaders(ChannelBuffer buf, HttpChunkTrailer trailer) {
|
|
|
|
try {
|
2010-01-08 09:29:37 +01:00
|
|
|
for (Map.Entry<String, String> h: trailer.getHeaders()) {
|
|
|
|
encodeHeader(buf, h.getKey(), h.getValue());
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-06-15 09:50:53 +02:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw (Error) new Error().initCause(e);
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-17 08:31:35 +01:00
|
|
|
private 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);
|
2009-11-17 08:31:35 +01:00
|
|
|
}
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
protected abstract void encodeInitialLine(ChannelBuffer buf, HttpMessage message) throws Exception;
|
|
|
|
}
|