1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.buffer.ChannelBuffers;
20 import org.jboss.netty.util.internal.StringUtil;
21
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26
27
28
29 public class DefaultHttpChunkTrailer implements HttpChunkTrailer {
30
31 private final HttpHeaders trailingHeaders = new TrailingHeaders(true);
32
33 public boolean isLast() {
34 return true;
35 }
36
37 public ChannelBuffer getContent() {
38 return ChannelBuffers.EMPTY_BUFFER;
39 }
40
41 public void setContent(ChannelBuffer content) {
42 throw new IllegalStateException("read-only");
43 }
44
45 public HttpHeaders trailingHeaders() {
46 return trailingHeaders;
47 }
48
49 @Override
50 public String toString() {
51 StringBuilder buf = new StringBuilder(super.toString());
52 buf.append(StringUtil.NEWLINE);
53 appendHeaders(buf);
54
55
56 buf.setLength(buf.length() - StringUtil.NEWLINE.length());
57 return buf.toString();
58 }
59
60 private void appendHeaders(StringBuilder buf) {
61 for (Map.Entry<String, String> e: trailingHeaders()) {
62 buf.append(e.getKey());
63 buf.append(": ");
64 buf.append(e.getValue());
65 buf.append(StringUtil.NEWLINE);
66 }
67 }
68
69 private static final class TrailingHeaders extends DefaultHttpHeaders {
70
71 TrailingHeaders(boolean validateHeaders) {
72 super(validateHeaders);
73 }
74
75 @Override
76 public HttpHeaders add(String name, Object value) {
77 if (validate) {
78 validateName(name);
79 }
80 return super.add(name, value);
81 }
82
83 @Override
84 public HttpHeaders add(String name, Iterable<?> values) {
85 if (validate) {
86 validateName(name);
87 }
88 return super.add(name, values);
89 }
90
91 @Override
92 public HttpHeaders set(String name, Iterable<?> values) {
93 if (validate) {
94 validateName(name);
95 }
96 return super.set(name, values);
97 }
98
99 @Override
100 public HttpHeaders set(String name, Object value) {
101 if (validate) {
102 validateName(name);
103 }
104 return super.set(name, value);
105 }
106
107 private static void validateName(String name) {
108 if (name.equalsIgnoreCase(HttpHeaders.Names.CONTENT_LENGTH) ||
109 name.equalsIgnoreCase(HttpHeaders.Names.TRANSFER_ENCODING) ||
110 name.equalsIgnoreCase(HttpHeaders.Names.TRAILER)) {
111 throw new IllegalArgumentException(
112 "prohibited trailing header: " + name);
113 }
114 }
115 }
116 }