2009-11-03 07:47:30 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-11-03 07:47:30 +01: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:
|
2009-11-03 07:47:30 +01:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-11-03 07:47:30 +01: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-11-03 07:47:30 +01:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.http;
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2013-02-21 22:58:13 +01:00
|
|
|
import io.netty.buffer.BufUtil;
|
2012-06-10 04:08:43 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2013-01-16 05:22:50 +01:00
|
|
|
import io.netty.buffer.ByteBufHolder;
|
2012-06-11 10:02:00 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
2012-06-07 07:52:33 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2012-06-09 14:05:59 +02:00
|
|
|
import io.netty.channel.embedded.EmbeddedByteChannel;
|
2012-05-23 20:42:10 +02:00
|
|
|
import io.netty.handler.codec.MessageToMessageCodec;
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2012-06-12 10:02:00 +02:00
|
|
|
import java.util.ArrayDeque;
|
2012-05-23 20:42:10 +02:00
|
|
|
import java.util.Queue;
|
|
|
|
|
2009-11-03 07:47:30 +01:00
|
|
|
/**
|
2013-01-16 05:22:50 +01:00
|
|
|
* Encodes the content of the outbound {@link HttpResponse} and {@link HttpContent}.
|
2009-11-03 08:11:52 +01:00
|
|
|
* The original content is replaced with the new content encoded by the
|
2013-01-16 05:22:50 +01:00
|
|
|
* {@link EmbeddedByteChannel}, which is created by {@link #beginEncode(HttpMessage, HttpContent, String)}.
|
2009-11-03 08:11:52 +01:00
|
|
|
* Once encoding is finished, the value of the <tt>'Content-Encoding'</tt> header
|
2011-10-23 07:34:03 +02:00
|
|
|
* is set to the target content encoding, as returned by
|
2013-01-16 05:22:50 +01:00
|
|
|
* {@link #beginEncode(HttpMessage, HttpContent, String)}.
|
2009-11-03 08:11:52 +01:00
|
|
|
* Also, the <tt>'Content-Length'</tt> header is updated to the length of the
|
2011-10-23 07:34:03 +02:00
|
|
|
* encoded content. If there is no supported or allowed encoding in the
|
2013-01-16 05:22:50 +01:00
|
|
|
* corresponding {@link HttpRequest}'s {@code "Accept-Encoding"} header,
|
|
|
|
* {@link #beginEncode(HttpMessage, HttpContent, String)} should return {@code null} so that
|
2011-10-23 07:34:03 +02:00
|
|
|
* no encoding occurs (i.e. pass-through).
|
2009-11-03 08:11:52 +01:00
|
|
|
* <p>
|
|
|
|
* Please note that this is an abstract class. You have to extend this class
|
2013-01-16 05:22:50 +01:00
|
|
|
* and implement {@link #beginEncode(HttpMessage, HttpContent, String)} properly to make
|
2011-10-23 07:34:03 +02:00
|
|
|
* this class functional. For example, refer to the source code of
|
|
|
|
* {@link HttpContentCompressor}.
|
2009-11-03 08:11:52 +01:00
|
|
|
* <p>
|
2013-01-14 16:52:30 +01:00
|
|
|
* This handler must be placed after {@link HttpObjectEncoder} in the pipeline
|
|
|
|
* so that this handler can intercept HTTP responses before {@link HttpObjectEncoder}
|
2012-06-10 04:08:43 +02:00
|
|
|
* converts them into {@link ByteBuf}s.
|
2009-11-03 07:47:30 +01:00
|
|
|
*/
|
2013-02-08 08:12:32 +01:00
|
|
|
public abstract class HttpContentEncoder extends MessageToMessageCodec<HttpMessage, HttpObject> {
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2012-06-12 10:02:00 +02:00
|
|
|
private final Queue<String> acceptEncodingQueue = new ArrayDeque<String>();
|
2012-06-09 14:05:59 +02:00
|
|
|
private EmbeddedByteChannel encoder;
|
2013-01-16 16:33:40 +01:00
|
|
|
private HttpMessage message;
|
2013-01-14 16:52:30 +01:00
|
|
|
private boolean encodeStarted;
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2012-05-29 22:34:01 +02:00
|
|
|
@Override
|
2013-01-16 05:22:50 +01:00
|
|
|
protected Object decode(ChannelHandlerContext ctx, HttpMessage msg)
|
2012-05-29 22:34:01 +02:00
|
|
|
throws Exception {
|
2013-01-16 05:22:50 +01:00
|
|
|
String acceptedEncoding = msg.headers().get(HttpHeaders.Names.ACCEPT_ENCODING);
|
2009-11-03 07:47:30 +01:00
|
|
|
if (acceptedEncoding == null) {
|
|
|
|
acceptedEncoding = HttpHeaders.Values.IDENTITY;
|
|
|
|
}
|
2013-02-21 22:58:13 +01:00
|
|
|
acceptEncodingQueue.add(acceptedEncoding);
|
|
|
|
BufUtil.retain(msg);
|
2012-05-29 22:34:01 +02:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2009-11-03 07:47:30 +01:00
|
|
|
@Override
|
2013-02-08 09:37:16 +01:00
|
|
|
protected Object encode(ChannelHandlerContext ctx, HttpObject msg)
|
2009-11-03 07:47:30 +01:00
|
|
|
throws Exception {
|
2013-01-30 07:42:18 +01:00
|
|
|
if (msg instanceof HttpResponse && ((HttpResponse) msg).getStatus().code() == 100) {
|
2010-10-18 10:19:03 +02:00
|
|
|
// 100-continue response must be passed through.
|
2013-02-21 22:58:13 +01:00
|
|
|
BufUtil.retain(msg);
|
2012-05-23 20:42:10 +02:00
|
|
|
return msg;
|
2012-11-12 01:31:40 +01:00
|
|
|
}
|
2013-01-30 20:58:07 +01:00
|
|
|
|
|
|
|
// handle the case of single complete message without content
|
2013-01-31 15:39:57 +01:00
|
|
|
if (msg instanceof FullHttpMessage && !((FullHttpMessage) msg).data().isReadable()) {
|
2013-01-30 20:58:07 +01:00
|
|
|
|
|
|
|
// Remove content encoding
|
|
|
|
String acceptEncoding = acceptEncodingQueue.poll();
|
|
|
|
if (acceptEncoding == null) {
|
|
|
|
throw new IllegalStateException("cannot send more responses than requests");
|
|
|
|
}
|
|
|
|
|
2013-02-21 06:48:53 +01:00
|
|
|
return ((FullHttpMessage) msg).retain();
|
2013-01-30 20:58:07 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
if (msg instanceof HttpMessage) {
|
2013-01-16 16:33:40 +01:00
|
|
|
assert message == null;
|
2013-01-14 16:52:30 +01:00
|
|
|
|
|
|
|
// check if this message is also of type HttpContent is such case just make a safe copy of the headers
|
|
|
|
// as the content will get handled later and this simplify the handling
|
|
|
|
if (msg instanceof HttpContent) {
|
2013-01-16 05:22:50 +01:00
|
|
|
if (msg instanceof HttpRequest) {
|
2013-01-16 16:33:40 +01:00
|
|
|
HttpRequest req = (HttpRequest) msg;
|
2013-01-30 07:42:18 +01:00
|
|
|
message = new DefaultHttpRequest(req.getProtocolVersion(), req.getMethod(), req.getUri());
|
2013-01-16 16:33:40 +01:00
|
|
|
message.headers().set(req.headers());
|
2013-01-16 05:22:50 +01:00
|
|
|
} else if (msg instanceof HttpResponse) {
|
2013-01-16 16:33:40 +01:00
|
|
|
HttpResponse res = (HttpResponse) msg;
|
2013-01-30 07:42:18 +01:00
|
|
|
message = new DefaultHttpResponse(res.getProtocolVersion(), res.getStatus());
|
2013-01-16 16:33:40 +01:00
|
|
|
message.headers().set(res.headers());
|
2013-01-14 16:52:30 +01:00
|
|
|
} else {
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
} else {
|
2013-01-16 16:33:40 +01:00
|
|
|
message = (HttpMessage) msg;
|
2013-01-14 16:52:30 +01:00
|
|
|
}
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2012-08-20 06:38:14 +02:00
|
|
|
cleanup();
|
2013-01-14 16:52:30 +01:00
|
|
|
}
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
if (msg instanceof HttpContent) {
|
|
|
|
HttpContent c = (HttpContent) msg;
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
if (!encodeStarted) {
|
|
|
|
encodeStarted = true;
|
2013-01-16 16:33:40 +01:00
|
|
|
HttpMessage message = this.message;
|
|
|
|
HttpHeaders headers = message.headers();
|
|
|
|
this.message = null;
|
2011-10-23 07:34:03 +02:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
// Determine the content encoding.
|
|
|
|
String acceptEncoding = acceptEncodingQueue.poll();
|
|
|
|
if (acceptEncoding == null) {
|
|
|
|
throw new IllegalStateException("cannot send more responses than requests");
|
|
|
|
}
|
2013-01-30 20:58:07 +01:00
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
Result result = beginEncode(message, c, acceptEncoding);
|
2011-10-23 07:34:03 +02:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
if (result == null) {
|
2013-01-16 05:22:50 +01:00
|
|
|
if (c instanceof LastHttpContent) {
|
2013-02-13 08:09:33 +01:00
|
|
|
return new Object[] { message, new DefaultLastHttpContent(c.data().retain()) };
|
2013-01-16 05:22:50 +01:00
|
|
|
} else {
|
2013-02-13 08:09:33 +01:00
|
|
|
return new Object[] { message, new DefaultHttpContent(c.data().retain()) };
|
2013-01-16 05:22:50 +01:00
|
|
|
}
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|
|
|
|
|
2013-01-17 06:48:03 +01:00
|
|
|
encoder = result.contentEncoder();
|
2013-01-14 16:52:30 +01:00
|
|
|
|
|
|
|
// Encode the content and remove or replace the existing headers
|
|
|
|
// so that the message looks like a decoded message.
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.set(
|
2013-01-14 16:52:30 +01:00
|
|
|
HttpHeaders.Names.CONTENT_ENCODING,
|
2013-01-17 06:48:03 +01:00
|
|
|
result.targetContentEncoding());
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
Object[] encoded = encodeContent(message, c);
|
2013-01-14 16:52:30 +01:00
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
if (!HttpHeaders.isTransferEncodingChunked(message) && encoded.length == 3) {
|
|
|
|
if (headers.contains(HttpHeaders.Names.CONTENT_LENGTH)) {
|
2013-01-16 05:22:50 +01:00
|
|
|
long length = ((ByteBufHolder) encoded[1]).data().readableBytes() +
|
|
|
|
((ByteBufHolder) encoded[2]).data().readableBytes();
|
2013-01-14 16:52:30 +01:00
|
|
|
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.set(
|
2013-01-14 16:52:30 +01:00
|
|
|
HttpHeaders.Names.CONTENT_LENGTH,
|
|
|
|
Long.toString(length));
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-14 16:52:30 +01:00
|
|
|
return encoded;
|
|
|
|
}
|
2013-02-10 05:31:31 +01:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
if (encoder != null) {
|
|
|
|
return encodeContent(null, c);
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|
2012-05-23 20:42:10 +02:00
|
|
|
|
2013-02-21 06:48:53 +01:00
|
|
|
return c.retain();
|
2013-01-21 10:41:23 +01:00
|
|
|
}
|
2013-02-10 05:31:31 +01:00
|
|
|
return null;
|
2013-01-21 10:41:23 +01:00
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
private Object[] encodeContent(HttpMessage header, HttpContent c) {
|
2013-01-14 16:52:30 +01:00
|
|
|
ByteBuf newContent = Unpooled.buffer();
|
2013-01-16 05:22:50 +01:00
|
|
|
ByteBuf content = c.data();
|
2013-01-14 16:52:30 +01:00
|
|
|
encode(content, newContent);
|
|
|
|
|
|
|
|
if (c instanceof LastHttpContent) {
|
|
|
|
ByteBuf lastProduct = Unpooled.buffer();
|
|
|
|
finishEncode(lastProduct);
|
|
|
|
|
|
|
|
// Generate an additional chunk if the decoder produced
|
|
|
|
// the last product on closure,
|
2013-01-31 15:39:57 +01:00
|
|
|
if (lastProduct.isReadable()) {
|
2013-01-14 16:52:30 +01:00
|
|
|
if (header == null) {
|
|
|
|
return new Object[] { new DefaultHttpContent(newContent), new DefaultLastHttpContent(lastProduct)};
|
|
|
|
} else {
|
|
|
|
return new Object[] { header, new DefaultHttpContent(newContent),
|
|
|
|
new DefaultLastHttpContent(lastProduct)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (header == null) {
|
|
|
|
return new Object[] { new DefaultHttpContent(newContent) };
|
|
|
|
} else {
|
|
|
|
return new Object[] { header, new DefaultHttpContent(newContent) };
|
|
|
|
}
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-23 07:34:03 +02:00
|
|
|
* Prepare to encode the HTTP message content.
|
2009-11-03 07:47:30 +01:00
|
|
|
*
|
2013-01-14 16:52:30 +01:00
|
|
|
* @param header
|
|
|
|
* the header
|
2011-10-23 07:34:03 +02:00
|
|
|
* @param msg
|
|
|
|
* the HTTP message whose content should be encoded
|
2009-11-03 07:47:30 +01:00
|
|
|
* @param acceptEncoding
|
2009-11-03 08:11:52 +01:00
|
|
|
* the value of the {@code "Accept-Encoding"} header
|
2009-11-03 07:47:30 +01:00
|
|
|
*
|
2011-10-23 07:34:03 +02:00
|
|
|
* @return the result of preparation, which is composed of the determined
|
2012-12-23 23:03:54 +01:00
|
|
|
* target content encoding and a new {@link EmbeddedByteChannel} that
|
2011-10-23 07:34:03 +02:00
|
|
|
* encodes the content into the target content encoding.
|
|
|
|
* {@code null} if {@code acceptEncoding} is unsupported or rejected
|
|
|
|
* and thus the content should be handled as-is (i.e. no encoding).
|
2009-11-03 07:47:30 +01:00
|
|
|
*/
|
2013-01-16 05:22:50 +01:00
|
|
|
protected abstract Result beginEncode(HttpMessage header, HttpContent msg, String acceptEncoding) throws Exception;
|
2009-11-03 07:47:30 +01:00
|
|
|
|
2012-08-20 06:38:14 +02:00
|
|
|
@Override
|
|
|
|
public void afterRemove(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
cleanup();
|
|
|
|
super.afterRemove(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
cleanup();
|
|
|
|
super.channelInactive(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cleanup() {
|
|
|
|
if (encoder != null) {
|
|
|
|
// Clean-up the previous encoder if not cleaned up correctly.
|
|
|
|
finishEncode(Unpooled.buffer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private void encode(ByteBuf in, ByteBuf out) {
|
2012-06-07 14:06:56 +02:00
|
|
|
encoder.writeOutbound(in);
|
|
|
|
fetchEncoderOutput(out);
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private void finishEncode(ByteBuf out) {
|
2009-11-03 07:47:30 +01:00
|
|
|
if (encoder.finish()) {
|
2012-06-07 14:06:56 +02:00
|
|
|
fetchEncoderOutput(out);
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|
2013-01-14 16:52:30 +01:00
|
|
|
encodeStarted = false;
|
2009-11-03 07:47:30 +01:00
|
|
|
encoder = null;
|
2012-06-07 14:06:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private void fetchEncoderOutput(ByteBuf out) {
|
2012-06-07 14:06:56 +02:00
|
|
|
for (;;) {
|
2012-06-10 04:08:43 +02:00
|
|
|
ByteBuf buf = encoder.readOutbound();
|
2012-06-07 14:06:56 +02:00
|
|
|
if (buf == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out.writeBytes(buf);
|
|
|
|
}
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|
2011-10-23 07:34:03 +02:00
|
|
|
|
|
|
|
public static final class Result {
|
|
|
|
private final String targetContentEncoding;
|
2012-06-09 14:05:59 +02:00
|
|
|
private final EmbeddedByteChannel contentEncoder;
|
2011-10-23 07:34:03 +02:00
|
|
|
|
2012-06-09 14:05:59 +02:00
|
|
|
public Result(String targetContentEncoding, EmbeddedByteChannel contentEncoder) {
|
2011-10-23 07:34:03 +02:00
|
|
|
if (targetContentEncoding == null) {
|
|
|
|
throw new NullPointerException("targetContentEncoding");
|
|
|
|
}
|
|
|
|
if (contentEncoder == null) {
|
|
|
|
throw new NullPointerException("contentEncoder");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.targetContentEncoding = targetContentEncoding;
|
|
|
|
this.contentEncoder = contentEncoder;
|
|
|
|
}
|
|
|
|
|
2013-01-17 06:48:03 +01:00
|
|
|
public String targetContentEncoding() {
|
2011-10-23 07:34:03 +02:00
|
|
|
return targetContentEncoding;
|
|
|
|
}
|
|
|
|
|
2013-01-17 06:48:03 +01:00
|
|
|
public EmbeddedByteChannel contentEncoder() {
|
2011-10-23 07:34:03 +02:00
|
|
|
return contentEncoder;
|
|
|
|
}
|
|
|
|
}
|
2009-11-03 07:47:30 +01:00
|
|
|
}
|