View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.eclipse.jetty.util.ArrayTrie;
24  import org.eclipse.jetty.util.StringUtil;
25  import org.eclipse.jetty.util.Trie;
26  
27  
28  public enum HttpHeader
29  {
30      /* ------------------------------------------------------------ */
31      /** General Fields.
32       */
33      CONNECTION("Connection"),
34      CACHE_CONTROL("Cache-Control"),
35      DATE("Date"),
36      PRAGMA("Pragma"),
37      PROXY_CONNECTION ("Proxy-Connection"),
38      TRAILER("Trailer"),
39      TRANSFER_ENCODING("Transfer-Encoding"),
40      UPGRADE("Upgrade"),
41      VIA("Via"),
42      WARNING("Warning"),
43      NEGOTIATE("Negotiate"),
44  
45      /* ------------------------------------------------------------ */
46      /** Entity Fields.
47       */
48      ALLOW("Allow"),
49      CONTENT_ENCODING("Content-Encoding"),
50      CONTENT_LANGUAGE("Content-Language"),
51      CONTENT_LENGTH("Content-Length"),
52      CONTENT_LOCATION("Content-Location"),
53      CONTENT_MD5("Content-MD5"),
54      CONTENT_RANGE("Content-Range"),
55      CONTENT_TYPE("Content-Type"),
56      EXPIRES("Expires"),
57      LAST_MODIFIED("Last-Modified"),
58  
59      /* ------------------------------------------------------------ */
60      /** Request Fields.
61       */
62      ACCEPT("Accept"),
63      ACCEPT_CHARSET("Accept-Charset"),
64      ACCEPT_ENCODING("Accept-Encoding"),
65      ACCEPT_LANGUAGE("Accept-Language"),
66      AUTHORIZATION("Authorization"),
67      EXPECT("Expect"),
68      FORWARDED("Forwarded"),
69      FROM("From"),
70      HOST("Host"),
71      IF_MATCH("If-Match"),
72      IF_MODIFIED_SINCE("If-Modified-Since"),
73      IF_NONE_MATCH("If-None-Match"),
74      IF_RANGE("If-Range"),
75      IF_UNMODIFIED_SINCE("If-Unmodified-Since"),
76      KEEP_ALIVE("Keep-Alive"),
77      MAX_FORWARDS("Max-Forwards"),
78      PROXY_AUTHORIZATION("Proxy-Authorization"),
79      RANGE("Range"),
80      REQUEST_RANGE("Request-Range"),
81      REFERER("Referer"),
82      TE("TE"),
83      USER_AGENT("User-Agent"),
84      X_FORWARDED_FOR("X-Forwarded-For"),
85      X_FORWARDED_PROTO("X-Forwarded-Proto"),
86      X_FORWARDED_SERVER("X-Forwarded-Server"),
87      X_FORWARDED_HOST("X-Forwarded-Host"),
88  
89      /* ------------------------------------------------------------ */
90      /** Response Fields.
91       */
92      ACCEPT_RANGES("Accept-Ranges"),
93      AGE("Age"),
94      ETAG("ETag"),
95      LOCATION("Location"),
96      PROXY_AUTHENTICATE("Proxy-Authenticate"),
97      RETRY_AFTER("Retry-After"),
98      SERVER("Server"),
99      SERVLET_ENGINE("Servlet-Engine"),
100     VARY("Vary"),
101     WWW_AUTHENTICATE("WWW-Authenticate"),
102 
103     /* ------------------------------------------------------------ */
104     /** Other Fields.
105      */
106     COOKIE("Cookie"),
107     SET_COOKIE("Set-Cookie"),
108     SET_COOKIE2("Set-Cookie2"),
109     MIME_VERSION("MIME-Version"),
110     IDENTITY("identity"),
111     
112     X_POWERED_BY("X-Powered-By"),
113     HTTP2_SETTINGS("HTTP2-Settings"),
114 
115     /* ------------------------------------------------------------ */
116     /** HTTP2 Fields.
117      */
118     C_METHOD(":method"),
119     C_SCHEME(":scheme"),
120     C_AUTHORITY(":authority"),
121     C_PATH(":path"),
122     C_STATUS(":status"),
123     
124     UNKNOWN("::UNKNOWN::");
125 
126 
127     /* ------------------------------------------------------------ */
128     public final static Trie<HttpHeader> CACHE= new ArrayTrie<>(530);
129     static
130     {
131         for (HttpHeader header : HttpHeader.values())
132             if (header!=UNKNOWN)
133                 if (!CACHE.put(header.toString(),header))
134                     throw new IllegalStateException();
135     }
136     
137     private final String _string;
138     private final byte[] _bytes;
139     private final byte[] _bytesColonSpace;
140     private final ByteBuffer _buffer;
141 
142     /* ------------------------------------------------------------ */
143     HttpHeader(String s)
144     {
145         _string=s;
146         _bytes=StringUtil.getBytes(s);
147         _bytesColonSpace=StringUtil.getBytes(s+": ");
148         _buffer=ByteBuffer.wrap(_bytes);
149     }
150 
151     /* ------------------------------------------------------------ */
152     public ByteBuffer toBuffer()
153     {
154         return _buffer.asReadOnlyBuffer();
155     }
156 
157     /* ------------------------------------------------------------ */
158     public byte[] getBytes()
159     {
160         return _bytes;
161     }
162 
163     /* ------------------------------------------------------------ */
164     public byte[] getBytesColonSpace()
165     {
166         return _bytesColonSpace;
167     }
168 
169     /* ------------------------------------------------------------ */
170     public boolean is(String s)
171     {
172         return _string.equalsIgnoreCase(s);    
173     }
174 
175     /* ------------------------------------------------------------ */
176     public String asString()
177     {
178         return _string;
179     }
180     
181     /* ------------------------------------------------------------ */
182     @Override
183     public String toString()
184     {
185         return _string;
186     }
187     
188 }
189