netty5/codec-http/src/main/java/io/netty/handler/codec/http/DefaultLastHttpContent.java

139 lines
4.3 KiB
Java
Raw Normal View History

/*
2012-06-04 22:31:44 +02:00
* Copyright 2012 The Netty Project
*
* 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:
*
2012-06-04 22:31:44 +02:00
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
2011-12-09 04:38:59 +01:00
package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.StringUtil;
2012-05-31 21:03:01 +02:00
import java.util.Map.Entry;
/**
* The default {@link LastHttpContent} implementation.
*/
public class DefaultLastHttpContent extends DefaultHttpContent implements LastHttpContent {
private final HttpHeaders trailingHeaders;
private final boolean validateHeaders;
public DefaultLastHttpContent() {
this(Unpooled.buffer(0));
}
public DefaultLastHttpContent(ByteBuf content) {
this(content, true);
}
public DefaultLastHttpContent(ByteBuf content, boolean validateHeaders) {
super(content);
trailingHeaders = new TrailingHttpHeaders(validateHeaders);
this.validateHeaders = validateHeaders;
}
@Override
public LastHttpContent copy() {
DefaultLastHttpContent copy = new DefaultLastHttpContent(content().copy(), validateHeaders);
copy.trailingHeaders().set(trailingHeaders());
return copy;
}
@Override
public LastHttpContent duplicate() {
DefaultLastHttpContent copy = new DefaultLastHttpContent(content().duplicate(), validateHeaders);
copy.trailingHeaders().set(trailingHeaders());
return copy;
}
@Override
public LastHttpContent retain(int increment) {
super.retain(increment);
return this;
}
@Override
public LastHttpContent retain() {
super.retain();
return this;
}
@Override
public LastHttpContent touch() {
super.touch();
return this;
}
@Override
public LastHttpContent touch(Object hint) {
super.touch(hint);
return this;
}
@Override
public HttpHeaders trailingHeaders() {
return trailingHeaders;
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(super.toString());
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) {
for (Entry<String, String> e : trailingHeaders()) {
buf.append(e.getKey());
buf.append(": ");
buf.append(e.getValue());
buf.append(StringUtil.NEWLINE);
}
}
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) {
Fix backward compatibility from the previous backport Motivation: The commit 50e06442c3f2753c9b2a506f68ea70273b829e21 changed the type of the constants in HttpHeaders.Names and HttpHeaders.Values, making 4.1 backward-incompatible with 4.0. It also introduces newer utility classes such as HttpHeaderUtil, which deprecates most static methods in HttpHeaders. To ease the migration between 4.1 and 5.0, we should deprecate all static methods that are non-existent in 5.0, and provide proper counterpart. Modification: - Revert the changes in HttpHeaders.Names and Values - Deprecate all static methods in HttpHeaders in favor of: - HttpHeaderUtil - the member methods of HttpHeaders - AsciiString - Add integer and date access methods to HttpHeaders for easier future migration to 5.0 - Add HttpHeaderNames and HttpHeaderValues which provide standard HTTP constants in AsciiString - Deprecate HttpHeaders.Names and Values - Make HttpHeaderValues.WEBSOCKET lowercased because it's actually lowercased in all WebSocket versions but the oldest one - Add RtspHeaderNames and RtspHeaderValues which provide standard RTSP constants in AsciiString - Deprecate RtspHeaders.* - Do not use AsciiString.equalsIgnoreCase(CharSeq, CharSeq) if one of the parameters are AsciiString - Avoid using AsciiString.toString() repetitively - Change the parameter type of some methods from String to CharSequence Result: Backward compatibility is recovered. New classes and methods will make the migration to 5.0 easier, once (Http|Rtsp)Header(Names|Values) are ported to master.
2014-10-31 08:48:28 +01:00
if (HttpHeaderNames.CONTENT_LENGTH.equalsIgnoreCase(name)
|| HttpHeaderNames.TRANSFER_ENCODING.equalsIgnoreCase(name)
|| HttpHeaderNames.TRAILER.equalsIgnoreCase(name)) {
throw new IllegalArgumentException("prohibited trailing header: " + name);
}
}
return name;
}
}
private static final TrailingHttpHeadersNameConverter
VALIDATE_NAME_CONVERTER = new TrailingHttpHeadersNameConverter(true);
private static final TrailingHttpHeadersNameConverter
NO_VALIDATE_NAME_CONVERTER = new TrailingHttpHeadersNameConverter(false);
TrailingHttpHeaders(boolean validate) {
super(validate, validate ? VALIDATE_NAME_CONVERTER : NO_VALIDATE_NAME_CONVERTER, false);
}
}
}