diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpMethod.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpMethod.java index 42aebd02d6a..3cd4e3d1165 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpMethod.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpMethod.java @@ -43,7 +43,7 @@ public enum HttpMethod /* ------------------------------------------------------------ */ /** - * Optimised lookup to find a method name and trailing space in a byte array. + * Optimized lookup to find a method name and trailing space in a byte array. * @param bytes Array containing ISO-8859-1 characters * @param position The first valid index * @param limit The first non valid index @@ -74,26 +74,26 @@ public enum HttpMethod break; case 'O': if (bytes[position+1]=='O' && bytes[position+2]=='T' && bytes[position+3]=='I' && length>=8 && - bytes[position+4]=='O' && bytes[position+5]=='N' && bytes[position+6]=='S' && bytes[position+7]==' ' ) + bytes[position+4]=='O' && bytes[position+5]=='N' && bytes[position+6]=='S' && bytes[position+7]==' ' ) return OPTIONS; break; case 'D': if (bytes[position+1]=='E' && bytes[position+2]=='L' && bytes[position+3]=='E' && length>=7 && - bytes[position+4]=='T' && bytes[position+5]=='E' && bytes[position+6]==' ' ) + bytes[position+4]=='T' && bytes[position+5]=='E' && bytes[position+6]==' ' ) return DELETE; break; case 'T': if (bytes[position+1]=='R' && bytes[position+2]=='A' && bytes[position+3]=='C' && length>=6 && - bytes[position+4]=='E' && bytes[position+5]==' ' ) + bytes[position+4]=='E' && bytes[position+5]==' ' ) return TRACE; break; case 'C': if (bytes[position+1]=='O' && bytes[position+2]=='N' && bytes[position+3]=='N' && length>=8 && - bytes[position+4]=='E' && bytes[position+5]=='C' && bytes[position+6]=='T' && bytes[position+7]==' ' ) + bytes[position+4]=='E' && bytes[position+5]=='C' && bytes[position+6]=='T' && bytes[position+7]==' ' ) return CONNECT; break; case 'M': - if (bytes[position+1]=='O' && bytes[position+2]=='V' && bytes[position+3]=='E' && bytes[position+4]==' ') + if (bytes[position+1]=='O' && bytes[position+2]=='V' && bytes[position+3]=='E' && length>=5 && bytes[position+4]==' ') return MOVE; break; @@ -105,17 +105,26 @@ public enum HttpMethod /* ------------------------------------------------------------ */ /** - * Optimised lookup to find a method name and trailing space in a byte array. - * @param buffer buffer containing ISO-8859-1 characters + * Optimized lookup to find a method name and trailing space in a byte array. + * @param buffer buffer containing ISO-8859-1 characters, it is not modified. * @return A HttpMethod if a match or null if no easy match. */ public static HttpMethod lookAheadGet(ByteBuffer buffer) { if (buffer.hasArray()) return lookAheadGet(buffer.array(),buffer.arrayOffset()+buffer.position(),buffer.arrayOffset()+buffer.limit()); - - // TODO use cache and check for space - // return CACHE.getBest(buffer,0,buffer.remaining()); + + int l = buffer.remaining(); + if (l>=4) + { + HttpMethod m = CACHE.getBest(buffer,0,l); + if (m!=null) + { + int ml = m.asString().length(); + if (l>ml && buffer.get(buffer.position()+ml)==' ') + return m; + } + } return null; } @@ -162,6 +171,7 @@ public enum HttpMethod return toString(); } + /* ------------------------------------------------------------ */ /** * Converts the given String parameter to an HttpMethod * @param method the String to get the equivalent HttpMethod from diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java index 057b9fc88ee..2ce93b839e6 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java @@ -63,6 +63,27 @@ public class HttpParserTest } } + @Test + public void HttpMethodTest() + { + assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("Wibble "))); + assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("GET"))); + assertNull(HttpMethod.lookAheadGet(BufferUtil.toBuffer("MO"))); + + assertEquals(HttpMethod.GET,HttpMethod.lookAheadGet(BufferUtil.toBuffer("GET "))); + assertEquals(HttpMethod.MOVE,HttpMethod.lookAheadGet(BufferUtil.toBuffer("MOVE "))); + + ByteBuffer b = BufferUtil.allocateDirect(128); + BufferUtil.append(b,BufferUtil.toBuffer("GET")); + assertNull(HttpMethod.lookAheadGet(b)); + + BufferUtil.append(b,BufferUtil.toBuffer(" ")); + assertEquals(HttpMethod.GET,HttpMethod.lookAheadGet(b)); + + + } + + @Test public void testLineParse0() throws Exception { diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java index ac3c5427356..cd5eb9af244 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java @@ -389,9 +389,9 @@ public class BufferUtil } /* ------------------------------------------------------------ */ - /** Appends a byte to a buffer + /** Appends a buffer to a buffer * @param to Buffer is flush mode - * @param b bytes to append + * @param b buffer to append */ public static int append(ByteBuffer to, ByteBuffer b) {