2009-11-02 10:21:13 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-11-02 10:21:13 +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-02 10:21:13 +01:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-11-02 10:21:13 +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-02 10:21:13 +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-02 10:21:13 +01:00
|
|
|
|
2013-02-28 07:06:39 +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;
|
2013-04-03 11:32:33 +02:00
|
|
|
import io.netty.buffer.MessageBuf;
|
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.MessageToMessageDecoder;
|
2009-11-02 10:21:13 +01:00
|
|
|
|
|
|
|
/**
|
2013-01-16 05:22:50 +01:00
|
|
|
* Decodes the content of the received {@link HttpRequest} and {@link HttpContent}.
|
2009-11-03 08:11:52 +01:00
|
|
|
* The original content is replaced with the new content decoded by the
|
2012-12-23 23:03:54 +01:00
|
|
|
* {@link EmbeddedByteChannel}, which is created by {@link #newContentDecoder(String)}.
|
2009-11-03 08:11:52 +01:00
|
|
|
* Once decoding is finished, the value of the <tt>'Content-Encoding'</tt>
|
|
|
|
* header is set to the target content encoding, as returned by {@link #getTargetContentEncoding(String)}.
|
|
|
|
* Also, the <tt>'Content-Length'</tt> header is updated to the length of the
|
2009-11-02 10:21:13 +01:00
|
|
|
* decoded content. If the content encoding of the original is not supported
|
2009-11-03 08:11:52 +01:00
|
|
|
* by the decoder, {@link #newContentDecoder(String)} should return {@code null}
|
|
|
|
* so that no decoding occurs (i.e. pass-through).
|
2009-11-02 10:21:13 +01:00
|
|
|
* <p>
|
|
|
|
* Please note that this is an abstract class. You have to extend this class
|
2009-11-03 07:47:30 +01:00
|
|
|
* and implement {@link #newContentDecoder(String)} properly to make this class
|
2009-11-02 10:21:13 +01:00
|
|
|
* functional. For example, refer to the source code of {@link HttpContentDecompressor}.
|
2009-11-03 08:11:52 +01:00
|
|
|
* <p>
|
2013-01-14 16:52:30 +01:00
|
|
|
* This handler must be placed after {@link HttpObjectDecoder} in the pipeline
|
|
|
|
* so that this handler can intercept HTTP requests after {@link HttpObjectDecoder}
|
2012-06-10 04:08:43 +02:00
|
|
|
* converts {@link ByteBuf}s into HTTP requests.
|
2009-11-02 10:21:13 +01:00
|
|
|
*/
|
2013-02-08 07:44:41 +01:00
|
|
|
public abstract class HttpContentDecoder extends MessageToMessageDecoder<HttpObject> {
|
2009-11-02 10:21:13 +01:00
|
|
|
|
2012-06-09 14:05:59 +02:00
|
|
|
private EmbeddedByteChannel decoder;
|
2013-01-16 16:33:40 +01:00
|
|
|
private HttpMessage message;
|
2013-01-14 16:52:30 +01:00
|
|
|
private boolean decodeStarted;
|
2013-02-28 07:06:39 +01:00
|
|
|
private boolean continueResponse;
|
2009-11-02 10:21:13 +01:00
|
|
|
|
|
|
|
@Override
|
2013-04-03 11:32:33 +02:00
|
|
|
protected void decode(ChannelHandlerContext ctx, HttpObject msg, MessageBuf<Object> out) throws Exception {
|
2013-01-30 07:42:18 +01:00
|
|
|
if (msg instanceof HttpResponse && ((HttpResponse) msg).getStatus().code() == 100) {
|
2013-04-03 11:32:33 +02:00
|
|
|
|
2013-02-28 07:06:39 +01:00
|
|
|
if (!(msg instanceof LastHttpContent)) {
|
|
|
|
continueResponse = true;
|
2013-02-21 19:31:05 +01:00
|
|
|
}
|
2013-04-03 11:32:33 +02:00
|
|
|
// 100-continue response must be passed through.
|
|
|
|
out.add(BufUtil.retain(msg));
|
|
|
|
return;
|
2012-11-12 01:31:40 +01:00
|
|
|
}
|
2013-02-28 07:06:39 +01:00
|
|
|
|
|
|
|
if (continueResponse) {
|
|
|
|
if (msg instanceof LastHttpContent) {
|
|
|
|
continueResponse = false;
|
|
|
|
}
|
|
|
|
// 100-continue response must be passed through.
|
2013-04-03 11:32:33 +02:00
|
|
|
out.add(BufUtil.retain(msg));
|
|
|
|
return;
|
2013-02-28 07:06:39 +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;
|
|
|
|
message = (HttpMessage) msg;
|
2013-02-09 17:35:01 +01:00
|
|
|
decodeStarted = false;
|
2012-08-20 06:38:14 +02:00
|
|
|
cleanup();
|
2013-01-14 16:52:30 +01:00
|
|
|
}
|
2009-11-02 10:21:13 +01:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
if (msg instanceof HttpContent) {
|
2013-02-10 05:31:31 +01:00
|
|
|
final HttpContent c = (HttpContent) msg;
|
2013-01-14 16:52:30 +01:00
|
|
|
|
|
|
|
if (!decodeStarted) {
|
|
|
|
decodeStarted = true;
|
2013-01-16 16:33:40 +01:00
|
|
|
HttpMessage message = this.message;
|
|
|
|
HttpHeaders headers = message.headers();
|
|
|
|
this.message = null;
|
2009-11-02 10:21:13 +01:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
// Determine the content encoding.
|
2013-01-16 16:33:40 +01:00
|
|
|
String contentEncoding = headers.get(HttpHeaders.Names.CONTENT_ENCODING);
|
2013-01-14 16:52:30 +01:00
|
|
|
if (contentEncoding != null) {
|
|
|
|
contentEncoding = contentEncoding.trim();
|
2013-01-01 07:13:35 +01:00
|
|
|
} else {
|
2013-01-14 16:52:30 +01:00
|
|
|
contentEncoding = HttpHeaders.Values.IDENTITY;
|
2013-01-01 07:13:35 +01:00
|
|
|
}
|
2009-11-02 10:21:13 +01:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
if ((decoder = newContentDecoder(contentEncoding)) != null) {
|
|
|
|
// Decode the content and remove or replace the existing headers
|
|
|
|
// so that the message looks like a decoded message.
|
|
|
|
String targetContentEncoding = getTargetContentEncoding(contentEncoding);
|
|
|
|
if (HttpHeaders.Values.IDENTITY.equals(targetContentEncoding)) {
|
|
|
|
// Do NOT set the 'Content-Encoding' header if the target encoding is 'identity'
|
|
|
|
// as per: http://tools.ietf.org/html/rfc2616#section-14.11
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.remove(HttpHeaders.Names.CONTENT_ENCODING);
|
2012-06-07 14:06:56 +02:00
|
|
|
} else {
|
2013-01-16 16:33:40 +01:00
|
|
|
headers.set(HttpHeaders.Names.CONTENT_ENCODING, targetContentEncoding);
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
2013-01-16 16:33:40 +01:00
|
|
|
Object[] decoded = decodeContent(message, c);
|
2009-11-02 10:21:13 +01:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
// Replace the content.
|
2013-01-16 16:33:40 +01:00
|
|
|
if (headers.contains(HttpHeaders.Names.CONTENT_LENGTH)) {
|
|
|
|
headers.set(
|
2013-01-14 16:52:30 +01:00
|
|
|
HttpHeaders.Names.CONTENT_LENGTH,
|
2013-01-16 05:22:50 +01:00
|
|
|
Integer.toString(((ByteBufHolder) decoded[1]).data().readableBytes()));
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
2013-04-03 11:32:33 +02:00
|
|
|
|
|
|
|
for (Object obj: decoded) {
|
|
|
|
out.add(obj);
|
|
|
|
}
|
|
|
|
return;
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
2013-02-10 05:31:31 +01:00
|
|
|
|
2013-03-25 08:12:48 +01:00
|
|
|
if (c instanceof LastHttpContent) {
|
|
|
|
decodeStarted = false;
|
|
|
|
}
|
2013-04-03 11:32:33 +02:00
|
|
|
out.add(message);
|
|
|
|
out.add(c.retain());
|
|
|
|
return;
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
2013-02-09 17:35:01 +01:00
|
|
|
|
|
|
|
if (decoder != null) {
|
2013-04-03 11:32:33 +02:00
|
|
|
Object[] decoded = decodeContent(null, c);
|
|
|
|
|
|
|
|
for (Object obj: decoded) {
|
|
|
|
out.add(obj);
|
|
|
|
}
|
2013-02-09 17:35:01 +01:00
|
|
|
} else {
|
2013-03-25 08:12:48 +01:00
|
|
|
if (c instanceof LastHttpContent) {
|
|
|
|
decodeStarted = false;
|
|
|
|
}
|
2013-04-03 11:32:33 +02:00
|
|
|
out.add(c.retain());
|
2013-02-09 17:35:01 +01:00
|
|
|
}
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 05:22:50 +01:00
|
|
|
private Object[] decodeContent(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
|
|
|
decode(content, newContent);
|
|
|
|
|
|
|
|
if (c instanceof LastHttpContent) {
|
|
|
|
ByteBuf lastProduct = Unpooled.buffer();
|
|
|
|
finishDecode(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)};
|
|
|
|
}
|
2013-02-26 07:47:30 +01:00
|
|
|
} else {
|
|
|
|
if (header == null) {
|
|
|
|
return new Object[] { new DefaultLastHttpContent(newContent) };
|
|
|
|
} else {
|
|
|
|
return new Object[] { header, new DefaultLastHttpContent(newContent) };
|
|
|
|
}
|
2013-01-14 16:52:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (header == null) {
|
|
|
|
return new Object[] { new DefaultHttpContent(newContent) };
|
|
|
|
} else {
|
|
|
|
return new Object[] { header, new DefaultHttpContent(newContent) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-02 10:21:13 +01:00
|
|
|
/**
|
2012-12-23 23:03:54 +01:00
|
|
|
* Returns a new {@link EmbeddedByteChannel} that decodes the HTTP message
|
2009-11-02 10:21:13 +01:00
|
|
|
* content encoded in the specified <tt>contentEncoding</tt>.
|
|
|
|
*
|
|
|
|
* @param contentEncoding the value of the {@code "Content-Encoding"} header
|
2012-12-23 23:03:54 +01:00
|
|
|
* @return a new {@link EmbeddedByteChannel} if the specified encoding is supported.
|
2009-11-02 10:21:13 +01:00
|
|
|
* {@code null} otherwise (alternatively, you can throw an exception
|
|
|
|
* to block unknown encoding).
|
|
|
|
*/
|
2012-06-09 14:05:59 +02:00
|
|
|
protected abstract EmbeddedByteChannel newContentDecoder(String contentEncoding) throws Exception;
|
2009-11-02 10:21:13 +01:00
|
|
|
|
2009-11-02 10:58:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the expected content encoding of the decoded content.
|
2013-01-30 07:42:18 +01:00
|
|
|
* This getMethod returns {@code "identity"} by default, which is the case for
|
2009-11-02 10:58:30 +01:00
|
|
|
* most decoders.
|
|
|
|
*
|
2009-11-03 08:11:52 +01:00
|
|
|
* @param contentEncoding the value of the {@code "Content-Encoding"} header
|
2009-11-02 10:58:30 +01:00
|
|
|
* @return the expected content encoding of the new content
|
|
|
|
*/
|
2012-12-23 23:03:54 +01:00
|
|
|
@SuppressWarnings("unused")
|
2009-11-03 07:47:30 +01:00
|
|
|
protected String getTargetContentEncoding(String contentEncoding) throws Exception {
|
2009-11-02 10:58:30 +01:00
|
|
|
return HttpHeaders.Values.IDENTITY;
|
|
|
|
}
|
|
|
|
|
2012-08-20 06:38:14 +02:00
|
|
|
@Override
|
2013-04-05 15:46:18 +02:00
|
|
|
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
2012-08-20 06:38:14 +02:00
|
|
|
cleanup();
|
2013-04-05 15:46:18 +02:00
|
|
|
super.handlerRemoved(ctx);
|
2012-08-20 06:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
cleanup();
|
|
|
|
super.channelInactive(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cleanup() {
|
|
|
|
if (decoder != null) {
|
|
|
|
// Clean-up the previous decoder if not cleaned up correctly.
|
|
|
|
finishDecode(Unpooled.buffer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private void decode(ByteBuf in, ByteBuf out) {
|
2013-04-22 11:04:56 +02:00
|
|
|
// call retain as it will be release after is written
|
|
|
|
decoder.writeInbound(in.retain());
|
2012-06-07 14:06:56 +02:00
|
|
|
fetchDecoderOutput(out);
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private void finishDecode(ByteBuf out) {
|
2009-11-02 10:21:13 +01:00
|
|
|
if (decoder.finish()) {
|
2012-06-07 14:06:56 +02:00
|
|
|
fetchDecoderOutput(out);
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
2013-03-25 08:12:48 +01:00
|
|
|
decodeStarted = false;
|
2009-11-03 07:47:30 +01:00
|
|
|
decoder = null;
|
2012-06-07 14:06:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
private void fetchDecoderOutput(ByteBuf out) {
|
2012-06-07 14:06:56 +02:00
|
|
|
for (;;) {
|
2012-06-10 04:08:43 +02:00
|
|
|
ByteBuf buf = (ByteBuf) decoder.readInbound();
|
2012-06-07 14:06:56 +02:00
|
|
|
if (buf == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
out.writeBytes(buf);
|
|
|
|
}
|
2009-11-02 10:21:13 +01:00
|
|
|
}
|
|
|
|
}
|