2 // ========================================================================
3 // Copyright (c) 1995-2014 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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http;
21 import java.nio.ByteBuffer;
23 import org.eclipse.jetty.util.ArrayTrie;
24 import org.eclipse.jetty.util.StringUtil;
25 import org.eclipse.jetty.util.Trie;
28 /* ------------------------------------------------------------------------------- */
29 public enum HttpVersion
31 HTTP_0_9("HTTP/0.9",9),
32 HTTP_1_0("HTTP/1.0",10),
33 HTTP_1_1("HTTP/1.1",11),
34 HTTP_2_0("HTTP/2.0",20);
36 /* ------------------------------------------------------------ */
37 public final static Trie<HttpVersion> CACHE= new ArrayTrie<HttpVersion>();
40 for (HttpVersion version : HttpVersion.values())
41 CACHE.put(version.toString(),version);
44 /* ------------------------------------------------------------ */
46 * Optimised lookup to find a Http Version and whitespace in a byte array.
47 * @param bytes Array containing ISO-8859-1 characters
48 * @param position The first valid index
49 * @param limit The first non valid index
50 * @return A HttpMethod if a match or null if no easy match.
52 public static HttpVersion lookAheadGet(byte[] bytes, int position, int limit)
54 int length=limit-position;
58 if (bytes[position+4]=='/' && bytes[position+6]=='.' && Character.isWhitespace((char)bytes[position+8]) &&
59 ((bytes[position]=='H' && bytes[position+1]=='T' && bytes[position+2]=='T' && bytes[position+3]=='P') ||
60 (bytes[position]=='h' && bytes[position+1]=='t' && bytes[position+2]=='t' && bytes[position+3]=='p')))
62 switch(bytes[position+5])
65 switch(bytes[position+7])
74 switch(bytes[position+7])
86 /* ------------------------------------------------------------ */
88 * Optimised lookup to find a HTTP Version and trailing white space in a byte array.
89 * @param buffer buffer containing ISO-8859-1 characters
90 * @return A HttpVersion if a match or null if no easy match.
92 public static HttpVersion lookAheadGet(ByteBuffer buffer)
94 if (buffer.hasArray())
95 return lookAheadGet(buffer.array(),buffer.arrayOffset()+buffer.position(),buffer.arrayOffset()+buffer.limit());
100 private final String _string;
101 private final byte[] _bytes;
102 private final ByteBuffer _buffer;
103 private final int _version;
105 /* ------------------------------------------------------------ */
106 HttpVersion(String s,int version)
109 _bytes=StringUtil.getBytes(s);
110 _buffer=ByteBuffer.wrap(_bytes);
114 /* ------------------------------------------------------------ */
115 public byte[] toBytes()
120 /* ------------------------------------------------------------ */
121 public ByteBuffer toBuffer()
123 return _buffer.asReadOnlyBuffer();
126 /* ------------------------------------------------------------ */
127 public int getVersion()
132 /* ------------------------------------------------------------ */
133 public boolean is(String s)
135 return _string.equalsIgnoreCase(s);
138 /* ------------------------------------------------------------ */
139 public String asString()
144 /* ------------------------------------------------------------ */
146 public String toString()
152 * Case insensitive fromString() conversion
153 * @param version the String to convert to enum constant
154 * @return the enum constant or null if version unknown
156 public static HttpVersion fromString(String version)
158 return CACHE.get(version);
161 /* ------------------------------------------------------------ */
162 public static HttpVersion fromVersion(int version)
166 case 9: return HttpVersion.HTTP_0_9;
167 case 10: return HttpVersion.HTTP_1_0;
168 case 11: return HttpVersion.HTTP_1_1;
169 default: throw new IllegalArgumentException();