View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.util.internal.StringUtil;
21  
22  import java.util.Map;
23  
24  /**
25   * The default {@link LastHttpContent} implementation.
26   */
27  public class DefaultLastHttpContent extends DefaultHttpContent implements LastHttpContent {
28  
29      private final HttpHeaders trailingHeaders;
30      private final boolean validateHeaders;
31  
32      public DefaultLastHttpContent() {
33          this(Unpooled.buffer(0));
34      }
35  
36      public DefaultLastHttpContent(ByteBuf content) {
37          this(content, true);
38      }
39  
40      public DefaultLastHttpContent(ByteBuf content, boolean validateHeaders) {
41          super(content);
42          trailingHeaders = new TrailingHttpHeaders(validateHeaders);
43          this.validateHeaders = validateHeaders;
44      }
45  
46      @Override
47      public LastHttpContent copy() {
48          DefaultLastHttpContent copy = new DefaultLastHttpContent(content().copy(), validateHeaders);
49          copy.trailingHeaders().set(trailingHeaders());
50          return copy;
51      }
52  
53      @Override
54      public LastHttpContent duplicate() {
55          DefaultLastHttpContent copy = new DefaultLastHttpContent(content().duplicate(), validateHeaders);
56          copy.trailingHeaders().set(trailingHeaders());
57          return copy;
58      }
59  
60      @Override
61      public LastHttpContent retain(int increment) {
62          super.retain(increment);
63          return this;
64      }
65  
66      @Override
67      public LastHttpContent retain() {
68          super.retain();
69          return this;
70      }
71  
72      @Override
73      public LastHttpContent touch() {
74          super.touch();
75          return this;
76      }
77  
78      @Override
79      public LastHttpContent touch(Object hint) {
80          super.touch(hint);
81          return this;
82      }
83  
84      @Override
85      public HttpHeaders trailingHeaders() {
86          return trailingHeaders;
87      }
88  
89      @Override
90      public String toString() {
91          StringBuilder buf = new StringBuilder(super.toString());
92          buf.append(StringUtil.NEWLINE);
93          appendHeaders(buf);
94  
95          // Remove the last newline.
96          buf.setLength(buf.length() - StringUtil.NEWLINE.length());
97          return buf.toString();
98      }
99  
100     private void appendHeaders(StringBuilder buf) {
101         for (Map.Entry<CharSequence, CharSequence> e : trailingHeaders()) {
102             buf.append(e.getKey());
103             buf.append(": ");
104             buf.append(e.getValue());
105             buf.append(StringUtil.NEWLINE);
106         }
107     }
108 
109     private static final class TrailingHttpHeaders extends DefaultHttpHeaders {
110         private static final class TrailingHttpHeadersNameConverter extends HttpHeadersNameConverter {
111             TrailingHttpHeadersNameConverter(boolean validate) {
112                 super(validate);
113             }
114 
115             @Override
116             public CharSequence convertName(CharSequence name) {
117                 name = super.convertName(name);
118                 if (validate) {
119                     if (HttpHeaderNames.CONTENT_LENGTH.equalsIgnoreCase(name)
120                                     || HttpHeaderNames.TRANSFER_ENCODING.equalsIgnoreCase(name)
121                                     || HttpHeaderNames.TRAILER.equalsIgnoreCase(name)) {
122                         throw new IllegalArgumentException("prohibited trailing header: " + name);
123                     }
124                 }
125                 return name;
126             }
127         }
128 
129         private static final TrailingHttpHeadersNameConverter
130             VALIDATE_NAME_CONVERTER = new TrailingHttpHeadersNameConverter(true);
131         private static final TrailingHttpHeadersNameConverter
132             NO_VALIDATE_NAME_CONVERTER = new TrailingHttpHeadersNameConverter(false);
133 
134         TrailingHttpHeaders(boolean validate) {
135             super(validate, validate ? VALIDATE_NAME_CONVERTER : NO_VALIDATE_NAME_CONVERTER, false);
136         }
137     }
138 }