2009-11-17 06:27:20 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-11-17 06:27:20 +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-17 06:27:20 +01:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-11-17 06:27:20 +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-17 06:27:20 +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-17 06:27:20 +01:00
|
|
|
|
2012-06-10 04:08:43 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2012-06-11 10:02:00 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
2012-09-28 08:16:29 +02:00
|
|
|
import io.netty.util.internal.StringUtil;
|
2012-05-31 21:03:01 +02:00
|
|
|
|
2014-09-19 01:04:35 +02:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2009-11-17 06:27:20 +01:00
|
|
|
/**
|
2013-01-14 16:52:30 +01:00
|
|
|
* The default {@link LastHttpContent} implementation.
|
2009-11-17 06:27:20 +01:00
|
|
|
*/
|
2013-01-14 16:52:30 +01:00
|
|
|
public class DefaultLastHttpContent extends DefaultHttpContent implements LastHttpContent {
|
2009-11-17 06:27:20 +01:00
|
|
|
|
2013-11-13 15:05:12 +01:00
|
|
|
private final HttpHeaders trailingHeaders;
|
2013-11-26 08:09:14 +01:00
|
|
|
private final boolean validateHeaders;
|
2009-11-17 06:27:20 +01:00
|
|
|
|
2013-01-14 16:52:30 +01:00
|
|
|
public DefaultLastHttpContent() {
|
2013-01-16 05:22:50 +01:00
|
|
|
this(Unpooled.buffer(0));
|
2013-01-14 16:52:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public DefaultLastHttpContent(ByteBuf content) {
|
2013-11-13 15:05:12 +01:00
|
|
|
this(content, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public DefaultLastHttpContent(ByteBuf content, boolean validateHeaders) {
|
2013-01-14 16:52:30 +01:00
|
|
|
super(content);
|
2014-09-19 01:04:35 +02:00
|
|
|
trailingHeaders = new TrailingHttpHeaders(validateHeaders);
|
2013-11-26 08:09:14 +01:00
|
|
|
this.validateHeaders = validateHeaders;
|
2009-11-17 06:27:20 +01:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2013-01-16 05:22:50 +01:00
|
|
|
public LastHttpContent copy() {
|
2013-11-26 08:09:14 +01:00
|
|
|
DefaultLastHttpContent copy = new DefaultLastHttpContent(content().copy(), validateHeaders);
|
2013-01-16 05:22:50 +01:00
|
|
|
copy.trailingHeaders().set(trailingHeaders());
|
|
|
|
return copy;
|
2009-11-17 06:27:20 +01:00
|
|
|
}
|
|
|
|
|
2013-07-06 21:17:51 +02:00
|
|
|
@Override
|
|
|
|
public LastHttpContent duplicate() {
|
2013-11-26 08:09:14 +01:00
|
|
|
DefaultLastHttpContent copy = new DefaultLastHttpContent(content().duplicate(), validateHeaders);
|
2013-07-06 21:17:51 +02:00
|
|
|
copy.trailingHeaders().set(trailingHeaders());
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2013-02-13 08:09:33 +01:00
|
|
|
@Override
|
|
|
|
public LastHttpContent retain(int increment) {
|
|
|
|
super.retain(increment);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public LastHttpContent retain() {
|
|
|
|
super.retain();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-01-28 12:00:28 +01:00
|
|
|
@Override
|
|
|
|
public LastHttpContent touch() {
|
|
|
|
super.touch();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-01-29 03:44:59 +01:00
|
|
|
@Override
|
|
|
|
public LastHttpContent touch(Object hint) {
|
|
|
|
super.touch(hint);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2013-01-16 05:22:50 +01:00
|
|
|
public HttpHeaders trailingHeaders() {
|
|
|
|
return trailingHeaders;
|
2009-11-17 06:27:20 +01:00
|
|
|
}
|
|
|
|
|
2012-09-28 08:16:29 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2013-01-16 05:22:50 +01:00
|
|
|
StringBuilder buf = new StringBuilder(super.toString());
|
2012-09-28 08:16:29 +02:00
|
|
|
buf.append(StringUtil.NEWLINE);
|
|
|
|
appendHeaders(buf);
|
|
|
|
|
|
|
|
// Remove the last newline.
|
|
|
|
buf.setLength(buf.length() - StringUtil.NEWLINE.length());
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void appendHeaders(StringBuilder buf) {
|
2014-09-19 01:04:35 +02:00
|
|
|
for (Entry<String, String> e : trailingHeaders()) {
|
2012-09-28 08:16:29 +02:00
|
|
|
buf.append(e.getKey());
|
|
|
|
buf.append(": ");
|
|
|
|
buf.append(e.getValue());
|
|
|
|
buf.append(StringUtil.NEWLINE);
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 15:05:12 +01:00
|
|
|
|
2014-09-19 01:04:35 +02:00
|
|
|
private static final class TrailingHttpHeaders extends DefaultHttpHeaders {
|
|
|
|
private static final class TrailingHttpHeadersNameConverter extends HttpHeadersNameConverter {
|
|
|
|
TrailingHttpHeadersNameConverter(boolean validate) {
|
|
|
|
super(validate);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CharSequence convertName(CharSequence name) {
|
|
|
|
name = super.convertName(name);
|
|
|
|
if (validate) {
|
2014-10-31 08:48:28 +01:00
|
|
|
if (HttpHeaderNames.CONTENT_LENGTH.equalsIgnoreCase(name)
|
|
|
|
|| HttpHeaderNames.TRANSFER_ENCODING.equalsIgnoreCase(name)
|
|
|
|
|| HttpHeaderNames.TRAILER.equalsIgnoreCase(name)) {
|
2014-09-19 01:04:35 +02:00
|
|
|
throw new IllegalArgumentException("prohibited trailing header: " + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name;
|
2013-11-13 15:05:12 +01:00
|
|
|
}
|
2014-09-19 01:04:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static final TrailingHttpHeadersNameConverter
|
|
|
|
VALIDATE_NAME_CONVERTER = new TrailingHttpHeadersNameConverter(true);
|
|
|
|
private static final TrailingHttpHeadersNameConverter
|
|
|
|
NO_VALIDATE_NAME_CONVERTER = new TrailingHttpHeadersNameConverter(false);
|
|
|
|
|
|
|
|
TrailingHttpHeaders(boolean validate) {
|
2014-12-12 13:46:54 +01:00
|
|
|
super(validate, validate ? VALIDATE_NAME_CONVERTER : NO_VALIDATE_NAME_CONVERTER, false);
|
2013-11-13 15:05:12 +01:00
|
|
|
}
|
|
|
|
}
|
2009-11-17 06:27:20 +01:00
|
|
|
}
|