View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * 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 distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package io.netty.handler.codec.http2;
17  
18  import io.netty.handler.codec.Headers;
19  import io.netty.util.AsciiString;
20  import io.netty.util.internal.UnstableApi;
21  
22  import java.util.Iterator;
23  import java.util.Map.Entry;
24  
25  /**
26   * A collection of headers sent or received via HTTP/2.
27   */
28  @UnstableApi
29  public interface Http2Headers extends Headers<CharSequence, CharSequence, Http2Headers> {
30  
31      /**
32       * HTTP/2 pseudo-headers names.
33       */
34      enum PseudoHeaderName {
35          /**
36           * {@code :method}.
37           */
38          METHOD(":method"),
39  
40          /**
41           * {@code :scheme}.
42           */
43          SCHEME(":scheme"),
44  
45          /**
46           * {@code :authority}.
47           */
48          AUTHORITY(":authority"),
49  
50          /**
51           * {@code :path}.
52           */
53          PATH(":path"),
54  
55          /**
56           * {@code :status}.
57           */
58          STATUS(":status");
59  
60          private final AsciiString value;
61          private static final CharSequenceMap<AsciiString> PSEUDO_HEADERS = new CharSequenceMap<AsciiString>();
62          static {
63              for (PseudoHeaderName pseudoHeader : PseudoHeaderName.values()) {
64                  PSEUDO_HEADERS.add(pseudoHeader.value(), AsciiString.EMPTY_STRING);
65              }
66          }
67  
68          PseudoHeaderName(String value) {
69              this.value = AsciiString.cached(value);
70          }
71  
72          public AsciiString value() {
73              // Return a slice so that the buffer gets its own reader index.
74              return value;
75          }
76  
77          /**
78           * Indicates whether the given header name is a valid HTTP/2 pseudo header.
79           */
80          public static boolean isPseudoHeader(CharSequence header) {
81              return PSEUDO_HEADERS.contains(header);
82          }
83      }
84  
85      /**
86       * Returns an iterator over all HTTP/2 headers. The iteration order is as follows:
87       *   1. All pseudo headers (order not specified).
88       *   2. All non-pseudo headers (in insertion order).
89       */
90      @Override
91      Iterator<Entry<CharSequence, CharSequence>> iterator();
92  
93      /**
94       * Equivalent to {@link #getAll(Object)} but no intermediate list is generated.
95       * @param name the name of the header to retrieve
96       * @return an {@link Iterator} of header values corresponding to {@code name}.
97       */
98      Iterator<CharSequence> valueIterator(CharSequence name);
99  
100     /**
101      * Sets the {@link PseudoHeaderName#METHOD} header or {@code null} if there is no such header
102      */
103     Http2Headers method(CharSequence value);
104 
105     /**
106      * Sets the {@link PseudoHeaderName#SCHEME} header if there is no such header
107      */
108     Http2Headers scheme(CharSequence value);
109 
110     /**
111      * Sets the {@link PseudoHeaderName#AUTHORITY} header or {@code null} if there is no such header
112      */
113     Http2Headers authority(CharSequence value);
114 
115     /**
116      * Sets the {@link PseudoHeaderName#PATH} header or {@code null} if there is no such header
117      */
118     Http2Headers path(CharSequence value);
119 
120     /**
121      * Sets the {@link PseudoHeaderName#STATUS} header or {@code null} if there is no such header
122      */
123     Http2Headers status(CharSequence value);
124 
125     /**
126      * Gets the {@link PseudoHeaderName#METHOD} header or {@code null} if there is no such header
127      */
128     CharSequence method();
129 
130     /**
131      * Gets the {@link PseudoHeaderName#SCHEME} header or {@code null} if there is no such header
132      */
133     CharSequence scheme();
134 
135     /**
136      * Gets the {@link PseudoHeaderName#AUTHORITY} header or {@code null} if there is no such header
137      */
138     CharSequence authority();
139 
140     /**
141      * Gets the {@link PseudoHeaderName#PATH} header or {@code null} if there is no such header
142      */
143     CharSequence path();
144 
145     /**
146      * Gets the {@link PseudoHeaderName#STATUS} header or {@code null} if there is no such header
147      */
148     CharSequence status();
149 }