netty5/src/main/java/org/jboss/netty/handler/codec/http/HttpMessageEncoder.java

163 lines
5.9 KiB
Java
Raw Normal View History

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
*/
package org.jboss.netty.handler.codec.http;
import static org.jboss.netty.buffer.ChannelBuffers.*;
2008-11-19 08:22:15 +01:00
import static org.jboss.netty.handler.codec.http.HttpCodecUtil.*;
import java.io.UnsupportedEncodingException;
import java.util.Map;
2008-11-19 08:22:15 +01:00
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
2008-11-19 08:22:15 +01:00
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipelineCoverage;
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
import org.jboss.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.
2008-11-19 08:22:15 +01:00
*
* @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
2008-11-19 08:22:15 +01:00
* @author Andy Taylor (andy.taylor@jboss.org)
* @author <a href="http://gleamynode.net/">Trustin Lee</a>
* @version $Rev$, $Date$
*
* @apiviz.landmark
2008-11-19 08:22:15 +01:00
*/
@ChannelPipelineCoverage("one")
public abstract class HttpMessageEncoder extends OneToOneEncoder {
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() {
super();
}
2008-11-19 08:22:15 +01:00
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
if (msg instanceof HttpMessage) {
HttpMessage m = (HttpMessage) msg;
boolean chunked = this.chunked = HttpCodecUtil.isTransferEncodingChunked(m);
ChannelBuffer header = ChannelBuffers.dynamicBuffer(
channel.getConfig().getBufferFactory());
encodeInitialLine(header, m);
encodeHeaders(header, m);
header.writeBytes(CRLF);
ChannelBuffer content = m.getContent();
if (!content.readable()) {
return header; // no content
} else if (chunked) {
throw new IllegalArgumentException(
"HttpMessage.content must be empty " +
"if Transfer-Encoding is chunked.");
} else {
return wrappedBuffer(header, content);
}
}
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) {
ChannelBuffer trailer = ChannelBuffers.dynamicBuffer(
channel.getConfig().getBufferFactory());
trailer.writeByte((byte) '0');
trailer.writeBytes(CRLF);
encodeTrailingHeaders(trailer, (HttpChunkTrailer) chunk);
trailer.writeBytes(CRLF);
return trailer;
} else {
return LAST_CHUNK.duplicate();
}
} else {
ChannelBuffer content = chunk.getContent();
int contentLength = content.readableBytes();
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()) {
return null;
} else {
return chunk.getContent();
}
}
2008-11-19 08:22:15 +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) {
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 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 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"));
buf.writeBytes(CRLF);
}
2008-11-19 08:22:15 +01:00
protected abstract void encodeInitialLine(ChannelBuffer buf, HttpMessage message) throws Exception;
}