From 55ab3440c48a22cc0e63ced967111f3bd096b32f Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Fri, 27 Mar 2009 02:37:55 -0700 Subject: [PATCH 1/4] removed fastthread dependency and fixed some ruby1.9 syntax issues --- Rakefile | 22 +++++++++++----------- lib/mongrel/handlers.rb | 10 +++++----- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Rakefile b/Rakefile index 41f62809..969c77d4 100644 --- a/Rakefile +++ b/Rakefile @@ -36,14 +36,14 @@ e = Echoe.new("mongrel") do |p| when /mswin/ self.files += ['lib/http11.so'] self.platform = Gem::Platform::CURRENT - add_dependency('cgi_multipart_eof_fix', '>= 2.4') + # add_dependency('cgi_multipart_eof_fix', '>= 2.4') when /java/ self.files += ['lib/http11.jar'] self.platform = 'jruby' # XXX Is this right? else add_dependency('daemons', '>= 1.0.3') - add_dependency('fastthread', '>= 1.0.1') - add_dependency('cgi_multipart_eof_fix', '>= 2.4') + # add_dependency('fastthread', '>= 1.0.1') + # add_dependency('cgi_multipart_eof_fix', '>= 2.4') end end @@ -134,8 +134,8 @@ end desc "Package Mongrel and all subprojects" task :package_all => [:package] do sub_project("gem_plugin", :package) - sub_project("cgi_multipart_eof_fix", :package) - sub_project("fastthread", :package) + # sub_project("cgi_multipart_eof_fix", :package) + # sub_project("fastthread", :package) sub_project("mongrel_status", :package) sub_project("mongrel_upload_progress", :package) sub_project("mongrel_console", :package) @@ -152,8 +152,8 @@ end task :install_requirements do # These run before Mongrel is installed sub_project("gem_plugin", :install) - sub_project("cgi_multipart_eof_fix", :install) - sub_project("fastthread", :install) + # sub_project("cgi_multipart_eof_fix", :install) + # sub_project("fastthread", :install) end desc "for Mongrel and all subprojects" @@ -170,11 +170,11 @@ end desc "for Mongrel and all its subprojects" task :uninstall => [:clean] do sub_project("mongrel_status", :uninstall) - sub_project("cgi_multipart_eof_fix", :uninstall) + # sub_project("cgi_multipart_eof_fix", :uninstall) sub_project("mongrel_upload_progress", :uninstall) sub_project("mongrel_console", :uninstall) sub_project("gem_plugin", :uninstall) - sub_project("fastthread", :uninstall) + # sub_project("fastthread", :uninstall) # sub_project("mongrel_experimental", :uninstall) sub_project("mongrel_service", :uninstall) if RUBY_PLATFORM =~ /mswin/ end @@ -182,8 +182,8 @@ end desc "for Mongrel and all its subprojects" task :clean do sub_project("gem_plugin", :clean) - sub_project("cgi_multipart_eof_fix", :clean) - sub_project("fastthread", :clean) + # sub_project("cgi_multipart_eof_fix", :clean) + # sub_project("fastthread", :clean) sub_project("mongrel_status", :clean) sub_project("mongrel_upload_progress", :clean) sub_project("mongrel_console", :clean) diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb index e643025a..8b8d5f66 100644 --- a/lib/mongrel/handlers.rb +++ b/lib/mongrel/handlers.rb @@ -205,11 +205,11 @@ module Mongrel # test to see if this is a conditional request, and test if # the response would be identical to the last response same_response = case - when modified_since && !last_response_time = Time.httpdate(modified_since) rescue nil : false - when modified_since && last_response_time > Time.now : false - when modified_since && mtime > last_response_time : false - when none_match && none_match == '*' : false - when none_match && !none_match.strip.split(/\s*,\s*/).include?(etag) : false + when modified_since && !last_response_time = Time.httpdate(modified_since) rescue nil then false + when modified_since && last_response_time > Time.now then false + when modified_since && mtime > last_response_time then false + when none_match && none_match == '*' then false + when none_match && !none_match.strip.split(/\s*,\s*/).include?(etag) then false else modified_since || none_match # validation successful if we get this far and at least one of the header exists end From 7c9d988d4de2e08d67f95ca209196427fd89c9af Mon Sep 17 00:00:00 2001 From: evanweaver Date: Mon, 24 Mar 2008 03:48:10 +0000 Subject: [PATCH 2/4] Backport Eric's changes to the http parser from trunk (Eric Wong). Apply fix for Ragel 6 (Eric Wong, Ry Dahl). Two tests fail with the new parser (1 failed with the old parser). Needs investigation. Close #12 (mongrel_rails send_signal leaves a filehandle open until gc). Close #14 (mongrel_rails command line option --num-procs does not change the max number of procs). Close #15 (mongrel squashes helpful exception in register method). Close #16, XXX needs audit! (CGIWrapper "options" attr_reader has no corresponding @options variable). Close #20 (Mongrel doesn't erase temporary files during it's operation on windows). Close #19, XXX needs audit! (HttpResponse#reset does not properly reset HeaderOut). Close #22 (gem_plugin should load gems from Gem.path not Gem.dir). Close #23 (mongrel_cluster's mongrel_rails configuration option isn't fully respected). If I had git, being offline wouldn't have resulted in one massive commit. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/branches/stable_1-2@995 19e92222-5c0b-0410-8929-a290d50e31e9 --- Rakefile | 4 +- bin/mongrel_rails | 2 +- ext/http11/ext_help.h | 1 + ext/http11/http11.c | 165 +++- ext/http11/http11_parser.c | 492 ++++++------ ext/http11/http11_parser.java.rl | 1 + ext/http11/http11_parser.rl | 30 +- ext/http11/http11_parser_common.rl | 2 +- .../org/jruby/mongrel/Http11Parser.java | 714 ++++++++---------- lib/mongrel.rb | 13 +- lib/mongrel/cgi.rb | 4 +- lib/mongrel/http_request.rb | 2 +- lib/mongrel/http_response.rb | 5 +- projects/gem_plugin/CHANGELOG | 2 + projects/gem_plugin/lib/gem_plugin.rb | 11 +- .../lib/mongrel_cluster/init.rb | 6 +- .../lib/mongrel_cluster/recipes_2.rb | 3 +- test/test_handlers.rb | 10 + test/test_http11.rb | 156 ---- 19 files changed, 772 insertions(+), 851 deletions(-) delete mode 100644 test/test_http11.rb diff --git a/Rakefile b/Rakefile index 969c77d4..c5da7940 100644 --- a/Rakefile +++ b/Rakefile @@ -56,13 +56,13 @@ task :ragel do Dir.chdir "ext/http11" do target = "http11_parser.c" File.unlink target if File.exist? target - sh "ragel http11_parser.rl | rlgen-cd -G2 -o #{target}" + sh "ragel http11_parser.rl -C -G2 -o #{target}" raise "Failed to build C source" unless File.exist? target end Dir.chdir "ext/http11" do target = "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" File.unlink target if File.exist? target - sh "ragel -J http11_parser.java.rl | rlgen-java -o #{target}" + sh "ragel http11_parser.rl -J -o #{target}" raise "Failed to build Java source" unless File.exist? target end end diff --git a/bin/mongrel_rails b/bin/mongrel_rails index 7b5f4fa3..894a0ce9 100644 --- a/bin/mongrel_rails +++ b/bin/mongrel_rails @@ -194,7 +194,7 @@ module Mongrel end def Mongrel::send_signal(signal, pid_file) - pid = open(pid_file).read.to_i + pid = File.read(pid_file).to_i print "Sending #{signal} to Mongrel at PID #{pid}..." begin Process.kill(signal, pid) diff --git a/ext/http11/ext_help.h b/ext/http11/ext_help.h index 8b4d754c..08c0e1e2 100644 --- a/ext/http11/ext_help.h +++ b/ext/http11/ext_help.h @@ -4,6 +4,7 @@ #define RAISE_NOT_NULL(T) if(T == NULL) rb_raise(rb_eArgError, "NULL found for " # T " when shouldn't be."); #define DATA_GET(from,type,name) Data_Get_Struct(from,type,name); RAISE_NOT_NULL(name); #define REQUIRE_TYPE(V, T) if(TYPE(V) != T) rb_raise(rb_eTypeError, "Wrong argument type for " # V " required " # T); +#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) #ifdef DEBUG #define TRACE() fprintf(stderr, "> %s:%d:%s\n", __FILE__, __LINE__, __FUNCTION__) diff --git a/ext/http11/http11.c b/ext/http11/http11.c index 756c8fcc..da1fbc27 100644 --- a/ext/http11/http11.c +++ b/ext/http11/http11.c @@ -7,7 +7,13 @@ #include #include #include "http11_parser.h" -#include + +#ifndef RSTRING_PTR +#define RSTRING_PTR(s) (RSTRING(s)->ptr) +#endif +#ifndef RSTRING_LEN +#define RSTRING_LEN(s) (RSTRING(s)->len) +#endif static VALUE mMongrel; static VALUE cHttpParser; @@ -15,8 +21,9 @@ static VALUE eHttpParserError; #define id_handler_map rb_intern("@handler_map") #define id_http_body rb_intern("@http_body") +#define HTTP_PREFIX "HTTP_" +#define HTTP_PREFIX_LEN (sizeof(HTTP_PREFIX) - 1) -static VALUE global_http_prefix; static VALUE global_request_method; static VALUE global_request_uri; static VALUE global_fragment; @@ -59,10 +66,119 @@ DEF_MAX_LENGTH(REQUEST_PATH, 1024); DEF_MAX_LENGTH(QUERY_STRING, (1024 * 10)); DEF_MAX_LENGTH(HEADER, (1024 * (80 + 32))); +struct common_field { + const signed long len; + const char *name; + VALUE value; +}; + +/* + * A list of common HTTP headers we expect to receive. + * This allows us to avoid repeatedly creating identical string + * objects to be used with rb_hash_aset(). + */ +static struct common_field common_http_fields[] = { +# define f(N) { (sizeof(N) - 1), N, Qnil } + f("ACCEPT"), + f("ACCEPT_CHARSET"), + f("ACCEPT_ENCODING"), + f("ACCEPT_LANGUAGE"), + f("ALLOW"), + f("AUTHORIZATION"), + f("CACHE_CONTROL"), + f("CONNECTION"), + f("CONTENT_ENCODING"), + f("CONTENT_LENGTH"), + f("CONTENT_TYPE"), + f("COOKIE"), + f("DATE"), + f("EXPECT"), + f("FROM"), + f("HOST"), + f("IF_MATCH"), + f("IF_MODIFIED_SINCE"), + f("IF_NONE_MATCH"), + f("IF_RANGE"), + f("IF_UNMODIFIED_SINCE"), + f("KEEP_ALIVE"), /* Firefox sends this */ + f("MAX_FORWARDS"), + f("PRAGMA"), + f("PROXY_AUTHORIZATION"), + f("RANGE"), + f("REFERER"), + f("TE"), + f("TRAILER"), + f("TRANSFER_ENCODING"), + f("UPGRADE"), + f("USER_AGENT"), + f("VIA"), + f("X_FORWARDED_FOR"), /* common for proxies */ + f("X_REAL_IP"), /* common for proxies */ + f("WARNING") +# undef f +}; + +/* + * qsort(3) and bsearch(3) improve average performance slightly, but may + * not be worth it for lack of portability to certain platforms... + */ +#if defined(HAVE_QSORT_BSEARCH) +/* sort by length, then by name if there's a tie */ +static int common_field_cmp(const void *a, const void *b) +{ + struct common_field *cfa = (struct common_field *)a; + struct common_field *cfb = (struct common_field *)b; + signed long diff = cfa->len - cfb->len; + return diff ? diff : memcmp(cfa->name, cfb->name, cfa->len); +} +#endif /* HAVE_QSORT_BSEARCH */ + +static void init_common_fields(void) +{ + int i; + struct common_field *cf = common_http_fields; + char tmp[256]; /* MAX_FIELD_NAME_LENGTH */ + memcpy(tmp, HTTP_PREFIX, HTTP_PREFIX_LEN); + + for(i = 0; i < ARRAY_SIZE(common_http_fields); cf++, i++) { + memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1); + cf->value = rb_obj_freeze(rb_str_new(tmp, HTTP_PREFIX_LEN + cf->len)); + rb_global_variable(&cf->value); + } + +#if defined(HAVE_QSORT_BSEARCH) + qsort(common_http_fields, + ARRAY_SIZE(common_http_fields), + sizeof(struct common_field), + common_field_cmp); +#endif /* HAVE_QSORT_BSEARCH */ +} + +static VALUE find_common_field_value(const char *field, size_t flen) +{ +#if defined(HAVE_QSORT_BSEARCH) + struct common_field key; + struct common_field *found; + key.name = field; + key.len = (signed long)flen; + found = (struct common_field *)bsearch(&key, common_http_fields, + ARRAY_SIZE(common_http_fields), + sizeof(struct common_field), + common_field_cmp); + return found ? found->value : Qnil; +#else /* !HAVE_QSORT_BSEARCH */ + int i; + struct common_field *cf = common_http_fields; + for(i = 0; i < ARRAY_SIZE(common_http_fields); i++, cf++) { + if (cf->len == flen && !memcmp(cf->name, field, flen)) + return cf->value; + } + return Qnil; +#endif /* !HAVE_QSORT_BSEARCH */ +} void http_field(void *data, const char *field, size_t flen, const char *value, size_t vlen) { - char *ch, *end; VALUE req = (VALUE)data; VALUE v = Qnil; VALUE f = Qnil; @@ -71,15 +187,25 @@ void http_field(void *data, const char *field, size_t flen, const char *value, s VALIDATE_MAX_LENGTH(vlen, FIELD_VALUE); v = rb_str_new(value, vlen); - f = rb_str_dup(global_http_prefix); - f = rb_str_buf_cat(f, field, flen); - for(ch = RSTRING(f)->ptr, end = ch + RSTRING(f)->len; ch < end; ch++) { - if(*ch == '-') { - *ch = '_'; - } else { - *ch = toupper(*ch); - } + f = find_common_field_value(field, flen); + + if (f == Qnil) { + /* + * We got a strange header that we don't have a memoized value for. + * Fallback to creating a new string to use as a hash key. + * + * using rb_str_new(NULL, len) here is faster than rb_str_buf_new(len) + * in my testing, because: there's no minimum allocation length (and + * no check for it, either), RSTRING_LEN(f) does not need to be + * written twice, and and RSTRING_PTR(f) will already be + * null-terminated for us. + */ + f = rb_str_new(NULL, HTTP_PREFIX_LEN + flen); + memcpy(RSTRING_PTR(f), HTTP_PREFIX, HTTP_PREFIX_LEN); + memcpy(RSTRING_PTR(f) + HTTP_PREFIX_LEN, field, flen); + assert(*(RSTRING_PTR(f) + RSTRING_LEN(f)) == '\0'); /* paranoia */ + /* fprintf(stderr, "UNKNOWN HEADER <%s>\n", RSTRING_PTR(f)); */ } rb_hash_aset(req, f, v); @@ -168,13 +294,12 @@ void header_done(void *data, const char *at, size_t length) rb_hash_aset(req, global_gateway_interface, global_gateway_interface_value); if((temp = rb_hash_aref(req, global_http_host)) != Qnil) { - /* ruby better close strings off with a '\0' dammit */ - colon = strchr(RSTRING(temp)->ptr, ':'); + colon = memchr(RSTRING_PTR(temp), ':', RSTRING_LEN(temp)); if(colon != NULL) { - rb_hash_aset(req, global_server_name, rb_str_substr(temp, 0, colon - RSTRING(temp)->ptr)); + rb_hash_aset(req, global_server_name, rb_str_substr(temp, 0, colon - RSTRING_PTR(temp))); rb_hash_aset(req, global_server_port, - rb_str_substr(temp, colon - RSTRING(temp)->ptr+1, - RSTRING(temp)->len)); + rb_str_substr(temp, colon - RSTRING_PTR(temp)+1, + RSTRING_LEN(temp))); } else { rb_hash_aset(req, global_server_name, temp); rb_hash_aset(req, global_server_port, global_port_80); @@ -295,8 +420,8 @@ VALUE HttpParser_execute(VALUE self, VALUE req_hash, VALUE data, VALUE start) DATA_GET(self, http_parser, http); from = FIX2INT(start); - dptr = RSTRING(data)->ptr; - dlen = RSTRING(data)->len; + dptr = RSTRING_PTR(data); + dlen = RSTRING_LEN(data); if(from >= dlen) { rb_raise(eHttpParserError, "Requested start is after data buffer end."); @@ -366,7 +491,6 @@ void Init_http11() mMongrel = rb_define_module("Mongrel"); - DEF_GLOBAL(http_prefix, "HTTP_"); DEF_GLOBAL(request_method, "REQUEST_METHOD"); DEF_GLOBAL(request_uri, "REQUEST_URI"); DEF_GLOBAL(fragment, "FRAGMENT"); @@ -384,7 +508,7 @@ void Init_http11() DEF_GLOBAL(server_protocol, "SERVER_PROTOCOL"); DEF_GLOBAL(server_protocol_value, "HTTP/1.1"); DEF_GLOBAL(http_host, "HTTP_HOST"); - DEF_GLOBAL(mongrel_version, "Mongrel 1.1.5"); /* XXX Why is this defined here? */ + DEF_GLOBAL(mongrel_version, "Mongrel 1.1.6"); /* XXX Why is this defined here? */ DEF_GLOBAL(server_software, "SERVER_SOFTWARE"); DEF_GLOBAL(port_80, "80"); @@ -399,4 +523,5 @@ void Init_http11() rb_define_method(cHttpParser, "error?", HttpParser_has_error,0); rb_define_method(cHttpParser, "finished?", HttpParser_is_finished,0); rb_define_method(cHttpParser, "nread", HttpParser_nread,0); + init_common_fields(); } diff --git a/ext/http11/http11_parser.c b/ext/http11/http11_parser.c index 1712d295..c2143de9 100644 --- a/ext/http11/http11_parser.c +++ b/ext/http11/http11_parser.c @@ -10,34 +10,46 @@ #include #include +/* + * capitalizes all lower-case ASCII characters, + * converts dashes to underscores. + */ +static void snake_upcase_char(char *c) +{ + if (*c >= 'a' && *c <= 'z') + *c &= ~0x20; + else if (*c == '-') + *c = '_'; +} + #define LEN(AT, FPC) (FPC - buffer - parser->AT) #define MARK(M,FPC) (parser->M = (FPC) - buffer) #define PTR_TO(F) (buffer + parser->F) /** Machine **/ -#line 74 "http11_parser.rl" +#line 87 "http11_parser.rl" /** Data **/ -#line 25 "http11_parser.c" +#line 37 "http11_parser.c" static const int http_parser_start = 1; static const int http_parser_first_final = 57; static const int http_parser_error = 0; static const int http_parser_en_main = 1; -#line 78 "http11_parser.rl" +#line 91 "http11_parser.rl" int http_parser_init(http_parser *parser) { int cs = 0; -#line 37 "http11_parser.c" +#line 49 "http11_parser.c" { cs = http_parser_start; } -#line 82 "http11_parser.rl" +#line 95 "http11_parser.rl" parser->cs = cs; parser->body_start = 0; parser->content_len = 0; @@ -60,15 +72,15 @@ size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len, p = buffer+off; pe = buffer+len; - assert(*pe == '\0' && "pointer does not end on NUL"); + /* assert(*pe == '\0' && "pointer does not end on NUL"); */ assert(pe - p == len - off && "pointers aren't same distance"); -#line 69 "http11_parser.c" +#line 81 "http11_parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { case 1: @@ -86,16 +98,17 @@ case 1: goto tr0; goto st0; st0: - goto _out0; +cs = 0; + goto _out; tr0: -#line 22 "http11_parser.rl" +#line 34 "http11_parser.rl" {MARK(mark, p); } goto st2; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: -#line 99 "http11_parser.c" +#line 112 "http11_parser.c" switch( (*p) ) { case 32: goto tr2; case 36: goto st38; @@ -111,7 +124,7 @@ case 2: goto st38; goto st0; tr2: -#line 36 "http11_parser.rl" +#line 49 "http11_parser.rl" { if(parser->request_method != NULL) parser->request_method(parser->data, PTR_TO(mark), LEN(mark, p)); @@ -119,9 +132,9 @@ tr2: goto st3; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: -#line 125 "http11_parser.c" +#line 138 "http11_parser.c" switch( (*p) ) { case 42: goto tr4; case 43: goto tr5; @@ -138,66 +151,75 @@ case 3: goto tr5; goto st0; tr4: -#line 22 "http11_parser.rl" +#line 34 "http11_parser.rl" {MARK(mark, p); } goto st4; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: -#line 149 "http11_parser.c" +#line 162 "http11_parser.c" switch( (*p) ) { case 32: goto tr8; case 35: goto tr9; } goto st0; tr8: -#line 40 "http11_parser.rl" +#line 53 "http11_parser.rl" { if(parser->request_uri != NULL) parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st5; -tr30: -#line 44 "http11_parser.rl" +tr31: +#line 34 "http11_parser.rl" + {MARK(mark, p); } +#line 57 "http11_parser.rl" { if(parser->fragment != NULL) parser->fragment(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st5; -tr40: -#line 60 "http11_parser.rl" +tr34: +#line 57 "http11_parser.rl" + { + if(parser->fragment != NULL) + parser->fragment(parser->data, PTR_TO(mark), LEN(mark, p)); + } + goto st5; +tr42: +#line 73 "http11_parser.rl" { if(parser->request_path != NULL) parser->request_path(parser->data, PTR_TO(mark), LEN(mark,p)); } -#line 40 "http11_parser.rl" +#line 53 "http11_parser.rl" { if(parser->request_uri != NULL) parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st5; -tr51: -#line 49 "http11_parser.rl" +tr53: +#line 62 "http11_parser.rl" {MARK(query_start, p); } -#line 50 "http11_parser.rl" +#line 63 "http11_parser.rl" { if(parser->query_string != NULL) parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, p)); } -#line 40 "http11_parser.rl" +#line 53 "http11_parser.rl" { if(parser->request_uri != NULL) parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st5; -tr55: -#line 50 "http11_parser.rl" +tr57: +#line 63 "http11_parser.rl" { if(parser->query_string != NULL) parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, p)); } -#line 40 "http11_parser.rl" +#line 53 "http11_parser.rl" { if(parser->request_uri != NULL) parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); @@ -205,55 +227,55 @@ tr55: goto st5; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: -#line 211 "http11_parser.c" +#line 233 "http11_parser.c" if ( (*p) == 72 ) goto tr10; goto st0; tr10: -#line 22 "http11_parser.rl" +#line 34 "http11_parser.rl" {MARK(mark, p); } goto st6; st6: if ( ++p == pe ) - goto _out6; + goto _test_eof6; case 6: -#line 223 "http11_parser.c" +#line 245 "http11_parser.c" if ( (*p) == 84 ) goto st7; goto st0; st7: if ( ++p == pe ) - goto _out7; + goto _test_eof7; case 7: if ( (*p) == 84 ) goto st8; goto st0; st8: if ( ++p == pe ) - goto _out8; + goto _test_eof8; case 8: if ( (*p) == 80 ) goto st9; goto st0; st9: if ( ++p == pe ) - goto _out9; + goto _test_eof9; case 9: if ( (*p) == 47 ) goto st10; goto st0; st10: if ( ++p == pe ) - goto _out10; + goto _test_eof10; case 10: if ( 48 <= (*p) && (*p) <= 57 ) goto st11; goto st0; st11: if ( ++p == pe ) - goto _out11; + goto _test_eof11; case 11: if ( (*p) == 46 ) goto st12; @@ -262,14 +284,14 @@ case 11: goto st0; st12: if ( ++p == pe ) - goto _out12; + goto _test_eof12; case 12: if ( 48 <= (*p) && (*p) <= 57 ) goto st13; goto st0; st13: if ( ++p == pe ) - goto _out13; + goto _test_eof13; case 13: if ( (*p) == 13 ) goto tr18; @@ -277,14 +299,24 @@ case 13: goto st13; goto st0; tr18: -#line 55 "http11_parser.rl" +#line 68 "http11_parser.rl" { if(parser->http_version != NULL) parser->http_version(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st14; tr26: -#line 31 "http11_parser.rl" +#line 43 "http11_parser.rl" + { MARK(mark, p); } +#line 44 "http11_parser.rl" + { + if(parser->http_field != NULL) { + parser->http_field(parser->data, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, p)); + } + } + goto st14; +tr29: +#line 44 "http11_parser.rl" { if(parser->http_field != NULL) { parser->http_field(parser->data, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, p)); @@ -293,15 +325,15 @@ tr26: goto st14; st14: if ( ++p == pe ) - goto _out14; + goto _test_eof14; case 14: -#line 299 "http11_parser.c" +#line 331 "http11_parser.c" if ( (*p) == 10 ) goto st15; goto st0; st15: if ( ++p == pe ) - goto _out15; + goto _test_eof15; case 15: switch( (*p) ) { case 13: goto st16; @@ -329,131 +361,137 @@ case 15: goto st0; st16: if ( ++p == pe ) - goto _out16; + goto _test_eof16; case 16: if ( (*p) == 10 ) goto tr22; goto st0; tr22: -#line 65 "http11_parser.rl" +#line 78 "http11_parser.rl" { parser->body_start = p - buffer + 1; if(parser->header_done != NULL) parser->header_done(parser->data, p + 1, pe - p - 1); - goto _out57; + {p++; cs = 57; goto _out;} } goto st57; st57: if ( ++p == pe ) - goto _out57; + goto _test_eof57; case 57: -#line 351 "http11_parser.c" +#line 383 "http11_parser.c" goto st0; tr21: -#line 25 "http11_parser.rl" +#line 37 "http11_parser.rl" { MARK(field_start, p); } +#line 38 "http11_parser.rl" + { snake_upcase_char((char *)p); } + goto st17; +tr23: +#line 38 "http11_parser.rl" + { snake_upcase_char((char *)p); } goto st17; st17: if ( ++p == pe ) - goto _out17; + goto _test_eof17; case 17: -#line 361 "http11_parser.c" +#line 399 "http11_parser.c" switch( (*p) ) { - case 33: goto st17; + case 33: goto tr23; case 58: goto tr24; - case 124: goto st17; - case 126: goto st17; + case 124: goto tr23; + case 126: goto tr23; } if ( (*p) < 45 ) { if ( (*p) > 39 ) { if ( 42 <= (*p) && (*p) <= 43 ) - goto st17; + goto tr23; } else if ( (*p) >= 35 ) - goto st17; + goto tr23; } else if ( (*p) > 46 ) { if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) - goto st17; + goto tr23; } else if ( (*p) > 90 ) { if ( 94 <= (*p) && (*p) <= 122 ) - goto st17; + goto tr23; } else - goto st17; + goto tr23; } else - goto st17; + goto tr23; goto st0; tr24: -#line 26 "http11_parser.rl" +#line 39 "http11_parser.rl" { parser->field_len = LEN(field_start, p); } goto st18; tr27: -#line 30 "http11_parser.rl" +#line 43 "http11_parser.rl" { MARK(mark, p); } goto st18; st18: if ( ++p == pe ) - goto _out18; + goto _test_eof18; case 18: -#line 400 "http11_parser.c" +#line 438 "http11_parser.c" switch( (*p) ) { case 13: goto tr26; case 32: goto tr27; } goto tr25; tr25: -#line 30 "http11_parser.rl" +#line 43 "http11_parser.rl" { MARK(mark, p); } goto st19; st19: if ( ++p == pe ) - goto _out19; + goto _test_eof19; case 19: -#line 414 "http11_parser.c" +#line 452 "http11_parser.c" if ( (*p) == 13 ) - goto tr26; + goto tr29; goto st19; tr9: -#line 40 "http11_parser.rl" +#line 53 "http11_parser.rl" { if(parser->request_uri != NULL) parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st20; -tr41: -#line 60 "http11_parser.rl" +tr43: +#line 73 "http11_parser.rl" { if(parser->request_path != NULL) parser->request_path(parser->data, PTR_TO(mark), LEN(mark,p)); } -#line 40 "http11_parser.rl" +#line 53 "http11_parser.rl" { if(parser->request_uri != NULL) parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st20; -tr52: -#line 49 "http11_parser.rl" +tr54: +#line 62 "http11_parser.rl" {MARK(query_start, p); } -#line 50 "http11_parser.rl" +#line 63 "http11_parser.rl" { if(parser->query_string != NULL) parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, p)); } -#line 40 "http11_parser.rl" +#line 53 "http11_parser.rl" { if(parser->request_uri != NULL) parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st20; -tr56: -#line 50 "http11_parser.rl" +tr58: +#line 63 "http11_parser.rl" { if(parser->query_string != NULL) parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, p)); } -#line 40 "http11_parser.rl" +#line 53 "http11_parser.rl" { if(parser->request_uri != NULL) parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); @@ -461,12 +499,12 @@ tr56: goto st20; st20: if ( ++p == pe ) - goto _out20; + goto _test_eof20; case 20: -#line 467 "http11_parser.c" +#line 505 "http11_parser.c" switch( (*p) ) { - case 32: goto tr30; - case 37: goto tr31; + case 32: goto tr31; + case 37: goto tr32; case 60: goto st0; case 62: goto st0; case 127: goto st0; @@ -476,18 +514,18 @@ case 20: goto st0; } else if ( (*p) >= 0 ) goto st0; - goto tr29; -tr29: -#line 22 "http11_parser.rl" + goto tr30; +tr30: +#line 34 "http11_parser.rl" {MARK(mark, p); } goto st21; st21: if ( ++p == pe ) - goto _out21; + goto _test_eof21; case 21: -#line 489 "http11_parser.c" +#line 527 "http11_parser.c" switch( (*p) ) { - case 32: goto tr30; + case 32: goto tr34; case 37: goto st22; case 60: goto st0; case 62: goto st0; @@ -499,15 +537,15 @@ case 21: } else if ( (*p) >= 0 ) goto st0; goto st21; -tr31: -#line 22 "http11_parser.rl" +tr32: +#line 34 "http11_parser.rl" {MARK(mark, p); } goto st22; st22: if ( ++p == pe ) - goto _out22; + goto _test_eof22; case 22: -#line 511 "http11_parser.c" +#line 549 "http11_parser.c" if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st23; @@ -519,7 +557,7 @@ case 22: goto st0; st23: if ( ++p == pe ) - goto _out23; + goto _test_eof23; case 23: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -531,14 +569,14 @@ case 23: goto st21; goto st0; tr5: -#line 22 "http11_parser.rl" +#line 34 "http11_parser.rl" {MARK(mark, p); } goto st24; st24: if ( ++p == pe ) - goto _out24; + goto _test_eof24; case 24: -#line 542 "http11_parser.c" +#line 580 "http11_parser.c" switch( (*p) ) { case 43: goto st24; case 58: goto st25; @@ -556,14 +594,14 @@ case 24: goto st24; goto st0; tr7: -#line 22 "http11_parser.rl" +#line 34 "http11_parser.rl" {MARK(mark, p); } goto st25; st25: if ( ++p == pe ) - goto _out25; + goto _test_eof25; case 25: -#line 567 "http11_parser.c" +#line 605 "http11_parser.c" switch( (*p) ) { case 32: goto tr8; case 34: goto st0; @@ -578,7 +616,7 @@ case 25: goto st25; st26: if ( ++p == pe ) - goto _out26; + goto _test_eof26; case 26: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -591,7 +629,7 @@ case 26: goto st0; st27: if ( ++p == pe ) - goto _out27; + goto _test_eof27; case 27: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -603,23 +641,23 @@ case 27: goto st25; goto st0; tr6: -#line 22 "http11_parser.rl" +#line 34 "http11_parser.rl" {MARK(mark, p); } goto st28; st28: if ( ++p == pe ) - goto _out28; + goto _test_eof28; case 28: -#line 614 "http11_parser.c" +#line 652 "http11_parser.c" switch( (*p) ) { - case 32: goto tr40; + case 32: goto tr42; case 34: goto st0; - case 35: goto tr41; + case 35: goto tr43; case 37: goto st29; - case 59: goto tr43; + case 59: goto tr45; case 60: goto st0; case 62: goto st0; - case 63: goto tr44; + case 63: goto tr46; case 127: goto st0; } if ( 0 <= (*p) && (*p) <= 31 ) @@ -627,7 +665,7 @@ case 28: goto st28; st29: if ( ++p == pe ) - goto _out29; + goto _test_eof29; case 29: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -640,7 +678,7 @@ case 29: goto st0; st30: if ( ++p == pe ) - goto _out30; + goto _test_eof30; case 30: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -651,8 +689,8 @@ case 30: } else goto st28; goto st0; -tr43: -#line 60 "http11_parser.rl" +tr45: +#line 73 "http11_parser.rl" { if(parser->request_path != NULL) parser->request_path(parser->data, PTR_TO(mark), LEN(mark,p)); @@ -660,9 +698,9 @@ tr43: goto st31; st31: if ( ++p == pe ) - goto _out31; + goto _test_eof31; case 31: -#line 666 "http11_parser.c" +#line 704 "http11_parser.c" switch( (*p) ) { case 32: goto tr8; case 34: goto st0; @@ -678,7 +716,7 @@ case 31: goto st31; st32: if ( ++p == pe ) - goto _out32; + goto _test_eof32; case 32: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -691,7 +729,7 @@ case 32: goto st0; st33: if ( ++p == pe ) - goto _out33; + goto _test_eof33; case 33: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -702,8 +740,8 @@ case 33: } else goto st31; goto st0; -tr44: -#line 60 "http11_parser.rl" +tr46: +#line 73 "http11_parser.rl" { if(parser->request_path != NULL) parser->request_path(parser->data, PTR_TO(mark), LEN(mark,p)); @@ -711,34 +749,34 @@ tr44: goto st34; st34: if ( ++p == pe ) - goto _out34; + goto _test_eof34; case 34: -#line 717 "http11_parser.c" +#line 755 "http11_parser.c" switch( (*p) ) { - case 32: goto tr51; + case 32: goto tr53; case 34: goto st0; - case 35: goto tr52; - case 37: goto tr53; + case 35: goto tr54; + case 37: goto tr55; case 60: goto st0; case 62: goto st0; case 127: goto st0; } if ( 0 <= (*p) && (*p) <= 31 ) goto st0; - goto tr50; -tr50: -#line 49 "http11_parser.rl" + goto tr52; +tr52: +#line 62 "http11_parser.rl" {MARK(query_start, p); } goto st35; st35: if ( ++p == pe ) - goto _out35; + goto _test_eof35; case 35: -#line 738 "http11_parser.c" +#line 776 "http11_parser.c" switch( (*p) ) { - case 32: goto tr55; + case 32: goto tr57; case 34: goto st0; - case 35: goto tr56; + case 35: goto tr58; case 37: goto st36; case 60: goto st0; case 62: goto st0; @@ -747,15 +785,15 @@ case 35: if ( 0 <= (*p) && (*p) <= 31 ) goto st0; goto st35; -tr53: -#line 49 "http11_parser.rl" +tr55: +#line 62 "http11_parser.rl" {MARK(query_start, p); } goto st36; st36: if ( ++p == pe ) - goto _out36; + goto _test_eof36; case 36: -#line 759 "http11_parser.c" +#line 797 "http11_parser.c" if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st37; @@ -767,7 +805,7 @@ case 36: goto st0; st37: if ( ++p == pe ) - goto _out37; + goto _test_eof37; case 37: if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) @@ -780,7 +818,7 @@ case 37: goto st0; st38: if ( ++p == pe ) - goto _out38; + goto _test_eof38; case 38: switch( (*p) ) { case 32: goto tr2; @@ -798,7 +836,7 @@ case 38: goto st0; st39: if ( ++p == pe ) - goto _out39; + goto _test_eof39; case 39: switch( (*p) ) { case 32: goto tr2; @@ -816,7 +854,7 @@ case 39: goto st0; st40: if ( ++p == pe ) - goto _out40; + goto _test_eof40; case 40: switch( (*p) ) { case 32: goto tr2; @@ -834,7 +872,7 @@ case 40: goto st0; st41: if ( ++p == pe ) - goto _out41; + goto _test_eof41; case 41: switch( (*p) ) { case 32: goto tr2; @@ -852,7 +890,7 @@ case 41: goto st0; st42: if ( ++p == pe ) - goto _out42; + goto _test_eof42; case 42: switch( (*p) ) { case 32: goto tr2; @@ -870,7 +908,7 @@ case 42: goto st0; st43: if ( ++p == pe ) - goto _out43; + goto _test_eof43; case 43: switch( (*p) ) { case 32: goto tr2; @@ -888,7 +926,7 @@ case 43: goto st0; st44: if ( ++p == pe ) - goto _out44; + goto _test_eof44; case 44: switch( (*p) ) { case 32: goto tr2; @@ -906,7 +944,7 @@ case 44: goto st0; st45: if ( ++p == pe ) - goto _out45; + goto _test_eof45; case 45: switch( (*p) ) { case 32: goto tr2; @@ -924,7 +962,7 @@ case 45: goto st0; st46: if ( ++p == pe ) - goto _out46; + goto _test_eof46; case 46: switch( (*p) ) { case 32: goto tr2; @@ -942,7 +980,7 @@ case 46: goto st0; st47: if ( ++p == pe ) - goto _out47; + goto _test_eof47; case 47: switch( (*p) ) { case 32: goto tr2; @@ -960,7 +998,7 @@ case 47: goto st0; st48: if ( ++p == pe ) - goto _out48; + goto _test_eof48; case 48: switch( (*p) ) { case 32: goto tr2; @@ -978,7 +1016,7 @@ case 48: goto st0; st49: if ( ++p == pe ) - goto _out49; + goto _test_eof49; case 49: switch( (*p) ) { case 32: goto tr2; @@ -996,7 +1034,7 @@ case 49: goto st0; st50: if ( ++p == pe ) - goto _out50; + goto _test_eof50; case 50: switch( (*p) ) { case 32: goto tr2; @@ -1014,7 +1052,7 @@ case 50: goto st0; st51: if ( ++p == pe ) - goto _out51; + goto _test_eof51; case 51: switch( (*p) ) { case 32: goto tr2; @@ -1032,7 +1070,7 @@ case 51: goto st0; st52: if ( ++p == pe ) - goto _out52; + goto _test_eof52; case 52: switch( (*p) ) { case 32: goto tr2; @@ -1050,7 +1088,7 @@ case 52: goto st0; st53: if ( ++p == pe ) - goto _out53; + goto _test_eof53; case 53: switch( (*p) ) { case 32: goto tr2; @@ -1068,7 +1106,7 @@ case 53: goto st0; st54: if ( ++p == pe ) - goto _out54; + goto _test_eof54; case 54: switch( (*p) ) { case 32: goto tr2; @@ -1086,7 +1124,7 @@ case 54: goto st0; st55: if ( ++p == pe ) - goto _out55; + goto _test_eof55; case 55: switch( (*p) ) { case 32: goto tr2; @@ -1104,73 +1142,73 @@ case 55: goto st0; st56: if ( ++p == pe ) - goto _out56; + goto _test_eof56; case 56: if ( (*p) == 32 ) goto tr2; goto st0; } - _out0: cs = 0; goto _out; - _out2: cs = 2; goto _out; - _out3: cs = 3; goto _out; - _out4: cs = 4; goto _out; - _out5: cs = 5; goto _out; - _out6: cs = 6; goto _out; - _out7: cs = 7; goto _out; - _out8: cs = 8; goto _out; - _out9: cs = 9; goto _out; - _out10: cs = 10; goto _out; - _out11: cs = 11; goto _out; - _out12: cs = 12; goto _out; - _out13: cs = 13; goto _out; - _out14: cs = 14; goto _out; - _out15: cs = 15; goto _out; - _out16: cs = 16; goto _out; - _out57: cs = 57; goto _out; - _out17: cs = 17; goto _out; - _out18: cs = 18; goto _out; - _out19: cs = 19; goto _out; - _out20: cs = 20; goto _out; - _out21: cs = 21; goto _out; - _out22: cs = 22; goto _out; - _out23: cs = 23; goto _out; - _out24: cs = 24; goto _out; - _out25: cs = 25; goto _out; - _out26: cs = 26; goto _out; - _out27: cs = 27; goto _out; - _out28: cs = 28; goto _out; - _out29: cs = 29; goto _out; - _out30: cs = 30; goto _out; - _out31: cs = 31; goto _out; - _out32: cs = 32; goto _out; - _out33: cs = 33; goto _out; - _out34: cs = 34; goto _out; - _out35: cs = 35; goto _out; - _out36: cs = 36; goto _out; - _out37: cs = 37; goto _out; - _out38: cs = 38; goto _out; - _out39: cs = 39; goto _out; - _out40: cs = 40; goto _out; - _out41: cs = 41; goto _out; - _out42: cs = 42; goto _out; - _out43: cs = 43; goto _out; - _out44: cs = 44; goto _out; - _out45: cs = 45; goto _out; - _out46: cs = 46; goto _out; - _out47: cs = 47; goto _out; - _out48: cs = 48; goto _out; - _out49: cs = 49; goto _out; - _out50: cs = 50; goto _out; - _out51: cs = 51; goto _out; - _out52: cs = 52; goto _out; - _out53: cs = 53; goto _out; - _out54: cs = 54; goto _out; - _out55: cs = 55; goto _out; - _out56: cs = 56; goto _out; + _test_eof2: cs = 2; goto _test_eof; + _test_eof3: cs = 3; goto _test_eof; + _test_eof4: cs = 4; goto _test_eof; + _test_eof5: cs = 5; goto _test_eof; + _test_eof6: cs = 6; goto _test_eof; + _test_eof7: cs = 7; goto _test_eof; + _test_eof8: cs = 8; goto _test_eof; + _test_eof9: cs = 9; goto _test_eof; + _test_eof10: cs = 10; goto _test_eof; + _test_eof11: cs = 11; goto _test_eof; + _test_eof12: cs = 12; goto _test_eof; + _test_eof13: cs = 13; goto _test_eof; + _test_eof14: cs = 14; goto _test_eof; + _test_eof15: cs = 15; goto _test_eof; + _test_eof16: cs = 16; goto _test_eof; + _test_eof57: cs = 57; goto _test_eof; + _test_eof17: cs = 17; goto _test_eof; + _test_eof18: cs = 18; goto _test_eof; + _test_eof19: cs = 19; goto _test_eof; + _test_eof20: cs = 20; goto _test_eof; + _test_eof21: cs = 21; goto _test_eof; + _test_eof22: cs = 22; goto _test_eof; + _test_eof23: cs = 23; goto _test_eof; + _test_eof24: cs = 24; goto _test_eof; + _test_eof25: cs = 25; goto _test_eof; + _test_eof26: cs = 26; goto _test_eof; + _test_eof27: cs = 27; goto _test_eof; + _test_eof28: cs = 28; goto _test_eof; + _test_eof29: cs = 29; goto _test_eof; + _test_eof30: cs = 30; goto _test_eof; + _test_eof31: cs = 31; goto _test_eof; + _test_eof32: cs = 32; goto _test_eof; + _test_eof33: cs = 33; goto _test_eof; + _test_eof34: cs = 34; goto _test_eof; + _test_eof35: cs = 35; goto _test_eof; + _test_eof36: cs = 36; goto _test_eof; + _test_eof37: cs = 37; goto _test_eof; + _test_eof38: cs = 38; goto _test_eof; + _test_eof39: cs = 39; goto _test_eof; + _test_eof40: cs = 40; goto _test_eof; + _test_eof41: cs = 41; goto _test_eof; + _test_eof42: cs = 42; goto _test_eof; + _test_eof43: cs = 43; goto _test_eof; + _test_eof44: cs = 44; goto _test_eof; + _test_eof45: cs = 45; goto _test_eof; + _test_eof46: cs = 46; goto _test_eof; + _test_eof47: cs = 47; goto _test_eof; + _test_eof48: cs = 48; goto _test_eof; + _test_eof49: cs = 49; goto _test_eof; + _test_eof50: cs = 50; goto _test_eof; + _test_eof51: cs = 51; goto _test_eof; + _test_eof52: cs = 52; goto _test_eof; + _test_eof53: cs = 53; goto _test_eof; + _test_eof54: cs = 54; goto _test_eof; + _test_eof55: cs = 55; goto _test_eof; + _test_eof56: cs = 56; goto _test_eof; + _test_eof: {} _out: {} } -#line 109 "http11_parser.rl" +#line 122 "http11_parser.rl" parser->cs = cs; parser->nread += p - (buffer + off); @@ -1182,27 +1220,11 @@ case 56: 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) { - /* final \r\n combo encountered so stop right here */ - -#line 1189 "http11_parser.c" -#line 123 "http11_parser.rl" - parser->nread++; - } - return(parser->nread); } int http_parser_finish(http_parser *parser) { - int cs = parser->cs; - - -#line 1202 "http11_parser.c" -#line 134 "http11_parser.rl" - - parser->cs = cs; - if (http_parser_has_error(parser) ) { return -1; } else if (http_parser_is_finished(parser) ) { @@ -1217,5 +1239,5 @@ int http_parser_has_error(http_parser *parser) { } int http_parser_is_finished(http_parser *parser) { - return parser->cs == http_parser_first_final; + return parser->cs >= http_parser_first_final; } diff --git a/ext/http11/http11_parser.java.rl b/ext/http11/http11_parser.java.rl index 71f8d3c1..c9be22e8 100644 --- a/ext/http11/http11_parser.java.rl +++ b/ext/http11/http11_parser.java.rl @@ -13,6 +13,7 @@ public class Http11Parser { action mark {parser.mark = fpc; } action start_field { parser.field_start = fpc; } + action snake_upcase_field { /* FIXME stub */ } action write_field { parser.field_len = fpc-parser.field_start; } diff --git a/ext/http11/http11_parser.rl b/ext/http11/http11_parser.rl index fffe57a6..5e551a33 100644 --- a/ext/http11/http11_parser.rl +++ b/ext/http11/http11_parser.rl @@ -9,6 +9,18 @@ #include #include +/* + * capitalizes all lower-case ASCII characters, + * converts dashes to underscores. + */ +static void snake_upcase_char(char *c) +{ + if (*c >= 'a' && *c <= 'z') + *c &= ~0x20; + else if (*c == '-') + *c = '_'; +} + #define LEN(AT, FPC) (FPC - buffer - parser->AT) #define MARK(M,FPC) (parser->M = (FPC) - buffer) #define PTR_TO(F) (buffer + parser->F) @@ -23,6 +35,7 @@ action start_field { MARK(field_start, fpc); } + action snake_upcase_field { snake_upcase_char((char *)fpc); } action write_field { parser->field_len = LEN(field_start, fpc); } @@ -101,10 +114,9 @@ size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len, p = buffer+off; pe = buffer+len; - assert(*pe == '\0' && "pointer does not end on NUL"); + /* assert(*pe == '\0' && "pointer does not end on NUL"); */ assert(pe - p == len - off && "pointers aren't same distance"); - %% write exec; parser->cs = cs; @@ -117,23 +129,11 @@ size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len, 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) { - /* final \r\n combo encountered so stop right here */ - %%write eof; - parser->nread++; - } - return(parser->nread); } int http_parser_finish(http_parser *parser) { - int cs = parser->cs; - - %%write eof; - - parser->cs = cs; - if (http_parser_has_error(parser) ) { return -1; } else if (http_parser_is_finished(parser) ) { @@ -148,5 +148,5 @@ int http_parser_has_error(http_parser *parser) { } int http_parser_is_finished(http_parser *parser) { - return parser->cs == http_parser_first_final; + return parser->cs >= http_parser_first_final; } diff --git a/ext/http11/http11_parser_common.rl b/ext/http11/http11_parser_common.rl index a70d4dad..53c805f0 100644 --- a/ext/http11/http11_parser_common.rl +++ b/ext/http11/http11_parser_common.rl @@ -41,7 +41,7 @@ HTTP_Version = ( "HTTP/" http_number ) >mark %http_version ; Request_Line = ( Method " " Request_URI ("#" Fragment){0,1} " " HTTP_Version CRLF ) ; - field_name = ( token -- ":" )+ >start_field %write_field; + field_name = ( token -- ":" )+ >start_field $snake_upcase_field %write_field; field_value = any* >start_value %write_value; diff --git a/ext/http11_java/org/jruby/mongrel/Http11Parser.java b/ext/http11_java/org/jruby/mongrel/Http11Parser.java index 30f188db..2dd00781 100644 --- a/ext/http11_java/org/jruby/mongrel/Http11Parser.java +++ b/ext/http11_java/org/jruby/mongrel/Http11Parser.java @@ -1,287 +1,215 @@ -// line 1 "http11_parser.java.rl" -package org.jruby.mongrel; +// line 1 "http11_parser.rl" +/** + * Copyright (c) 2005 Zed A. Shaw + * You can redistribute it and/or modify it under the same terms as Ruby. + */ +#include "http11_parser.h" +#include +#include +#include +#include +#include -import org.jruby.util.ByteList; +/* + * capitalizes all lower-case ASCII characters, + * converts dashes to underscores. + */ +static void snake_upcase_char(char *c) +{ + if (*c >= 'a' && *c <= 'z') + *c &= ~0x20; + else if (*c == '-') + *c = '_'; +} -public class Http11Parser { +#define LEN(AT, FPC) (FPC - buffer - parser->AT) +#define MARK(M,FPC) (parser->M = (FPC) - buffer) +#define PTR_TO(F) (buffer + parser->F) /** Machine **/ -// line 64 "http11_parser.java.rl" +// line 87 "http11_parser.rl" /** Data **/ -// line 16 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" -private static void init__http_parser_actions_0( byte[] r ) +// line 37 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" +private static byte[] init__http_parser_actions_0() { - 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]=8; r[19]=1; r[20]=10; r[21]=1; r[22]=11; r[23]=1; - r[24]=12; r[25]=2; r[26]=9; r[27]=6; r[28]=2; r[29]=11; r[30]=6; r[31]=3; - r[32]=8; r[33]=9; r[34]=6; + return new byte [] { + 0, 1, 0, 1, 2, 1, 3, 1, 4, 1, 5, 1, + 6, 1, 7, 1, 8, 1, 9, 1, 11, 1, 12, 1, + 13, 2, 0, 8, 2, 1, 2, 2, 4, 5, 2, 10, + 7, 2, 12, 7, 3, 9, 10, 7 + }; } -private static byte[] create__http_parser_actions( ) +private static final byte _http_parser_actions[] = init__http_parser_actions_0(); + + +private static short[] init__http_parser_key_offsets_0() { - byte[] r = new byte[35]; - init__http_parser_actions_0( r ); - return r; + return new short [] { + 0, 0, 8, 17, 27, 29, 30, 31, 32, 33, 34, 36, + 39, 41, 44, 45, 61, 62, 78, 80, 81, 90, 99, 105, + 111, 121, 130, 136, 142, 153, 159, 165, 175, 181, 187, 196, + 205, 211, 217, 226, 235, 244, 253, 262, 271, 280, 289, 298, + 307, 316, 325, 334, 343, 352, 361, 370, 379, 380 + }; } -private static final byte _http_parser_actions[] = create__http_parser_actions(); +private static final short _http_parser_key_offsets[] = init__http_parser_key_offsets_0(); -private static void init__http_parser_key_offsets_0( short[] r ) +private static char[] init__http_parser_trans_keys_0() { - r[0]=0; r[1]=0; r[2]=8; r[3]=17; r[4]=27; r[5]=29; r[6]=30; r[7]=31; - r[8]=32; r[9]=33; r[10]=34; r[11]=36; r[12]=39; r[13]=41; r[14]=44; r[15]=45; - r[16]=61; r[17]=62; r[18]=78; r[19]=80; r[20]=81; r[21]=90; r[22]=99; r[23]=105; - r[24]=111; r[25]=121; r[26]=130; r[27]=136; r[28]=142; r[29]=153; r[30]=159; r[31]=165; - r[32]=175; r[33]=181; r[34]=187; r[35]=196; r[36]=205; r[37]=211; r[38]=217; r[39]=226; - r[40]=235; r[41]=244; r[42]=253; r[43]=262; r[44]=271; r[45]=280; r[46]=289; r[47]=298; - r[48]=307; r[49]=316; r[50]=325; r[51]=334; r[52]=343; r[53]=352; r[54]=361; r[55]=370; - r[56]=379; r[57]=380; + return new char [] { + 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, + 46, 48, 57, 65, 90, 42, 43, 47, 58, 45, 57, 65, + 90, 97, 122, 32, 35, 72, 84, 84, 80, 47, 48, 57, + 46, 48, 57, 48, 57, 13, 48, 57, 10, 13, 33, 124, + 126, 35, 39, 42, 43, 45, 46, 48, 57, 65, 90, 94, + 122, 10, 33, 58, 124, 126, 35, 39, 42, 43, 45, 46, + 48, 57, 65, 90, 94, 122, 13, 32, 13, 32, 37, 60, + 62, 127, 0, 31, 34, 35, 32, 37, 60, 62, 127, 0, + 31, 34, 35, 48, 57, 65, 70, 97, 102, 48, 57, 65, + 70, 97, 102, 43, 58, 45, 46, 48, 57, 65, 90, 97, + 122, 32, 34, 35, 37, 60, 62, 127, 0, 31, 48, 57, + 65, 70, 97, 102, 48, 57, 65, 70, 97, 102, 32, 34, + 35, 37, 59, 60, 62, 63, 127, 0, 31, 48, 57, 65, + 70, 97, 102, 48, 57, 65, 70, 97, 102, 32, 34, 35, + 37, 60, 62, 63, 127, 0, 31, 48, 57, 65, 70, 97, + 102, 48, 57, 65, 70, 97, 102, 32, 34, 35, 37, 60, + 62, 127, 0, 31, 32, 34, 35, 37, 60, 62, 127, 0, + 31, 48, 57, 65, 70, 97, 102, 48, 57, 65, 70, 97, + 102, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, + 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, + 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, + 90, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, + 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, + 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, + 90, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, + 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, + 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, + 90, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, + 95, 45, 46, 48, 57, 65, 90, 32, 36, 95, 45, 46, + 48, 57, 65, 90, 32, 36, 95, 45, 46, 48, 57, 65, + 90, 32, 36, 95, 45, 46, 48, 57, 65, 90, 32, 36, + 95, 45, 46, 48, 57, 65, 90, 32, 0 + }; } -private static short[] create__http_parser_key_offsets( ) +private static final char _http_parser_trans_keys[] = init__http_parser_trans_keys_0(); + + +private static byte[] init__http_parser_single_lengths_0() { - short[] r = new short[58]; - init__http_parser_key_offsets_0( r ); - return r; + return new byte [] { + 0, 2, 3, 4, 2, 1, 1, 1, 1, 1, 0, 1, + 0, 1, 1, 4, 1, 4, 2, 1, 5, 5, 0, 0, + 2, 7, 0, 0, 9, 0, 0, 8, 0, 0, 7, 7, + 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 1, 0 + }; } -private static final short _http_parser_key_offsets[] = create__http_parser_key_offsets(); +private static final byte _http_parser_single_lengths[] = init__http_parser_single_lengths_0(); -private static void init__http_parser_trans_keys_0( char[] r ) +private static byte[] init__http_parser_range_lengths_0() { - 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]=35; r[29]=72; r[30]=84; r[31]=84; - r[32]=80; r[33]=47; r[34]=48; r[35]=57; r[36]=46; r[37]=48; r[38]=57; r[39]=48; - r[40]=57; r[41]=13; r[42]=48; r[43]=57; r[44]=10; r[45]=13; r[46]=33; r[47]=124; - r[48]=126; r[49]=35; r[50]=39; r[51]=42; r[52]=43; r[53]=45; r[54]=46; r[55]=48; - r[56]=57; r[57]=65; r[58]=90; r[59]=94; r[60]=122; r[61]=10; r[62]=33; r[63]=58; - r[64]=124; r[65]=126; r[66]=35; r[67]=39; r[68]=42; r[69]=43; r[70]=45; r[71]=46; - r[72]=48; r[73]=57; r[74]=65; r[75]=90; r[76]=94; r[77]=122; r[78]=13; r[79]=32; - r[80]=13; r[81]=32; r[82]=37; r[83]=60; r[84]=62; r[85]=127; r[86]=0; r[87]=31; - r[88]=34; r[89]=35; 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]=43; - r[112]=58; r[113]=45; r[114]=46; r[115]=48; r[116]=57; r[117]=65; r[118]=90; r[119]=97; - r[120]=122; r[121]=32; r[122]=34; r[123]=35; r[124]=37; r[125]=60; r[126]=62; r[127]=127; - r[128]=0; r[129]=31; r[130]=48; r[131]=57; r[132]=65; r[133]=70; r[134]=97; r[135]=102; - r[136]=48; r[137]=57; r[138]=65; r[139]=70; r[140]=97; r[141]=102; r[142]=32; r[143]=34; - r[144]=35; r[145]=37; r[146]=59; r[147]=60; r[148]=62; r[149]=63; r[150]=127; r[151]=0; - r[152]=31; r[153]=48; r[154]=57; r[155]=65; r[156]=70; r[157]=97; r[158]=102; r[159]=48; - r[160]=57; r[161]=65; r[162]=70; r[163]=97; r[164]=102; r[165]=32; r[166]=34; r[167]=35; - r[168]=37; r[169]=60; r[170]=62; r[171]=63; r[172]=127; r[173]=0; r[174]=31; r[175]=48; - r[176]=57; r[177]=65; r[178]=70; r[179]=97; r[180]=102; r[181]=48; r[182]=57; r[183]=65; - r[184]=70; r[185]=97; r[186]=102; r[187]=32; r[188]=34; r[189]=35; r[190]=37; r[191]=60; - r[192]=62; r[193]=127; r[194]=0; r[195]=31; r[196]=32; r[197]=34; r[198]=35; r[199]=37; - r[200]=60; r[201]=62; r[202]=127; r[203]=0; r[204]=31; r[205]=48; r[206]=57; r[207]=65; - r[208]=70; r[209]=97; r[210]=102; r[211]=48; r[212]=57; r[213]=65; r[214]=70; r[215]=97; - r[216]=102; r[217]=32; r[218]=36; r[219]=95; r[220]=45; r[221]=46; r[222]=48; r[223]=57; - r[224]=65; r[225]=90; r[226]=32; r[227]=36; r[228]=95; r[229]=45; r[230]=46; r[231]=48; - r[232]=57; r[233]=65; r[234]=90; r[235]=32; r[236]=36; r[237]=95; r[238]=45; r[239]=46; - r[240]=48; r[241]=57; r[242]=65; r[243]=90; r[244]=32; r[245]=36; r[246]=95; r[247]=45; - r[248]=46; r[249]=48; r[250]=57; r[251]=65; r[252]=90; r[253]=32; r[254]=36; r[255]=95; - r[256]=45; r[257]=46; r[258]=48; r[259]=57; r[260]=65; r[261]=90; r[262]=32; r[263]=36; - r[264]=95; r[265]=45; r[266]=46; r[267]=48; r[268]=57; r[269]=65; r[270]=90; r[271]=32; - r[272]=36; r[273]=95; r[274]=45; r[275]=46; r[276]=48; r[277]=57; r[278]=65; r[279]=90; - r[280]=32; r[281]=36; r[282]=95; r[283]=45; r[284]=46; r[285]=48; r[286]=57; r[287]=65; - r[288]=90; r[289]=32; r[290]=36; r[291]=95; r[292]=45; r[293]=46; r[294]=48; r[295]=57; - r[296]=65; r[297]=90; r[298]=32; r[299]=36; r[300]=95; r[301]=45; r[302]=46; r[303]=48; - r[304]=57; r[305]=65; r[306]=90; r[307]=32; r[308]=36; r[309]=95; r[310]=45; r[311]=46; - r[312]=48; r[313]=57; r[314]=65; r[315]=90; r[316]=32; r[317]=36; r[318]=95; r[319]=45; - r[320]=46; r[321]=48; r[322]=57; r[323]=65; r[324]=90; r[325]=32; r[326]=36; r[327]=95; - r[328]=45; r[329]=46; r[330]=48; r[331]=57; r[332]=65; r[333]=90; r[334]=32; r[335]=36; - r[336]=95; r[337]=45; r[338]=46; r[339]=48; r[340]=57; r[341]=65; r[342]=90; r[343]=32; - r[344]=36; r[345]=95; r[346]=45; r[347]=46; r[348]=48; r[349]=57; r[350]=65; r[351]=90; - r[352]=32; r[353]=36; r[354]=95; r[355]=45; r[356]=46; r[357]=48; r[358]=57; r[359]=65; - r[360]=90; r[361]=32; r[362]=36; r[363]=95; r[364]=45; r[365]=46; r[366]=48; r[367]=57; - r[368]=65; r[369]=90; r[370]=32; r[371]=36; r[372]=95; r[373]=45; r[374]=46; r[375]=48; - r[376]=57; r[377]=65; r[378]=90; r[379]=32; r[380]=0; + return new byte [] { + 0, 3, 3, 3, 0, 0, 0, 0, 0, 0, 1, 1, + 1, 1, 0, 6, 0, 6, 0, 0, 2, 2, 3, 3, + 4, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 1, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 0, 0 + }; } -private static char[] create__http_parser_trans_keys( ) +private static final byte _http_parser_range_lengths[] = init__http_parser_range_lengths_0(); + + +private static short[] init__http_parser_index_offsets_0() { - char[] r = new char[381]; - init__http_parser_trans_keys_0( r ); - return r; + return new short [] { + 0, 0, 6, 13, 21, 24, 26, 28, 30, 32, 34, 36, + 39, 41, 44, 46, 57, 59, 70, 73, 75, 83, 91, 95, + 99, 106, 115, 119, 123, 134, 138, 142, 152, 156, 160, 169, + 178, 182, 186, 193, 200, 207, 214, 221, 228, 235, 242, 249, + 256, 263, 270, 277, 284, 291, 298, 305, 312, 314 + }; } -private static final char _http_parser_trans_keys[] = create__http_parser_trans_keys(); +private static final short _http_parser_index_offsets[] = init__http_parser_index_offsets_0(); -private static void init__http_parser_single_lengths_0( byte[] r ) +private static byte[] init__http_parser_indicies_0() { - r[0]=0; r[1]=2; r[2]=3; r[3]=4; r[4]=2; 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]=5; r[21]=5; r[22]=0; r[23]=0; - r[24]=2; r[25]=7; r[26]=0; r[27]=0; r[28]=9; r[29]=0; r[30]=0; r[31]=8; - r[32]=0; r[33]=0; r[34]=7; r[35]=7; r[36]=0; r[37]=0; 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]=3; r[53]=3; r[54]=3; r[55]=3; - r[56]=1; r[57]=0; + return new byte [] { + 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, + 1, 4, 5, 6, 7, 5, 5, 5, 1, 8, 9, 1, + 10, 1, 11, 1, 12, 1, 13, 1, 14, 1, 15, 1, + 16, 15, 1, 17, 1, 18, 17, 1, 19, 1, 20, 21, + 21, 21, 21, 21, 21, 21, 21, 21, 1, 22, 1, 23, + 24, 23, 23, 23, 23, 23, 23, 23, 23, 1, 26, 27, + 25, 29, 28, 30, 32, 1, 1, 1, 1, 1, 31, 33, + 35, 1, 1, 1, 1, 1, 34, 36, 36, 36, 1, 34, + 34, 34, 1, 37, 38, 37, 37, 37, 37, 1, 8, 1, + 9, 39, 1, 1, 1, 1, 38, 40, 40, 40, 1, 38, + 38, 38, 1, 41, 1, 43, 44, 45, 1, 1, 46, 1, + 1, 42, 47, 47, 47, 1, 42, 42, 42, 1, 8, 1, + 9, 49, 1, 1, 50, 1, 1, 48, 51, 51, 51, 1, + 48, 48, 48, 1, 52, 1, 54, 55, 1, 1, 1, 1, + 53, 56, 1, 58, 59, 1, 1, 1, 1, 57, 60, 60, + 60, 1, 57, 57, 57, 1, 2, 61, 61, 61, 61, 61, + 1, 2, 62, 62, 62, 62, 62, 1, 2, 63, 63, 63, + 63, 63, 1, 2, 64, 64, 64, 64, 64, 1, 2, 65, + 65, 65, 65, 65, 1, 2, 66, 66, 66, 66, 66, 1, + 2, 67, 67, 67, 67, 67, 1, 2, 68, 68, 68, 68, + 68, 1, 2, 69, 69, 69, 69, 69, 1, 2, 70, 70, + 70, 70, 70, 1, 2, 71, 71, 71, 71, 71, 1, 2, + 72, 72, 72, 72, 72, 1, 2, 73, 73, 73, 73, 73, + 1, 2, 74, 74, 74, 74, 74, 1, 2, 75, 75, 75, + 75, 75, 1, 2, 76, 76, 76, 76, 76, 1, 2, 77, + 77, 77, 77, 77, 1, 2, 78, 78, 78, 78, 78, 1, + 2, 1, 1, 0 + }; } -private static byte[] create__http_parser_single_lengths( ) +private static final byte _http_parser_indicies[] = init__http_parser_indicies_0(); + + +private static byte[] init__http_parser_trans_targs_wi_0() { - byte[] r = new byte[58]; - init__http_parser_single_lengths_0( r ); - return r; + return new byte [] { + 2, 0, 3, 38, 4, 24, 28, 25, 5, 20, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 57, 17, + 18, 19, 14, 18, 19, 14, 5, 21, 22, 5, 21, 22, + 23, 24, 25, 26, 27, 5, 28, 20, 29, 31, 34, 30, + 31, 32, 34, 33, 5, 35, 20, 36, 5, 35, 20, 36, + 37, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, + 50, 51, 52, 53, 54, 55, 56 + }; } -private static final byte _http_parser_single_lengths[] = create__http_parser_single_lengths(); +private static final byte _http_parser_trans_targs_wi[] = init__http_parser_trans_targs_wi_0(); -private static void init__http_parser_range_lengths_0( byte[] r ) +private static byte[] init__http_parser_trans_actions_wi_0() { - 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]=2; r[21]=2; r[22]=3; r[23]=3; - r[24]=4; r[25]=1; r[26]=3; r[27]=3; r[28]=1; r[29]=3; r[30]=3; r[31]=1; - r[32]=3; r[33]=3; r[34]=1; r[35]=1; 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]=3; r[53]=3; r[54]=3; r[55]=3; - r[56]=0; r[57]=0; + return new byte [] { + 1, 0, 11, 0, 1, 1, 1, 1, 13, 13, 1, 0, + 0, 0, 0, 0, 0, 0, 19, 0, 0, 28, 23, 3, + 5, 7, 31, 7, 0, 9, 25, 1, 1, 15, 0, 0, + 0, 0, 0, 0, 0, 37, 0, 37, 0, 21, 21, 0, + 0, 0, 0, 0, 40, 17, 40, 17, 34, 0, 34, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0 + }; } -private static byte[] create__http_parser_range_lengths( ) -{ - byte[] r = new byte[58]; - 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]=24; r[6]=26; r[7]=28; - r[8]=30; r[9]=32; r[10]=34; r[11]=36; r[12]=39; r[13]=41; r[14]=44; r[15]=46; - r[16]=57; r[17]=59; r[18]=70; r[19]=73; r[20]=75; r[21]=83; r[22]=91; r[23]=95; - r[24]=99; r[25]=106; r[26]=115; r[27]=119; r[28]=123; r[29]=134; r[30]=138; r[31]=142; - r[32]=152; r[33]=156; r[34]=160; r[35]=169; r[36]=178; r[37]=182; r[38]=186; r[39]=193; - r[40]=200; r[41]=207; r[42]=214; r[43]=221; r[44]=228; r[45]=235; r[46]=242; r[47]=249; - r[48]=256; r[49]=263; r[50]=270; r[51]=277; r[52]=284; r[53]=291; r[54]=298; r[55]=305; - r[56]=312; r[57]=314; -} - -private static short[] create__http_parser_index_offsets( ) -{ - short[] r = new short[58]; - 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]=9; r[23]=1; - r[24]=10; r[25]=1; r[26]=11; r[27]=1; r[28]=12; r[29]=1; r[30]=13; r[31]=1; - r[32]=14; r[33]=1; r[34]=15; r[35]=1; r[36]=16; r[37]=15; r[38]=1; r[39]=17; - r[40]=1; r[41]=18; r[42]=17; r[43]=1; r[44]=19; r[45]=1; r[46]=20; r[47]=21; - r[48]=21; r[49]=21; r[50]=21; r[51]=21; r[52]=21; r[53]=21; r[54]=21; r[55]=21; - r[56]=1; r[57]=22; r[58]=1; r[59]=23; r[60]=24; r[61]=23; r[62]=23; r[63]=23; - r[64]=23; r[65]=23; r[66]=23; r[67]=23; r[68]=23; r[69]=1; r[70]=26; r[71]=27; - r[72]=25; r[73]=26; r[74]=28; r[75]=29; r[76]=31; r[77]=1; r[78]=1; r[79]=1; - r[80]=1; r[81]=1; r[82]=30; r[83]=29; r[84]=33; r[85]=1; r[86]=1; r[87]=1; - r[88]=1; r[89]=1; r[90]=32; r[91]=34; r[92]=34; r[93]=34; r[94]=1; r[95]=32; - r[96]=32; r[97]=32; r[98]=1; r[99]=35; r[100]=36; r[101]=35; r[102]=35; r[103]=35; - r[104]=35; r[105]=1; r[106]=8; r[107]=1; r[108]=9; r[109]=37; r[110]=1; r[111]=1; - r[112]=1; r[113]=1; r[114]=36; r[115]=38; r[116]=38; r[117]=38; r[118]=1; r[119]=36; - r[120]=36; r[121]=36; r[122]=1; r[123]=39; r[124]=1; r[125]=41; r[126]=42; r[127]=43; - r[128]=1; r[129]=1; r[130]=44; r[131]=1; r[132]=1; r[133]=40; r[134]=45; r[135]=45; - r[136]=45; r[137]=1; r[138]=40; r[139]=40; r[140]=40; r[141]=1; r[142]=8; r[143]=1; - r[144]=9; r[145]=47; r[146]=1; r[147]=1; r[148]=48; r[149]=1; r[150]=1; r[151]=46; - r[152]=49; r[153]=49; r[154]=49; r[155]=1; r[156]=46; r[157]=46; r[158]=46; r[159]=1; - r[160]=50; r[161]=1; r[162]=52; r[163]=53; r[164]=1; r[165]=1; r[166]=1; r[167]=1; - r[168]=51; r[169]=54; r[170]=1; r[171]=56; r[172]=57; r[173]=1; r[174]=1; r[175]=1; - r[176]=1; r[177]=55; r[178]=58; r[179]=58; r[180]=58; r[181]=1; r[182]=55; r[183]=55; - r[184]=55; r[185]=1; r[186]=2; r[187]=59; r[188]=59; r[189]=59; r[190]=59; r[191]=59; - r[192]=1; r[193]=2; r[194]=60; r[195]=60; r[196]=60; r[197]=60; r[198]=60; r[199]=1; - r[200]=2; r[201]=61; r[202]=61; r[203]=61; r[204]=61; r[205]=61; r[206]=1; r[207]=2; - r[208]=62; r[209]=62; r[210]=62; r[211]=62; r[212]=62; r[213]=1; r[214]=2; r[215]=63; - r[216]=63; r[217]=63; r[218]=63; r[219]=63; r[220]=1; r[221]=2; r[222]=64; r[223]=64; - r[224]=64; r[225]=64; r[226]=64; r[227]=1; r[228]=2; r[229]=65; r[230]=65; r[231]=65; - r[232]=65; r[233]=65; r[234]=1; r[235]=2; r[236]=66; r[237]=66; r[238]=66; r[239]=66; - r[240]=66; r[241]=1; r[242]=2; r[243]=67; r[244]=67; r[245]=67; r[246]=67; r[247]=67; - r[248]=1; r[249]=2; r[250]=68; r[251]=68; r[252]=68; r[253]=68; r[254]=68; r[255]=1; - r[256]=2; r[257]=69; r[258]=69; r[259]=69; r[260]=69; r[261]=69; r[262]=1; r[263]=2; - r[264]=70; r[265]=70; r[266]=70; r[267]=70; r[268]=70; r[269]=1; r[270]=2; r[271]=71; - r[272]=71; r[273]=71; r[274]=71; r[275]=71; r[276]=1; r[277]=2; r[278]=72; r[279]=72; - r[280]=72; r[281]=72; r[282]=72; r[283]=1; r[284]=2; r[285]=73; r[286]=73; r[287]=73; - r[288]=73; r[289]=73; r[290]=1; r[291]=2; r[292]=74; r[293]=74; r[294]=74; r[295]=74; - r[296]=74; r[297]=1; r[298]=2; r[299]=75; r[300]=75; r[301]=75; r[302]=75; r[303]=75; - r[304]=1; r[305]=2; r[306]=76; r[307]=76; r[308]=76; r[309]=76; r[310]=76; r[311]=1; - r[312]=2; r[313]=1; r[314]=1; r[315]=0; -} - -private static byte[] create__http_parser_indicies( ) -{ - byte[] r = new byte[316]; - 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]=38; r[4]=4; r[5]=24; r[6]=28; r[7]=25; - r[8]=5; r[9]=20; r[10]=6; r[11]=7; r[12]=8; r[13]=9; r[14]=10; r[15]=11; - r[16]=12; r[17]=13; r[18]=14; r[19]=15; r[20]=16; r[21]=17; r[22]=57; r[23]=17; - r[24]=18; r[25]=19; r[26]=14; r[27]=18; r[28]=19; r[29]=5; r[30]=21; r[31]=22; - r[32]=21; r[33]=22; r[34]=23; r[35]=24; r[36]=25; r[37]=26; r[38]=27; r[39]=5; - r[40]=28; r[41]=20; r[42]=29; r[43]=31; r[44]=34; r[45]=30; r[46]=31; r[47]=32; - r[48]=34; r[49]=33; r[50]=5; r[51]=35; r[52]=20; r[53]=36; r[54]=5; r[55]=35; - r[56]=20; r[57]=36; r[58]=37; r[59]=39; r[60]=40; r[61]=41; r[62]=42; r[63]=43; - r[64]=44; r[65]=45; r[66]=46; r[67]=47; r[68]=48; r[69]=49; r[70]=50; r[71]=51; - r[72]=52; r[73]=53; r[74]=54; r[75]=55; r[76]=56; -} - -private static byte[] create__http_parser_trans_targs_wi( ) -{ - byte[] r = new byte[77]; - 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]=13; r[10]=1; r[11]=0; r[12]=0; r[13]=0; r[14]=0; r[15]=0; - r[16]=0; r[17]=0; r[18]=19; r[19]=0; r[20]=0; r[21]=3; r[22]=23; r[23]=0; - r[24]=5; r[25]=7; r[26]=9; r[27]=7; r[28]=0; r[29]=15; r[30]=1; r[31]=1; - r[32]=0; r[33]=0; r[34]=0; r[35]=0; r[36]=0; r[37]=0; r[38]=0; r[39]=28; - r[40]=0; r[41]=28; r[42]=0; r[43]=21; r[44]=21; r[45]=0; r[46]=0; r[47]=0; - r[48]=0; r[49]=0; r[50]=31; r[51]=17; r[52]=31; r[53]=17; r[54]=25; r[55]=0; - r[56]=25; 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; r[67]=0; r[68]=0; r[69]=0; r[70]=0; r[71]=0; - r[72]=0; r[73]=0; r[74]=0; r[75]=0; r[76]=0; -} - -private static byte[] create__http_parser_trans_actions_wi( ) -{ - byte[] r = new byte[77]; - 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(); +private static final byte _http_parser_trans_actions_wi[] = init__http_parser_trans_actions_wi_0(); static final int http_parser_start = 1; @@ -290,83 +218,64 @@ static final int http_parser_error = 0; static final int http_parser_en_main = 1; -// line 68 "http11_parser.java.rl" +// line 91 "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 fragment; - public ElementCB request_path; - public ElementCB query_string; - public ElementCB http_version; - public ElementCB header_done; - - public void init() { - cs = 0; - - -// line 330 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" +int http_parser_init(http_parser *parser) { + int cs = 0; + +// line 227 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" { cs = http_parser_start; } -// line 103 "http11_parser.java.rl" +// line 95 "http11_parser.rl" + parser->cs = cs; + parser->body_start = 0; + parser->content_len = 0; + parser->mark = 0; + parser->nread = 0; + parser->field_len = 0; + parser->field_start = 0; - body_start = 0; - content_len = 0; - mark = 0; - nread = 0; - field_len = 0; - field_start = 0; - } - } + return(1); +} - 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"; +/** exec **/ +size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len, size_t off) { + const char *p, *pe; + int cs = parser->cs; - p = off; - pe = len; - byte[] data = buffer.bytes; - parser.buffer = buffer; + assert(off <= len && "offset past end of buffer"); - -// line 359 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" + p = buffer+off; + pe = buffer+len; + + /* assert(*pe == '\0' && "pointer does not end on NUL"); */ + assert(pe - p == len - off && "pointers aren't same distance"); + + + +// line 259 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" { int _klen; - int _trans; + int _trans = 0; int _acts; int _nacts; int _keys; + int _goto_targ = 0; - if ( p != pe ) { - if ( cs != 0 ) { - _resume: while ( true ) { - _again: do { + _goto: while (true) { + switch ( _goto_targ ) { + case 0: + if ( p == pe ) { + _goto_targ = 4; + continue _goto; + } + if ( cs == 0 ) { + _goto_targ = 5; + continue _goto; + } +case 1: _match: do { _keys = _http_parser_key_offsets[cs]; _trans = _http_parser_index_offsets[cs]; @@ -419,154 +328,147 @@ static final int http_parser_en_main = 1; _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 ) + if ( _http_parser_trans_actions_wi[_trans] != 0 ) { + _acts = _http_parser_trans_actions_wi[_trans]; + _nacts = (int) _http_parser_actions[_acts++]; + while ( _nacts-- > 0 ) { - switch ( _http_parser_actions[_acts++] ) - { + switch ( _http_parser_actions[_acts++] ) + { case 0: -// line 13 "http11_parser.java.rl" - {parser.mark = p; } +// line 34 "http11_parser.rl" + {MARK(mark, p); } break; case 1: -// line 15 "http11_parser.java.rl" - { parser.field_start = p; } +// line 37 "http11_parser.rl" + { MARK(field_start, p); } break; case 2: -// line 16 "http11_parser.java.rl" - { - parser.field_len = p-parser.field_start; - } +// line 38 "http11_parser.rl" + { snake_upcase_char((char *)p); } break; case 3: -// line 20 "http11_parser.java.rl" - { parser.mark = p; } - break; - case 4: -// line 21 "http11_parser.java.rl" +// line 39 "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); - } + parser->field_len = LEN(field_start, p); } + break; + case 4: +// line 43 "http11_parser.rl" + { MARK(mark, p); } break; case 5: -// line 26 "http11_parser.java.rl" +// line 44 "http11_parser.rl" { - if(parser.request_method != null) - parser.request_method.call(parser.data, parser.mark, p-parser.mark); + if(parser->http_field != NULL) { + parser->http_field(parser->data, PTR_TO(field_start), parser->field_len, PTR_TO(mark), LEN(mark, p)); + } } break; case 6: -// line 30 "http11_parser.java.rl" +// line 49 "http11_parser.rl" { - if(parser.request_uri != null) - parser.request_uri.call(parser.data, parser.mark, p-parser.mark); + if(parser->request_method != NULL) + parser->request_method(parser->data, PTR_TO(mark), LEN(mark, p)); } break; case 7: -// line 34 "http11_parser.java.rl" +// line 53 "http11_parser.rl" { - if(parser.fragment != null) - parser.fragment.call(parser.data, parser.mark, p-parser.mark); + if(parser->request_uri != NULL) + parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } break; case 8: -// line 39 "http11_parser.java.rl" - {parser.query_start = p; } - break; - case 9: -// line 40 "http11_parser.java.rl" +// line 57 "http11_parser.rl" { - if(parser.query_string != null) - parser.query_string.call(parser.data, parser.query_start, p-parser.query_start); + if(parser->fragment != NULL) + parser->fragment(parser->data, PTR_TO(mark), LEN(mark, p)); } + break; + case 9: +// line 62 "http11_parser.rl" + {MARK(query_start, p); } break; case 10: -// line 45 "http11_parser.java.rl" - { - if(parser.http_version != null) - parser.http_version.call(parser.data, parser.mark, p-parser.mark); +// line 63 "http11_parser.rl" + { + if(parser->query_string != NULL) + parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, p)); } break; case 11: -// line 50 "http11_parser.java.rl" - { - if(parser.request_path != null) - parser.request_path.call(parser.data, parser.mark, p-parser.mark); +// line 68 "http11_parser.rl" + { + if(parser->http_version != NULL) + parser->http_version(parser->data, PTR_TO(mark), LEN(mark, p)); } break; case 12: -// line 55 "http11_parser.java.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; +// line 73 "http11_parser.rl" + { + if(parser->request_path != NULL) + parser->request_path(parser->data, PTR_TO(mark), LEN(mark,p)); } break; -// line 513 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" + case 13: +// line 78 "http11_parser.rl" + { + parser->body_start = p - buffer + 1; + if(parser->header_done != NULL) + parser->header_done(parser->data, p + 1, pe - p - 1); + { p += 1; _goto_targ = 5; if (true) continue _goto;} + } + break; +// line 424 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" + } } } - } while (false); - if ( cs == 0 ) - break _resume; - if ( ++p == pe ) - break _resume; +case 2: + if ( cs == 0 ) { + _goto_targ = 5; + continue _goto; } - } } + if ( ++p != pe ) { + _goto_targ = 1; + continue _goto; } -// line 127 "http11_parser.java.rl" +case 4: +case 5: + } + break; } + } +// line 122 "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"; + parser->cs = cs; + parser->nread += p - (buffer + off); - if(parser.body_start>0) { - /* final \r\n combo encountered so stop right here */ - -// line 540 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" -// line 141 "http11_parser.java.rl" - parser.nread++; - } + 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"); - return parser.nread; - } + return(parser->nread); +} - public int finish() { - int cs = parser.cs; - - -// line 552 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" -// line 151 "http11_parser.java.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; +int http_parser_finish(http_parser *parser) +{ + if (http_parser_has_error(parser) ) { + return -1; + } else if (http_parser_is_finished(parser) ) { + return 1; + } else { + return 0; } } + +int http_parser_has_error(http_parser *parser) { + return parser->cs == http_parser_error; +} + +int http_parser_is_finished(http_parser *parser) { + return parser->cs >= http_parser_first_final; +} diff --git a/lib/mongrel.rb b/lib/mongrel.rb index d99c56d0..19633220 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -200,7 +200,7 @@ module Mongrel STDERR.puts "#{Time.now}: Client error: #{e.inspect}" STDERR.puts e.backtrace.join("\n") end - request.body.delete if request and request.body.class == Tempfile + request.body.close! if request and request.body.class == Tempfile end end @@ -320,10 +320,15 @@ module Mongrel def register(uri, handler, in_front=false) begin @classifier.register(uri, [handler]) - rescue URIClassifier::RegistrationError + rescue URIClassifier::RegistrationError => e handlers = @classifier.resolve(uri)[2] - method_name = in_front ? 'unshift' : 'push' - handlers.send(method_name, handler) + if handlers + # Already registered + method_name = in_front ? 'unshift' : 'push' + handlers.send(method_name, handler) + else + raise + end end handler.listener = self end diff --git a/lib/mongrel/cgi.rb b/lib/mongrel/cgi.rb index 4173bde9..39576111 100644 --- a/lib/mongrel/cgi.rb +++ b/lib/mongrel/cgi.rb @@ -26,7 +26,7 @@ module Mongrel # Refer to DirHandler#can_serve for more information on this. class CGIWrapper < ::CGI public :env_table - attr_reader :options + attr_reader :head attr_accessor :handler # Set this to false if you want calls to CGIWrapper.out to not actually send # the response until you force it. @@ -105,7 +105,7 @@ module Mongrel when Hash cookie.each_value {|c| to['Set-Cookie'] = c.to_s} else - to['Set-Cookie'] = options['cookie'].to_s + to['Set-Cookie'] = head['cookie'].to_s end @head.delete('cookie') diff --git a/lib/mongrel/http_request.rb b/lib/mongrel/http_request.rb index 82ffe428..c8d4ce40 100644 --- a/lib/mongrel/http_request.rb +++ b/lib/mongrel/http_request.rb @@ -93,7 +93,7 @@ module Mongrel STDERR.puts e.backtrace.join("\n") # any errors means we should delete the file, including if the file is dumped @socket.close rescue nil - @body.delete if @body.class == Tempfile + @body.close! if @body.class == Tempfile @body = nil # signals that there was a problem end end diff --git a/lib/mongrel/http_response.rb b/lib/mongrel/http_response.rb index 32e433e3..30767127 100644 --- a/lib/mongrel/http_response.rb +++ b/lib/mongrel/http_response.rb @@ -75,7 +75,10 @@ module Mongrel elsif @header_sent raise "You have already sent the request headers." else - @header.out.truncate(0) + # XXX Dubious ( http://mongrel.rubyforge.org/ticket/19 ) + @header.out.close + @header = HeaderOut.new(StringIO.new) + @body.close @body = StringIO.new end diff --git a/projects/gem_plugin/CHANGELOG b/projects/gem_plugin/CHANGELOG index 7e1103da..cf39b5ba 100644 --- a/projects/gem_plugin/CHANGELOG +++ b/projects/gem_plugin/CHANGELOG @@ -1,2 +1,4 @@ +v0.3. Use Gem.path, not Gem.dir, so that local gem repositories work (rooster). + v0.2.3. Signed gem. diff --git a/projects/gem_plugin/lib/gem_plugin.rb b/projects/gem_plugin/lib/gem_plugin.rb index 1996a613..4664aa13 100644 --- a/projects/gem_plugin/lib/gem_plugin.rb +++ b/projects/gem_plugin/lib/gem_plugin.rb @@ -105,8 +105,8 @@ module GemPlugin # To prevent this load requires the full path to the "init.rb" file, which # avoids the RubyGems autorequire magic. def load(needs = {}) - sdir = File.join(Gem.dir, "specifications") - gems = Gem::SourceIndex.from_installed_gems(sdir) + sdirs = Gem::SourceIndex.installed_spec_directories + gems = Gem::SourceIndex.from_gems_in(sdirs) needs = needs.merge({"gem_plugin" => INCLUDE}) gems.each do |path, gem| @@ -128,8 +128,11 @@ module GemPlugin # looks like no needs were set to false, so it's good # Previously was set wrong, we already have the correct gem path! - #gem_dir = File.join(Gem.dir, "gems", "#{gem.name}-#{gem.version}") - gem_dir = File.join(Gem.dir, "gems", path) + gem_dir = "" + Gem.path.each do |gem_path| + gem_dir = File.join(gem_path, "gems", path) + break if File.exists?(gem_dir) + end require File.join(gem_dir, "lib", gem.name, "init.rb") @gems[gem.name] = gem_dir diff --git a/projects/mongrel_cluster/lib/mongrel_cluster/init.rb b/projects/mongrel_cluster/lib/mongrel_cluster/init.rb index 1c3a613c..0f18fe98 100644 --- a/projects/mongrel_cluster/lib/mongrel_cluster/init.rb +++ b/projects/mongrel_cluster/lib/mongrel_cluster/init.rb @@ -59,7 +59,7 @@ module Cluster def start read_options - argv = [ "mongrel_rails" ] + argv = @options['mongrel_rails'] argv << "start" argv << "-d" argv << "-e #{@options['environment']}" if @options['environment'] @@ -103,7 +103,7 @@ module Cluster def stop read_options - argv = [ "mongrel_rails" ] + argv = @options['mongrel_rails'] argv << "stop" argv << "-c #{@options["cwd"]}" if @options["cwd"] argv << "-f" if @force @@ -300,6 +300,7 @@ module Cluster ['-C', '--config PATH', "Path to cluster configuration file", :@config_file, "config/mongrel_cluster.yml"], ['', '--user USER', "User to run as", :@user, nil], ['', '--group GROUP', "Group to run as", :@group, nil], + ['', '--mongrel_rails PATH', "Full path to mongrel_rails script", :@mongrel_rails, "mongrel_rails"], ['', '--prefix PREFIX', "Rails prefix to use", :@prefix, nil] ] end @@ -334,6 +335,7 @@ module Cluster @options["user"] = @user if @user @options["group"] = @group if @group @options["prefix"] = @prefix if @prefix + @options["mongrel_rails"] = @mongrel_rails if @mongrel_rails log "Writing configuration file to #{@config_file}." File.open(@config_file,"w") {|f| f.write(@options.to_yaml)} diff --git a/projects/mongrel_cluster/lib/mongrel_cluster/recipes_2.rb b/projects/mongrel_cluster/lib/mongrel_cluster/recipes_2.rb index a82c424b..312ef5c4 100644 --- a/projects/mongrel_cluster/lib/mongrel_cluster/recipes_2.rb +++ b/projects/mongrel_cluster/lib/mongrel_cluster/recipes_2.rb @@ -7,7 +7,7 @@ Capistrano::Configuration.instance.load do set :mongrel_user, nil set :mongrel_group, nil set :mongrel_prefix, nil - set :mongrel_rails, 'mongrel_rails' + set :mongrel_rails, "mongrel_rails" set :mongrel_clean, false set :mongrel_pid_file, nil set :mongrel_log_file, nil @@ -37,6 +37,7 @@ Capistrano::Configuration.instance.load do argv << "--group #{mongrel_group}" if mongrel_group argv << "--prefix #{mongrel_prefix}" if mongrel_prefix argv << "-S #{mongrel_config_script}" if mongrel_config_script + argv << "--mongrel_rails #{mongrel_rails}" if mongrel_rails cmd = argv.join " " send(run_method, cmd) end diff --git a/test/test_handlers.rb b/test/test_handlers.rb index 1005dd05..b490a8ed 100644 --- a/test/test_handlers.rb +++ b/test/test_handlers.rb @@ -61,6 +61,16 @@ class HandlersTest < Test::Unit::TestCase @config.stop(false, true) File.delete "/tmp/testfile" end + + def test_registration_exception_is_not_lost + assert_raises(Mongrel::URIClassifier::RegistrationError) do + @config = Mongrel::Configurator.new do + listener do + uri "bogus", :handler => SimpleHandler.new + end + end + end + end def test_more_web_server res = hit([ "http://localhost:9998/test", diff --git a/test/test_http11.rb b/test/test_http11.rb deleted file mode 100644 index da311af8..00000000 --- a/test/test_http11.rb +++ /dev/null @@ -1,156 +0,0 @@ -# Copyright (c) 2005 Zed A. Shaw -# You can redistribute it and/or modify it under the same terms as Ruby. -# -# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html -# for more information. - -require 'test/testhelp' - -include Mongrel - -class HttpParserTest < Test::Unit::TestCase - - def 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" - - assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL'] - assert_equal '/', req['REQUEST_PATH'] - assert_equal 'HTTP/1.1', req['HTTP_VERSION'] - assert_equal '/', req['REQUEST_URI'] - assert_equal 'CGI/1.2', req['GATEWAY_INTERFACE'] - assert_equal 'GET', req['REQUEST_METHOD'] - assert_nil req['FRAGMENT'] - assert_nil req['QUERY_STRING'] - - parser.reset - assert parser.nread == 0, "Number read after reset should be 0" - end - - def test_parse_dumbfuck_headers - parser = HttpParser.new - req = {} - should_be_good = "GET / HTTP/1.1\r\naaaaaaaaaaaaa:++++++++++\r\n\r\n" - nread = parser.execute(req, should_be_good, 0) - assert_equal should_be_good.length, nread - assert parser.finished? - assert !parser.error? - - nasty_pound_header = "GET / HTTP/1.1\r\nX-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgEBBAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n\tRA==\r\n\t-----END CERTIFICATE-----\r\n\r\n" - parser = HttpParser.new - req = {} - #nread = parser.execute(req, nasty_pound_header, 0) - #assert_equal nasty_pound_header.length, nread - #assert parser.finished? - #assert !parser.error? - end - - def 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 - - def test_fragment_in_uri - parser = HttpParser.new - req = {} - get = "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n\r\n" - assert_nothing_raised do - parser.execute(req, get, 0) - end - assert parser.finished? - assert_equal '/forums/1/topics/2375?page=1', req['REQUEST_URI'] - assert_equal 'posts-17408', req['FRAGMENT'] - 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 * 100).to_s) * (count / 40) - else - res << Digest::SHA1.digest(rand(count * 100).to_s) * (count / 20) - end - - return res - end - - - def test_horrible_queries - parser = HttpParser.new - - # then that large header names are caught - 10.times do |c| - 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 - - # then that large mangled field values are caught - 10.times do |c| - 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 - - # 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 - - # finally just that random garbage gets blocked all the time - 10.times do |c| - 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 - - - - def test_query_parse - res = HttpRequest.query_parse("zed=1&frank=#{HttpRequest.escape('&&& ')}") - assert res["zed"], "didn't get the request right" - assert res["frank"], "no frank" - assert_equal "1", res["zed"], "wrong result" - assert_equal "&&& ", HttpRequest.unescape(res["frank"]), "wrong result" - - res = HttpRequest.query_parse("zed=1&zed=2&zed=3&frank=11;zed=45") - assert res["zed"], "didn't get the request right" - assert res["frank"], "no frank" - assert_equal 4,res["zed"].length, "wrong number for zed" - assert_equal "11",res["frank"], "wrong number for frank" - end - -end - From 8a0b96905e3f71bbfc5a12106f8eb0f0a5b80cc8 Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Fri, 27 Mar 2009 02:48:43 -0700 Subject: [PATCH 3/4] bumped the release to 1.1.6 --- CHANGELOG | 1 + lib/mongrel/const.rb | 2 +- test/test_http11.rb | 156 +++++++++++++++++++++++++++++ test/unit/test_http_parser.rb.orig | 156 +++++++++++++++++++++++++++++ 4 files changed, 314 insertions(+), 1 deletion(-) create mode 100644 test/test_http11.rb create mode 100644 test/unit/test_http_parser.rb.orig diff --git a/CHANGELOG b/CHANGELOG index 637fe895..44346c92 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +v1.1.6 Ruby1.9 compatible v1.1.5. Fix bug where num_processors is not actually set from mongrel_rails. diff --git a/lib/mongrel/const.rb b/lib/mongrel/const.rb index 9b4109ec..d87ed8a3 100644 --- a/lib/mongrel/const.rb +++ b/lib/mongrel/const.rb @@ -65,7 +65,7 @@ module Mongrel REQUEST_URI='REQUEST_URI'.freeze REQUEST_PATH='REQUEST_PATH'.freeze - MONGREL_VERSION="1.1.5".freeze + MONGREL_VERSION="1.1.6".freeze MONGREL_TMP_BASE="mongrel".freeze diff --git a/test/test_http11.rb b/test/test_http11.rb new file mode 100644 index 00000000..da311af8 --- /dev/null +++ b/test/test_http11.rb @@ -0,0 +1,156 @@ +# Copyright (c) 2005 Zed A. Shaw +# You can redistribute it and/or modify it under the same terms as Ruby. +# +# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html +# for more information. + +require 'test/testhelp' + +include Mongrel + +class HttpParserTest < Test::Unit::TestCase + + def 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" + + assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL'] + assert_equal '/', req['REQUEST_PATH'] + assert_equal 'HTTP/1.1', req['HTTP_VERSION'] + assert_equal '/', req['REQUEST_URI'] + assert_equal 'CGI/1.2', req['GATEWAY_INTERFACE'] + assert_equal 'GET', req['REQUEST_METHOD'] + assert_nil req['FRAGMENT'] + assert_nil req['QUERY_STRING'] + + parser.reset + assert parser.nread == 0, "Number read after reset should be 0" + end + + def test_parse_dumbfuck_headers + parser = HttpParser.new + req = {} + should_be_good = "GET / HTTP/1.1\r\naaaaaaaaaaaaa:++++++++++\r\n\r\n" + nread = parser.execute(req, should_be_good, 0) + assert_equal should_be_good.length, nread + assert parser.finished? + assert !parser.error? + + nasty_pound_header = "GET / HTTP/1.1\r\nX-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgEBBAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n\tRA==\r\n\t-----END CERTIFICATE-----\r\n\r\n" + parser = HttpParser.new + req = {} + #nread = parser.execute(req, nasty_pound_header, 0) + #assert_equal nasty_pound_header.length, nread + #assert parser.finished? + #assert !parser.error? + end + + def 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 + + def test_fragment_in_uri + parser = HttpParser.new + req = {} + get = "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n\r\n" + assert_nothing_raised do + parser.execute(req, get, 0) + end + assert parser.finished? + assert_equal '/forums/1/topics/2375?page=1', req['REQUEST_URI'] + assert_equal 'posts-17408', req['FRAGMENT'] + 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 * 100).to_s) * (count / 40) + else + res << Digest::SHA1.digest(rand(count * 100).to_s) * (count / 20) + end + + return res + end + + + def test_horrible_queries + parser = HttpParser.new + + # then that large header names are caught + 10.times do |c| + 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 + + # then that large mangled field values are caught + 10.times do |c| + 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 + + # 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 + + # finally just that random garbage gets blocked all the time + 10.times do |c| + 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 + + + + def test_query_parse + res = HttpRequest.query_parse("zed=1&frank=#{HttpRequest.escape('&&& ')}") + assert res["zed"], "didn't get the request right" + assert res["frank"], "no frank" + assert_equal "1", res["zed"], "wrong result" + assert_equal "&&& ", HttpRequest.unescape(res["frank"]), "wrong result" + + res = HttpRequest.query_parse("zed=1&zed=2&zed=3&frank=11;zed=45") + assert res["zed"], "didn't get the request right" + assert res["frank"], "no frank" + assert_equal 4,res["zed"].length, "wrong number for zed" + assert_equal "11",res["frank"], "wrong number for frank" + end + +end + diff --git a/test/unit/test_http_parser.rb.orig b/test/unit/test_http_parser.rb.orig new file mode 100644 index 00000000..d8f3fe8e --- /dev/null +++ b/test/unit/test_http_parser.rb.orig @@ -0,0 +1,156 @@ +# Copyright (c) 2005 Zed A. Shaw +# You can redistribute it and/or modify it under the same terms as Ruby. +# +# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html +# for more information. + +require 'test/test_helper' + +include Mongrel + +class HttpParserTest < Test::Unit::TestCase + + def 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" + + assert_equal 'HTTP/1.1', req['SERVER_PROTOCOL'] + assert_equal '/', req['REQUEST_PATH'] + assert_equal 'HTTP/1.1', req['HTTP_VERSION'] + assert_equal '/', req['REQUEST_URI'] + assert_equal 'CGI/1.2', req['GATEWAY_INTERFACE'] + assert_equal 'GET', req['REQUEST_METHOD'] + assert_nil req['FRAGMENT'] + assert_nil req['QUERY_STRING'] + + parser.reset + assert parser.nread == 0, "Number read after reset should be 0" + end + + def test_parse_strange_headers + parser = HttpParser.new + req = {} + should_be_good = "GET / HTTP/1.1\r\naaaaaaaaaaaaa:++++++++++\r\n\r\n" + nread = parser.execute(req, should_be_good, 0) + assert_equal should_be_good.length, nread + assert parser.finished? + assert !parser.error? + + nasty_pound_header = "GET / HTTP/1.1\r\nX-SSL-Bullshit: -----BEGIN CERTIFICATE-----\r\n\tMIIFbTCCBFWgAwIBAgICH4cwDQYJKoZIhvcNAQEFBQAwcDELMAkGA1UEBhMCVUsx\r\n\tETAPBgNVBAoTCGVTY2llbmNlMRIwEAYDVQQLEwlBdXRob3JpdHkxCzAJBgNVBAMT\r\n\tAkNBMS0wKwYJKoZIhvcNAQkBFh5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMu\r\n\tdWswHhcNMDYwNzI3MTQxMzI4WhcNMDcwNzI3MTQxMzI4WjBbMQswCQYDVQQGEwJV\r\n\tSzERMA8GA1UEChMIZVNjaWVuY2UxEzARBgNVBAsTCk1hbmNoZXN0ZXIxCzAJBgNV\r\n\tBAcTmrsogriqMWLAk1DMRcwFQYDVQQDEw5taWNoYWVsIHBhcmQYJKoZIhvcNAQEB\r\n\tBQADggEPADCCAQoCggEBANPEQBgl1IaKdSS1TbhF3hEXSl72G9J+WC/1R64fAcEF\r\n\tW51rEyFYiIeZGx/BVzwXbeBoNUK41OK65sxGuflMo5gLflbwJtHBRIEKAfVVp3YR\r\n\tgW7cMA/s/XKgL1GEC7rQw8lIZT8RApukCGqOVHSi/F1SiFlPDxuDfmdiNzL31+sL\r\n\t0iwHDdNkGjy5pyBSB8Y79dsSJtCW/iaLB0/n8Sj7HgvvZJ7x0fr+RQjYOUUfrePP\r\n\tu2MSpFyf+9BbC/aXgaZuiCvSR+8Snv3xApQY+fULK/xY8h8Ua51iXoQ5jrgu2SqR\r\n\twgA7BUi3G8LFzMBl8FRCDYGUDy7M6QaHXx1ZWIPWNKsCAwEAAaOCAiQwggIgMAwG\r\n\tA1UdEwEB/wQCMAAwEQYJYIZIAYb4QgEBBAQDAgWgMA4GA1UdDwEB/wQEAwID6DAs\r\n\tBglghkgBhvhCAQ0EHxYdVUsgZS1TY2llbmNlIFVzZXIgQ2VydGlmaWNhdGUwHQYD\r\n\tVR0OBBYEFDTt/sf9PeMaZDHkUIldrDYMNTBZMIGaBgNVHSMEgZIwgY+AFAI4qxGj\r\n\tloCLDdMVKwiljjDastqooXSkcjBwMQswCQYDVQQGEwJVSzERMA8GA1UEChMIZVNj\r\n\taWVuY2UxEjAQBgNVBAsTCUF1dGhvcml0eTELMAkGA1UEAxMCQ0ExLTArBgkqhkiG\r\n\t9w0BCQEWHmNhLW9wZXJhdG9yQGdyaWQtc3VwcG9ydC5hYy51a4IBADApBgNVHRIE\r\n\tIjAggR5jYS1vcGVyYXRvckBncmlkLXN1cHBvcnQuYWMudWswGQYDVR0gBBIwEDAO\r\n\tBgwrBgEEAdkvAQEBAQYwPQYJYIZIAYb4QgEEBDAWLmh0dHA6Ly9jYS5ncmlkLXN1\r\n\tcHBvcnQuYWMudmT4sopwqlBWsvcHViL2NybC9jYWNybC5jcmwwPQYJYIZIAYb4QgEDBDAWLmh0\r\n\tdHA6Ly9jYS5ncmlkLXN1cHBvcnQuYWMudWsvcHViL2NybC9jYWNybC5jcmwwPwYD\r\n\tVR0fBDgwNjA0oDKgMIYuaHR0cDovL2NhLmdyaWQt5hYy51ay9wdWIv\r\n\tY3JsL2NhY3JsLmNybDANBgkqhkiG9w0BAQUFAAOCAQEAS/U4iiooBENGW/Hwmmd3\r\n\tXCy6Zrt08YjKCzGNjorT98g8uGsqYjSxv/hmi0qlnlHs+k/3Iobc3LjS5AMYr5L8\r\n\tUO7OSkgFFlLHQyC9JzPfmLCAugvzEbyv4Olnsr8hbxF1MbKZoQxUZtMVu29wjfXk\r\n\thTeApBv7eaKCWpSp7MCbvgzm74izKhu3vlDk9w6qVrxePfGgpKPqfHiOoGhFnbTK\r\n\twTC6o2xq5y0qZ03JonF7OJspEd3I5zKY3E+ov7/ZhW6DqT8UFvsAdjvQbXyhV8Eu\r\n\tYhixw1aKEPzNjNowuIseVogKOLXxWI5vAi5HgXdS0/ES5gDGsABo4fqovUKlgop3\r\n\tRA==\r\n\t-----END CERTIFICATE-----\r\n\r\n" + parser = HttpParser.new + req = {} + nread = parser.execute(req, nasty_pound_header, 0) + assert_equal nasty_pound_header.length, nread + assert parser.finished? + assert !parser.error? + end + + def 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 + + def test_fragment_in_uri + parser = HttpParser.new + req = {} + get = "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n\r\n" + assert_nothing_raised do + parser.execute(req, get, 0) + end + assert parser.finished? + assert_equal '/forums/1/topics/2375?page=1', req['REQUEST_URI'] + assert_equal 'posts-17408', req['FRAGMENT'] + 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 * 100).to_s) * (count / 40) + else + res << Digest::SHA1.digest(rand(count * 100).to_s) * (count / 20) + end + + return res + end + + + def test_horrible_queries + parser = HttpParser.new + + # then that large header names are caught + 10.times do |c| + 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 + + # then that large mangled field values are caught + 10.times do |c| + 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 + + # 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 + + # finally just that random garbage gets blocked all the time + 10.times do |c| + 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 + + + + def test_query_parse + res = HttpRequest.query_parse("zed=1&frank=#{HttpRequest.escape('&&& ')}") + assert res["zed"], "didn't get the request right" + assert res["frank"], "no frank" + assert_equal "1", res["zed"], "wrong result" + assert_equal "&&& ", HttpRequest.unescape(res["frank"]), "wrong result" + + res = HttpRequest.query_parse("zed=1&zed=2&zed=3&frank=11;zed=45") + assert res["zed"], "didn't get the request right" + assert res["frank"], "no frank" + assert_equal 4,res["zed"].length, "wrong number for zed" + assert_equal "11",res["frank"], "wrong number for frank" + end + +end + From 237c3a0c397265d6e2e78a8f83f984d7c8d04d9b Mon Sep 17 00:00:00 2001 From: Evan Weaver Date: Tue, 7 Jul 2009 16:21:43 -0700 Subject: [PATCH 4/4] Missed a constant. --- ext/http11_java/org/jruby/mongrel/Http11.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/http11_java/org/jruby/mongrel/Http11.java b/ext/http11_java/org/jruby/mongrel/Http11.java index 79a950bc..63acd5df 100644 --- a/ext/http11_java/org/jruby/mongrel/Http11.java +++ b/ext/http11_java/org/jruby/mongrel/Http11.java @@ -215,7 +215,7 @@ public class Http11 extends RubyObject { 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.1.5")); + req.aset(runtime.newString("SERVER_SOFTWARE"),runtime.newString("Mongrel 1.1.6")); } };