View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  import java.util.HashSet;
23  import java.util.Set;
24  
25  import org.eclipse.jetty.util.ArrayTrie;
26  import org.eclipse.jetty.util.BufferUtil;
27  import org.eclipse.jetty.util.StringUtil;
28  import org.eclipse.jetty.util.Trie;
29  
30  
31  /* ------------------------------------------------------------ */
32  /** A HTTP Field
33   */
34  public class HttpField
35  {
36      public final static Trie<HttpField> CACHE = new ArrayTrie<>(1024);
37      public final static Trie<HttpField> CONTENT_TYPE = new ArrayTrie<>(512);
38      
39      static
40      {
41          CACHE.put(new CachedHttpField(HttpHeader.CONNECTION,HttpHeaderValue.CLOSE));
42          CACHE.put(new CachedHttpField(HttpHeader.CONNECTION,HttpHeaderValue.KEEP_ALIVE));
43          CACHE.put(new CachedHttpField(HttpHeader.CONNECTION,HttpHeaderValue.UPGRADE));
44          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT_ENCODING,"gzip"));
45          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT_ENCODING,"gzip, deflate"));
46          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT_ENCODING,"gzip,deflate,sdch"));
47          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT_LANGUAGE,"en-US,en;q=0.5"));
48          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT_LANGUAGE,"en-GB,en-US;q=0.8,en;q=0.6"));
49          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT_CHARSET,"ISO-8859-1,utf-8;q=0.7,*;q=0.3"));
50          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT,"*/*"));
51          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT,"image/png,image/*;q=0.8,*/*;q=0.5"));
52          CACHE.put(new CachedHttpField(HttpHeader.ACCEPT,"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
53          CACHE.put(new CachedHttpField(HttpHeader.PRAGMA,"no-cache"));
54          CACHE.put(new CachedHttpField(HttpHeader.CACHE_CONTROL,"private, no-cache, no-cache=Set-Cookie, proxy-revalidate"));
55          CACHE.put(new CachedHttpField(HttpHeader.CACHE_CONTROL,"no-cache"));
56          CACHE.put(new CachedHttpField(HttpHeader.CONTENT_LENGTH,"0"));
57          CACHE.put(new CachedHttpField(HttpHeader.CONTENT_ENCODING,"gzip"));
58          CACHE.put(new CachedHttpField(HttpHeader.CONTENT_ENCODING,"deflate"));
59          CACHE.put(new CachedHttpField(HttpHeader.EXPIRES,"Fri, 01 Jan 1990 00:00:00 GMT"));
60          
61          // Content types
62          for (String type : new String[]{"text/plain","text/html","text/xml","text/json","application/x-www-form-urlencoded"})
63          {
64              HttpField field=new CachedHttpField(HttpHeader.CONTENT_TYPE,type);
65              CACHE.put(field);
66              CONTENT_TYPE.put(type,field);
67              
68              for (String charset : new String[]{"UTF-8","ISO-8859-1"})
69              {
70                  String type_charset=type+"; charset="+charset;
71                  field=new CachedHttpField(HttpHeader.CONTENT_TYPE,type_charset);
72                  CACHE.put(field);
73                  CACHE.put(new CachedHttpField(HttpHeader.CONTENT_TYPE,type+";charset="+charset));
74                  CONTENT_TYPE.put(type_charset,field);
75                  CONTENT_TYPE.put(type+";charset="+charset,field);
76              }
77          }
78  
79          // Add headers with null values so HttpParser can avoid looking up name again for unknown values
80          Set<HttpHeader> headers = new HashSet<>();
81          for (String key:CACHE.keySet())
82              headers.add(CACHE.get(key).getHeader());
83          for (HttpHeader h:headers)
84              if (!CACHE.put(new HttpField(h,(String)null)))
85                  throw new IllegalStateException("CACHE FULL");
86          // Add some more common headers
87          CACHE.put(new HttpField(HttpHeader.REFERER,(String)null));
88          CACHE.put(new HttpField(HttpHeader.IF_MODIFIED_SINCE,(String)null));
89          CACHE.put(new HttpField(HttpHeader.IF_NONE_MATCH,(String)null));
90          CACHE.put(new HttpField(HttpHeader.AUTHORIZATION,(String)null));
91          CACHE.put(new HttpField(HttpHeader.COOKIE,(String)null));
92      }
93  
94      private final static byte[] __colon_space = new byte[] {':',' '};
95      
96      private final HttpHeader _header;
97      private final String _name;
98      private final String _value;
99          
100     public HttpField(HttpHeader header, String name, String value)
101     {
102         _header = header;
103         _name = name;
104         _value = value;
105     }  
106     
107     public HttpField(HttpHeader header, String value)
108     {
109         this(header,header.asString(),value);
110     }
111  
112     
113     public HttpField(HttpHeader header, HttpHeaderValue value)
114     {
115         this(header,header.asString(),value.asString());
116     }
117     
118     public HttpField(String name, String value)
119     {
120         this(HttpHeader.CACHE.get(name),name,value);
121     }
122 
123     public HttpHeader getHeader()
124     {
125         return _header;
126     }
127 
128     public String getName()
129     {
130         return _name;
131     }
132 
133     public String getValue()
134     {
135         return _value;
136     }
137 
138     public boolean contains(String value)
139     {
140         if (_value==null)
141             return false;
142 
143         if (value.equalsIgnoreCase(_value))
144             return true;
145 
146         String[] split = _value.split("\\s*,\\s*");
147         for (String s : split)
148         {
149             if (value.equalsIgnoreCase(s))
150                 return true;
151         }
152 
153         return false;
154     }
155 
156 
157     public int getIntValue()
158     {
159         return StringUtil.toInt(_value);
160     }
161 
162     public long getLongValue()
163     {
164         return StringUtil.toLong(_value);
165     }
166     
167     private static byte[] toSanitisedName(String s)
168     {
169         byte[] bytes = s.getBytes(StringUtil.__ISO_8859_1_CHARSET);
170         for (int i=bytes.length;i-->0;)
171         {
172             switch(bytes[i])
173             {
174                 case '\r':
175                 case '\n':
176                 case ':' :
177                     bytes[i]=(byte)'?';
178             }
179         }
180         return bytes;
181     }
182 
183     private static byte[] toSanitisedValue(String s)
184     {
185         byte[] bytes = s.getBytes(StringUtil.__ISO_8859_1_CHARSET);
186         for (int i=bytes.length;i-->0;)
187         {
188             switch(bytes[i])
189             {
190                 case '\r':
191                 case '\n':
192                     bytes[i]=(byte)'?';
193             }
194         }
195         return bytes;
196     }
197 
198     public void putTo(ByteBuffer bufferInFillMode)
199     {
200         if (_header!=null)
201         {
202             bufferInFillMode.put(_header.getBytesColonSpace());
203             bufferInFillMode.put(toSanitisedValue(_value));
204         }
205         else
206         {
207             bufferInFillMode.put(toSanitisedName(_name));
208             bufferInFillMode.put(__colon_space);
209             bufferInFillMode.put(toSanitisedValue(_value));
210         }
211 
212         BufferUtil.putCRLF(bufferInFillMode);
213     }
214 
215     public void putValueTo(ByteBuffer buffer)
216     {
217         buffer.put(toSanitisedValue(_value));
218     }
219 
220     @Override
221     public String toString()
222     {
223         String v=getValue();
224         return getName() + ": " + (v==null?"":v.toString());
225     }
226 
227     public boolean isSame(HttpField field)
228     {
229         if (field==null)
230             return false;
231         if (field==this)
232             return true;
233         if (_header!=null && _header==field.getHeader())
234             return true;
235         if (_name.equalsIgnoreCase(field.getName()))
236             return true;
237         return false;
238     }
239     
240     
241     /* ------------------------------------------------------------ */
242     /** A HTTP Field optimised to be reused.
243      */
244     public static class CachedHttpField extends HttpField
245     {
246         final byte[] _bytes;
247         public CachedHttpField(HttpHeader header, String value)
248         {
249             super(header,value);
250             _bytes=new byte[header.asString().length()+2+value.length()+2];
251             System.arraycopy(header.getBytesColonSpace(),0,_bytes,0,header.asString().length()+2);
252             System.arraycopy(toSanitisedValue(value),0,_bytes,header.asString().length()+2,value.length());
253             _bytes[_bytes.length-2]='\r';
254             _bytes[_bytes.length-1]='\n';
255         }
256 
257         CachedHttpField(HttpHeader header, HttpHeaderValue value)
258         {
259             this(header,value.asString());
260         }
261         
262         @Override
263         public void putTo(ByteBuffer bufferInFillMode)
264         {
265             bufferInFillMode.put(_bytes);
266         }
267     }
268 }