diff options
author | evanweaver <evanweaver@19e92222-5c0b-0410-8929-a290d50e31e9> | 2007-10-16 20:56:52 +0000 |
---|---|---|
committer | evanweaver <evanweaver@19e92222-5c0b-0410-8929-a290d50e31e9> | 2007-10-16 20:56:52 +0000 |
commit | c9569530b1ca52220dddf48e32c39279328e7e5a (patch) | |
tree | 19ccd057dd95cbd7aa9cf1698ba3b4fc4ceef984 | |
parent | 2d0d8454a4b4d0345ed2e126bdd55ecd81d64f37 (diff) | |
download | unicorn-c9569530b1ca52220dddf48e32c39279328e7e5a.tar.gz |
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@659 19e92222-5c0b-0410-8929-a290d50e31e9
-rwxr-xr-x | jruby/.classpath | 8 | ||||
-rw-r--r-- | jruby/.project | 17 | ||||
-rw-r--r-- | jruby/Rakefile | 63 | ||||
-rw-r--r-- | jruby/build.properties.SAMPLE | 2 | ||||
-rw-r--r-- | jruby/build.xml | 58 | ||||
-rw-r--r-- | jruby/lib/jruby.jar | bin | 0 -> 2253643 bytes | |||
-rw-r--r-- | jruby/src/java/Http11Service.java | 13 | ||||
-rw-r--r-- | jruby/src/java/org/jruby/mongrel/Http11.java | 256 | ||||
-rw-r--r-- | jruby/src/java/org/jruby/mongrel/Http11Parser.java | 548 | ||||
-rw-r--r-- | jruby/src/java/org/jruby/mongrel/URIClassifier.java | 129 | ||||
-rw-r--r-- | jruby/src/java/org/jruby/mongrel/http11_parser.rl | 209 | ||||
-rw-r--r-- | jruby/src/java/org/jruby/tst/Node.java | 18 | ||||
-rw-r--r-- | jruby/src/java/org/jruby/tst/NodeLines.java | 16 | ||||
-rw-r--r-- | jruby/src/java/org/jruby/tst/TernarySearchTree.java | 433 | ||||
-rw-r--r-- | jruby/src/test/testParser.rb | 21 | ||||
-rw-r--r-- | jruby/src/test/testParserImpl.rb | 22 | ||||
-rw-r--r-- | jruby/src/test/test_http11.rb | 119 | ||||
-rw-r--r-- | jruby/src/test/test_performance.rb | 21 |
18 files changed, 1953 insertions, 0 deletions
diff --git a/jruby/.classpath b/jruby/.classpath new file mode 100755 index 0000000..2b7bb05 --- /dev/null +++ b/jruby/.classpath @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src/java"/> + <classpathentry kind="src" path="target/java"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="lib" path="lib/jruby.jar"/> + <classpathentry kind="output" path="target/classes"/> +</classpath> diff --git a/jruby/.project b/jruby/.project new file mode 100644 index 0000000..28ac06d --- /dev/null +++ b/jruby/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>mongrel-support</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/jruby/Rakefile b/jruby/Rakefile new file mode 100644 index 0000000..d9f0fa1 --- /dev/null +++ b/jruby/Rakefile @@ -0,0 +1,63 @@ +# Usage: +# Checkout mongrel from source +# Set the MONGREL_SUPPORT environment variable to the directory +# where this file is located. +# Go to the mongrel source tree +# Run this command +# jruby -S rake -f $MONGREL_SUPPORT/Rakefile +# + +require 'rake' +require 'rake/testtask' +require 'rake/clean' +require 'rake/gempackagetask' +require 'rake/rdoctask' +require 'tools/rakehelp' +require 'fileutils' +include FileUtils + +$mongrel_support = ENV['MONGREL_SUPPORT'] + +unless $mongrel_support + warn "MONGREL_SUPPORT needs to be set to the source directory of Mongrel" + exit 1 +end + +desc "Does a compile and package" +task :default => [:package] + +desc "Compiles Mongrel-support" +task :compile do + Dir.chdir($mongrel_support) do + sh %(ant jar) + end +end + +task :package => [:compile] + +task 'lib/http11.jar' do + cp "#{$mongrel_support}/lib/http11.jar", 'lib' +end + +name="mongrel" +version="1.0.1" + +setup_gem(name, version) do |spec| + spec.summary = "A small fast HTTP library and server that runs Rails, Camping, Nitro and Iowa apps." + spec.description = spec.summary + spec.test_files = Dir.glob("test/test_*.rb") + spec.author="Zed A. Shaw" + spec.executables=['mongrel_rails'] + spec.files += %w(README Rakefile setup.rb lib/mongrel/mime_types.yml) + + spec.required_ruby_version = '>= 1.8.4' + + spec.files += ['lib/http11.jar'] + spec.extensions.clear + spec.platform = 'jruby' + + spec.add_dependency('gem_plugin', '>= 0.2.2') + spec.add_dependency('cgi_multipart_eof_fix', '>= 1.0.0') +end + + diff --git a/jruby/build.properties.SAMPLE b/jruby/build.properties.SAMPLE new file mode 100644 index 0000000..0d832e2 --- /dev/null +++ b/jruby/build.properties.SAMPLE @@ -0,0 +1,2 @@ +version.target=1.4 +version.source=1.4 diff --git a/jruby/build.xml b/jruby/build.xml new file mode 100644 index 0000000..352a624 --- /dev/null +++ b/jruby/build.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project basedir="." default="build" name="mongrel-support"> + <property environment="env"/> + <property file="build.properties"/> + + <property name="src.java" value="src/java"/> + <property name="src.javacc" value="src/javacc"/> + <property name="target" value="target"/> + <property name="target.classes" value="${target}/classes"/> + <property name="target.classes.test" value="${target}/test-classes"/> + <property name="lib.dir" value="lib"/> + + <path id="build.classpath"> + <fileset dir="${lib.dir}" includes="*.jar" excludes="mongrel-support.jar,http11.jar"/> + </path> + + <target name="init"> + <mkdir dir="${target}"/> + <mkdir dir="${target}/java/httpp"/> + <mkdir dir="${target.classes}"/> + </target> + + <target name="clean"> + <delete dir="target"/> + </target> + + <target name="ragel" description="Standalone target that generates all our ragel based source files. Requires ragel and rlgen-java to be on the classpath"> + <exec executable="ragel" output="__ragel_out"> + <arg line="-J"/> + <arg line="${src.java}/org/jruby/mongrel/http11_parser.rl"/> + </exec> + <exec executable="rlgen-java" input="__ragel_out"> + <arg line="-o ${src.java}/org/jruby/mongrel/Http11Parser.java"/> + </exec> + <delete file="__ragel_out"/> + </target> + + <target depends="init" name="wbuild" description="Builds version without references to JRuby or the Extension API"> + <javac debug="true" destdir="${target.classes}" source="${version.source}" target="${version.target}"> + <classpath refid="build.classpath"/> + <src path="${src.java}"/> + <include name="**/HttpParserImpl.java"/> + <include name="httpp/**/*.java"/> + </javac> + </target> + + <target depends="init" name="build" description="Compiles Java source files"> + <javac debug="true" destdir="${target.classes}" source="${version.source}" target="${version.target}"> + <classpath refid="build.classpath"/> + <src path="${src.java}"/> + </javac> + </target> + + <target depends="build" name="jar" description="Build a JAR file with the generated Java class files"> + <jar destfile="${lib.dir}/mongrel-support.jar" basedir="${target.classes}"/> + <jar destfile="${lib.dir}/http11.jar" basedir="${target.classes}"/> + </target> +</project> diff --git a/jruby/lib/jruby.jar b/jruby/lib/jruby.jar Binary files differnew file mode 100644 index 0000000..92a87e7 --- /dev/null +++ b/jruby/lib/jruby.jar diff --git a/jruby/src/java/Http11Service.java b/jruby/src/java/Http11Service.java new file mode 100644 index 0000000..5d78c49 --- /dev/null +++ b/jruby/src/java/Http11Service.java @@ -0,0 +1,13 @@ +import java.io.IOException; + +import org.jruby.Ruby; +import org.jruby.runtime.load.BasicLibraryService; + +import org.jruby.mongrel.Http11; + +public class Http11Service implements BasicLibraryService { + public boolean basicLoad(final Ruby runtime) throws IOException { + Http11.createHttp11(runtime); + return true; + } +} diff --git a/jruby/src/java/org/jruby/mongrel/Http11.java b/jruby/src/java/org/jruby/mongrel/Http11.java new file mode 100644 index 0000000..fff8b41 --- /dev/null +++ b/jruby/src/java/org/jruby/mongrel/Http11.java @@ -0,0 +1,256 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: CPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Common Public + * License Version 1.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.eclipse.org/legal/cpl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2007 Ola Bini <ola@ologix.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the CPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the CPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ +package org.jruby.mongrel; + +import org.jruby.Ruby; +import org.jruby.RubyClass; +import org.jruby.RubyHash; +import org.jruby.RubyModule; +import org.jruby.RubyNumeric; +import org.jruby.RubyObject; +import org.jruby.RubyString; + +import org.jruby.runtime.CallbackFactory; +import org.jruby.runtime.ObjectAllocator; +import org.jruby.runtime.builtin.IRubyObject; + +import org.jruby.exceptions.RaiseException; + +import org.jruby.util.ByteList; + +/** + * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a> + */ +public class Http11 extends RubyObject { + public final static int MAX_FIELD_NAME_LENGTH = 256; + public final static String MAX_FIELD_NAME_LENGTH_ERR = "HTTP element FIELD_NAME is longer than the 256 allowed length."; + public final static int MAX_FIELD_VALUE_LENGTH = 80 * 1024; + public final static String MAX_FIELD_VALUE_LENGTH_ERR = "HTTP element FIELD_VALUE is longer than the 81920 allowed length."; + public final static int MAX_REQUEST_URI_LENGTH = 1024 * 12; + public final static String MAX_REQUEST_URI_LENGTH_ERR = "HTTP element REQUEST_URI is longer than the 12288 allowed length."; + public final static int MAX_REQUEST_PATH_LENGTH = 1024; + public final static String MAX_REQUEST_PATH_LENGTH_ERR = "HTTP element REQUEST_PATH is longer than the 1024 allowed length."; + public final static int MAX_QUERY_STRING_LENGTH = 1024 * 10; + public final static String MAX_QUERY_STRING_LENGTH_ERR = "HTTP element QUERY_STRING is longer than the 10240 allowed length."; + public final static int MAX_HEADER_LENGTH = 1024 * (80 + 32); + public final static String MAX_HEADER_LENGTH_ERR = "HTTP element HEADER is longer than the 114688 allowed length."; + + + private static ObjectAllocator ALLOCATOR = new ObjectAllocator() { + public IRubyObject allocate(Ruby runtime, RubyClass klass) { + return new Http11(runtime, klass); + } + }; + + public static void createHttp11(Ruby runtime) { + RubyModule mMongrel = runtime.defineModule("Mongrel"); + mMongrel.defineClassUnder("HttpParserError",runtime.getClass("IOError"),runtime.getClass("IOError").getAllocator()); + + CallbackFactory cf = runtime.callbackFactory(Http11.class); + + RubyClass cHttpParser = mMongrel.defineClassUnder("HttpParser",runtime.getObject(),ALLOCATOR); + cHttpParser.defineFastMethod("initialize",cf.getFastMethod("initialize")); + cHttpParser.defineFastMethod("reset",cf.getFastMethod("reset")); + cHttpParser.defineFastMethod("finish",cf.getFastMethod("finish")); + cHttpParser.defineFastMethod("execute",cf.getFastMethod("execute", IRubyObject.class, IRubyObject.class, IRubyObject.class)); + cHttpParser.defineFastMethod("error?",cf.getFastMethod("has_error")); + cHttpParser.defineFastMethod("finished?",cf.getFastMethod("is_finished")); + cHttpParser.defineFastMethod("nread",cf.getFastMethod("nread")); + + URIClassifier.createURIClassifier(runtime, mMongrel); + } + + private Ruby runtime; + private RubyClass eHttpParserError; + private Http11Parser hp; + + public Http11(Ruby runtime, RubyClass clazz) { + super(runtime,clazz); + this.runtime = runtime; + this.eHttpParserError = (RubyClass)runtime.getModule("Mongrel").getConstant("HttpParserError"); + this.hp = new Http11Parser(); + this.hp.parser.http_field = http_field; + this.hp.parser.request_method = request_method; + this.hp.parser.request_uri = request_uri; + this.hp.parser.request_path = request_path; + this.hp.parser.query_string = query_string; + this.hp.parser.http_version = http_version; + this.hp.parser.header_done = header_done; + this.hp.parser.init(); + } + + public void validateMaxLength(int len, int max, String msg) { + if(len>max) { + throw new RaiseException(runtime, eHttpParserError, msg, true); + } + } + + private Http11Parser.FieldCB http_field = new Http11Parser.FieldCB() { + public void call(Object data, int field, int flen, int value, int vlen) { + RubyHash req = (RubyHash)data; + RubyString v,f; + validateMaxLength(flen, MAX_FIELD_NAME_LENGTH, MAX_FIELD_NAME_LENGTH_ERR); + validateMaxLength(vlen, MAX_FIELD_VALUE_LENGTH, MAX_FIELD_VALUE_LENGTH_ERR); + + v = RubyString.newString(runtime, new ByteList(Http11.this.hp.parser.buffer,value,vlen)); + f = RubyString.newString(runtime, "HTTP_"); + ByteList b = new ByteList(Http11.this.hp.parser.buffer,field,flen); + for(int i=0,j=b.realSize;i<j;i++) { + if((b.bytes[i]&0xFF) == '-') { + b.bytes[i] = (byte)'_'; + } else { + b.bytes[i] = (byte)Character.toUpperCase((char)b.bytes[i]); + } + } + f.cat(b); + req.aset(f,v); + } + }; + + private Http11Parser.ElementCB request_method = new Http11Parser.ElementCB() { + public void call(Object data, int at, int length) { + RubyHash req = (RubyHash)data; + RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); + req.aset(runtime.newString("REQUEST_METHOD"),val); + } + }; + + private Http11Parser.ElementCB request_uri = new Http11Parser.ElementCB() { + public void call(Object data, int at, int length) { + RubyHash req = (RubyHash)data; + validateMaxLength(length, MAX_REQUEST_URI_LENGTH, MAX_REQUEST_URI_LENGTH_ERR); + RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); + req.aset(runtime.newString("REQUEST_URI"),val); + } + }; + + private Http11Parser.ElementCB request_path = new Http11Parser.ElementCB() { + public void call(Object data, int at, int length) { + RubyHash req = (RubyHash)data; + validateMaxLength(length, MAX_REQUEST_PATH_LENGTH, MAX_REQUEST_PATH_LENGTH_ERR); + RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); + req.aset(runtime.newString("REQUEST_PATH"),val); + } + }; + + private Http11Parser.ElementCB query_string = new Http11Parser.ElementCB() { + public void call(Object data, int at, int length) { + RubyHash req = (RubyHash)data; + validateMaxLength(length, MAX_QUERY_STRING_LENGTH, MAX_QUERY_STRING_LENGTH_ERR); + RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); + req.aset(runtime.newString("QUERY_STRING"),val); + } + }; + + private Http11Parser.ElementCB http_version = new Http11Parser.ElementCB() { + public void call(Object data, int at, int length) { + RubyHash req = (RubyHash)data; + RubyString val = RubyString.newString(runtime,new ByteList(hp.parser.buffer,at,length)); + req.aset(runtime.newString("HTTP_VERSION"),val); + } + }; + + private Http11Parser.ElementCB header_done = new Http11Parser.ElementCB() { + public void call(Object data, int at, int length) { + RubyHash req = (RubyHash)data; + IRubyObject temp,ctype,clen; + + clen = req.aref(runtime.newString("HTTP_CONTENT_LENGTH")); + if(!clen.isNil()) { + req.aset(runtime.newString("CONTENT_LENGTH"),clen); + } + + ctype = req.aref(runtime.newString("HTTP_CONTENT_TYPE")); + if(!ctype.isNil()) { + req.aset(runtime.newString("CONTENT_TYPE"),ctype); + } + + req.aset(runtime.newString("GATEWAY_INTERFACE"),runtime.newString("CGI/1.2")); + if(!(temp = req.aref(runtime.newString("HTTP_HOST"))).isNil()) { + String s = temp.toString(); + int colon = s.indexOf(':'); + if(colon != -1) { + req.aset(runtime.newString("SERVER_NAME"),runtime.newString(s.substring(0,colon))); + req.aset(runtime.newString("SERVER_PORT"),runtime.newString(s.substring(colon+1))); + } else { + req.aset(runtime.newString("SERVER_NAME"),temp); + req.aset(runtime.newString("SERVER_PORT"),runtime.newString("80")); + } + } + + req.setInstanceVariable("@http_body", RubyString.newString(runtime, new ByteList(hp.parser.buffer, at, length))); + req.aset(runtime.newString("SERVER_PROTOCOL"),runtime.newString("HTTP/1.1")); + req.aset(runtime.newString("SERVER_SOFTWARE"),runtime.newString("Mongrel 1.0.1")); + } + }; + + public IRubyObject initialize() { + this.hp.parser.init(); + return this; + } + + public IRubyObject reset() { + this.hp.parser.init(); + return runtime.getNil(); + } + + public IRubyObject finish() { + this.hp.finish(); + return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse(); + } + + public IRubyObject execute(IRubyObject req_hash, IRubyObject data, IRubyObject start) { + int from = 0; + from = RubyNumeric.fix2int(start); + ByteList d = ((RubyString)data).getByteList(); + if(from >= d.realSize) { + throw new RaiseException(runtime, eHttpParserError, "Requested start is after data buffer end.", true); + } else { + this.hp.parser.data = req_hash; + this.hp.execute(d,from); + validateMaxLength(this.hp.parser.nread,MAX_HEADER_LENGTH, MAX_HEADER_LENGTH_ERR); + if(this.hp.has_error()) { + throw new RaiseException(runtime, eHttpParserError, "Invalid HTTP format, parsing fails.", true); + } else { + return runtime.newFixnum(this.hp.parser.nread); + } + } + } + + public IRubyObject has_error() { + return this.hp.has_error() ? runtime.getTrue() : runtime.getFalse(); + } + + public IRubyObject is_finished() { + return this.hp.is_finished() ? runtime.getTrue() : runtime.getFalse(); + } + + public IRubyObject nread() { + return runtime.newFixnum(this.hp.parser.nread); + } +}// Http11 diff --git a/jruby/src/java/org/jruby/mongrel/Http11Parser.java b/jruby/src/java/org/jruby/mongrel/Http11Parser.java new file mode 100644 index 0000000..d1edf5b --- /dev/null +++ b/jruby/src/java/org/jruby/mongrel/Http11Parser.java @@ -0,0 +1,548 @@ +// line 1 "src/java/org/jruby/mongrel/http11_parser.rl" +package org.jruby.mongrel; + +import org.jruby.util.ByteList; + +public class Http11Parser { + +/** machine **/ +// line 104 "src/java/org/jruby/mongrel/http11_parser.rl" + + +/** Data **/ + +// line 15 "src/java/org/jruby/mongrel/Http11Parser.java" +private static void init__http_parser_actions_0( byte[] r ) +{ + r[0]=0; r[1]=1; r[2]=0; r[3]=1; r[4]=1; r[5]=1; r[6]=2; r[7]=1; + r[8]=3; r[9]=1; r[10]=4; r[11]=1; r[12]=5; r[13]=1; r[14]=6; r[15]=1; + r[16]=7; r[17]=1; r[18]=9; r[19]=1; r[20]=10; r[21]=1; r[22]=11; r[23]=2; + r[24]=8; r[25]=6; r[26]=2; r[27]=10; r[28]=6; r[29]=3; r[30]=7; r[31]=8; + r[32]=6; +} + +private static byte[] create__http_parser_actions( ) +{ + byte[] r = new byte[33]; + init__http_parser_actions_0( r ); + return r; +} + +private static final byte _http_parser_actions[] = create__http_parser_actions(); + + +private static void init__http_parser_key_offsets_0( short[] r ) +{ + r[0]=0; r[1]=0; r[2]=8; r[3]=17; r[4]=27; r[5]=28; r[6]=29; r[7]=30; + r[8]=31; r[9]=32; r[10]=33; r[11]=35; r[12]=38; r[13]=40; r[14]=43; r[15]=44; + r[16]=60; r[17]=61; r[18]=77; r[19]=79; r[20]=80; r[21]=90; r[22]=99; r[23]=105; + r[24]=111; r[25]=122; r[26]=128; r[27]=134; r[28]=144; r[29]=150; r[30]=156; r[31]=165; + r[32]=174; r[33]=180; r[34]=186; r[35]=195; r[36]=204; r[37]=213; r[38]=222; r[39]=231; + r[40]=240; r[41]=249; r[42]=258; r[43]=267; r[44]=276; r[45]=285; r[46]=294; r[47]=303; + r[48]=312; r[49]=321; r[50]=330; r[51]=339; r[52]=348; r[53]=349; +} + +private static short[] create__http_parser_key_offsets( ) +{ + short[] r = new short[54]; + init__http_parser_key_offsets_0( r ); + return r; +} + +private static final short _http_parser_key_offsets[] = create__http_parser_key_offsets(); + + +private static void init__http_parser_trans_keys_0( char[] r ) +{ + r[0]=36; r[1]=95; r[2]=45; r[3]=46; r[4]=48; r[5]=57; r[6]=65; r[7]=90; + r[8]=32; r[9]=36; r[10]=95; r[11]=45; r[12]=46; r[13]=48; r[14]=57; r[15]=65; + r[16]=90; r[17]=42; r[18]=43; r[19]=47; r[20]=58; r[21]=45; r[22]=57; r[23]=65; + r[24]=90; r[25]=97; r[26]=122; r[27]=32; r[28]=72; r[29]=84; r[30]=84; r[31]=80; + r[32]=47; r[33]=48; r[34]=57; r[35]=46; r[36]=48; r[37]=57; r[38]=48; r[39]=57; + r[40]=13; r[41]=48; r[42]=57; r[43]=10; r[44]=13; r[45]=33; r[46]=124; r[47]=126; + r[48]=35; r[49]=39; r[50]=42; r[51]=43; r[52]=45; r[53]=46; r[54]=48; r[55]=57; + r[56]=65; r[57]=90; r[58]=94; r[59]=122; r[60]=10; r[61]=33; r[62]=58; r[63]=124; + r[64]=126; r[65]=35; r[66]=39; r[67]=42; r[68]=43; r[69]=45; r[70]=46; r[71]=48; + r[72]=57; r[73]=65; r[74]=90; r[75]=94; r[76]=122; r[77]=13; r[78]=32; r[79]=13; + r[80]=43; r[81]=58; r[82]=45; r[83]=46; r[84]=48; r[85]=57; r[86]=65; r[87]=90; + r[88]=97; r[89]=122; r[90]=32; r[91]=37; r[92]=60; r[93]=62; r[94]=127; r[95]=0; + r[96]=31; r[97]=34; r[98]=35; r[99]=48; r[100]=57; r[101]=65; r[102]=70; r[103]=97; + r[104]=102; r[105]=48; r[106]=57; r[107]=65; r[108]=70; r[109]=97; r[110]=102; r[111]=32; + r[112]=37; r[113]=59; r[114]=60; r[115]=62; r[116]=63; r[117]=127; r[118]=0; r[119]=31; + r[120]=34; r[121]=35; r[122]=48; r[123]=57; r[124]=65; r[125]=70; r[126]=97; r[127]=102; + r[128]=48; r[129]=57; r[130]=65; r[131]=70; r[132]=97; r[133]=102; r[134]=32; r[135]=37; + r[136]=60; r[137]=62; r[138]=63; r[139]=127; r[140]=0; r[141]=31; r[142]=34; r[143]=35; + r[144]=48; r[145]=57; r[146]=65; r[147]=70; r[148]=97; r[149]=102; r[150]=48; r[151]=57; + r[152]=65; r[153]=70; r[154]=97; r[155]=102; r[156]=32; r[157]=37; r[158]=60; r[159]=62; + r[160]=127; r[161]=0; r[162]=31; r[163]=34; r[164]=35; r[165]=32; r[166]=37; r[167]=60; + r[168]=62; r[169]=127; r[170]=0; r[171]=31; r[172]=34; r[173]=35; r[174]=48; r[175]=57; + r[176]=65; r[177]=70; r[178]=97; r[179]=102; r[180]=48; r[181]=57; r[182]=65; r[183]=70; + r[184]=97; r[185]=102; r[186]=32; r[187]=36; r[188]=95; r[189]=45; r[190]=46; r[191]=48; + r[192]=57; r[193]=65; r[194]=90; r[195]=32; r[196]=36; r[197]=95; r[198]=45; r[199]=46; + r[200]=48; r[201]=57; r[202]=65; r[203]=90; r[204]=32; r[205]=36; r[206]=95; r[207]=45; + r[208]=46; r[209]=48; r[210]=57; r[211]=65; r[212]=90; r[213]=32; r[214]=36; r[215]=95; + r[216]=45; r[217]=46; r[218]=48; r[219]=57; r[220]=65; r[221]=90; r[222]=32; r[223]=36; + r[224]=95; r[225]=45; r[226]=46; r[227]=48; r[228]=57; r[229]=65; r[230]=90; r[231]=32; + r[232]=36; r[233]=95; r[234]=45; r[235]=46; r[236]=48; r[237]=57; r[238]=65; r[239]=90; + r[240]=32; r[241]=36; r[242]=95; r[243]=45; r[244]=46; r[245]=48; r[246]=57; r[247]=65; + r[248]=90; r[249]=32; r[250]=36; r[251]=95; r[252]=45; r[253]=46; r[254]=48; r[255]=57; + r[256]=65; r[257]=90; r[258]=32; r[259]=36; r[260]=95; r[261]=45; r[262]=46; r[263]=48; + r[264]=57; r[265]=65; r[266]=90; r[267]=32; r[268]=36; r[269]=95; r[270]=45; r[271]=46; + r[272]=48; r[273]=57; r[274]=65; r[275]=90; r[276]=32; r[277]=36; r[278]=95; r[279]=45; + r[280]=46; r[281]=48; r[282]=57; r[283]=65; r[284]=90; r[285]=32; r[286]=36; r[287]=95; + r[288]=45; r[289]=46; r[290]=48; r[291]=57; r[292]=65; r[293]=90; r[294]=32; r[295]=36; + r[296]=95; r[297]=45; r[298]=46; r[299]=48; r[300]=57; r[301]=65; r[302]=90; r[303]=32; + r[304]=36; r[305]=95; r[306]=45; r[307]=46; r[308]=48; r[309]=57; r[310]=65; r[311]=90; + r[312]=32; r[313]=36; r[314]=95; r[315]=45; r[316]=46; r[317]=48; r[318]=57; r[319]=65; + r[320]=90; r[321]=32; r[322]=36; r[323]=95; r[324]=45; r[325]=46; r[326]=48; r[327]=57; + r[328]=65; r[329]=90; r[330]=32; r[331]=36; r[332]=95; r[333]=45; r[334]=46; r[335]=48; + r[336]=57; r[337]=65; r[338]=90; r[339]=32; r[340]=36; r[341]=95; r[342]=45; r[343]=46; + r[344]=48; r[345]=57; r[346]=65; r[347]=90; r[348]=32; r[349]=0; +} + +private static char[] create__http_parser_trans_keys( ) +{ + char[] r = new char[350]; + init__http_parser_trans_keys_0( r ); + return r; +} + +private static final char _http_parser_trans_keys[] = create__http_parser_trans_keys(); + + +private static void init__http_parser_single_lengths_0( byte[] r ) +{ + r[0]=0; r[1]=2; r[2]=3; r[3]=4; r[4]=1; r[5]=1; r[6]=1; r[7]=1; + r[8]=1; r[9]=1; r[10]=0; r[11]=1; r[12]=0; r[13]=1; r[14]=1; r[15]=4; + r[16]=1; r[17]=4; r[18]=2; r[19]=1; r[20]=2; r[21]=5; r[22]=0; r[23]=0; + r[24]=7; r[25]=0; r[26]=0; r[27]=6; r[28]=0; r[29]=0; r[30]=5; r[31]=5; + r[32]=0; r[33]=0; r[34]=3; r[35]=3; r[36]=3; r[37]=3; r[38]=3; r[39]=3; + r[40]=3; r[41]=3; r[42]=3; r[43]=3; r[44]=3; r[45]=3; r[46]=3; r[47]=3; + r[48]=3; r[49]=3; r[50]=3; r[51]=3; r[52]=1; r[53]=0; +} + +private static byte[] create__http_parser_single_lengths( ) +{ + byte[] r = new byte[54]; + init__http_parser_single_lengths_0( r ); + return r; +} + +private static final byte _http_parser_single_lengths[] = create__http_parser_single_lengths(); + + +private static void init__http_parser_range_lengths_0( byte[] r ) +{ + r[0]=0; r[1]=3; r[2]=3; r[3]=3; r[4]=0; r[5]=0; r[6]=0; r[7]=0; + r[8]=0; r[9]=0; r[10]=1; r[11]=1; r[12]=1; r[13]=1; r[14]=0; r[15]=6; + r[16]=0; r[17]=6; r[18]=0; r[19]=0; r[20]=4; r[21]=2; r[22]=3; r[23]=3; + r[24]=2; r[25]=3; r[26]=3; r[27]=2; r[28]=3; r[29]=3; r[30]=2; r[31]=2; + r[32]=3; r[33]=3; r[34]=3; r[35]=3; r[36]=3; r[37]=3; r[38]=3; r[39]=3; + r[40]=3; r[41]=3; r[42]=3; r[43]=3; r[44]=3; r[45]=3; r[46]=3; r[47]=3; + r[48]=3; r[49]=3; r[50]=3; r[51]=3; r[52]=0; r[53]=0; +} + +private static byte[] create__http_parser_range_lengths( ) +{ + byte[] r = new byte[54]; + init__http_parser_range_lengths_0( r ); + return r; +} + +private static final byte _http_parser_range_lengths[] = create__http_parser_range_lengths(); + + +private static void init__http_parser_index_offsets_0( short[] r ) +{ + r[0]=0; r[1]=0; r[2]=6; r[3]=13; r[4]=21; r[5]=23; r[6]=25; r[7]=27; + r[8]=29; r[9]=31; r[10]=33; r[11]=35; r[12]=38; r[13]=40; r[14]=43; r[15]=45; + r[16]=56; r[17]=58; r[18]=69; r[19]=72; r[20]=74; r[21]=81; r[22]=89; r[23]=93; + r[24]=97; r[25]=107; r[26]=111; r[27]=115; r[28]=124; r[29]=128; r[30]=132; r[31]=140; + r[32]=148; r[33]=152; r[34]=156; r[35]=163; r[36]=170; r[37]=177; r[38]=184; r[39]=191; + r[40]=198; r[41]=205; r[42]=212; r[43]=219; r[44]=226; r[45]=233; r[46]=240; r[47]=247; + r[48]=254; r[49]=261; r[50]=268; r[51]=275; r[52]=282; r[53]=284; +} + +private static short[] create__http_parser_index_offsets( ) +{ + short[] r = new short[54]; + init__http_parser_index_offsets_0( r ); + return r; +} + +private static final short _http_parser_index_offsets[] = create__http_parser_index_offsets(); + + +private static void init__http_parser_indicies_0( byte[] r ) +{ + r[0]=0; r[1]=0; r[2]=0; r[3]=0; r[4]=0; r[5]=1; r[6]=2; r[7]=3; + r[8]=3; r[9]=3; r[10]=3; r[11]=3; r[12]=1; r[13]=4; r[14]=5; r[15]=6; + r[16]=7; r[17]=5; r[18]=5; r[19]=5; r[20]=1; r[21]=8; r[22]=1; r[23]=9; + r[24]=1; r[25]=10; r[26]=1; r[27]=11; r[28]=1; r[29]=12; r[30]=1; r[31]=13; + r[32]=1; r[33]=14; r[34]=1; r[35]=15; r[36]=14; r[37]=1; r[38]=16; r[39]=1; + r[40]=17; r[41]=16; r[42]=1; r[43]=18; r[44]=1; r[45]=19; r[46]=20; r[47]=20; + r[48]=20; r[49]=20; r[50]=20; r[51]=20; r[52]=20; r[53]=20; r[54]=20; r[55]=1; + r[56]=21; r[57]=1; r[58]=22; r[59]=23; r[60]=22; r[61]=22; r[62]=22; r[63]=22; + r[64]=22; r[65]=22; r[66]=22; r[67]=22; r[68]=1; r[69]=25; r[70]=26; r[71]=24; + r[72]=25; r[73]=27; r[74]=28; r[75]=29; r[76]=28; r[77]=28; r[78]=28; r[79]=28; + r[80]=1; r[81]=8; r[82]=30; r[83]=1; r[84]=1; r[85]=1; r[86]=1; r[87]=1; + r[88]=29; r[89]=31; r[90]=31; r[91]=31; r[92]=1; r[93]=29; r[94]=29; r[95]=29; + r[96]=1; r[97]=32; r[98]=34; r[99]=35; r[100]=1; r[101]=1; r[102]=36; r[103]=1; + r[104]=1; r[105]=1; r[106]=33; r[107]=37; r[108]=37; r[109]=37; r[110]=1; r[111]=33; + r[112]=33; r[113]=33; r[114]=1; r[115]=8; r[116]=39; r[117]=1; r[118]=1; r[119]=40; + r[120]=1; r[121]=1; r[122]=1; r[123]=38; r[124]=41; r[125]=41; r[126]=41; r[127]=1; + r[128]=38; r[129]=38; r[130]=38; r[131]=1; r[132]=42; r[133]=44; r[134]=1; r[135]=1; + r[136]=1; r[137]=1; r[138]=1; r[139]=43; r[140]=45; r[141]=47; r[142]=1; r[143]=1; + r[144]=1; r[145]=1; r[146]=1; r[147]=46; r[148]=48; r[149]=48; r[150]=48; r[151]=1; + r[152]=46; r[153]=46; r[154]=46; r[155]=1; r[156]=2; r[157]=49; r[158]=49; r[159]=49; + r[160]=49; r[161]=49; r[162]=1; r[163]=2; r[164]=50; r[165]=50; r[166]=50; r[167]=50; + r[168]=50; r[169]=1; r[170]=2; r[171]=51; r[172]=51; r[173]=51; r[174]=51; r[175]=51; + r[176]=1; r[177]=2; r[178]=52; r[179]=52; r[180]=52; r[181]=52; r[182]=52; r[183]=1; + r[184]=2; r[185]=53; r[186]=53; r[187]=53; r[188]=53; r[189]=53; r[190]=1; r[191]=2; + r[192]=54; r[193]=54; r[194]=54; r[195]=54; r[196]=54; r[197]=1; r[198]=2; r[199]=55; + r[200]=55; r[201]=55; r[202]=55; r[203]=55; r[204]=1; r[205]=2; r[206]=56; r[207]=56; + r[208]=56; r[209]=56; r[210]=56; r[211]=1; r[212]=2; r[213]=57; r[214]=57; r[215]=57; + r[216]=57; r[217]=57; r[218]=1; r[219]=2; r[220]=58; r[221]=58; r[222]=58; r[223]=58; + r[224]=58; r[225]=1; r[226]=2; r[227]=59; r[228]=59; r[229]=59; r[230]=59; r[231]=59; + r[232]=1; r[233]=2; r[234]=60; r[235]=60; r[236]=60; r[237]=60; r[238]=60; r[239]=1; + r[240]=2; r[241]=61; r[242]=61; r[243]=61; r[244]=61; r[245]=61; r[246]=1; r[247]=2; + r[248]=62; r[249]=62; r[250]=62; r[251]=62; r[252]=62; r[253]=1; r[254]=2; r[255]=63; + r[256]=63; r[257]=63; r[258]=63; r[259]=63; r[260]=1; r[261]=2; r[262]=64; r[263]=64; + r[264]=64; r[265]=64; r[266]=64; r[267]=1; r[268]=2; r[269]=65; r[270]=65; r[271]=65; + r[272]=65; r[273]=65; r[274]=1; r[275]=2; r[276]=66; r[277]=66; r[278]=66; r[279]=66; + r[280]=66; r[281]=1; r[282]=2; r[283]=1; r[284]=1; r[285]=0; +} + +private static byte[] create__http_parser_indicies( ) +{ + byte[] r = new byte[286]; + init__http_parser_indicies_0( r ); + return r; +} + +private static final byte _http_parser_indicies[] = create__http_parser_indicies(); + + +private static void init__http_parser_trans_targs_wi_0( byte[] r ) +{ + r[0]=2; r[1]=0; r[2]=3; r[3]=34; r[4]=4; r[5]=20; r[6]=24; r[7]=21; + r[8]=5; r[9]=6; r[10]=7; r[11]=8; r[12]=9; r[13]=10; r[14]=11; r[15]=12; + r[16]=13; r[17]=14; r[18]=15; r[19]=16; r[20]=17; r[21]=53; r[22]=17; r[23]=18; + r[24]=19; r[25]=14; r[26]=18; r[27]=19; r[28]=20; r[29]=21; r[30]=22; r[31]=23; + r[32]=5; r[33]=24; r[34]=25; r[35]=27; r[36]=30; r[37]=26; r[38]=27; r[39]=28; + r[40]=30; r[41]=29; r[42]=5; r[43]=31; r[44]=32; r[45]=5; r[46]=31; r[47]=32; + r[48]=33; r[49]=35; r[50]=36; r[51]=37; r[52]=38; r[53]=39; r[54]=40; r[55]=41; + r[56]=42; r[57]=43; r[58]=44; r[59]=45; r[60]=46; r[61]=47; r[62]=48; r[63]=49; + r[64]=50; r[65]=51; r[66]=52; +} + +private static byte[] create__http_parser_trans_targs_wi( ) +{ + byte[] r = new byte[67]; + init__http_parser_trans_targs_wi_0( r ); + return r; +} + +private static final byte _http_parser_trans_targs_wi[] = create__http_parser_trans_targs_wi(); + + +private static void init__http_parser_trans_actions_wi_0( byte[] r ) +{ + r[0]=1; r[1]=0; r[2]=11; r[3]=0; r[4]=1; r[5]=1; r[6]=1; r[7]=1; + r[8]=13; r[9]=1; r[10]=0; r[11]=0; r[12]=0; r[13]=0; r[14]=0; r[15]=0; + r[16]=0; r[17]=17; r[18]=0; r[19]=0; r[20]=3; r[21]=21; r[22]=0; r[23]=5; + r[24]=7; r[25]=9; r[26]=7; r[27]=0; r[28]=0; r[29]=0; r[30]=0; r[31]=0; + r[32]=26; r[33]=0; r[34]=0; r[35]=19; r[36]=19; r[37]=0; r[38]=0; r[39]=0; + r[40]=0; r[41]=0; r[42]=29; r[43]=15; r[44]=15; r[45]=23; r[46]=0; r[47]=0; + r[48]=0; r[49]=0; r[50]=0; r[51]=0; r[52]=0; r[53]=0; r[54]=0; r[55]=0; + r[56]=0; r[57]=0; r[58]=0; r[59]=0; r[60]=0; r[61]=0; r[62]=0; r[63]=0; + r[64]=0; r[65]=0; r[66]=0; +} + +private static byte[] create__http_parser_trans_actions_wi( ) +{ + byte[] r = new byte[67]; + init__http_parser_trans_actions_wi_0( r ); + return r; +} + +private static final byte _http_parser_trans_actions_wi[] = create__http_parser_trans_actions_wi(); + + +static final int http_parser_start = 1; + +static final int http_parser_first_final = 53; + +static final int http_parser_error = 0; + +// line 108 "src/java/org/jruby/mongrel/http11_parser.rl" + + public static interface ElementCB { + public void call(Object data, int at, int length); + } + + public static interface FieldCB { + public void call(Object data, int field, int flen, int value, int vlen); + } + + public static class HttpParser { + int cs; + int body_start; + int content_len; + int nread; + int mark; + int field_start; + int field_len; + int query_start; + + Object data; + ByteList buffer; + + public FieldCB http_field; + public ElementCB request_method; + public ElementCB request_uri; + public ElementCB request_path; + public ElementCB query_string; + public ElementCB http_version; + public ElementCB header_done; + + public void init() { + cs = 0; + + +// line 314 "src/java/org/jruby/mongrel/Http11Parser.java" + { + cs = http_parser_start; + } +// line 142 "src/java/org/jruby/mongrel/http11_parser.rl" + + body_start = 0; + content_len = 0; + mark = 0; + nread = 0; + field_len = 0; + field_start = 0; + } + } + + public final HttpParser parser = new HttpParser(); + + public int execute(ByteList buffer, int off) { + int p, pe; + int cs = parser.cs; + int len = buffer.realSize; + assert off<=len : "offset past end of buffer"; + + p = off; + pe = len; + byte[] data = buffer.bytes; + parser.buffer = buffer; + + +// line 343 "src/java/org/jruby/mongrel/Http11Parser.java" + { + int _klen; + int _trans; + int _acts; + int _nacts; + int _keys; + + if ( p != pe ) { + _resume: while ( true ) { + _again: do { + if ( cs == 0 ) + break _resume; + _match: do { + _keys = _http_parser_key_offsets[cs]; + _trans = _http_parser_index_offsets[cs]; + _klen = _http_parser_single_lengths[cs]; + if ( _klen > 0 ) { + int _lower = _keys; + int _mid; + int _upper = _keys + _klen - 1; + while (true) { + if ( _upper < _lower ) + break; + + _mid = _lower + ((_upper-_lower) >> 1); + if ( data[p] < _http_parser_trans_keys[_mid] ) + _upper = _mid - 1; + else if ( data[p] > _http_parser_trans_keys[_mid] ) + _lower = _mid + 1; + else { + _trans += (_mid - _keys); + break _match; + } + } + _keys += _klen; + _trans += _klen; + } + + _klen = _http_parser_range_lengths[cs]; + if ( _klen > 0 ) { + int _lower = _keys; + int _mid; + int _upper = _keys + (_klen<<1) - 2; + while (true) { + if ( _upper < _lower ) + break; + + _mid = _lower + (((_upper-_lower) >> 1) & ~1); + if ( data[p] < _http_parser_trans_keys[_mid] ) + _upper = _mid - 2; + else if ( data[p] > _http_parser_trans_keys[_mid+1] ) + _lower = _mid + 2; + else { + _trans += ((_mid - _keys)>>1); + break _match; + } + } + _trans += _klen; + } + } while (false); + + _trans = _http_parser_indicies[_trans]; + cs = _http_parser_trans_targs_wi[_trans]; + + if ( _http_parser_trans_actions_wi[_trans] == 0 ) + break _again; + + _acts = _http_parser_trans_actions_wi[_trans]; + _nacts = (int) _http_parser_actions[_acts++]; + while ( _nacts-- > 0 ) + { + switch ( _http_parser_actions[_acts++] ) + { + case 0: +// line 11 "src/java/org/jruby/mongrel/http11_parser.rl" + {parser.mark = p; } + break; + case 1: +// line 13 "src/java/org/jruby/mongrel/http11_parser.rl" + { parser.field_start = p; } + break; + case 2: +// line 14 "src/java/org/jruby/mongrel/http11_parser.rl" + { + parser.field_len = p-parser.field_start; + } + break; + case 3: +// line 18 "src/java/org/jruby/mongrel/http11_parser.rl" + { parser.mark = p; } + break; + case 4: +// line 19 "src/java/org/jruby/mongrel/http11_parser.rl" + { + if(parser.http_field != null) { + parser.http_field.call(parser.data, parser.field_start, parser.field_len, parser.mark, p-parser.mark); + } + } + break; + case 5: +// line 24 "src/java/org/jruby/mongrel/http11_parser.rl" + { + if(parser.request_method != null) + parser.request_method.call(parser.data, parser.mark, p-parser.mark); + } + break; + case 6: +// line 28 "src/java/org/jruby/mongrel/http11_parser.rl" + { + if(parser.request_uri != null) + parser.request_uri.call(parser.data, parser.mark, p-parser.mark); + } + break; + case 7: +// line 33 "src/java/org/jruby/mongrel/http11_parser.rl" + {parser.query_start = p; } + break; + case 8: +// line 34 "src/java/org/jruby/mongrel/http11_parser.rl" + { + if(parser.query_string != null) + parser.query_string.call(parser.data, parser.query_start, p-parser.query_start); + } + break; + case 9: +// line 39 "src/java/org/jruby/mongrel/http11_parser.rl" + { + if(parser.http_version != null) + parser.http_version.call(parser.data, parser.mark, p-parser.mark); + } + break; + case 10: +// line 44 "src/java/org/jruby/mongrel/http11_parser.rl" + { + if(parser.request_path != null) + parser.request_path.call(parser.data, parser.mark, p-parser.mark); + } + break; + case 11: +// line 49 "src/java/org/jruby/mongrel/http11_parser.rl" + { + parser.body_start = p + 1; + if(parser.header_done != null) + parser.header_done.call(parser.data, p + 1, pe - p - 1); + if (true) break _resume; + } + break; +// line 491 "src/java/org/jruby/mongrel/Http11Parser.java" + } + } + + } while (false); + if ( ++p == pe ) + break _resume; + } + } + } +// line 166 "src/java/org/jruby/mongrel/http11_parser.rl" + + parser.cs = cs; + parser.nread += (p - off); + + assert p <= pe : "buffer overflow after parsing execute"; + assert parser.nread <= len : "nread longer than length"; + assert parser.body_start <= len : "body starts after buffer end"; + assert parser.mark < len : "mark is after buffer end"; + assert parser.field_len <= len : "field has length longer than whole buffer"; + assert parser.field_start < len : "field starts after buffer end"; + + if(parser.body_start>0) { + /* final \r\n combo encountered so stop right here */ + +// line 516 "src/java/org/jruby/mongrel/Http11Parser.java" +// line 180 "src/java/org/jruby/mongrel/http11_parser.rl" + parser.nread++; + } + + return parser.nread; + } + + public int finish() { + int cs = parser.cs; + + +// line 528 "src/java/org/jruby/mongrel/Http11Parser.java" +// line 190 "src/java/org/jruby/mongrel/http11_parser.rl" + + parser.cs = cs; + + if(has_error()) { + return -1; + } else if(is_finished()) { + return 1; + } else { + return 0; + } + } + + public boolean has_error() { + return parser.cs == http_parser_error; + } + + public boolean is_finished() { + return parser.cs == http_parser_first_final; + } +} diff --git a/jruby/src/java/org/jruby/mongrel/URIClassifier.java b/jruby/src/java/org/jruby/mongrel/URIClassifier.java new file mode 100644 index 0000000..73ba1ba --- /dev/null +++ b/jruby/src/java/org/jruby/mongrel/URIClassifier.java @@ -0,0 +1,129 @@ +/***** BEGIN LICENSE BLOCK ***** + * Version: CPL 1.0/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Common Public + * License Version 1.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.eclipse.org/legal/cpl-v10.html + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * Copyright (C) 2007 Ola Bini <ola@ologix.com> + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the CPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the CPL, the GPL or the LGPL. + ***** END LICENSE BLOCK *****/ +package org.jruby.mongrel; + +import org.jruby.Ruby; +import org.jruby.RubyArray; +import org.jruby.RubyClass; +import org.jruby.RubyHash; +import org.jruby.RubyModule; +import org.jruby.RubyObject; +import org.jruby.RubyString; + +import org.jruby.runtime.Block; +import org.jruby.runtime.CallbackFactory; +import org.jruby.runtime.ObjectAllocator; +import org.jruby.runtime.builtin.IRubyObject; + +import org.jruby.tst.TernarySearchTree; + +/** + * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a> + */ +public class URIClassifier extends RubyObject { + public final static int TRIE_INCREASE = 30; + + private static ObjectAllocator ALLOCATOR = new ObjectAllocator() { + public IRubyObject allocate(Ruby runtime, RubyClass klass) { + return new URIClassifier(runtime, klass); + } + }; + + public static void createURIClassifier(Ruby runtime, RubyModule mMongrel) { + RubyClass cURIClassifier = mMongrel.defineClassUnder("URIClassifier",runtime.getObject(),ALLOCATOR); + CallbackFactory cf = runtime.callbackFactory(URIClassifier.class); + cURIClassifier.defineFastMethod("initialize",cf.getFastMethod("initialize")); + cURIClassifier.defineFastMethod("register",cf.getFastMethod("register",IRubyObject.class,IRubyObject.class)); + cURIClassifier.defineFastMethod("unregister",cf.getFastMethod("unregister",IRubyObject.class)); + cURIClassifier.defineFastMethod("resolve",cf.getFastMethod("resolve",IRubyObject.class)); + } + + private TernarySearchTree tst; + + public URIClassifier(Ruby runtime, RubyClass clazz) { + super(runtime,clazz); + tst = new TernarySearchTree(TRIE_INCREASE); + } + + public IRubyObject initialize() { + setInstanceVariable("@handler_map",RubyHash.newHash(getRuntime())); + return this; + } + + public IRubyObject register(IRubyObject uri, IRubyObject handler) { + Object[] ptr = new Object[]{null}; + int rc = 0; + rc = tst.insert(uri.toString(),handler,0,ptr); + if(rc == TernarySearchTree.TST_DUPLICATE_KEY) { + throw getRuntime().newStandardError("Handler already registered with that name"); + } else if(rc == TernarySearchTree.TST_ERROR) { + throw getRuntime().newStandardError("Memory error registering handler"); + } else if(rc == TernarySearchTree.TST_NULL_KEY) { + throw getRuntime().newStandardError("URI was empty"); + } + ((RubyHash)getInstanceVariable("@handler_map")).aset(uri,handler); + return getRuntime().getNil(); + } + + public IRubyObject unregister(IRubyObject uri) { + IRubyObject handler = (IRubyObject)tst.delete(uri.toString()); + if(null != handler) { + ((RubyHash)getInstanceVariable("@handler_map")).delete(uri,Block.NULL_BLOCK); + return handler; + } + return getRuntime().getNil(); + } + + public IRubyObject resolve(IRubyObject _ri) { + IRubyObject handler = null; + int[] pref_len = new int[]{0}; + RubyArray result; + String uri_str; + RubyString uri = _ri.convertToString(); + + uri_str = uri.toString(); + handler = (IRubyObject)tst.search(uri_str,pref_len); + + result = getRuntime().newArray(); + + if(handler != null) { + result.append(uri.substr(0, pref_len[0])); + if(pref_len[0] == 1 && uri_str.startsWith("/")) { + result.append(uri); + } else { + result.append(uri.substr(pref_len[0],uri.getByteList().length())); + } + result.append(handler); + } else { + result.append(getRuntime().getNil()); + result.append(getRuntime().getNil()); + result.append(getRuntime().getNil()); + } + return result; + } +}// URIClassifier diff --git a/jruby/src/java/org/jruby/mongrel/http11_parser.rl b/jruby/src/java/org/jruby/mongrel/http11_parser.rl new file mode 100644 index 0000000..5136993 --- /dev/null +++ b/jruby/src/java/org/jruby/mongrel/http11_parser.rl @@ -0,0 +1,209 @@ +package org.jruby.mongrel; + +import org.jruby.util.ByteList; + +public class Http11Parser { + +/** machine **/ +%%{ + machine http_parser; + + action mark {parser.mark = fpc; } + + action start_field { parser.field_start = fpc; } + action write_field { + parser.field_len = fpc-parser.field_start; + } + + action start_value { parser.mark = fpc; } + action write_value { + if(parser.http_field != null) { + parser.http_field.call(parser.data, parser.field_start, parser.field_len, parser.mark, fpc-parser.mark); + } + } + action request_method { + if(parser.request_method != null) + parser.request_method.call(parser.data, parser.mark, fpc-parser.mark); + } + action request_uri { + if(parser.request_uri != null) + parser.request_uri.call(parser.data, parser.mark, fpc-parser.mark); + } + + action start_query {parser.query_start = fpc; } + action query_string { + if(parser.query_string != null) + parser.query_string.call(parser.data, parser.query_start, fpc-parser.query_start); + } + + action http_version { + if(parser.http_version != null) + parser.http_version.call(parser.data, parser.mark, fpc-parser.mark); + } + + action request_path { + if(parser.request_path != null) + parser.request_path.call(parser.data, parser.mark, fpc-parser.mark); + } + + action done { + parser.body_start = fpc + 1; + if(parser.header_done != null) + parser.header_done.call(parser.data, fpc + 1, pe - fpc - 1); + fbreak; + } + + +#### HTTP PROTOCOL GRAMMAR +# line endings + CRLF = "\r\n"; + +# character types + CTL = (cntrl | 127); + safe = ("$" | "-" | "_" | "."); + extra = ("!" | "*" | "'" | "(" | ")" | ","); + reserved = (";" | "/" | "?" | ":" | "@" | "&" | "=" | "+"); + unsafe = (CTL | " " | "\"" | "#" | "%" | "<" | ">"); + national = any -- (alpha | digit | reserved | extra | safe | unsafe); + unreserved = (alpha | digit | safe | extra | national); + escape = ("%" xdigit xdigit); + uchar = (unreserved | escape); + pchar = (uchar | ":" | "@" | "&" | "=" | "+"); + tspecials = ("(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\\" | "\"" | "/" | "[" | "]" | "?" | "=" | "{" | "}" | " " | "\t"); + +# elements + token = (ascii -- (CTL | tspecials)); + +# URI schemes and absolute paths + scheme = ( alpha | digit | "+" | "-" | "." )* ; + absolute_uri = (scheme ":" (uchar | reserved )*); + + path = (pchar+ ( "/" pchar* )*) ; + query = ( uchar | reserved )* %query_string ; + param = ( pchar | "/" )* ; + params = (param ( ";" param )*) ; + rel_path = (path? %request_path (";" params)?) ("?" %start_query query)?; + absolute_path = ("/"+ rel_path); + + Request_URI = ("*" | absolute_uri | absolute_path) >mark %request_uri; + Method = (upper | digit | safe){1,20} >mark %request_method; + + http_number = (digit+ "." digit+) ; + HTTP_Version = ("HTTP/" http_number) >mark %http_version ; + Request_Line = (Method " " Request_URI " " HTTP_Version CRLF) ; + + field_name = (token -- ":")+ >start_field %write_field; + + field_value = any* >start_value %write_value; + + message_header = field_name ":" " "* field_value :> CRLF; + + Request = Request_Line (message_header)* ( CRLF @done); + +main := Request; +}%% + +/** Data **/ +%% write data; + + public static interface ElementCB { + public void call(Object data, int at, int length); + } + + public static interface FieldCB { + public void call(Object data, int field, int flen, int value, int vlen); + } + + public static class HttpParser { + int cs; + int body_start; + int content_len; + int nread; + int mark; + int field_start; + int field_len; + int query_start; + + Object data; + ByteList buffer; + + public FieldCB http_field; + public ElementCB request_method; + public ElementCB request_uri; + public ElementCB request_path; + public ElementCB query_string; + public ElementCB http_version; + public ElementCB header_done; + + public void init() { + cs = 0; + + %% write init; + + body_start = 0; + content_len = 0; + mark = 0; + nread = 0; + field_len = 0; + field_start = 0; + } + } + + public final HttpParser parser = new HttpParser(); + + public int execute(ByteList buffer, int off) { + int p, pe; + int cs = parser.cs; + int len = buffer.realSize; + assert off<=len : "offset past end of buffer"; + + p = off; + pe = len; + byte[] data = buffer.bytes; + parser.buffer = buffer; + + %% write exec; + + parser.cs = cs; + parser.nread += (p - off); + + assert p <= pe : "buffer overflow after parsing execute"; + assert parser.nread <= len : "nread longer than length"; + assert parser.body_start <= len : "body starts after buffer end"; + assert parser.mark < len : "mark is after buffer end"; + assert parser.field_len <= len : "field has length longer than whole buffer"; + assert parser.field_start < len : "field starts after buffer end"; + + if(parser.body_start>0) { + /* final \r\n combo encountered so stop right here */ + %%write eof; + parser.nread++; + } + + return parser.nread; + } + + public int finish() { + int cs = parser.cs; + + %%write eof; + + parser.cs = cs; + + if(has_error()) { + return -1; + } else if(is_finished()) { + return 1; + } else { + return 0; + } + } + + public boolean has_error() { + return parser.cs == http_parser_error; + } + + public boolean is_finished() { + return parser.cs == http_parser_first_final; + } +} diff --git a/jruby/src/java/org/jruby/tst/Node.java b/jruby/src/java/org/jruby/tst/Node.java new file mode 100644 index 0000000..334763d --- /dev/null +++ b/jruby/src/java/org/jruby/tst/Node.java @@ -0,0 +1,18 @@ +/* + * See LICENSE file. + */ +/** + * $Id: $ + */ +package org.jruby.tst; + +/** + * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a> + * @version $Revision: $ + */ +public class Node { + public char value; + public Node left; + public Object middle; + public Node right; +}// Node diff --git a/jruby/src/java/org/jruby/tst/NodeLines.java b/jruby/src/java/org/jruby/tst/NodeLines.java new file mode 100644 index 0000000..87b56d7 --- /dev/null +++ b/jruby/src/java/org/jruby/tst/NodeLines.java @@ -0,0 +1,16 @@ +/* + * See LICENSE file. + */ +/** + * $Id: $ + */ +package org.jruby.tst; + +/** + * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a> + * @version $Revision: $ + */ +public class NodeLines { + public Node[] node_line; + public NodeLines next; +}// NodeLines diff --git a/jruby/src/java/org/jruby/tst/TernarySearchTree.java b/jruby/src/java/org/jruby/tst/TernarySearchTree.java new file mode 100644 index 0000000..5637bf1 --- /dev/null +++ b/jruby/src/java/org/jruby/tst/TernarySearchTree.java @@ -0,0 +1,433 @@ +/* + * See LICENSE file. + */ +/** + * $Id: $ + */ +package org.jruby.tst; + +/** + * @author <a href="mailto:ola.bini@ki.se">Ola Bini</a> + * @version $Revision: $ + */ +public class TernarySearchTree { + public final static int TST_OK = 0x1; + public final static int TST_ERROR = 0x2; + public final static int TST_NULL_KEY = 0x4; + public final static int TST_DUPLICATE_KEY = 0x8; + public final static int TST_REPLACE = 0x10; + + public int node_line_width; + public NodeLines node_lines; + public Node free_list; + public Node[] head = new Node[127]; + + public TernarySearchTree(final int width) { + this.node_lines = new NodeLines(); + this.node_line_width = width; + + this.node_lines.next = null; + this.node_lines.node_line = new Node[width]; + for(int i=0;i<width;i++) { + this.node_lines.node_line[i] = new Node(); + } + + Node current_node = this.node_lines.node_line[0]; + this.free_list = current_node; + + for(int i=1; i<width; i++) { + current_node.middle = this.node_lines.node_line[i]; + current_node = (Node)current_node.middle; + } + current_node.middle = null; + } + + public int insert(final String key, final Object data, final int option, final Object[] exist_ptr) { + Node current_node = null; + Node new_node_tree_begin = null; + int key_index = 0; + boolean perform_loop = true; + + if(null == key || key.length() == 0) { + return TST_NULL_KEY; + } + + if(this.head[key.charAt(0)] == null) { + if(this.free_list == null) { + if(!grow_node_free_list()) { + return TST_ERROR; + } + } + this.head[key.charAt(0)] = this.free_list; + this.free_list = (Node)this.free_list.middle; + current_node = this.head[key.charAt(0)]; + + if(key.length() == 1) { + current_node.value = 0; + current_node.middle = data; + return TST_OK; + } else { + current_node.value = key.charAt(1); + perform_loop = false; + } + } + + current_node = this.head[key.charAt(0)]; + key_index = 1; + char curr = 0; + while(perform_loop) { + if(key_index < key.length()) { + curr = key.charAt(key_index); + } else { + curr = 0; + } + + if(curr == current_node.value) { + if(curr == 0) { + if(option == TST_REPLACE) { + if(exist_ptr.length > 0) { + exist_ptr[0] = current_node.middle; + } + current_node.middle = data; + return TST_OK; + } else { + if(exist_ptr.length > 0) { + exist_ptr[0] = current_node.middle; + } + return TST_DUPLICATE_KEY; + } + + } else { + if(current_node.middle == null) { + if(this.free_list == null) { + if(!grow_node_free_list()) { + return TST_ERROR; + } + } + current_node.middle = this.free_list; + this.free_list = (Node)this.free_list.middle; + new_node_tree_begin = current_node; + current_node = (Node)current_node.middle; + current_node.value = curr; + break; + } else { + current_node = (Node)current_node.middle; + key_index++; + continue; + } + } + } + + if((current_node.value == 0 && curr < 64) || + (current_node.value != 0 && curr < current_node.value)) { + if(current_node.left == null) { + if(this.free_list == null) { + if(!grow_node_free_list()) { + return TST_ERROR; + } + } + current_node.left = this.free_list; + this.free_list = (Node)this.free_list.middle; + new_node_tree_begin = current_node; + current_node = current_node.left; + current_node.value = curr; + if(curr == 0) { + current_node.middle = data; + return TST_OK; + } else { + break; + } + } else { + current_node = current_node.left; + continue; + } + } else { + if(current_node.right == null) { + if(this.free_list == null) { + if(!grow_node_free_list()) { + return TST_ERROR; + } + } + current_node.right = this.free_list; + this.free_list = (Node)this.free_list.middle; + new_node_tree_begin = current_node; + current_node = current_node.right; + current_node.value = curr; + break; + } else { + current_node = current_node.right; + continue; + } + } + } + + do { + key_index++; + if(key_index < key.length()) { + curr = key.charAt(key_index); + } else { + curr = 0; + } + + if(this.free_list == null) { + if(!grow_node_free_list()) { + current_node = (Node)new_node_tree_begin.middle; + while(current_node.middle != null) { + current_node = (Node)current_node.middle; + } + current_node.middle = this.free_list; + this.free_list = (Node)new_node_tree_begin.middle; + new_node_tree_begin.middle = null; + return TST_ERROR; + } + } + + if(this.free_list == null) { + if(!grow_node_free_list()) { + return TST_ERROR; + } + } + current_node.middle = this.free_list; + this.free_list = (Node)this.free_list.middle; + current_node = (Node)current_node.middle; + + current_node.value = curr; + } while(curr != 0); + + current_node.middle = data; + return TST_OK; + } + + public Object search(final String key, final int[] prefix_len) { + Node current_node = null; + Object longest_match = null; + int key_index = 0; + + if(key == null) { + throw new IllegalArgumentException("key can't be null"); + } + + if(key.length() == 0 || this.head[key.charAt(0)] == null) { + return null; + } + + if(prefix_len.length > 0) { + prefix_len[0] = 0; + } + + current_node = this.head[key.charAt(0)]; + key_index = 1; + + char curr = 0; + while(null != current_node) { + if(key_index < key.length()) { + curr = key.charAt(key_index); + } else { + curr = 0; + } + + if(curr == current_node.value) { + if(current_node.value == 0) { + if(prefix_len.length>0) { + prefix_len[0] = key_index; + } + return current_node.middle; + } else { + current_node = (Node)current_node.middle; + if(current_node != null && current_node.value == 0) { + if(prefix_len.length>0) { + prefix_len[0] = key_index+1; + } + longest_match = current_node.middle; + } + key_index++; + continue; + } + } else if((current_node.value == 0 && curr < 64) || + (current_node.value != 0 && curr < current_node.value)) { + if(current_node.value == 0) { + if(prefix_len.length>0) { + prefix_len[0] = key_index; + } + longest_match = current_node.middle; + } + current_node = current_node.left; + continue; + } else { + if(current_node.value == 0) { + if(prefix_len.length>0) { + prefix_len[0] = key_index; + } + longest_match = current_node.middle; + } + current_node = current_node.right; + continue; + } + } + + return longest_match; + } + + public Object delete(final String key) { + Node current_node = null; + Node current_node_parent = null; + Node last_branch = null; + Node last_branch_parent = null; + Object next_node = null; + Node last_branch_replacement = null; + Node last_branch_dangling_child = null; + int key_index = 1; + + if(key.length() == 0 || this.head[key.charAt(0)] == null) { + return null; + } + + current_node = this.head[key.charAt(0)]; + + char curr = 0; + while(null != current_node) { + if(key_index < key.length()) { + curr = key.charAt(key_index); + } else { + curr = 0; + } + if(curr == current_node.value) { + if(current_node.left != null || current_node.right != null) { + last_branch = current_node; + last_branch_parent = current_node_parent; + } + if(curr == 0) { + break; + } + current_node_parent = current_node; + current_node = (Node)current_node.middle; + key_index++; + continue; + } else if((current_node.value == 0 && curr < 64) || + (current_node.value != 0&& curr < current_node.value)) { + last_branch_parent = current_node; + current_node_parent = current_node; + current_node = current_node.left; + last_branch = current_node; + continue; + } else { + last_branch_parent = current_node; + current_node_parent = current_node; + current_node = current_node.right; + last_branch = current_node; + continue; + } + } + + if(null == current_node) { + return null; + } + + if(null == last_branch) { + next_node = this.head[key.charAt(0)]; + this.head[key.charAt(0)] = null; + } else if(last_branch.left == null && last_branch.right == null) { + if(last_branch_parent.left == last_branch) { + last_branch_parent.left = null; + } else { + last_branch_parent.right = null; + } + next_node = last_branch; + } else { + if(last_branch.left != null && last_branch.right != null) { + last_branch_replacement = last_branch.right; + last_branch_dangling_child = last_branch.left; + } else if(last_branch.right != null) { + last_branch_replacement = last_branch.right; + last_branch_dangling_child = null; + } else { + last_branch_replacement = last_branch.left; + last_branch_dangling_child = null; + } + + if(last_branch_parent == null) { + this.head[key.charAt(0)] = last_branch_replacement; + } else { + if(last_branch_parent.left == last_branch) { + last_branch_parent.left = last_branch_replacement; + } else if(last_branch_parent.right == last_branch) { + last_branch_parent.right = last_branch_replacement; + } else { + last_branch_parent.middle = last_branch_replacement; + } + } + + if(last_branch_dangling_child != null) { + current_node = last_branch_replacement; + while(current_node.left != null) { + current_node = current_node.left; + } + current_node.left = last_branch_dangling_child; + } + + next_node = last_branch; + } + + do { + current_node = (Node)next_node; + next_node = current_node.middle; + current_node.left = null; + current_node.right = null; + current_node.middle = this.free_list; + this.free_list = current_node; + } while(current_node.value != 0); + + + return next_node; + } + + public boolean grow_node_free_list() { + Node current_node = null; + NodeLines new_line = null; + + new_line = new NodeLines(); + + new_line.node_line = new Node[this.node_line_width]; + for(int i=0;i<this.node_line_width;i++) { + new_line.node_line[i] = new Node(); + } + + new_line.next = this.node_lines; + this.node_lines = new_line; + + current_node = this.node_lines.node_line[0]; + this.free_list = current_node; + for(int i=1;i<this.node_line_width;i++) { + current_node.middle = this.node_lines.node_line[i]; + current_node = (Node)current_node.middle; + } + current_node.middle = null; + + return true; + } + + + public static void main(final String[] args) { + final TernarySearchTree tst = new TernarySearchTree(30); + int ret = tst.insert("fOO","VAL1",0, new Object[0]); + System.err.println("ret: " + ret); + ret = tst.insert("bar","VAL2",0, new Object[0]); + System.err.println("ret: " + ret); + ret = tst.insert("baz","VAL3",0, new Object[0]); + System.err.println("ret: " + ret); + ret = tst.insert("zydsfgfd","VAL4",0, new Object[0]); + System.err.println("ret: " + ret); + ret = tst.insert("1242","VAL5",0, new Object[0]); + System.err.println("ret: " + ret); + + Object val = tst.delete("fOO"); + System.err.println("del: " + val); + + int[] pref_len = new int[1]; + pref_len[0] = 0; + val = tst.search("ba",pref_len); + System.err.println("search: " + val + " pref_len: " + pref_len[0]); + val = tst.search("bar",pref_len); + System.err.println("search: " + val + " pref_len: " + pref_len[0]); + } +}// TernarySearchTree diff --git a/jruby/src/test/testParser.rb b/jruby/src/test/testParser.rb new file mode 100644 index 0000000..476d1b3 --- /dev/null +++ b/jruby/src/test/testParser.rb @@ -0,0 +1,21 @@ +
+require 'http11'
+
+request_data = "GET /events/show/2 HTTP/1.1\r\n\r\nfield1=value1&field2=value2&field3=value3"
+request_data += ("X" * (16 * 1024 - request_data.size))
+
+parser = Mongrel::HttpParser.new
+parser.reset
+params = {}
+nread = parser.execute(params, request_data, 0)
+puts "finished=#{parser.finished?}"
+nread = parser.execute(params, request_data, nread)
+
+puts "params="
+params.each {|k,v| puts " #{k} = #{v}"}
+puts "nread=#{nread}"
+puts "error?=#{parser.error?}"
+puts "finished=#{parser.finished?}"
+puts "nread=#{parser.nread}"
+
+puts request_data[parser.nread..-1]
diff --git a/jruby/src/test/testParserImpl.rb b/jruby/src/test/testParserImpl.rb new file mode 100644 index 0000000..58ef2c7 --- /dev/null +++ b/jruby/src/test/testParserImpl.rb @@ -0,0 +1,22 @@ +require "java"
+
+include_class "org.jruby.mongrel.HttpParserImpl"
+
+request_data = "GET /events/show/2 HTTP/1.1\r\n\r\nfield1=value1&field2=value2&field3=value3"
+request_data += ("X" * (16 * 1024 - request_data.size))
+
+parser = HttpParser.new
+parser.reset
+params = {}
+nread = parser.execute(params, request_data)
+puts "finished=#{parser.finished?}"
+nread = parser.execute(params, request_data)
+
+puts "params="
+params.each {|k,v| puts " #{k} = #{v}"}
+puts "nread=#{nread}"
+puts "error?=#{parser.error?}"
+puts "finished=#{parser.finished?}"
+puts "nread=#{parser.nread}"
+
+puts request_data[parser.nread..-1]
diff --git a/jruby/src/test/test_http11.rb b/jruby/src/test/test_http11.rb new file mode 100644 index 0000000..df4e914 --- /dev/null +++ b/jruby/src/test/test_http11.rb @@ -0,0 +1,119 @@ +# Original work by Zed A. Shaw
+
+require 'test/unit'
+require 'http11'
+require 'benchmark'
+require 'digest/sha1'
+
+include Mongrel
+
+class HttpParserTest < Test::Unit::TestCase
+
+ def test_parse_simple
+ $stderr.puts "test_parse_simple"
+ parser = HttpParser.new
+ req = {}
+ http = "GET / HTTP/1.1\r\n\r\n"
+ nread = parser.execute(req, http, 0)
+ assert nread == http.length, "Failed to parse the full HTTP request"
+ assert parser.finished?, "Parser didn't finish"
+ assert !parser.error?, "Parser had error"
+ assert nread == parser.nread, "Number read returned from execute does not match"
+ parser.reset
+ assert parser.nread == 0, "Number read after reset should be 0"
+ end
+
+
+ def test_parse_error
+ $stderr.puts "test_parse_error"
+ parser = HttpParser.new
+ req = {}
+ bad_http = "GET / SsUTF/1.1"
+
+ error = false
+ begin
+ nread = parser.execute(req, bad_http, 0)
+ rescue => details
+ error = true
+ end
+
+ assert error, "failed to throw exception"
+ assert !parser.finished?, "Parser shouldn't be finished"
+ assert parser.error?, "Parser SHOULD have error"
+ end
+
+ # lame random garbage maker
+ def rand_data(min, max, readable=true)
+ count = min + ((rand(max)+1) *10).to_i
+ res = count.to_s + "/"
+
+ if readable
+ res << Digest::SHA1.hexdigest(rand(count * 1000).to_s) * (count / 40)
+ else
+ res << Digest::SHA1.digest(rand(count * 1000).to_s) * (count / 20)
+ end
+
+ return res
+ end
+
+
+ def test_horrible_queries
+ $stderr.puts "test_horrible_queries"
+ parser = HttpParser.new
+
+ $stderr.puts "test_horrible_queries.first"
+ # first verify that large random get requests fail
+ 20.times do |c|
+ $stderr.write '.'
+ get = "GET /#{rand_data(1024, 1024+(c*1024))} HTTP/1.1\r\n"
+ assert_raises Mongrel::HttpParserError do
+ parser.execute({}, get, 0)
+ parser.reset
+ end
+ end
+
+ $stderr.puts "test_horrible_queries.second"
+ # then that large header names are caught
+ 20.times do |c|
+ $stderr.write '.'
+ get = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-#{rand_data(1024, 1024+(c*1024))}: Test\r\n\r\n"
+ assert_raises Mongrel::HttpParserError do
+ parser.execute({}, get, 0)
+ parser.reset
+ end
+ end
+
+ $stderr.puts "test_horrible_queries.third"
+ # then that large mangled field values are caught
+ 20.times do |c|
+ $stderr.write '.'
+ get = "GET /#{rand_data(10,120)} HTTP/1.1\r\nX-Test: #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n"
+ assert_raises Mongrel::HttpParserError do
+ parser.execute({}, get, 0)
+ parser.reset
+ end
+ end
+
+ $stderr.puts "test_horrible_queries.fourth"
+ # then large headers are rejected too
+ get = "GET /#{rand_data(10,120)} HTTP/1.1\r\n"
+ get << "X-Test: test\r\n" * (80 * 1024)
+ assert_raises Mongrel::HttpParserError do
+ parser.execute({}, get, 0)
+ parser.reset
+ end
+
+ $stderr.puts "test_horrible_queries.fifth"
+ # finally just that random garbage gets blocked all the time
+ 20.times do |c|
+ $stderr.write '.'
+ get = "GET #{rand_data(1024, 1024+(c*1024), false)} #{rand_data(1024, 1024+(c*1024), false)}\r\n\r\n"
+ assert_raises Mongrel::HttpParserError do
+ parser.execute({}, get, 0)
+ parser.reset
+ end
+ end
+
+ end
+end
+
diff --git a/jruby/src/test/test_performance.rb b/jruby/src/test/test_performance.rb new file mode 100644 index 0000000..2bed6f2 --- /dev/null +++ b/jruby/src/test/test_performance.rb @@ -0,0 +1,21 @@ +require 'http11'
+
+include Mongrel
+
+def one_test(pr)
+ req = {}
+ http = "GET / HTTP/1.1\r\n\r\n"
+ nread = pr.execute(req, http, 0)
+ pr.reset
+end
+
+parser = HttpParser.new
+
+before = Time.now
+for n in (1..100000)
+ one_test(parser)
+end
+after = Time.now
+
+puts "Doing 100000 parses took #{after-before} seconds"
+
|