diff options
61 files changed, 1677 insertions, 1201 deletions
@@ -1,4 +1,8 @@ +v1.1.4. Fix camping handler. Correct treatment of @throttle parameter. + +v1.1.3. Fix security flaw of DirHandler; reported on mailing list. + v1.1.2. Fix worker termination bug; fix JRuby 1.0.3 load order issue; fix require issue on systems without Rubygems. v1.1.1. Fix mongrel_rails restart bug; fix bug with Rack status codes. diff --git a/CONTRIBUTORS b/CONTRIBUTORS new file mode 100644 index 0000000..3268298 --- /dev/null +++ b/CONTRIBUTORS @@ -0,0 +1,17 @@ + +Zed A. Shaw +Luis Lavena +Wilson Bilkovich +Why the Lucky Stiff +Dan Kubb +MenTaLguY +Filipe Lautert +Rick Olson +Wayne E. Seguin +Kirk Haines +Bradley Taylor +Matt Pelletier +Ry Dahl +Nick Sieger +Evan Weaver +Marc-André Cournoyer @@ -1,5 +1,6 @@ bin/mongrel_rails CHANGELOG +CONTRIBUTORS COPYING examples/builder.rb examples/camping/blog.rb @@ -37,7 +38,6 @@ lib/mongrel/header_out.rb lib/mongrel/http_request.rb lib/mongrel/http_response.rb lib/mongrel/init.rb -lib/mongrel/logger.rb lib/mongrel/mime_types.yml lib/mongrel/rails.rb lib/mongrel/stats.rb @@ -47,24 +47,25 @@ lib/mongrel.rb LICENSE Manifest mongrel-public_cert.pem -mongrel.gemspec README setup.rb +test/benchmark/previous.rb +test/benchmark/simple.rb +test/benchmark/utils.rb test/mime.yaml test/mongrel.conf -test/test_cgi_wrapper.rb -test/test_command.rb -test/test_conditional.rb -test/test_configurator.rb -test/test_debug.rb -test/test_handlers.rb -test/test_http11.rb -test/test_redirect_handler.rb -test/test_request_progress.rb -test/test_response.rb -test/test_stats.rb -test/test_uriclassifier.rb -test/test_ws.rb -test/testhelp.rb -TODO -tools/trickletest.rb +test/test_helper.rb +test/tools/trickletest.rb +test/unit/test_cgi_wrapper.rb +test/unit/test_command.rb +test/unit/test_conditional.rb +test/unit/test_configurator.rb +test/unit/test_debug.rb +test/unit/test_handlers.rb +test/unit/test_http_parser.rb +test/unit/test_redirect_handler.rb +test/unit/test_request_progress.rb +test/unit/test_response.rb +test/unit/test_stats.rb +test/unit/test_uriclassifier.rb +test/unit/test_ws.rb @@ -1,48 +1,46 @@ require 'rubygems' -gem 'echoe', '>=2.7.5' +gem 'echoe', '>=2.7.11' require 'echoe' e = Echoe.new("mongrel") do |p| p.summary = "A small fast HTTP library and server that runs Rails, Camping, Nitro and Iowa apps." - p.author ="Zed A. Shaw" - p.clean_pattern = ['ext/http11/*.{bundle,so,o,obj,pdb,lib,def,exp}', 'lib/*.{bundle,so,o,obj,pdb,lib,def,exp}', 'ext/http11/Makefile', 'pkg', 'lib/*.bundle', '*.gem', 'site/output', '.config', 'lib/http11.jar', 'ext/http11_java/classes', 'coverage'] + p.author = "Zed A. Shaw" + p.email = "mongrel-development@rubyforge.org" + p.clean_pattern = ['ext/http11/*.{bundle,so,o,obj,pdb,lib,def,exp}', 'lib/*.{bundle,so,o,obj,pdb,lib,def,exp}', 'ext/http11/Makefile', 'pkg', 'lib/*.bundle', '*.gem', 'site/output', '.config', 'lib/http11.jar', 'ext/http11_java/classes', 'coverage', 'test_*.log', 'log', 'doc'] p.url = "http://mongrel.rubyforge.org" - p.rdoc_pattern = ['README', 'LICENSE', 'CHANGELOG', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc'] + p.rdoc_pattern = ['README', 'LICENSE', 'CONTRIBUTORS', 'CHANGELOG', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc'] + p.docs_host = 'mongrel.cloudbur.st:/home/eweaver/www/mongrel/htdocs/web' p.ignore_pattern = /^(pkg|site|projects|doc|log)|CVS|\.log/ p.ruby_version = '>=1.8.4' p.dependencies = ['gem_plugin >=0.2.3'] p.extension_pattern = nil - p.certificate_chain = case ENV['USER'] + p.certificate_chain = case (ENV['USER'] || ENV['USERNAME']).downcase when 'eweaver' ['~/p/configuration/gem_certificates/mongrel/mongrel-public_cert.pem', '~/p/configuration/gem_certificates/evan_weaver-mongrel-public_cert.pem'] - when 'luislavena' - ['~/gem_certificates/mongrel-public_cert.pem', - '~/gem_certificates/luislavena-mongrel-public_cert.pem'] + when 'luislavena', 'luis' + ['~/projects/gem_certificates/mongrel-public_cert.pem', + '~/projects/gem_certificates/luislavena-mongrel-public_cert.pem'] end p.need_tar_gz = false p.need_tgz = true - if RUBY_PLATFORM !~ /mswin|java/ + unless Platform.windows? or Platform.java? p.extension_pattern = ["ext/**/extconf.rb"] end p.eval = proc do - case RUBY_PLATFORM - when /mswin/ + if Platform.windows? self.files += ['lib/http11.so'] self.platform = Gem::Platform::CURRENT - add_dependency('cgi_multipart_eof_fix', '>= 2.4') - when /java/ + elsif Platform.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') end end @@ -55,13 +53,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 @@ -84,19 +82,18 @@ def java_classpath_arg classpath ? "-cp #{classpath}" : "" end -case RUBY_PLATFORM -when /mswin/ +if Platform.windows? filename = "lib/http11.so" file filename do Dir.chdir("ext/http11") do ruby "extconf.rb" - system(PLATFORM =~ /mswin/ ? 'nmake' : 'make') + system(Platform.make) end move_extensions end task :compile => [filename] -when /java/ +elsif Platform.java? # Avoid JRuby in-process launching problem begin @@ -123,13 +120,17 @@ end def sub_project(project, *targets) targets.each do |target| Dir.chdir "projects/#{project}" do - unless RUBY_PLATFORM =~ /mswin/ - sh("rake #{target.to_s}") # --trace - end + sh("#{Platform.rake} #{target.to_s}") # --trace end end end +desc "Compile all the projects" +task :compile_all => [:compile] do + sub_project("fastthread", :compile) + sub_project("mongrel_service", :compile) +end + desc "Package Mongrel and all subprojects" task :package_all => [:package] do sub_project("gem_plugin", :package) @@ -141,11 +142,9 @@ task :package_all => [:package] do sub_project("mongrel_cluster", :package) sub_project("mongrel_experimental", :package) - sh("rake java package") unless RUBY_PLATFORM =~ /java/ + sh("rake java package") unless Platform.windows? - # XXX Broken by RubyGems 0.9.5 - # sub_project("mongrel_service", :package) if RUBY_PLATFORM =~ /mswin/ - # sh("rake mswin package") unless RUBY_PLATFORM =~ /mswin/ + sub_project("mongrel_service", :package) if Platform.windows? end task :install_requirements do @@ -163,7 +162,7 @@ task :install => [:install_requirements] do sub_project("mongrel_console", :install) sub_project("mongrel_cluster", :install) # sub_project("mongrel_experimental", :install) - sub_project("mongrel_service", :install) if RUBY_PLATFORM =~ /mswin/ + sub_project("mongrel_service", :install) if Platform.windows? end desc "for Mongrel and all its subprojects" @@ -175,11 +174,11 @@ task :uninstall => [:clean] do sub_project("gem_plugin", :uninstall) sub_project("fastthread", :uninstall) # sub_project("mongrel_experimental", :uninstall) - sub_project("mongrel_service", :uninstall) if RUBY_PLATFORM =~ /mswin/ + sub_project("mongrel_service", :uninstall) if Platform.windows? end desc "for Mongrel and all its subprojects" -task :clean do +task :clean_all => [:clean] do sub_project("gem_plugin", :clean) sub_project("cgi_multipart_eof_fix", :clean) sub_project("fastthread", :clean) @@ -188,50 +187,14 @@ task :clean do sub_project("mongrel_console", :clean) sub_project("mongrel_cluster", :clean) sub_project("mongrel_experimental", :clean) - sub_project("mongrel_service", :clean) if RUBY_PLATFORM =~ /mswin/ + sub_project("mongrel_service", :clean) if Platform.windows? end #### Site upload tasks namespace :site do - - desc "Package and upload .gem files and .tgz files for Mongrel and all subprojects to http://mongrel.rubyforge.org/releases/" - task :source => [:package_all] do - rm_rf "pkg/gems" - rm_rf "pkg/tars" - mkdir_p "pkg/gems" - mkdir_p "pkg/tars" - - FileList["**/*.gem"].each { |gem| mv gem, "pkg/gems" } - FileList["**/*.tgz"].each {|tgz| mv tgz, "pkg/tars" } - - sh "rm -rf pkg/mongrel*" - sh "gem generate_index -d pkg" - sh "scp -r CHANGELOG pkg/* rubyforge.org:/var/www/gforge-projects/mongrel/releases/" - sh "svn log -v > SVN_LOG" - sh "scp -r SVN_LOG pkg/* rubyforge.org:/var/www/gforge-projects/mongrel/releases/" - rm "SVN_LOG" - end - - desc "Upload the website" - task :web do - # Requires the 'webgem' gem - sh "cd site; webgen; webgen; curl 'http://feed43.com/mongrel.xml' > output/rss.xml; rsync -azv --no-perms --no-times output/* rubyforge.org:/var/www/gforge-projects/mongrel/" - puts "\nMake sure to re-run the site update 6 hours later if you updated the news. This delay is required for Feed43 to pick up the site changes." - end - - desc "Upload the rdocs" - task :rdoc => [:doc] do - sh "rsync -azv --no-perms --no-times doc/* rubyforge.org:/var/www/gforge-projects/mongrel/rdoc/" - sh "cd projects/gem_plugin; rake site:rdoc" - end - desc "Upload the coverage report" task :coverage => [:rcov] do - sh "rsync -azv --no-perms --no-times test/coverage/* rubyforge.org:/var/www/gforge-projects/mongrel/coverage/" rescue nil + sh "rsync -azv --no-perms --no-times test/coverage/* mongrel.cloudbur.st:/home/eweaver/www/mongrel/htdocs/web/coverage" rescue nil end - - desc "Upload the website, the rdocs, and the coverage report" - task :all => [:clean, :web, :rdoc, :coverage] - end @@ -1,4 +0,0 @@ - -* Rewrite and merge mongrel cluster and mongrel_rails into something small and maintainable. -* Initial 1.9 compatibility. -* 1.9 performance (requires removing the thread queue). diff --git a/bin/mongrel_rails b/bin/mongrel_rails index 83473e5..42ecf31 100644 --- a/bin/mongrel_rails +++ b/bin/mongrel_rails @@ -1,3 +1,5 @@ +#!/usr/bin/env ruby +# # Copyright (c) 2005 Zed A. Shaw # You can redistribute it and/or modify it under the same terms as Ruby. # @@ -28,7 +30,7 @@ module Mongrel ['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"], ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"], ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"], - ['-n', '--num-procs INT', "Number of processors active before clients denied", :@num_processors, 1024], + ['-n', '--num-processors INT', "Number of processors active before clients denied", :@num_processors, 1024], ['-o', '--timeout TIME', "Time to wait (in seconds) before killing a stalled thread", :@timeout, 60], ['-t', '--throttle TIME', "Time to pause (in hundredths of a second) between accepting clients", :@throttle, 0], ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil], @@ -74,51 +76,51 @@ module Mongrel def run if @generate - @generate = File.expand_path(@generate) - Mongrel.log(:error, "** Writing config to \"#@generate\".") + @generate = File.expand_path(@generate) + STDERR.puts "** Writing config to \"#@generate\"." open(@generate, "w") {|f| f.write(settings.to_yaml) } - Mongrel.log(:error, "** Finished. Run \"mongrel_rails start -C #@generate\" to use the config file.") + STDERR.puts "** Finished. Run \"mongrel_rails start -C #@generate\" to use the config file." exit 0 end config = Mongrel::Rails::RailsConfigurator.new(settings) do if defaults[:daemon] if File.exist? defaults[:pid_file] - Mongrel.log(:error, "!!! PID file #{defaults[:pid_file]} already exists. Mongrel could be running already. Check your #{defaults[:log_file]} for errors.") - Mongrel.log(:error, "!!! Exiting with error. You must stop mongrel and clear the .pid before I'll attempt a start.") + log "!!! PID file #{defaults[:pid_file]} already exists. Mongrel could be running already. Check your #{defaults[:log_file]} for errors." + log "!!! Exiting with error. You must stop mongrel and clear the .pid before I'll attempt a start." exit 1 end daemonize write_pid_file - Mongrel.log("Daemonized, any open files are closed. Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info.") - Mongrel.log("Settings loaded from #{@config_file} (they override command line).") if @config_file + log "Daemonized, any open files are closed. Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info." + log "Settings loaded from #{@config_file} (they override command line)." if @config_file end - Mongrel.log("Starting Mongrel listening at #{defaults[:host]}:#{defaults[:port]}, further information can be found in log/mongrel-#{defaults[:host]}-#{defaults[:port]}.log") + log "Starting Mongrel listening at #{defaults[:host]}:#{defaults[:port]}" listener do mime = {} if defaults[:mime_map] - Mongrel.log("Loading additional MIME types from #{defaults[:mime_map]}") + log "Loading additional MIME types from #{defaults[:mime_map]}" mime = load_mime_map(defaults[:mime_map], mime) end if defaults[:debug] - Mongrel.log("Installing debugging prefixed filters. Look in log/mongrel_debug for the files.") + log "Installing debugging prefixed filters. Look in log/mongrel_debug for the files." debug "/" end - Mongrel.log("Starting Rails with #{defaults[:environment]} environment...") - Mongrel.log("Mounting Rails at #{defaults[:prefix]}...") if defaults[:prefix] + log "Starting Rails with #{defaults[:environment]} environment..." + log "Mounting Rails at #{defaults[:prefix]}..." if defaults[:prefix] uri defaults[:prefix] || "/", :handler => rails(:mime => mime, :prefix => defaults[:prefix]) - Mongrel.log("Rails loaded.") + log "Rails loaded." - Mongrel.log("Loading any Rails specific GemPlugins" ) + log "Loading any Rails specific GemPlugins" load_plugins if defaults[:config_script] - Mongrel.log("Loading #{defaults[:config_script]} external config script") + log "Loading #{defaults[:config_script]} external config script" run_config(defaults[:config_script]) end @@ -127,29 +129,29 @@ module Mongrel end config.run - Mongrel.log("Mongrel #{Mongrel::Const::MONGREL_VERSION} available at #{@address}:#{@port}") + config.log "Mongrel #{Mongrel::Const::MONGREL_VERSION} available at #{@address}:#{@port}" unless config.defaults[:daemon] - Mongrel.log("Use CTRL-C to stop.") + config.log "Use CTRL-C to stop." end config.join if config.needs_restart - unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ + if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ cmd = "ruby #{__FILE__} start #{original_args.join(' ')}" - Mongrel.log("Restarting with arguments: #{cmd}") + config.log "Restarting with arguments: #{cmd}" config.stop(false, true) config.remove_pid_file if config.defaults[:daemon] system cmd else - Mongrel.log(:error, "Can't restart unless in daemon mode.") + STDERR.puts "Can't restart unless in daemon mode." exit 1 end else - Mongrel.log("Win32 does not support restarts. Exiting.") + config.log "Win32 does not support restarts. Exiting." end end end @@ -159,7 +161,7 @@ module Mongrel begin settings = YAML.load_file(@config_file) ensure - Mongrel.log(:error, "** Loading settings from #{@config_file} (they override command line).") unless @daemon || settings[:daemon] + STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon] end settings[:includes] ||= ["mongrel"] @@ -169,7 +171,6 @@ module Mongrel key = key.to_s if config_keys.include?(key) key = 'address' if key == 'host' - key = 'num_processors' if key == 'num_procs' self.instance_variable_set("@#{key}", value) else failure "Unknown configuration setting: #{key}" @@ -180,14 +181,13 @@ module Mongrel def config_keys @config_keys ||= - %w(address host port cwd log_file pid_file environment docroot mime_map daemon debug includes config_script num_processors num_procs timeout throttle user group prefix) + %w(address host port cwd log_file pid_file environment docroot mime_map daemon debug includes config_script num_processors timeout throttle user group prefix) end def settings config_keys.inject({}) do |hash, key| value = self.instance_variable_get("@#{key}") key = 'host' if key == 'address' - key = 'num_processors' if key == 'num_procs' hash[key.to_sym] ||= value hash end @@ -195,7 +195,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) @@ -215,7 +215,7 @@ module Mongrel ['-c', '--chdir PATH', "Change to dir before starting (will be expanded).", :@cwd, "."], ['-f', '--force', "Force the shutdown (kill -9).", :@force, false], ['-w', '--wait SECONDS', "Wait SECONDS before forcing shutdown", :@wait, "0"], - ['-P', '--pid FILE', "Where the PID file is located.", :@pid_file, "log/mongrel.pid"] + ['-P', '--pid FILE', "Where the PID file is located (cannot be changed via soft restart).", :@pid_file, "log/mongrel.pid"] ] end diff --git a/examples/builder.rb b/examples/builder.rb index 63103bf..5f0803a 100644 --- a/examples/builder.rb +++ b/examples/builder.rb @@ -4,9 +4,9 @@ class TestPlugin < GemPlugin::Plugin "/handlers" include Mongrel::HttpHandlerPlugin def process(request, response) - Mongrel.log(:error, "My options are: #{options.inspect}") - Mongrel.log(:error, "Request Was:") - Mongrel.log(:error, request.params.to_yaml) + STDERR.puts "My options are: #{options.inspect}" + STDERR.puts "Request Was:" + STDERR.puts request.params.to_yaml end end diff --git a/ext/http11/http11_parser.c b/ext/http11/http11_parser.c index a1322c9..c2143de 100644 --- a/ext/http11/http11_parser.c +++ b/ext/http11/http11_parser.c @@ -72,7 +72,7 @@ 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"); @@ -80,7 +80,7 @@ size_t http_parser_execute(http_parser *parser, const char *buffer, size_t len, #line 81 "http11_parser.c" { if ( p == pe ) - goto _out; + goto _test_eof; switch ( cs ) { case 1: @@ -98,16 +98,17 @@ case 1: goto tr0; goto st0; st0: - goto _out0; +cs = 0; + goto _out; tr0: #line 34 "http11_parser.rl" {MARK(mark, p); } goto st2; st2: if ( ++p == pe ) - goto _out2; + goto _test_eof2; case 2: -#line 111 "http11_parser.c" +#line 112 "http11_parser.c" switch( (*p) ) { case 32: goto tr2; case 36: goto st38; @@ -131,9 +132,9 @@ tr2: goto st3; st3: if ( ++p == pe ) - goto _out3; + goto _test_eof3; case 3: -#line 137 "http11_parser.c" +#line 138 "http11_parser.c" switch( (*p) ) { case 42: goto tr4; case 43: goto tr5; @@ -155,9 +156,9 @@ tr4: goto st4; st4: if ( ++p == pe ) - goto _out4; + goto _test_eof4; case 4: -#line 161 "http11_parser.c" +#line 162 "http11_parser.c" switch( (*p) ) { case 32: goto tr8; case 35: goto tr9; @@ -170,14 +171,23 @@ tr8: parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st5; -tr30: +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: +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) @@ -189,7 +199,7 @@ tr40: parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st5; -tr51: +tr53: #line 62 "http11_parser.rl" {MARK(query_start, p); } #line 63 "http11_parser.rl" @@ -203,7 +213,7 @@ tr51: parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st5; -tr55: +tr57: #line 63 "http11_parser.rl" { if(parser->query_string != NULL) @@ -217,9 +227,9 @@ tr55: goto st5; st5: if ( ++p == pe ) - goto _out5; + goto _test_eof5; case 5: -#line 223 "http11_parser.c" +#line 233 "http11_parser.c" if ( (*p) == 72 ) goto tr10; goto st0; @@ -229,43 +239,43 @@ tr10: goto st6; st6: if ( ++p == pe ) - goto _out6; + goto _test_eof6; case 6: -#line 235 "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; @@ -274,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; @@ -296,6 +306,16 @@ tr18: } goto st14; tr26: +#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) { @@ -305,15 +325,15 @@ tr26: goto st14; st14: if ( ++p == pe ) - goto _out14; + goto _test_eof14; case 14: -#line 311 "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; @@ -341,7 +361,7 @@ case 15: goto st0; st16: if ( ++p == pe ) - goto _out16; + goto _test_eof16; case 16: if ( (*p) == 10 ) goto tr22; @@ -352,14 +372,14 @@ tr22: 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 363 "http11_parser.c" +#line 383 "http11_parser.c" goto st0; tr21: #line 37 "http11_parser.rl" @@ -373,9 +393,9 @@ tr23: goto st17; st17: if ( ++p == pe ) - goto _out17; + goto _test_eof17; case 17: -#line 379 "http11_parser.c" +#line 399 "http11_parser.c" switch( (*p) ) { case 33: goto tr23; case 58: goto tr24; @@ -412,9 +432,9 @@ tr27: goto st18; st18: if ( ++p == pe ) - goto _out18; + goto _test_eof18; case 18: -#line 418 "http11_parser.c" +#line 438 "http11_parser.c" switch( (*p) ) { case 13: goto tr26; case 32: goto tr27; @@ -426,11 +446,11 @@ tr25: goto st19; st19: if ( ++p == pe ) - goto _out19; + goto _test_eof19; case 19: -#line 432 "http11_parser.c" +#line 452 "http11_parser.c" if ( (*p) == 13 ) - goto tr26; + goto tr29; goto st19; tr9: #line 53 "http11_parser.rl" @@ -439,7 +459,7 @@ tr9: parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st20; -tr41: +tr43: #line 73 "http11_parser.rl" { if(parser->request_path != NULL) @@ -451,7 +471,7 @@ tr41: parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st20; -tr52: +tr54: #line 62 "http11_parser.rl" {MARK(query_start, p); } #line 63 "http11_parser.rl" @@ -465,7 +485,7 @@ tr52: parser->request_uri(parser->data, PTR_TO(mark), LEN(mark, p)); } goto st20; -tr56: +tr58: #line 63 "http11_parser.rl" { if(parser->query_string != NULL) @@ -479,45 +499,53 @@ tr56: goto st20; st20: if ( ++p == pe ) - goto _out20; + goto _test_eof20; case 20: -#line 485 "http11_parser.c" +#line 505 "http11_parser.c" switch( (*p) ) { - case 32: goto tr30; - case 35: goto st0; - case 37: goto tr31; + case 32: goto tr31; + case 37: goto tr32; + case 60: goto st0; + case 62: goto st0; case 127: goto st0; } - if ( 0 <= (*p) && (*p) <= 31 ) + if ( (*p) > 31 ) { + if ( 34 <= (*p) && (*p) <= 35 ) + goto st0; + } else if ( (*p) >= 0 ) goto st0; - goto tr29; -tr29: + 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 503 "http11_parser.c" +#line 527 "http11_parser.c" switch( (*p) ) { - case 32: goto tr30; - case 35: goto st0; + case 32: goto tr34; case 37: goto st22; + case 60: goto st0; + case 62: goto st0; case 127: goto st0; } - if ( 0 <= (*p) && (*p) <= 31 ) + if ( (*p) > 31 ) { + if ( 34 <= (*p) && (*p) <= 35 ) + goto st0; + } else if ( (*p) >= 0 ) goto st0; goto st21; -tr31: +tr32: #line 34 "http11_parser.rl" {MARK(mark, p); } goto st22; st22: if ( ++p == pe ) - goto _out22; + goto _test_eof22; case 22: -#line 521 "http11_parser.c" +#line 549 "http11_parser.c" if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st23; @@ -529,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 ) @@ -546,9 +574,9 @@ tr5: goto st24; st24: if ( ++p == pe ) - goto _out24; + goto _test_eof24; case 24: -#line 552 "http11_parser.c" +#line 580 "http11_parser.c" switch( (*p) ) { case 43: goto st24; case 58: goto st25; @@ -571,13 +599,16 @@ tr7: goto st25; st25: if ( ++p == pe ) - goto _out25; + goto _test_eof25; case 25: -#line 577 "http11_parser.c" +#line 605 "http11_parser.c" switch( (*p) ) { case 32: goto tr8; + case 34: goto st0; case 35: goto tr9; case 37: goto st26; + case 60: goto st0; + case 62: goto st0; case 127: goto st0; } if ( 0 <= (*p) && (*p) <= 31 ) @@ -585,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 ) @@ -598,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 ) @@ -615,15 +646,18 @@ tr6: goto st28; st28: if ( ++p == pe ) - goto _out28; + goto _test_eof28; case 28: -#line 621 "http11_parser.c" +#line 652 "http11_parser.c" switch( (*p) ) { - case 32: goto tr40; - case 35: goto tr41; + case 32: goto tr42; + case 34: goto st0; + case 35: goto tr43; case 37: goto st29; - case 59: goto tr43; - case 63: goto tr44; + case 59: goto tr45; + case 60: goto st0; + case 62: goto st0; + case 63: goto tr46; case 127: goto st0; } if ( 0 <= (*p) && (*p) <= 31 ) @@ -631,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 ) @@ -644,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 ) @@ -655,7 +689,7 @@ case 30: } else goto st28; goto st0; -tr43: +tr45: #line 73 "http11_parser.rl" { if(parser->request_path != NULL) @@ -664,13 +698,16 @@ tr43: goto st31; st31: if ( ++p == pe ) - goto _out31; + goto _test_eof31; case 31: -#line 670 "http11_parser.c" +#line 704 "http11_parser.c" switch( (*p) ) { case 32: goto tr8; + case 34: goto st0; case 35: goto tr9; case 37: goto st32; + case 60: goto st0; + case 62: goto st0; case 63: goto st34; case 127: goto st0; } @@ -679,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 ) @@ -692,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 ) @@ -703,7 +740,7 @@ case 33: } else goto st31; goto st0; -tr44: +tr46: #line 73 "http11_parser.rl" { if(parser->request_path != NULL) @@ -712,45 +749,51 @@ tr44: goto st34; st34: if ( ++p == pe ) - goto _out34; + goto _test_eof34; case 34: -#line 718 "http11_parser.c" +#line 755 "http11_parser.c" switch( (*p) ) { - case 32: goto tr51; - case 35: goto tr52; - case 37: goto tr53; + case 32: goto tr53; + case 34: goto st0; + 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: + 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 736 "http11_parser.c" +#line 776 "http11_parser.c" switch( (*p) ) { - case 32: goto tr55; - case 35: goto tr56; + case 32: goto tr57; + case 34: goto st0; + case 35: goto tr58; case 37: goto st36; + case 60: goto st0; + case 62: goto st0; case 127: goto st0; } if ( 0 <= (*p) && (*p) <= 31 ) goto st0; goto st35; -tr53: +tr55: #line 62 "http11_parser.rl" {MARK(query_start, p); } goto st36; st36: if ( ++p == pe ) - goto _out36; + goto _test_eof36; case 36: -#line 754 "http11_parser.c" +#line 797 "http11_parser.c" if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) goto st37; @@ -762,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 ) @@ -775,7 +818,7 @@ case 37: goto st0; st38: if ( ++p == pe ) - goto _out38; + goto _test_eof38; case 38: switch( (*p) ) { case 32: goto tr2; @@ -793,7 +836,7 @@ case 38: goto st0; st39: if ( ++p == pe ) - goto _out39; + goto _test_eof39; case 39: switch( (*p) ) { case 32: goto tr2; @@ -811,7 +854,7 @@ case 39: goto st0; st40: if ( ++p == pe ) - goto _out40; + goto _test_eof40; case 40: switch( (*p) ) { case 32: goto tr2; @@ -829,7 +872,7 @@ case 40: goto st0; st41: if ( ++p == pe ) - goto _out41; + goto _test_eof41; case 41: switch( (*p) ) { case 32: goto tr2; @@ -847,7 +890,7 @@ case 41: goto st0; st42: if ( ++p == pe ) - goto _out42; + goto _test_eof42; case 42: switch( (*p) ) { case 32: goto tr2; @@ -865,7 +908,7 @@ case 42: goto st0; st43: if ( ++p == pe ) - goto _out43; + goto _test_eof43; case 43: switch( (*p) ) { case 32: goto tr2; @@ -883,7 +926,7 @@ case 43: goto st0; st44: if ( ++p == pe ) - goto _out44; + goto _test_eof44; case 44: switch( (*p) ) { case 32: goto tr2; @@ -901,7 +944,7 @@ case 44: goto st0; st45: if ( ++p == pe ) - goto _out45; + goto _test_eof45; case 45: switch( (*p) ) { case 32: goto tr2; @@ -919,7 +962,7 @@ case 45: goto st0; st46: if ( ++p == pe ) - goto _out46; + goto _test_eof46; case 46: switch( (*p) ) { case 32: goto tr2; @@ -937,7 +980,7 @@ case 46: goto st0; st47: if ( ++p == pe ) - goto _out47; + goto _test_eof47; case 47: switch( (*p) ) { case 32: goto tr2; @@ -955,7 +998,7 @@ case 47: goto st0; st48: if ( ++p == pe ) - goto _out48; + goto _test_eof48; case 48: switch( (*p) ) { case 32: goto tr2; @@ -973,7 +1016,7 @@ case 48: goto st0; st49: if ( ++p == pe ) - goto _out49; + goto _test_eof49; case 49: switch( (*p) ) { case 32: goto tr2; @@ -991,7 +1034,7 @@ case 49: goto st0; st50: if ( ++p == pe ) - goto _out50; + goto _test_eof50; case 50: switch( (*p) ) { case 32: goto tr2; @@ -1009,7 +1052,7 @@ case 50: goto st0; st51: if ( ++p == pe ) - goto _out51; + goto _test_eof51; case 51: switch( (*p) ) { case 32: goto tr2; @@ -1027,7 +1070,7 @@ case 51: goto st0; st52: if ( ++p == pe ) - goto _out52; + goto _test_eof52; case 52: switch( (*p) ) { case 32: goto tr2; @@ -1045,7 +1088,7 @@ case 52: goto st0; st53: if ( ++p == pe ) - goto _out53; + goto _test_eof53; case 53: switch( (*p) ) { case 32: goto tr2; @@ -1063,7 +1106,7 @@ case 53: goto st0; st54: if ( ++p == pe ) - goto _out54; + goto _test_eof54; case 54: switch( (*p) ) { case 32: goto tr2; @@ -1081,7 +1124,7 @@ case 54: goto st0; st55: if ( ++p == pe ) - goto _out55; + goto _test_eof55; case 55: switch( (*p) ) { case 32: goto tr2; @@ -1099,70 +1142,70 @@ 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 122 "http11_parser.rl" @@ -1177,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 1184 "http11_parser.c" -#line 136 "http11_parser.rl" - parser->nread++; - } - return(parser->nread); } int http_parser_finish(http_parser *parser) { - int cs = parser->cs; - - -#line 1197 "http11_parser.c" -#line 147 "http11_parser.rl" - - parser->cs = cs; - if (http_parser_has_error(parser) ) { return -1; } else if (http_parser_is_finished(parser) ) { @@ -1212,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.rl b/ext/http11/http11_parser.rl index a418605..5e551a3 100644 --- a/ext/http11/http11_parser.rl +++ b/ext/http11/http11_parser.rl @@ -114,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; @@ -130,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) ) { @@ -161,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 ee970b1..53c805f 100644 --- a/ext/http11/http11_parser_common.rl +++ b/ext/http11/http11_parser_common.rl @@ -11,12 +11,11 @@ safe = ("$" | "-" | "_" | "."); extra = ("!" | "*" | "'" | "(" | ")" | ","); reserved = (";" | "/" | "?" | ":" | "@" | "&" | "=" | "+"); - sorta_safe = ("\"" | "<" | ">"); - unsafe = (CTL | " " | "#" | "%" | sorta_safe); + unsafe = (CTL | " " | "\"" | "#" | "%" | "<" | ">"); national = any -- (alpha | digit | reserved | extra | safe | unsafe); unreserved = (alpha | digit | safe | extra | national); escape = ("%" xdigit xdigit); - uchar = (unreserved | escape | sorta_safe); + uchar = (unreserved | escape); pchar = (uchar | ":" | "@" | "&" | "=" | "+"); tspecials = ("(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\\" | "\"" | "/" | "[" | "]" | "?" | "=" | "{" | "}" | " " | "\t"); diff --git a/ext/http11_java/org/jruby/mongrel/Http11.java b/ext/http11_java/org/jruby/mongrel/Http11.java index 1cf5e41..3dbfdd6 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.2")); + req.aset(runtime.newString("SERVER_SOFTWARE"),runtime.newString("Mongrel 1.2.0")); } }; @@ -247,7 +247,7 @@ public class Http11 extends RubyObject { if(this.hp.has_error()) { throw new RaiseException(runtime, eHttpParserError, "Invalid HTTP format, parsing fails.", true); } else { - return runtime.newFixnum(this.hp.parser.nread); + return runtime.newFixnum((long)this.hp.parser.nread); } } } diff --git a/ext/http11_java/org/jruby/mongrel/Http11Parser.java b/ext/http11_java/org/jruby/mongrel/Http11Parser.java index 30f188d..2dd0078 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; - -import org.jruby.util.ByteList; +// 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 <stdio.h> +#include <assert.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> + +/* + * 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 ) -{ - 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; -} - -private static byte[] create__http_parser_actions( ) +// line 37 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" +private static byte[] init__http_parser_actions_0() { - byte[] r = new byte[35]; - init__http_parser_actions_0( r ); - return r; + 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 final byte _http_parser_actions[] = create__http_parser_actions(); +private static final byte _http_parser_actions[] = init__http_parser_actions_0(); -private static void init__http_parser_key_offsets_0( short[] r ) +private static short[] init__http_parser_key_offsets_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 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 short[] create__http_parser_key_offsets( ) -{ - short[] r = new short[58]; - init__http_parser_key_offsets_0( r ); - return r; -} +private static final short _http_parser_key_offsets[] = init__http_parser_key_offsets_0(); -private static final short _http_parser_key_offsets[] = create__http_parser_key_offsets(); - -private static void init__http_parser_trans_keys_0( char[] r ) +private static char[] init__http_parser_trans_keys_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 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 char[] create__http_parser_trans_keys( ) -{ - char[] r = new char[381]; - init__http_parser_trans_keys_0( r ); - return r; -} - -private static final char _http_parser_trans_keys[] = create__http_parser_trans_keys(); - +private static final char _http_parser_trans_keys[] = init__http_parser_trans_keys_0(); -private static void init__http_parser_single_lengths_0( byte[] r ) -{ - r[0]=0; r[1]=2; r[2]=3; r[3]=4; r[4]=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; -} -private static byte[] create__http_parser_single_lengths( ) +private static byte[] init__http_parser_single_lengths_0() { - byte[] r = new byte[58]; - init__http_parser_single_lengths_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 byte _http_parser_single_lengths[] = create__http_parser_single_lengths(); +private static final byte _http_parser_single_lengths[] = init__http_parser_single_lengths_0(); -private static void init__http_parser_range_lengths_0( byte[] r ) +private static byte[] init__http_parser_range_lengths_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 [] { + 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 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 final byte _http_parser_range_lengths[] = init__http_parser_range_lengths_0(); -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( ) +private static short[] init__http_parser_index_offsets_0() { - short[] r = new short[58]; - init__http_parser_index_offsets_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 short _http_parser_index_offsets[] = create__http_parser_index_offsets(); - +private static final short _http_parser_index_offsets[] = init__http_parser_index_offsets_0(); -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( ) +private static byte[] init__http_parser_indicies_0() { - byte[] r = new byte[316]; - init__http_parser_indicies_0( r ); - return r; + 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 final byte _http_parser_indicies[] = create__http_parser_indicies(); +private static final byte _http_parser_indicies[] = init__http_parser_indicies_0(); -private static void init__http_parser_trans_targs_wi_0( byte[] r ) +private static byte[] init__http_parser_trans_targs_wi_0() { - 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; + 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 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 final byte _http_parser_trans_targs_wi[] = init__http_parser_trans_targs_wi_0(); -private static void init__http_parser_trans_actions_wi_0( byte[] r ) +private static byte[] init__http_parser_trans_actions_wi_0() { - 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; + 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_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" - - 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" +// line 91 "http11_parser.rl" + +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" - - body_start = 0; - content_len = 0; - mark = 0; - nread = 0; - field_len = 0; - field_start = 0; - } - } - - public final HttpParser parser = new HttpParser(); - - public int execute(ByteList buffer, int off) { - int p, pe; - int cs = parser.cs; - int len = buffer.realSize; - assert off<=len : "offset past end of buffer"; - - p = off; - pe = len; - byte[] data = buffer.bytes; - parser.buffer = buffer; - - -// line 359 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" +// 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; + + return(1); +} + + +/** 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; + + assert(off <= len && "offset past end of buffer"); + + 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; } +// line 57 "http11_parser.rl" + { + if(parser->fragment != NULL) + parser->fragment(parser->data, PTR_TO(mark), LEN(mark, p)); + } break; case 9: -// line 40 "http11_parser.java.rl" +// line 62 "http11_parser.rl" + {MARK(query_start, p); } + break; + case 10: +// line 63 "http11_parser.rl" { - if(parser.query_string != null) - parser.query_string.call(parser.data, parser.query_start, p-parser.query_start); + if(parser->query_string != NULL) + parser->query_string(parser->data, PTR_TO(query_start), LEN(query_start, p)); } break; - case 10: -// line 45 "http11_parser.java.rl" + case 11: +// line 68 "http11_parser.rl" { - if(parser.http_version != null) - parser.http_version.call(parser.data, parser.mark, p-parser.mark); + if(parser->http_version != NULL) + parser->http_version(parser->data, PTR_TO(mark), LEN(mark, p)); } break; - case 11: -// line 50 "http11_parser.java.rl" + case 12: +// line 73 "http11_parser.rl" { - if(parser.request_path != null) - parser.request_path.call(parser.data, parser.mark, p-parser.mark); + if(parser->request_path != NULL) + parser->request_path(parser->data, PTR_TO(mark), LEN(mark,p)); } break; - case 12: -// line 55 "http11_parser.java.rl" + case 13: +// line 78 "http11_parser.rl" { - parser.body_start = p + 1; - if(parser.header_done != null) - parser.header_done.call(parser.data, p + 1, pe - p - 1); - if (true) break _resume; + 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 513 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" +// 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" - - parser.cs = cs; - parser.nread += (p - off); - - assert p <= pe : "buffer overflow after parsing execute"; - assert parser.nread <= len : "nread longer than length"; - assert parser.body_start <= len : "body starts after buffer end"; - assert parser.mark < len : "mark is after buffer end"; - assert parser.field_len <= len : "field has length longer than whole buffer"; - assert parser.field_start < len : "field starts after buffer end"; - - if(parser.body_start>0) { - /* final \r\n combo encountered so stop right here */ - -// line 540 "../../ext/http11_java/org/jruby/mongrel/Http11Parser.java" -// line 141 "http11_parser.java.rl" - 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; - } - } +case 4: +case 5: + } + break; } + } +// line 122 "http11_parser.rl" - public boolean has_error() { - return parser.cs == http_parser_error; - } + parser->cs = cs; + parser->nread += p - (buffer + 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"); - public boolean is_finished() { - return parser.cs == http_parser_first_final; + return(parser->nread); +} + +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 b06e071..1963322 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -1,4 +1,5 @@ -# Ruby + +# Standard libraries require 'socket' require 'tempfile' require 'yaml' @@ -7,19 +8,16 @@ require 'etc' require 'uri' require 'stringio' -# Ensure working require -require 'mongrel/gems' +# Compiled Mongrel extension +require 'http11' -# TODO: Only require these for RUBY_VERSION <= 1.8.6 -# and only for platforms that require it, exclusive matching -if !RUBY_PLATFORM.match(/java|mswin/) && !RUBY_VERSION.match(/1\.8\.\d/) +# Gem conditional loader +require 'mongrel/gems' Mongrel::Gems.require 'cgi_multipart_eof_fix' Mongrel::Gems.require 'fastthread' -end require 'thread' -require 'http11' -require 'mongrel/logger' +# Ruby Mongrel require 'mongrel/cgi' require 'mongrel/handlers' require 'mongrel/command' @@ -47,6 +45,7 @@ module Mongrel attr_accessor :http_body end + # This is the main driver of Mongrel, while the Mongrel::HttpParser and Mongrel::URIClassifier # make up the majority of how the server functions. It's a very simple class that just # has a thread accepting connections and a simple HttpServer.process_client function @@ -54,7 +53,7 @@ module Mongrel # # You use it by doing the following: # - # server = 2("0.0.0.0", 3000) + # server = HttpServer.new("0.0.0.0", 3000) # server.register("/stuff", MyNiftyHandler.new) # server.run.join # @@ -75,8 +74,6 @@ module Mongrel attr_reader :timeout attr_reader :num_processors - attr_accessor :logger - # Creates a working server on host:port (strange things happen if port isn't a Number). # Use HttpServer::run to start the server and HttpServer.acceptor.join to # join the thread that's processing incoming requests on the socket. @@ -90,7 +87,7 @@ module Mongrel # The throttle parameter is a sleep timeout (in hundredths of a second) that is placed between # socket.accept calls in order to give the server a cheap throttle time. It defaults to 0 and # actually if it is 0 then the sleep is not done at all. - def initialize(host, port, num_processors=950, throttle=0, timeout=60, log=nil, log_level=:debug) + def initialize(host, port, num_processors=950, throttle=0, timeout=60) tries = 0 @socket = TCPServer.new(host, port) @@ -99,12 +96,11 @@ module Mongrel @host = host @port = port @workers = ThreadGroup.new - @throttle = throttle + @throttle = throttle / 100.0 @num_processors = num_processors @timeout = timeout - @logger = Mongrel::Log.new(log || "log/mongrel-#{host}-#{port}.log", log_level) end - + # Does the majority of the IO processing. It has been written in Ruby using # about 7 different IO processing strategies and no matter how it's done # the performance just does not improve. It is currently carefully constructed @@ -188,25 +184,23 @@ module Mongrel rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF client.close rescue nil rescue HttpParserError => e - Mongrel.log(:error, "#{Time.now.httpdate}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}") - Mongrel.log(:error, "#{Time.now.httpdate}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n") - # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4 - client.write(Const::ERROR_400_RESPONSE) + STDERR.puts "#{Time.now}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}" + STDERR.puts "#{Time.now}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n" rescue Errno::EMFILE reap_dead_workers('too many files') rescue Object => e - Mongrel.log(:error, "#{Time.now.httpdate}: Read error: #{e.inspect}") - Mongrel.log(:error, e.backtrace.join("\n")) + STDERR.puts "#{Time.now}: Read error: #{e.inspect}" + STDERR.puts e.backtrace.join("\n") ensure begin client.close rescue IOError # Already closed rescue Object => e - Mongrel.log(:error, "#{Time.now.httpdate}: Client error: #{e.inspect}") - Mongrel.log(:error, e.backtrace.join("\n")) + 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 @@ -216,14 +210,14 @@ module Mongrel # after the reap is done. It only runs if there are workers to reap. def reap_dead_workers(reason='unknown') if @workers.list.length > 0 - Mongrel.log(:error, "#{Time.now.httpdate}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'") - error_msg = "#{Time.now.httpdate}: Mongrel timed out this thread: #{reason}" + STDERR.puts "#{Time.now}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'" + error_msg = "Mongrel timed out this thread: #{reason}" mark = Time.now @workers.list.each do |worker| worker[:started_on] = Time.now if not worker[:started_on] if mark - worker[:started_on] > @timeout + @throttle - Mongrel.log(:error, "#{Time.now.httpdate}: Thread #{worker.inspect} is too old, killing.") + STDERR.puts "Thread #{worker.inspect} is too old, killing." worker.raise(TimeoutError.new(error_msg)) end end @@ -238,7 +232,7 @@ module Mongrel # that much longer. def graceful_shutdown while reap_dead_workers("shutdown") > 0 - Mongrel.log(:error, "#{Time.now.httpdate}: Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds.") + STDERR.puts "Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds." sleep @timeout / 10 end end @@ -276,21 +270,23 @@ module Mongrel while true begin client = @socket.accept - - num_workers = @workers.list.length - if num_workers >= @num_processors - Mongrel.log(:error, "#{Time.now.httpdate}: Server overloaded with #{num_workers} processors (#@num_processors max). Dropping connection.") + + if defined?($tcp_cork_opts) and $tcp_cork_opts + client.setsockopt(*$tcp_cork_opts) rescue nil + end + + worker_list = @workers.list + + if worker_list.length >= @num_processors + STDERR.puts "Server overloaded with #{worker_list.length} processors (#@num_processors max). Dropping connection." client.close rescue nil reap_dead_workers("max processors") else - if defined?($tcp_cork_opts) and $tcp_cork_opts - client.setsockopt(*$tcp_cork_opts) rescue nil - end thread = Thread.new(client) {|c| process_client(c) } thread[:started_on] = Time.now @workers.add(thread) - sleep @throttle/100.0 if @throttle > 0 + sleep @throttle if @throttle > 0 end rescue StopServer break @@ -301,14 +297,14 @@ module Mongrel # client closed the socket even before accept client.close rescue nil rescue Object => e - Mongrel.log(:error, "#{Time.now.httpdate}: Unhandled listen loop exception #{e.inspect}.") - Mongrel.log(:error, e.backtrace.join("\n")) + STDERR.puts "#{Time.now}: Unhandled listen loop exception #{e.inspect}." + STDERR.puts e.backtrace.join("\n") end end graceful_shutdown ensure @socket.close - # Mongrel.log(:error, "#{Time.now.httpdate}: Closed socket.") + # STDERR.puts "#{Time.now}: Closed socket." end end @@ -324,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 e2ac10c..3957611 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') @@ -173,10 +173,9 @@ module Mongrel # The stdoutput should be completely bypassed but we'll drop a warning just in case def stdoutput - STDERR.puts "#{Time.now.httpdate}: WARNING: Your program is doing something not expected. Please tell Zed that stdoutput was used and what software you are running. Thanks." + STDERR.puts "WARNING: Your program is doing something not expected. Please tell Zed that stdoutput was used and what software you are running. Thanks." @response.body end end - end diff --git a/lib/mongrel/command.rb b/lib/mongrel/command.rb index df993d6..a3ee57d 100644 --- a/lib/mongrel/command.rb +++ b/lib/mongrel/command.rb @@ -55,13 +55,15 @@ module Mongrel # I need to add my own -h definition to prevent the -h by default from exiting. @opt.on_tail("-h", "--help", "Show this message") do @done_validating = true - Mongrel.log(@opt) + puts @opt end # I need to add my own -v definition to prevent the -v from exiting by default as well. @opt.on_tail("--version", "Show version") do @done_validating = true - Mongrel.log("Version #{Mongrel::Const::MONGREL_VERSION}") + if VERSION + puts "Version #{Mongrel::Const::MONGREL_VERSION}" + end end @opt.parse! argv @@ -153,17 +155,17 @@ module Mongrel # Prints a list of available commands. def print_command_list - Mongrel.log("#{Mongrel::Command::BANNER}\nAvailable commands are:\n\n") + puts "#{Mongrel::Command::BANNER}\nAvailable commands are:\n\n" self.commands.each do |name| if /mongrel::/ =~ name name = name[9 .. -1] end - Mongrel.log(" - #{name[1 .. -1]}\n") + puts " - #{name[1 .. -1]}\n" end - Mongrel.log("\nEach command takes -h as an option to get help.") + puts "\nEach command takes -h as an option to get help." end @@ -178,7 +180,7 @@ module Mongrel print_command_list return true elsif cmd_name == "--version" - Mongrel.log("Mongrel Web Server #{Mongrel::Const::MONGREL_VERSION}") + puts "Mongrel Web Server #{Mongrel::Const::MONGREL_VERSION}" return true end diff --git a/lib/mongrel/configurator.rb b/lib/mongrel/configurator.rb index 26035b8..439b44c 100644 --- a/lib/mongrel/configurator.rb +++ b/lib/mongrel/configurator.rb @@ -59,18 +59,18 @@ module Mongrel target_gid = Etc.getgrnam(group).gid if group if uid != target_uid or gid != target_gid - Mongrel.log("Initiating groups for #{user.inspect}:#{group.inspect}.") + log "Initiating groups for #{user.inspect}:#{group.inspect}." Process.initgroups(user, target_gid) - Mongrel.log("Changing group to #{group.inspect}.") + log "Changing group to #{group.inspect}." Process::GID.change_privilege(target_gid) - Mongrel.log("Changing user to #{user.inspect}." ) + log "Changing user to #{user.inspect}." Process::UID.change_privilege(target_uid) end rescue Errno::EPERM => e - Mongrel.log(:critical, "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}.") - Mongrel.log(:critical, "Mongrel failed to start.") + log "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}." + log "Mongrel failed to start." exit 1 end end @@ -81,13 +81,13 @@ module Mongrel # Writes the PID file if we're not on Windows. def write_pid_file - unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ - Mongrel.log("Writing PID file to #{@pid_file}") + if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ + log "Writing PID file to #{@pid_file}" open(@pid_file,"w") {|f| f.write(Process.pid) } open(@pid_file,"w") do |f| f.write(Process.pid) File.chmod(0644, @pid_file) - end + end end end @@ -131,21 +131,17 @@ module Mongrel # def listener(options={},&block) raise "Cannot call listener inside another listener block." if (@listener or @listener_name) - opts = resolve_defaults(options) - opts[:num_processors] ||= 950 - opts[:throttle] ||= 0 - opts[:timeout] ||= 60 - - @listener = Mongrel::HttpServer.new( - opts[:host], opts[:port].to_i, opts[:num_processors].to_i, - opts[:throttle].to_i, opts[:timeout].to_i, - opts[:log], opts[:log_level] - ) - @listener_name = "#{opts[:host]}:#{opts[:port]}" + ops = resolve_defaults(options) + ops[:num_processors] ||= 950 + ops[:throttle] ||= 0 + ops[:timeout] ||= 60 + + @listener = Mongrel::HttpServer.new(ops[:host], ops[:port].to_i, ops[:num_processors].to_i, ops[:throttle].to_i, ops[:timeout].to_i) + @listener_name = "#{ops[:host]}:#{ops[:port]}" @listeners[@listener_name] = @listener - if opts[:user] and opts[:group] - change_privilege(opts[:user], opts[:group]) + if ops[:user] and ops[:group] + change_privilege(ops[:user], ops[:group]) end # Does the actual cloaking operation to give the new implicit self. @@ -167,8 +163,8 @@ module Mongrel # * :handler => HttpHandler -- Handler to use for this location. # * :in_front => true/false -- Rather than appending, it prepends this handler. def uri(location, options={}) - opts = resolve_defaults(options) - @listener.register(location, opts[:handler], opts[:in_front]) + ops = resolve_defaults(options) + @listener.register(location, ops[:handler], ops[:in_front]) end @@ -187,16 +183,16 @@ module Mongrel # It is safe to call this on win32 as it will only require the daemons # gem/library if NOT win32. def daemonize(options={}) - opts = resolve_defaults(options) + ops = resolve_defaults(options) # save this for later since daemonize will hose it - unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ + if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ require 'daemons/daemonize' - logfile = opts[:log_file] + logfile = ops[:log_file] if logfile[0].chr != "/" - logfile = File.join(opts[:cwd],logfile) + logfile = File.join(ops[:cwd],logfile) if not File.exist?(File.dirname(logfile)) - Mongrel.log(:critical, "!!! Log file directory not found at full path #{File.dirname(logfile)}. Update your configuration to use a full path.") + log "!!! Log file directory not found at full path #{File.dirname(logfile)}. Update your configuration to use a full path." exit 1 end end @@ -204,10 +200,10 @@ module Mongrel Daemonize.daemonize(logfile) # change back to the original starting directory - Dir.chdir(opts[:cwd]) + Dir.chdir(ops[:cwd]) else - Mongrel.log(:warning, "WARNING: Win32 does not support daemon mode.") + log "WARNING: Win32 does not support daemon mode." end end @@ -217,17 +213,17 @@ module Mongrel # :excludes => [] setting listing the names of plugins to include # or exclude from the determining the dependencies. def load_plugins(options={}) - opts = resolve_defaults(options) + ops = resolve_defaults(options) load_settings = {} - if opts[:includes] - opts[:includes].each do |plugin| + if ops[:includes] + ops[:includes].each do |plugin| load_settings[plugin] = GemPlugin::INCLUDE end end - if opts[:excludes] - opts[:excludes].each do |plugin| + if ops[:excludes] + ops[:excludes].each do |plugin| load_settings[plugin] = GemPlugin::EXCLUDE end end @@ -253,7 +249,7 @@ module Mongrel mime = load_yaml(file, mime) # check all the mime types to make sure they are the right format - mime.each {|k,v| Mongrel.log(:warning, "WARNING: MIME type #{k} must start with '.'") if k.index(".") != 0 } + mime.each {|k,v| log "WARNING: MIME type #{k} must start with '.'" if k.index(".") != 0 } return mime end @@ -263,8 +259,8 @@ module Mongrel # name and configured with the selected options. The options # are merged with the defaults prior to passing them in. def plugin(name, options={}) - opts = resolve_defaults(options) - GemPlugin::Manager.instance.create(name, opts) + ops = resolve_defaults(options) + GemPlugin::Manager.instance.create(name, ops) end # Lets you do redirects easily as described in Mongrel::RedirectHandler. @@ -348,7 +344,7 @@ module Mongrel # it reads it in and does an eval on the contents passing in the right # binding so they can put their own Configurator statements. def run_config(script) - open(script) {|f| eval(f.read, proc {self}.binding) } + open(script) {|f| eval(f.read, proc {self}) } end # Sets up the standard signal handlers that are used on most Ruby @@ -362,28 +358,31 @@ module Mongrel # # This command is safely ignored if the platform is win32 (with a warning) def setup_signals(options={}) - opts = resolve_defaults(options) + ops = resolve_defaults(options) # forced shutdown, even if previously restarted (actually just like TERM but for CTRL-C) - trap("INT") { Mongrel.log(:notice, "INT signal received."); stop(false) } + trap("INT") { log "INT signal received."; stop(false) } - # always clean up the pid file + # clean up the pid file always at_exit { remove_pid_file } - unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ + if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ # graceful shutdown - trap("TERM") { Mongrel.log(:notice, "TERM signal received."); stop } - # debug mode - trap("USR1") { Mongrel.log(:notice, "USR1 received, toggling $mongrel_debug_client to #{!$mongrel_debug_client}"); $mongrel_debug_client = !$mongrel_debug_client } + trap("TERM") { log "TERM signal received."; stop } + trap("USR1") { log "USR1 received, toggling $mongrel_debug_client to #{!$mongrel_debug_client}"; $mongrel_debug_client = !$mongrel_debug_client } # restart - trap("USR2") { Mongrel.log(:notice, "USR2 signal received."); stop(true) } + trap("USR2") { log "USR2 signal received."; stop(true) } - Mongrel.log(:notice, "Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).") + log "Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart)." else - Mongrel.log(:notice, "Signals ready. INT => stop (no restart).") + log "Signals ready. INT => stop (no restart)." end end - end + # Logs a simple message to STDERR (or the mongrel log if in daemon mode). + def log(msg) + STDERR.print "** ", msg, "\n" + end + end end diff --git a/lib/mongrel/const.rb b/lib/mongrel/const.rb index e01df15..a9b100e 100644 --- a/lib/mongrel/const.rb +++ b/lib/mongrel/const.rb @@ -65,13 +65,10 @@ module Mongrel REQUEST_URI='REQUEST_URI'.freeze REQUEST_PATH='REQUEST_PATH'.freeze - MONGREL_VERSION="1.2".freeze + MONGREL_VERSION="1.2.0".freeze MONGREL_TMP_BASE="mongrel".freeze - # A standard 400 response for a request which generates a http parse exception - ERROR_400_RESPONSE="HTTP/1.1 400 Bad Request\r\nConnection: close\r\nServer: Mongrel #{MONGREL_VERSION}\r\n\r\nBAD REQUEST".freeze - # The standard empty 404 response for bad requests. Use Error4040Handler for custom stuff. ERROR_404_RESPONSE="HTTP/1.1 404 Not Found\r\nConnection: close\r\nServer: Mongrel #{MONGREL_VERSION}\r\n\r\nNOT FOUND".freeze @@ -110,5 +107,4 @@ module Mongrel REDIRECT = "HTTP/1.1 302 Found\r\nLocation: %s\r\nConnection: close\r\n\r\n".freeze HOST = "HOST".freeze end - -end +end
\ No newline at end of file diff --git a/lib/mongrel/debug.rb b/lib/mongrel/debug.rb index 7bde9ec..2686c1e 100644 --- a/lib/mongrel/debug.rb +++ b/lib/mongrel/debug.rb @@ -16,7 +16,7 @@ module MongrelDbg def MongrelDbg::configure(log_dir = File.join("log","mongrel_debug")) FileUtils.mkdir_p(log_dir) @log_dir = log_dir - $objects_out = open(File.join("log","mongrel_debug","objects.log"),"w") + $objects_out=open(File.join("log","mongrel_debug","objects.log"),"w") $objects_out.puts "run,classname,last,count,delta,lenmean,lensd,lenmax" $objects_out.sync = true $last_stat = nil @@ -35,12 +35,12 @@ module MongrelDbg if not LOGGING[target] LOGGING[target] = Logger.new(File.join(@log_dir, "#{target.to_s}.log")) end - MongrelDbg::trace(target, "#{Time.now.httpdate}: TRACING ON") + MongrelDbg::trace(target, "TRACING ON #{Time.now}") end def MongrelDbg::end_trace(target) SETTINGS[:tracing][target] = false - MongrelDbg::trace(target, "#{Time.now.httpdate}: TRACING OFF") + MongrelDbg::trace(target, "TRACING OFF #{Time.now}") LOGGING[target].close LOGGING[target] = nil end @@ -86,17 +86,20 @@ module Kernel end MongrelDbg::trace(:files, open_counts.to_yaml) end -end +end + module RequestLog + # Just logs whatever requests it gets to STDERR (which ends up in the mongrel + # log when daemonized). class Access < GemPlugin::Plugin "/handlers" include Mongrel::HttpHandlerPlugin def process(request,response) p = request.params - Mongrel.log("#{p['REMOTE_ADDR']} \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"") + STDERR.puts "#{p['REMOTE_ADDR']} - [#{Time.now.httpdate}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"" end end @@ -105,13 +108,12 @@ module RequestLog include Mongrel::HttpHandlerPlugin def process(request, response) - MongrelDbg::trace(:files, "#{Time.now.httpdate}: FILES OPEN BEFORE REQUEST #{request.params['PATH_INFO']}") + MongrelDbg::trace(:files, "#{Time.now} FILES OPEN BEFORE REQUEST #{request.params['PATH_INFO']}") log_open_files end end - # stolen from Robert Klemme class Objects < GemPlugin::Plugin "/handlers" include Mongrel::HttpHandlerPlugin @@ -123,7 +125,7 @@ module RequestLog begin ObjectSpace.each_object do |o| begin - if o.respond_to?(:length) + if o.respond_to? :length len = o.length lengths[o.class] ||= Mongrel::Stats.new(o.class) lengths[o.class].sample(len) @@ -152,28 +154,26 @@ module RequestLog $run_count += 1 $last_stat = stats rescue Object - STDERR.puts "#{Time.now.httpdate}: object.log ERROR: #$!" + STDERR.puts "object.log ERROR: #$!" end end end - class Params < GemPlugin::Plugin "/handlers" include Mongrel::HttpHandlerPlugin def process(request, response) - MongrelDbg::trace(:rails, "#{Time.now.httpdate}: REQUEST #{request.params['PATH_INFO']}") + MongrelDbg::trace(:rails, "#{Time.now} REQUEST #{request.params['PATH_INFO']}") MongrelDbg::trace(:rails, request.params.to_yaml) end end - class Threads < GemPlugin::Plugin "/handlers" include Mongrel::HttpHandlerPlugin def process(request, response) - MongrelDbg::trace(:threads, "#{Time.now.httpdate}: REQUEST #{request.params['PATH_INFO']}") + MongrelDbg::trace(:threads, "#{Time.now} REQUEST #{request.params['PATH_INFO']}") begin ObjectSpace.each_object do |obj| begin @@ -185,7 +185,7 @@ module RequestLog worker_list.each {|t| keys << "\n\t\t-- #{t}: #{t.keys.inspect}" } end - MongrelDbg::trace(:threads, "#{Time.now.httpdate}: #{obj.host}:#{obj.port} -- THREADS: #{worker_list.length} #{keys}") + MongrelDbg::trace(:threads, "#{obj.host}:#{obj.port} -- THREADS: #{worker_list.length} #{keys}") end rescue Object # Ignore since obj.class can sometimes take parameters end @@ -198,6 +198,6 @@ end END { - MongrelDbg::trace(:files, "#{Time.now.httpdate}: FILES OPEN AT EXIT") + MongrelDbg::trace(:files, "FILES OPEN AT EXIT") log_open_files } diff --git a/lib/mongrel/gems.rb b/lib/mongrel/gems.rb index 7c09b42..eb6d0d6 100644 --- a/lib/mongrel/gems.rb +++ b/lib/mongrel/gems.rb @@ -19,4 +19,4 @@ module Mongrel end end -end +end
\ No newline at end of file diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb index bcee6a0..e643025 100644 --- a/lib/mongrel/handlers.rb +++ b/lib/mongrel/handlers.rb @@ -8,7 +8,6 @@ require 'mongrel/stats' require 'zlib' require 'yaml' - module Mongrel # You implement your application handler with this. It's very light giving @@ -102,7 +101,8 @@ module Mongrel # # If you pass nil as the root path, it will not check any locations or # expand any paths. This lets you serve files from multiple drives - # on win32. + # on win32. It should probably not be used in a public-facing way + # without additional checks. # # The default content type is "text/plain; charset=ISO-8859-1" but you # can change it anything you want using the DirHandler.default_content_type @@ -120,7 +120,7 @@ module Mongrel # You give it the path to the directory root and and optional listing_allowed and index_html def initialize(path, listing_allowed=true, index_html="index.html") @path = File.expand_path(path) if path - @listing_allowed=listing_allowed + @listing_allowed = listing_allowed @index_html = index_html @default_content_type = "application/octet-stream".freeze end @@ -132,12 +132,8 @@ module Mongrel # Add the drive letter or root path req_path = File.join(@path, req_path) if @path req_path = File.expand_path req_path - - # do not remove the check for @path at the beginning, it's what prevents - # the serving of arbitrary files (and good programmer Rule #1 Says: If - # you don't understand something, it's not because I'm stupid, it's - # because you are). - if req_path.index(@path) == 0 and File.exist? req_path + + if File.exist? req_path and (!@path or req_path.index(@path) == 0) # It exists and it's in the right location if File.directory? req_path # The request is for a directory @@ -157,7 +153,7 @@ module Mongrel return req_path end else - # does not exist or isn't in the right spot or isn't valid because not start with @path + # does not exist or isn't in the right spot return nil end end @@ -209,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 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 + 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 else modified_since || none_match # validation successful if we get this far and at least one of the header exists end @@ -270,7 +266,7 @@ module Mongrel response.start(403) {|head,out| out.write(ONLY_HEAD_GET) } end rescue => details - STDERR.puts "#{Time.now.httpdate}: Error sending file #{req_path}: #{details}" + STDERR.puts "Error sending file #{req_path}: #{details}" end end end diff --git a/lib/mongrel/http_request.rb b/lib/mongrel/http_request.rb index 3ffff9f..c8d4ce4 100644 --- a/lib/mongrel/http_request.rb +++ b/lib/mongrel/http_request.rb @@ -89,11 +89,11 @@ module Mongrel update_request_progress(remain, total) end rescue Object => e - STDERR.puts "#{Time.now.httpdate}: Error reading HTTP body: #{e.inspect}" + STDERR.puts "#{Time.now}: Error reading HTTP body: #{e.inspect}" 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 @@ -102,14 +102,14 @@ module Mongrel if !@socket.closed? data = @socket.read(len) if !data - raise "#{Time.now.httpdate}: Socket read return nil" + raise "Socket read return nil" elsif data.length != len - raise "#{Time.now.httpdate}: Socket read returned insufficient data: #{data.length}" + raise "Socket read returned insufficient data: #{data.length}" else data end else - raise "#{Time.now.httpdate}: Socket already closed when reading." + raise "Socket already closed when reading." end end diff --git a/lib/mongrel/http_response.rb b/lib/mongrel/http_response.rb index e30bdaf..3076712 100644 --- a/lib/mongrel/http_response.rb +++ b/lib/mongrel/http_response.rb @@ -71,11 +71,14 @@ module Mongrel # sent the header or the body. This is pretty catastrophic actually. def reset if @body_sent - raise "#{Time.now.httpdate}: You have already sent the request body." + raise "You have already sent the request body." elsif @header_sent - raise "#{Time.now.httpdate}: You have already sent the request headers." + 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/lib/mongrel/logger.rb b/lib/mongrel/logger.rb deleted file mode 100644 index b990484..0000000 --- a/lib/mongrel/logger.rb +++ /dev/null @@ -1,74 +0,0 @@ -# Note: Logger concepts are from a combination of: -# AlogR: http://alogr.rubyforge.org -# Merb: http://merbivore.com -module Mongrel - - class Log - attr_accessor :logger - attr_accessor :log_level - - Levels = { - :name => { :emergency => 0, :alert => 1, :critical => 2, :error => 3, - :warning => 4, :notice => 5, :info => 6, :debug => 7 }, - :id => { 0 => :emergency, 1 => :alert, 2 => :critical, 3 => :error, - 4 => :warning, 5 => :notice, 6 => :info, 7 => :debug } - } - - def initialize(log, log_level = :debug) - @logger = initialize_io(log) - @log_level = Levels[:name][log_level] || 7 - - if !RUBY_PLATFORM.match(/java|mswin/) && !(@log == STDOUT) && - @log.respond_to?(:write_nonblock) - @aio = true - end - $MongrelLogger = self - end - - # Writes a string to the logger. Writing of the string is skipped if the string's log level is - # higher than the logger's log level. If the logger responds to write_nonblock and is not on - # the java or windows platforms then the logger will use non-blocking asynchronous writes. - def log(*args) - if args[0].is_a?(String) - level, string = 6, args[0] - else - level = (args[0].is_a?(Fixnum) ? args[0] : Levels[:name][args[0]]) || 6 - string = args[1] - end - - return if (level > log_level) - - if @aio - @log.write_nonblock("#{Time.now.httpdate} | #{Levels[:id][level]} | #{string}\n") - else - @log.write("#{Time.now.httpdate} | #{Levels[:id][level]} | #{string}\n") - end - end - - private - - def initialize_io(log) - if log.respond_to?(:write) - @log = log - @log.sync if log.respond_to?(:sync) - elsif File.exist?(log) - @log = open(log, (File::WRONLY | File::APPEND)) - @log.sync = true - else - FileUtils.mkdir_p(File.dirname(log)) unless File.exist?(File.dirname(log)) - @log = open(log, (File::WRONLY | File::APPEND | File::CREAT)) - @log.sync = true - @log.write("#{Time.now.httpdate} | info | Logfile created\n") - end - end - - end - - # Convenience wrapper for logging, allows us to use Mongrel.log - def self.log(*args) - # If no logger has been defined yet at this point, log to STDOUT. - $MongrelLogger ||= Mongrel::Log.new(STDOUT, :debug) - $MongrelLogger.log(*args) - end - -end diff --git a/lib/mongrel/rails.rb b/lib/mongrel/rails.rb index 2366ab0..7f66a5e 100644 --- a/lib/mongrel/rails.rb +++ b/lib/mongrel/rails.rb @@ -82,7 +82,7 @@ module Mongrel rescue Errno::EPIPE response.socket.close rescue Object => rails_error - STDERR.puts "#{Time.now.httpdate}: Error calling Dispatcher.dispatch #{rails_error.inspect}" + STDERR.puts "#{Time.now}: Error calling Dispatcher.dispatch #{rails_error.inspect}" STDERR.puts rails_error.backtrace.join("\n") end end @@ -161,9 +161,9 @@ module Mongrel raise "Rails was not configured. Read the docs for RailsConfigurator." end - Mongrel.log("Reloading Rails...") + log "Reloading Rails..." @rails_handler.reload! - Mongrel.log("Done reloading Rails.") + log "Done reloading Rails." end @@ -173,11 +173,11 @@ module Mongrel ops = resolve_defaults(options) setup_signals(options) - unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ + if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ # rails reload - trap("HUP") { Mongrel.log("HUP signal received."); reload! } + trap("HUP") { log "HUP signal received."; reload! } - Mongrel.log("Rails signals registered. HUP => reload (without restart). It might not work well.") + log "Rails signals registered. HUP => reload (without restart). It might not work well." end end end diff --git a/projects/fastthread/Rakefile b/projects/fastthread/Rakefile index c7d1269..e418a5a 100644 --- a/projects/fastthread/Rakefile +++ b/projects/fastthread/Rakefile @@ -1,48 +1,28 @@ +require 'rubygems' +gem 'echoe', '>=2.7.11' require 'echoe' Echoe.new("fastthread") do |p| p.project = "mongrel" p.author = "MenTaLguY <mental@rydia.net>" + p.email = "mental@rydia.net" p.summary = "Optimized replacement for thread.rb primitives" p.extensions = "ext/fastthread/extconf.rb" p.clean_pattern = ['build/*', '**/*.o', '**/*.so', '**/*.a', 'lib/*-*', '**/*.log', "ext/fastthread/*.{bundle,so,obj,pdb,lib,def,exp}", "ext/fastthread/Makefile", "pkg", "lib/*.bundle", "*.gem", ".config"] p.need_tar_gz = false p.need_tgz = true - # FIXME: find a workaround to have multiple key chains outside the Rakefile - # tried GEM_CERTIFICATE_CHAIN but produces an asn1 error - p.certificate_chain = [ - '~/projects/gem_certificates/mongrel-public_cert.pem', - '~/projects/gem_certificates/luislavena-mongrel-public_cert.pem' - ] - #p.certificate_chain = ['/Users/eweaver/p/configuration/gem_certificates/mongrel/mongrel-public_cert.pem', - # '/Users/eweaver/p/configuration/gem_certificates/evan_weaver-mongrel-public_cert.pem'] + p.certificate_chain = ['/Users/eweaver/p/configuration/gem_certificates/mongrel/mongrel-public_cert.pem', + '/Users/eweaver/p/configuration/gem_certificates/evan_weaver-mongrel-public_cert.pem'] p.require_signed = true - p.eval = proc do - if RUBY_PLATFORM.match("win32") - extensions.clear - self.files += ['lib/fastthread.so'] + p.eval = proc do + if Platform.windows? self.platform = Gem::Platform::CURRENT + self.files += ['lib/fastthread.so'] task :package => [:clean, :compile] end end -end -def move_extensions - Dir["ext/**/*.#{Config::CONFIG['DLEXT']}"].each { |file| mv file, "lib/" } -end - -case RUBY_PLATFORM -when /mswin/ - filename = "lib/fastthread.so" - file filename do - Dir.chdir("ext/fastthread") do - ruby "extconf.rb" - system(PLATFORM =~ /mswin/ ? 'nmake' : 'make') - end - move_extensions - end - task :compile => [filename] end diff --git a/projects/gem_plugin/CHANGELOG b/projects/gem_plugin/CHANGELOG index 7e1103d..cf39b5b 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/Rakefile b/projects/gem_plugin/Rakefile index f1bd428..b312bfe 100644 --- a/projects/gem_plugin/Rakefile +++ b/projects/gem_plugin/Rakefile @@ -1,8 +1,11 @@ +require 'rubygems' +gem 'echoe', '>=2.7.11' require 'echoe' Echoe.new("gem_plugin") do |p| - p.author="Zed A. Shaw" + p.author= "Zed A. Shaw" + p.email = "mongrel-development@rubyforge.org" p.project = "mongrel" p.summary = "A plugin system based on rubygems that uses dependencies only" @@ -15,7 +18,7 @@ Echoe.new("gem_plugin") do |p| p.test_pattern = "test/test_plugins.rb" p.clean_pattern += ["pkg", "lib/*.bundle", "*.gem", ".config"] p.rdoc_pattern = ['README', 'LICENSE', 'COPYING', 'lib/**/*.rb', 'doc/**/*.rdoc'] - p.rdoc_template = `allison --path`.chomp + p.rdoc_template = `#{Platform.windows? ? 'allison.bat' : 'allison'} --path`.chomp end namespace :site do diff --git a/projects/gem_plugin/lib/gem_plugin.rb b/projects/gem_plugin/lib/gem_plugin.rb index 1996a61..782a990 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,11 +128,17 @@ 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.exist?(gem_dir) + end - require File.join(gem_dir, "lib", gem.name, "init.rb") - @gems[gem.name] = gem_dir + gem_init = File.join(gem_dir, "lib", gem.name, "init.rb") + if File.exist?(gem_init) + require gem_init + @gems[gem.name] = gem_dir + end end end diff --git a/projects/mongrel_cluster/CHANGELOG b/projects/mongrel_cluster/CHANGELOG index 6e6f31e..d860d6d 100644 --- a/projects/mongrel_cluster/CHANGELOG +++ b/projects/mongrel_cluster/CHANGELOG @@ -1,5 +1,5 @@ -v1.0.5. Close #15406 (find_pid returning non-false) (Eden Li); close #15616 (wrong Cap 2 detection) (Ryan McGeary). +v1.0.5. Close #15406 (find_pid returning non-false) (Eden Li). v1.0.4. Actually ship the Cap 2 tasks; use an autorequire based on whether namespace() is available (Kevin Runde). diff --git a/projects/mongrel_cluster/lib/mongrel_cluster/init.rb b/projects/mongrel_cluster/lib/mongrel_cluster/init.rb index 9e678d3..0f18fe9 100644 --- a/projects/mongrel_cluster/lib/mongrel_cluster/init.rb +++ b/projects/mongrel_cluster/lib/mongrel_cluster/init.rb @@ -13,7 +13,7 @@ module Cluster valid_exists?(@config_file, "Configuration file does not exist. Run mongrel_rails cluster::configure.") @valid end - + def read_options @options = { "environment" => ENV['RAILS_ENV'] || "development", @@ -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'] @@ -80,30 +80,30 @@ module Cluster @ports.each do |port| if @clean && pid_file_exists?(port) && !check_process(port) pid_file = port_pid_file(port) - Mongrel.log("missing process: removing #{pid_file}") + log "missing process: removing #{pid_file}" chdir_cwd do File.unlink(pid_file) end end if pid_file_exists?(port) && check_process(port) - Mongrel.log("already started port #{port}") + log "already started port #{port}" next end exec_cmd = cmd + " -p #{port} -P #{port_pid_file(port)}" exec_cmd += " -l #{port_log_file(port)}" - Mongrel.log("starting port #{port}") + log "starting port #{port}" log_verbose exec_cmd output = `#{exec_cmd}` - Mongrel.log(:error, output) unless $?.success? + log_error output unless $?.success? end end def stop read_options - argv = [ "mongrel_rails" ] + argv = @options['mongrel_rails'] argv << "stop" argv << "-c #{@options["cwd"]}" if @options["cwd"] argv << "-f" if @force @@ -112,20 +112,20 @@ module Cluster @ports.each do |port| pid = check_process(port) if @clean && pid && !pid_file_exists?(port) - Mongrel.log("missing pid_file: killing mongrel_rails port #{port}, pid #{pid}") + log "missing pid_file: killing mongrel_rails port #{port}, pid #{pid}" Process.kill("KILL", pid.to_i) end if !check_process(port) - Mongrel.log("already stopped port #{port}") + log "already stopped port #{port}" next end exec_cmd = cmd + " -P #{port_pid_file(port)}" - Mongrel.log("stopping port #{port}") + log "stopping port #{port}" log_verbose exec_cmd output = `#{exec_cmd}` - Mongrel.log(:error, output) unless $?.success? + log_error output unless $?.success? end end @@ -138,18 +138,18 @@ module Cluster @ports.each do |port| pid = check_process(port) unless pid_file_exists?(port) - Mongrel.log(:error, "missing pid_file: #{port_pid_file(port)}") + log "missing pid_file: #{port_pid_file(port)}" status = STATUS_ERROR else - Mongrel.log("found pid_file: #{port_pid_file(port)}") + log "found pid_file: #{port_pid_file(port)}" end if pid - Mongrel.log("found mongrel_rails: port #{port}, pid #{pid}") + log "found mongrel_rails: port #{port}, pid #{pid}" else - Mongrel.log(:error, "missing mongrel_rails: port #{port}") + log "missing mongrel_rails: port #{port}" status = STATUS_ERROR end - Mongrel.log("") + puts "" end status @@ -211,12 +211,18 @@ module Cluster nil end + def log_error(message) + log(message) + end + def log_verbose(message) - Mongrel.log(message) if @verbose + log(message) if @verbose end + def log(message) + puts message + end end - class Start < GemPlugin::Plugin "/commands" include ExecBase @@ -294,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 @@ -328,8 +335,9 @@ 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 - Mongrel.log("Writing configuration file to #{@config_file}.") + log "Writing configuration file to #{@config_file}." File.open(@config_file,"w") {|f| f.write(@options.to_yaml)} end end diff --git a/projects/mongrel_cluster/lib/mongrel_cluster/recipes.rb b/projects/mongrel_cluster/lib/mongrel_cluster/recipes.rb index 6b62712..74ec468 100644 --- a/projects/mongrel_cluster/lib/mongrel_cluster/recipes.rb +++ b/projects/mongrel_cluster/lib/mongrel_cluster/recipes.rb @@ -1,5 +1,5 @@ -if Capistrano::Configuration.respond_to?(:instance) +if respond_to?(:namespace) require 'mongrel_cluster/recipes_2' # Cap 2 else require 'mongrel_cluster/recipes_1' # Cap 1 diff --git a/projects/mongrel_cluster/lib/mongrel_cluster/recipes_2.rb b/projects/mongrel_cluster/lib/mongrel_cluster/recipes_2.rb index a82c424..312ef5c 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/projects/mongrel_service/CHANGELOG b/projects/mongrel_service/CHANGELOG index 67f5086..074c87a 100644 --- a/projects/mongrel_service/CHANGELOG +++ b/projects/mongrel_service/CHANGELOG @@ -1,3 +1,8 @@ +* 0.3.5 * + + * Wait longer for child process terminate properly (max 20 seconds). Imported + tests from RubyServices project. (Closes #18). + * Updated ServiceFB to work with FB > 0.18. * 0.3.4 * diff --git a/projects/mongrel_service/Rakefile b/projects/mongrel_service/Rakefile index 1584ca3..efc9810 100644 --- a/projects/mongrel_service/Rakefile +++ b/projects/mongrel_service/Rakefile @@ -1,3 +1,6 @@ + +require 'rubygems' +gem 'echoe', '>=2.7.11' require 'echoe' require 'tools/freebasic' @@ -10,6 +13,7 @@ echoe_spec = Echoe.new("mongrel_service") do |p| p.summary += " (debug build)" unless ENV['RELEASE'] p.description = "This plugin offer native win32 services for rails, powered by Mongrel." p.author = "Luis Lavena" + p.email = "luislavena@gmail.com" p.platform = Gem::Platform::CURRENT p.dependencies = [['gem_plugin', '>=0.2.3', '<0.3.0'], ['mongrel', '>=1.0.2', '<1.2.0'], @@ -93,3 +97,42 @@ end #include_projects_of :native task :native_service => "native:build" task :clean => "native:clobber" + +project_task :mock_process do + executable :mock_process + build_to 'tests' + + main 'tests/fixtures/mock_process.bas' + + option OPTIONS +end + +task "all_tests:build" => "lib:build" +project_task :all_tests do + executable :all_tests + build_to 'tests' + + search_path 'src', 'lib', 'native' + lib_path 'lib' + + main 'tests/all_tests.bas' + + # this temporally fix the inverse namespace ctors of FB + source Dir.glob("tests/test_*.bas").reverse + + library 'testly' + + source 'native/console_process.bas' + + option OPTIONS +end + +desc "Run all the internal tests for the library" +task "all_tests:run" => ["mock_process:build", "all_tests:build"] do + Dir.chdir('tests') do + sh %{all_tests} + end +end + +desc "Run all the test for this project" +task :test => "all_tests:run" diff --git a/projects/mongrel_service/lib/ServiceFB/ServiceFB_Utils.bas b/projects/mongrel_service/lib/ServiceFB/ServiceFB_Utils.bas index c5150ea..c8a77a3 100644 --- a/projects/mongrel_service/lib/ServiceFB/ServiceFB_Utils.bas +++ b/projects/mongrel_service/lib/ServiceFB/ServiceFB_Utils.bas @@ -15,6 +15,8 @@ namespace svc namespace utils '# fb.svc.utils
'# private (internals) for ServiceProcess.Console()
dim shared _svc_stop_signal as any ptr
+ dim shared _svc_stop_mutex as any ptr
+ dim shared _svc_stopped as BOOL
dim shared _svc_in_console as ServiceProcess ptr
dim shared _svc_in_console_stop_flag as BOOL
@@ -168,6 +170,7 @@ namespace utils '# fb.svc.utils '# create the signal used to stop the service thread.
_svc_stop_signal = condcreate()
+ _svc_stop_mutex = mutexcreate()
'# register the Console Handler
SetConsoleCtrlHandler(@_console_handler, TRUE)
@@ -189,6 +192,9 @@ namespace utils '# fb.svc.utils if not (service->onStart = 0) then
'# create the thread
working_thread = threadcreate(@ServiceProcess.call_onStart, service)
+ if (working_thread = 0) then
+ print "Failed to create working thread."
+ end if
end if
print "Service is in running state."
@@ -197,7 +203,11 @@ namespace utils '# fb.svc.utils '# now that onStart is running, must monitor the stop_signal
'# in case it arrives, the service state must change to exit the
'# working thread.
- condwait(_svc_stop_signal)
+ mutexlock(_svc_stop_mutex)
+ do while (_svc_stopped = FALSE)
+ condwait(_svc_stop_signal, _svc_stop_mutex)
+ loop
+ mutexunlock(_svc_stop_mutex)
print "Stop signal received, stopping..."
@@ -222,7 +232,7 @@ namespace utils '# fb.svc.utils '# now that service was stopped, destroy the references.
conddestroy(_svc_stop_signal)
-
+ mutexdestroy(_svc_stop_mutex)
print "Done."
end if
else
@@ -287,9 +297,12 @@ namespace utils '# fb.svc.utils '# now fire the signal
_dprint("fire stop signal")
+ mutexlock(_svc_stop_mutex)
condsignal(_svc_stop_signal)
result = TRUE
_svc_in_console_stop_flag = FALSE
+ _svc_stopped = TRUE
+ mutexunlock(_svc_stop_mutex)
case else:
_dprint("unsupported CTRL EVENT")
diff --git a/projects/mongrel_service/lib/ServiceFB/_utils_internals.bi b/projects/mongrel_service/lib/ServiceFB/_utils_internals.bi index 67acc87..fab00f0 100644 --- a/projects/mongrel_service/lib/ServiceFB/_utils_internals.bi +++ b/projects/mongrel_service/lib/ServiceFB/_utils_internals.bi @@ -42,6 +42,8 @@ namespace utils '# fb.svc.utils '# will raise in case Ctrl+C / Ctrl+Break or other events are
'# received.
extern _svc_stop_signal as any ptr
+ extern _svc_stop_mutex as any ptr
+ extern _svc_stopped as BOOL
extern _svc_in_console as ServiceProcess ptr
extern _svc_in_console_stop_flag as BOOL
end namespace '# fb.svc.utils
diff --git a/projects/mongrel_service/native/console_process.bas b/projects/mongrel_service/native/console_process.bas index 78dc1a0..a2bb5c9 100644 --- a/projects/mongrel_service/native/console_process.bas +++ b/projects/mongrel_service/native/console_process.bas @@ -81,6 +81,7 @@ property ConsoleProcess.pid() as uinteger end property
property ConsoleProcess.exit_code() as uinteger
+ static previous_code as uinteger
dim result as uinteger
result = 0
@@ -90,9 +91,16 @@ property ConsoleProcess.exit_code() as uinteger if not (_process_info.hProcess = NULL) then
'# the process reference is valid, get the exit_code
if not (GetExitCodeProcess(_process_info.hProcess, @result) = 0) then
+ previous_code = result
'# OK
'# no error in the query, get result
+ if not (result = STILL_ACTIVE) then
+ CloseHandle(_process_info.hProcess)
+ _process_info.hProcess = NULL
+ end if '# (result = STILL_ACTIVE)
end if '# not (GetExitCodeProcess() = 0)
+ else
+ result = previous_code
end if '# not (proc = NULL)
end if '# not (_pid = 0)
@@ -228,7 +236,7 @@ function ConsoleProcess.start() as boolean else
'# use pipes instead
'# StdOut
- if (CreatePipe(@StdErrRd, @StdErrWr, @proc_sa, 0) = 0) then
+ if (CreatePipe(@StdErrRd, @StdErrWr, @proc_sa, 0) = 0) then
success = false
end if
@@ -283,10 +291,10 @@ function ConsoleProcess.start() as boolean CloseHandle(StdErrRd)
CloseHandle(StdErrWr)
- '# close children main Thread handle
- 'CloseHandle(proc.hThread)
- 'CloseHandle(proc.hProcess)
-
+ '# close children main Thread handle and
+ '# NULLify to avoid issues
+ CloseHandle(_process_info.hThread)
+ _process_info.hThread = NULL
end if '# (CreateProcess() = 0)
else
result = false
@@ -322,7 +330,7 @@ function ConsoleProcess.terminate(byval force as boolean = false) as boolean '# send CTRL_C_EVENT and wait for result
if not (GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) = 0) then
'# it worked, wait 5 seconds terminates.
- wait_code = WaitForSingleObject(proc, 5000)
+ wait_code = WaitForSingleObject(proc, 10000)
if not (wait_code = WAIT_TIMEOUT) then
success = true
end if
@@ -335,7 +343,7 @@ function ConsoleProcess.terminate(byval force as boolean = false) as boolean '# send CTRL_BREAK_EVENT and wait for result
if not (GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0) = 0) then
'# it worked, wait 5 seconds terminates.
- wait_code = WaitForSingleObject(proc, 5000)
+ wait_code = WaitForSingleObject(proc, 10000)
if not (wait_code = WAIT_TIMEOUT) then
success = true
end if
diff --git a/projects/mongrel_service/tests/all_tests.bas b/projects/mongrel_service/tests/all_tests.bas new file mode 100644 index 0000000..0a8864f --- /dev/null +++ b/projects/mongrel_service/tests/all_tests.bas @@ -0,0 +1,18 @@ +'#--
+'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
+'#
+'# This source code is released under the MIT License.
+'# See MIT-LICENSE file for details
+'#++
+
+#include once "testly.bi"
+
+'# the code in this module runs after all
+'# the other modules have "registered" their suites.
+
+'# evaluate the result from run_tests() to
+'# return a error to the OS or not.
+if (run_tests() = false) then
+ end 1
+end if
+
diff --git a/projects/mongrel_service/tests/fixtures/mock_process.bas b/projects/mongrel_service/tests/fixtures/mock_process.bas new file mode 100644 index 0000000..e9405ab --- /dev/null +++ b/projects/mongrel_service/tests/fixtures/mock_process.bas @@ -0,0 +1,92 @@ +'#--
+'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
+'#
+'# This source code is released under the MIT License.
+'# See MIT-LICENSE file for details
+'#++
+
+'# this program mock a common process that will:
+'# output some text to stdout
+'# output some error messages to stderr
+'# will wait until Ctrl-C is hit (only if commandline contains "wait")
+'# or drop an error if commandline contains "error"
+
+#include once "crt.bi"
+#include once "windows.bi"
+
+dim shared as any ptr control_signal, control_mutex
+dim shared flagged as byte
+dim shared result as integer
+
+function slow_console_handler(byval dwCtrlType as DWORD) as BOOL
+ dim result as BOOL
+
+ if (dwCtrlType = CTRL_C_EVENT) then
+ fprintf(stdout, !"out: CTRL-C received\r\n")
+ mutexlock(control_mutex)
+ result = 1
+ flagged = 1
+ condsignal(control_signal)
+ mutexunlock(control_mutex)
+ elseif (dwCtrlType = CTRL_BREAK_EVENT) then
+ fprintf(stdout, !"out: CTRL-BREAK received\r\n")
+ mutexlock(control_mutex)
+ result = 1
+ flagged = 2
+ condsignal(control_signal)
+ mutexunlock(control_mutex)
+ end if
+
+ return result
+end function
+
+sub wait_for(byval flag_level as integer)
+ flagged = 0
+ '# set handler
+ if (SetConsoleCtrlHandler(@slow_console_handler, 1) = 0) then
+ fprintf(stderr, !"err: cannot set console handler\r\n")
+ end if
+ fprintf(stdout, !"out: waiting for keyboard signal\r\n")
+ mutexlock(control_mutex)
+ do until (flagged = flag_level)
+ condwait(control_signal, control_mutex)
+ loop
+ mutexunlock(control_mutex)
+ fprintf(stdout, !"out: got keyboard signal\r\n")
+ if (SetConsoleCtrlHandler(@slow_console_handler, 0) = 0) then
+ fprintf(stderr, !"err: cannot unset console handler\r\n")
+ end if
+end sub
+
+function main() as integer
+ fprintf(stdout, !"out: message\r\n")
+ fprintf(stderr, !"err: error\r\n")
+
+ select case lcase(command(1))
+ case "wait":
+ sleep
+ return 0
+
+ case "error":
+ '# terminate with error code
+ return 1
+
+ case "slow1":
+ wait_for(1)
+ return 10
+
+ case "slow2":
+ wait_for(2)
+ return 20
+ end select
+end function
+
+control_signal = condcreate()
+control_mutex = mutexcreate()
+
+result = main()
+
+conddestroy(control_signal)
+mutexdestroy(control_mutex)
+
+end result
diff --git a/projects/mongrel_service/tests/test_console_process.bas b/projects/mongrel_service/tests/test_console_process.bas new file mode 100644 index 0000000..d41c0fb --- /dev/null +++ b/projects/mongrel_service/tests/test_console_process.bas @@ -0,0 +1,402 @@ +'#--
+'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
+'#
+'# This source code is released under the MIT License.
+'# See MIT-LICENSE file for details
+'#++
+
+#include once "console_process.bi"
+#include once "file.bi"
+#include once "testly.bi"
+#include once "test_helpers.bi"
+
+namespace Suite_Test_Console_Process
+ '# test helpers
+ declare function process_cleanup() as boolean
+
+ dim shared child as ConsoleProcess ptr
+
+ sub before_all()
+ kill("out.log")
+ kill("err.log")
+ kill("both.log")
+ kill("both_slow.log")
+ kill("both_forced.log")
+ end sub
+
+ sub after_each()
+ process_cleanup()
+ end sub
+
+ sub test_process_create()
+ child = new ConsoleProcess()
+ assert_not_equal(0, child)
+ assert_equal("", child->filename)
+ assert_equal("", child->arguments)
+ assert_false(child->running)
+ delete child
+ end sub
+
+ sub test_process_create_args()
+ child = new ConsoleProcess("mock_process.exe", "some params")
+ assert_equal("mock_process.exe", child->filename)
+ assert_equal("some params", child->arguments)
+ delete child
+ end sub
+
+ sub test_properly_quoted_filename()
+ child = new ConsoleProcess("C:\path with spaces\my_executable.exe", "some params")
+ assert_not_equal(0, instr(child->filename, !"\""))
+ delete child
+ end sub
+
+ sub test_failed_unexistant_process()
+ child = new ConsoleProcess("no_valid_file.exe", "some params")
+ assert_false(child->start())
+ assert_equal(0, child->pid)
+ assert_false(child->running)
+ delete child
+ end sub
+
+ sub test_process_spawn_exit_code()
+ child = new ConsoleProcess("mock_process.exe", "error")
+
+ '# start() should return true since it started, no matter if was terminated
+ '# improperly
+ assert_true(child->start())
+ sleep 500
+
+ '# should not be running, but pid should be != than 0
+ assert_not_equal(0, child->pid)
+
+ '# we need to wait a bit prior asking for state
+ '# the process could be still running
+ assert_false(child->running)
+
+ '# get exit code, should be 1
+ assert_equal(1, child->exit_code)
+
+ delete child
+ end sub
+
+ sub test_redirected_output()
+ assert_false(fileexists("out.log"))
+ assert_false(fileexists("err.log"))
+
+ '# redirected output is used with logging files.
+ child = new ConsoleProcess("mock_process.exe")
+
+ '# redirect stdout
+ assert_true(child->redirect(ProcessStdOut, "out.log"))
+ assert_string_equal("out.log", child->redirected_stdout)
+
+ '# redirect stderr
+ assert_true(child->redirect(ProcessStdErr, "err.log"))
+ assert_string_equal("err.log", child->redirected_stderr)
+
+ '# start() will be true since process terminated nicely
+ assert_true(child->start())
+ sleep 500
+
+ '# running should be false
+ assert_false(child->running)
+
+ '# exit_code should be 0
+ assert_equal(0, child->exit_code)
+
+ delete child
+
+ '# now out.log and err.log must exist and content must be valid.
+ assert_true(fileexists("out.log"))
+ assert_string_equal("out: message", content_of_file("out.log"))
+
+ assert_true(fileexists("err.log"))
+ assert_string_equal("err: error", content_of_file("err.log"))
+
+ assert_equal(0, kill("out.log"))
+ assert_equal(0, kill("err.log"))
+ end sub
+
+ sub test_redirected_merged_output()
+ dim content as string
+
+ '# redirected output is used with logging files.
+ child = new ConsoleProcess("mock_process.exe")
+
+ '# redirect both stdout and stderr
+ child->redirect(ProcessStdBoth, "both.log")
+ assert_equal("both.log", child->redirected_stdout)
+ assert_equal("both.log", child->redirected_stderr)
+
+ '# start() will be true since process terminated nicely
+ assert_true(child->start())
+ sleep 500
+
+ '# running should be false
+ assert_false(child->running)
+
+ '# exit_code should be 0
+ assert_equal(0, child->exit_code)
+
+ delete child
+
+ '# file must exists
+ assert_true(fileexists("both.log"))
+
+ '# contents must match
+ content = content_of_file("both.log")
+
+ assert_not_equal(0, instr(content, "out: message"))
+ assert_not_equal(0, instr(content, "err: error"))
+
+ assert_equal(0, kill("both.log"))
+ end sub
+
+ sub test_redirected_output_append()
+ dim content as string
+
+ child = new ConsoleProcess("mock_process.exe")
+
+ '# redirect both stdout and stderr
+ child->redirect(ProcessStdBoth, "both.log")
+
+ '# start() will be true since process terminated nicely
+ assert_true(child->start())
+ sleep 500
+
+ content = content_of_file("both.log")
+
+ '# start() again
+ assert_true(child->start())
+ sleep 500
+
+ delete child
+
+ assert_not_equal(len(content), len(content_of_file("both.log")))
+
+ assert_equal(0, kill("both.log"))
+ end sub
+
+ sub test_process_terminate()
+ dim content as string
+
+ '# redirected output is used with logging files.
+ child = new ConsoleProcess("mock_process.exe", "wait")
+ child->redirect(ProcessStdBoth, "both.log")
+
+ '# start
+ assert_true(child->start())
+ sleep 500
+
+ '# validate if running
+ assert_true(child->running)
+
+ '# validate PID
+ assert_not_equal(0, child->pid)
+
+ '# now terminates it
+ assert_true(child->terminate())
+ sleep 500
+
+ assert_equal(9, child->exit_code)
+
+ '# it should be done
+ assert_false(child->running)
+
+ delete child
+
+ '# validate output
+ '# file must exists
+ assert_true(fileexists("both.log"))
+
+ '# contents must match
+ content = content_of_file("both.log")
+
+ assert_not_equal(0, instr(content, "out: message"))
+ assert_not_equal(0, instr(content, "err: error"))
+ assert_not_equal(0, instr(content, "interrupted"))
+
+ assert_equal(0, kill("both.log"))
+ end sub
+
+ sub test_process_terminate_slow1()
+ dim content as string
+
+ '# redirected output is used with logging files.
+ child = new ConsoleProcess("mock_process.exe", "slow1")
+ child->redirect(ProcessStdBoth, "both_slow1.log")
+
+ '# start
+ assert_true(child->start())
+ sleep 500
+
+ '# validate if running
+ assert_true(child->running)
+
+ '# validate PID
+ assert_not_equal(0, child->pid)
+
+ '# now terminates it
+ assert_true(child->terminate())
+ sleep 500
+
+ assert_equal(10, child->exit_code)
+
+ '# it should be done
+ assert_false(child->running)
+
+ delete child
+
+ '# validate output
+ '# file must exists
+ assert_true(fileexists("both_slow1.log"))
+
+ '# contents must match
+ content = content_of_file("both_slow1.log")
+
+ assert_equal(0, instr(content, "interrupted"))
+ assert_not_equal(0, instr(content, "out: CTRL-C received"))
+ assert_equal(0, instr(content, "out: CTRL-BREAK received"))
+
+ assert_equal(0, kill("both_slow1.log"))
+ end sub
+
+ sub test_process_terminate_slow2()
+ dim content as string
+
+ '# redirected output is used with logging files.
+ child = new ConsoleProcess("mock_process.exe", "slow2")
+ child->redirect(ProcessStdBoth, "both_slow2.log")
+
+ '# start
+ assert_true(child->start())
+ sleep 500
+
+ '# validate if running
+ assert_true(child->running)
+
+ '# validate PID
+ assert_not_equal(0, child->pid)
+
+ '# now terminates it
+ assert_true(child->terminate())
+ sleep 500
+
+ assert_equal(20, child->exit_code)
+
+ '# it should be done
+ assert_false(child->running)
+
+ delete child
+
+ '# validate output
+ '# file must exists
+ assert_true(fileexists("both_slow2.log"))
+
+ '# contents must match
+ content = content_of_file("both_slow2.log")
+
+ assert_equal(0, instr(content, "interrupted"))
+ assert_not_equal(0, instr(content, "out: CTRL-C received"))
+ assert_not_equal(0, instr(content, "out: CTRL-BREAK received"))
+
+ '# cleanup
+ assert_equal(0, kill("both_slow2.log"))
+ end sub
+
+ sub test_process_terminate_forced()
+ dim content as string
+
+ '# redirected output is used with logging files.
+ child = new ConsoleProcess("mock_process.exe", "wait")
+ child->redirect(ProcessStdBoth, "both_forced.log")
+
+ '# start
+ assert_true(child->start())
+ sleep 500
+
+ '# validate if running
+ assert_true(child->running)
+
+ '# validate PID
+ assert_not_equal(0, child->pid)
+
+ '# now terminates it
+ assert_true(child->terminate(true))
+ sleep 500
+
+ '# it should be done
+ assert_false(child->running)
+
+ '# look for termination code
+ assert_equal(0, child->exit_code)
+
+ delete child
+
+ '# validate output
+ '# file must exists
+ assert_true(fileexists("both_forced.log"))
+
+ '# contents must match
+ content = content_of_file("both_forced.log")
+
+ assert_equal(0, instr(content, "out: message"))
+ assert_equal(0, instr(content, "err: error"))
+ assert_equal(0, instr(content, "interrupted"))
+
+ assert_equal(0, kill("both_forced.log"))
+ end sub
+
+ sub test_reuse_object_instance()
+ dim first_pid as uinteger
+
+ child = new ConsoleProcess("mock_process.exe")
+
+ '# start
+ assert_true(child->start())
+ sleep 500
+
+ '# validate not running
+ assert_false(child->running)
+
+ '# validate PID
+ assert_not_equal(0, child->pid)
+
+ '# saves PID
+ first_pid = child->pid
+
+ '# start it again
+ assert_true(child->start())
+ sleep 500
+
+ '# it should have stopped by now
+ assert_false(child->running)
+ assert_not_equal(0, child->pid)
+ assert_not_equal(first_pid, child->pid)
+
+ delete child
+ end sub
+
+ private sub register() constructor
+ add_suite(Suite_Test_Console_Process)
+ add_test(test_process_create)
+ add_test(test_process_create_args)
+ add_test(test_properly_quoted_filename)
+ add_test(test_failed_unexistant_process)
+ add_test(test_process_spawn_exit_code)
+ add_test(test_redirected_output)
+ add_test(test_redirected_merged_output)
+ add_test(test_redirected_output_append)
+ add_test(test_process_terminate)
+ add_test(test_process_terminate_slow1)
+ add_test(test_process_terminate_slow2)
+ add_test(test_process_terminate_forced)
+ add_test(test_reuse_object_instance)
+ end sub
+
+ '# test helpers below this point
+ private function process_cleanup() as boolean
+ shell "taskkill /f /im mock_process.exe 1>NUL 2>&1"
+ return true
+ end function
+end namespace
diff --git a/projects/mongrel_service/tests/test_helpers.bas b/projects/mongrel_service/tests/test_helpers.bas new file mode 100644 index 0000000..c4647c0 --- /dev/null +++ b/projects/mongrel_service/tests/test_helpers.bas @@ -0,0 +1,35 @@ +'#--
+'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
+'#
+'# This source code is released under the MIT License.
+'# See MIT-LICENSE file for details
+'#++
+
+#include once "testly.bi"
+#include once "test_helpers.bi"
+#include once "file.bi"
+
+'# Global Helpers
+function content_of_file(byref filename as string) as string
+ dim result as string
+ dim handle as integer
+ dim buffer as string
+
+ result = ""
+ buffer = ""
+
+ if (fileexists(filename) = true) then
+ handle = freefile
+ open filename for input as #handle
+ do while not (eof(handle))
+ input #handle, buffer
+ result += buffer
+ buffer = ""
+ loop
+ close #handle
+ else
+ result = ""
+ end if
+
+ return result
+end function
diff --git a/projects/mongrel_service/tests/test_helpers.bi b/projects/mongrel_service/tests/test_helpers.bi new file mode 100644 index 0000000..9397962 --- /dev/null +++ b/projects/mongrel_service/tests/test_helpers.bi @@ -0,0 +1,8 @@ +'#--
+'# Copyright (c) 2006-2007 Luis Lavena, Multimedia systems
+'#
+'# This source code is released under the MIT License.
+'# See MIT-LICENSE file for details
+'#++
+
+declare function content_of_file(byref as string) as string
\ No newline at end of file diff --git a/projects/mongrel_service/tools/freebasic.rb b/projects/mongrel_service/tools/freebasic.rb index 8902059..0b3e445 100644 --- a/projects/mongrel_service/tools/freebasic.rb +++ b/projects/mongrel_service/tools/freebasic.rb @@ -63,7 +63,7 @@ module FreeBASIC @libraries_path = []
@options = {}
- instance_eval &block
+ instance_eval(&block) if block_given?
do_cleanup
@@ -90,7 +90,7 @@ module FreeBASIC # as output_name for the project
def lib(lib_name)
@type = :lib
- @output_name = lib_name
+ @output_name = "#{lib_name}"
@real_file_name = "lib#{lib_name}.a"
end
@@ -197,7 +197,9 @@ module FreeBASIC # return the compiled name version of the passed source file (src)
# compiled_form("test.bas") => "test.o"
def compiled_form(src)
- src.ext({ ".bas" => "o", ".rc" => "obj" }[File.extname(src)])
+ unless src.nil?
+ src.ext({ ".bas" => "o", ".rc" => "obj" }[File.extname(src)])
+ end
end
def compiled_project_file
@@ -207,11 +209,11 @@ module FreeBASIC def fbc_compile(source, target, main = nil)
cmdline = []
cmdline << "fbc"
+ cmdline << "-w pedantic" if (@options.has_key?(:pedantic) && @options[:pedantic] == true)
cmdline << "-g" if (@options.has_key?(:debug) && @options[:debug] == true)
cmdline << "-#{@options[:errorchecking].to_s}" if @options.has_key?(:errorchecking)
- cmdline << "-profile" if (@options.has_key?(:profile) && @options[:profile] == true)
cmdline << "-mt" if (@options.has_key?(:mt) && @options[:mt] == true)
- cmdline << "-w pedantic" if (@options.has_key?(:pedantic) && @options[:pedantic] == true)
+ cmdline << "-profile" if (@options.has_key?(:profile) && @options[:profile] == true)
cmdline << "-c #{source}"
cmdline << "-o #{target}"
cmdline << "-m #{main}" unless main.nil?
@@ -224,8 +226,8 @@ module FreeBASIC cmdline = []
cmdline << "fbc"
cmdline << "-g" if (@options.has_key?(:debug) && @options[:debug] == true)
- cmdline << "-profile" if (@options.has_key?(:profile) && @options[:profile] == true)
cmdline << "-mt" if (@options.has_key?(:mt) && @options[:mt] == true)
+ cmdline << "-profile" if (@options.has_key?(:profile) && @options[:profile] == true)
cmdline << "-#{@type.to_s}" unless @type == :executable
cmdline << "-x #{target}"
cmdline << files << extra_files
@@ -241,11 +243,15 @@ module FreeBASIC desc "Remove all compiled files for #{@name}"
task :clobber do
# remove compiled and linked file
- rm compiled_project_file rescue nil #unless @type == :lib
- rm File.join(@build_path, @complement_file) rescue nil if @type == :dylib
+ rm compiled_project_file rescue nil if File.exist?(compiled_project_file)
+ if @type == :dylib
+ rm File.join(@build_path, @complement_file) rescue nil if File.exist?(File.join(@build_path, @complement_file))
+ end
# remove main file
- rm compiled_form(@main_file) rescue nil
+ unless @main_file.nil? || !File.exists?(compiled_form(@main_file))
+ rm compiled_form(@main_file) rescue nil
+ end
# now the sources files
# avoid attempt to remove the file two times (this is a bug in Rake)
@@ -255,7 +261,7 @@ module FreeBASIC target = compiled_form(src)
unless CLOBBER.include?(target)
CLOBBER.include(target)
- rm target rescue nil
+ rm target rescue nil if File.exist?(target)
end
end
end
diff --git a/test/benchmark/previous.rb b/test/benchmark/previous.rb new file mode 100644 index 0000000..8b6182a --- /dev/null +++ b/test/benchmark/previous.rb @@ -0,0 +1,11 @@ +# Benchmark to compare Mongrel performance against +# previous Mongrel version (the one installed as a gem). +# +# Run with: +# +# ruby previous.rb [num of request] +# + +require File.dirname(__FILE__) + '/utils' + +benchmark "print", %w(current gem), 1000, [1, 10, 100] diff --git a/test/benchmark/simple.rb b/test/benchmark/simple.rb new file mode 100644 index 0000000..906f74c --- /dev/null +++ b/test/benchmark/simple.rb @@ -0,0 +1,11 @@ +# +# Simple benchmark to compare Mongrel performance against +# other webservers supported by Rack. +# + +require File.dirname(__FILE__) + '/utils' + +libs = %w(current gem WEBrick EMongrel Thin) +libs = ARGV if ARGV.any? + +benchmark "print", libs, 1000, [1, 10, 100] diff --git a/test/benchmark/utils.rb b/test/benchmark/utils.rb new file mode 100644 index 0000000..feb22c1 --- /dev/null +++ b/test/benchmark/utils.rb @@ -0,0 +1,82 @@ + +require 'rubygems' +require 'rack' +require 'rack/lobster' + +def run(handler_name, n=1000, c=1) + port = 7000 + + server = fork do + [STDOUT, STDERR].each { |o| o.reopen "/dev/null" } + + case handler_name + when 'EMongrel' + require 'swiftcore/evented_mongrel' + handler_name = 'Mongrel' + + when 'Thin' + require 'thin' + hander_name = 'Thin' + + when 'gem' # Load the current Mongrel gem + require 'mongrel' + handler_name = 'Mongrel' + + when 'current' # Load the current Mongrel version under /lib + require File.dirname(__FILE__) + '/../lib/mongrel' + handler_name = 'Mongrel' + + end + + app = Rack::Lobster.new + + handler = Rack::Handler.const_get(handler_name) + handler.run app, :Host => '0.0.0.0', :Port => port + end + + sleep 2 + + out = `nice -n20 ab -c #{c} -n #{n} http://127.0.0.1:#{port}/ 2> /dev/null` + + Process.kill('SIGKILL', server) + Process.wait + + if requests = out.match(/^Requests.+?(\d+\.\d+)/) + requests[1].to_i + else + 0 + end +end + +def benchmark(type, servers, request, concurrency_levels) + send "#{type}_benchmark", servers, request, concurrency_levels +end + +def graph_benchmark(servers, request, concurrency_levels) + require '/usr/local/lib/ruby/gems/1.8/gems/gruff-0.2.9/lib/gruff' + g = Gruff::Area.new + g.title = "Server benchmark" + + servers.each do |server| + g.data(server, concurrency_levels.collect { |c| print '.'; run(server, request, c) }) + end + puts + + g.x_axis_label = 'Concurrency' + g.y_axis_label = 'Requests / sec' + g.labels = {} + concurrency_levels.each_with_index { |c, i| g.labels[i] = c.to_s } + + g.write('bench.png') + `open bench.png` +end + +def print_benchmark(servers, request, concurrency_levels) + puts 'server request concurrency req/s' + puts '=' * 42 + concurrency_levels.each do |c| + servers.each do |server| + puts "#{server.ljust(8)} #{request} #{c.to_s.ljust(4)} #{run(server, request, c)}" + end + end +end
\ No newline at end of file diff --git a/test/testhelp.rb b/test/test_helper.rb index 4fe112d..7615fdd 100644 --- a/test/testhelp.rb +++ b/test/test_helper.rb @@ -65,10 +65,15 @@ def hit(uris) return results end -# process_based_port provides a port number, usable for TCP and UDP -# connections based on $$ and with a 5000 as base. -# this is required if you perform several builds of mongrel in parallel -# (like continuous integration systems) -def process_based_port - 5000 + $$ % 1000 +# process_based_port provides a port number, usable for TCP and UDP +# connections based on $$ and with a 5000 as base. +# this is required if you perform several builds of mongrel in parallel +# (like continuous integration systems) +def process_based_port + 5000 + $$ % 1000 +end + +# Platform check helper ;-) +def windows? + result = RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ end
\ No newline at end of file diff --git a/tools/trickletest.rb b/test/tools/trickletest.rb index e19ed71..e19ed71 100644 --- a/tools/trickletest.rb +++ b/test/tools/trickletest.rb diff --git a/test/test_cgi_wrapper.rb b/test/unit/test_cgi_wrapper.rb index 449f6d0..a494655 100644 --- a/test/test_cgi_wrapper.rb +++ b/test/unit/test_cgi_wrapper.rb @@ -1,5 +1,5 @@ -require 'test/testhelp' +require 'test/test_helper' class MockHttpRequest attr_reader :body diff --git a/test/test_command.rb b/test/unit/test_command.rb index 3cb9643..2e49ff2 100644 --- a/test/test_command.rb +++ b/test/unit/test_command.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' class TestCommand < GemPlugin::Plugin "/commands" include Mongrel::Command::Base diff --git a/test/test_conditional.rb b/test/unit/test_conditional.rb index 792a880..64517db 100644 --- a/test/test_conditional.rb +++ b/test/unit/test_conditional.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' include Mongrel diff --git a/test/test_configurator.rb b/test/unit/test_configurator.rb index fde2682..dc9713a 100644 --- a/test/test_configurator.rb +++ b/test/unit/test_configurator.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' $test_plugin_fired = 0 @@ -29,12 +29,12 @@ end class ConfiguratorTest < Test::Unit::TestCase def test_base_handler_config - port = process_based_port + @port = process_based_port @config = nil redirect_test_io do @config = Mongrel::Configurator.new :host => "localhost" do - listener :port => port do + listener :port => process_based_port do # 2 in front should run, but the sentinel shouldn't since dirhandler processes the request uri "/", :handler => plugin("/handlers/testplugin") uri "/", :handler => plugin("/handlers/testplugin") @@ -49,14 +49,14 @@ class ConfiguratorTest < Test::Unit::TestCase debug "/" setup_signals - run_config(File.dirname(__FILE__) + "/../test/mongrel.conf") - load_mime_map(File.dirname(__FILE__) + "/../test/mime.yaml") + run_config(HERE + "/mongrel.conf") + load_mime_map(HERE + "/mime.yaml") run end end end - + # pp @config.listeners.values.first.classifier.routes @config.listeners.each do |host,listener| @@ -65,12 +65,12 @@ class ConfiguratorTest < Test::Unit::TestCase assert listener.classifier.uris.include?("/test"), "/test not registered" end - res = Net::HTTP.get(URI.parse("http://localhost:#{port}/test")) + res = Net::HTTP.get(URI.parse("http://localhost:#{@port}/test")) assert res != nil, "Didn't get a response" assert $test_plugin_fired == 3, "Test filter plugin didn't run 3 times." redirect_test_io do - res = Net::HTTP.get(URI.parse("http://localhost:#{port}/")) + res = Net::HTTP.get(URI.parse("http://localhost:#{@port}/")) assert res != nil, "Didn't get a response" assert $test_plugin_fired == 6, "Test filter plugin didn't run 6 times." @@ -81,7 +81,7 @@ class ConfiguratorTest < Test::Unit::TestCase end assert_raise Errno::EBADF, Errno::ECONNREFUSED do - res = Net::HTTP.get(URI.parse("http://localhost:#{port}/")) + res = Net::HTTP.get(URI.parse("http://localhost:#{@port}/")) end end diff --git a/test/test_debug.rb b/test/unit/test_debug.rb index 2f7be24..05d92d8 100644 --- a/test/test_debug.rb +++ b/test/unit/test_debug.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' require 'mongrel/debug' class MongrelDbgTest < Test::Unit::TestCase diff --git a/test/test_handlers.rb b/test/unit/test_handlers.rb index dc7667c..66bf010 100644 --- a/test/test_handlers.rb +++ b/test/unit/test_handlers.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' class SimpleHandler < Mongrel::HttpHandler def process(request, response) @@ -34,11 +34,11 @@ end class HandlersTest < Test::Unit::TestCase def setup - stats = Mongrel::StatisticsFilter.new(:sample_rate => 1) @port = process_based_port - - @config = Mongrel::Configurator.new :host => '127.0.0.1', :port => @port do - listener do + stats = Mongrel::StatisticsFilter.new(:sample_rate => 1) + + @config = Mongrel::Configurator.new :host => '127.0.0.1' do + listener :port => process_based_port do uri "/", :handler => SimpleHandler.new uri "/", :handler => stats uri "/404", :handler => Mongrel::Error404Handler.new("Not found") @@ -50,11 +50,29 @@ class HandlersTest < Test::Unit::TestCase uri "/relative", :handler => Mongrel::DirHandler.new(nil, listing_allowed=false, index_html="none") end end + + unless windows? + File.open('/tmp/testfile', 'w') do + # Do nothing + end + end + @config.run end def teardown @config.stop(false, true) + File.delete '/tmp/testfile' unless windows? + 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 @@ -67,14 +85,29 @@ class HandlersTest < Test::Unit::TestCase "http://localhost:#{@port}/files_nodir/rdoc/", "http://localhost:#{@port}/status", ]) - - # XXX This can't possibly have good coverage. check_status res, String end + + def test_nil_dirhandler + return if windows? + # Camping uses this internally + handler = Mongrel::DirHandler.new(nil, false) + assert handler.can_serve("/tmp/testfile") + # Not a bug! A nil @file parameter is the only circumstance under which + # we are allowed to serve any existing file + assert handler.can_serve("../../../../../../../../../../tmp/testfile") + end + + def test_non_nil_dirhandler_is_not_vulnerable_to_path_traversal + # The famous security bug of Mongrel 1.1.2 + handler = Mongrel::DirHandler.new("/doc", false) + assert_nil handler.can_serve("/tmp/testfile") + assert_nil handler.can_serve("../../../../../../../../../../tmp/testfile") + end def test_deflate Net::HTTP.start("localhost", @port) do |h| - # test that no accept-encoding returns a non-deflated response + # Test that no accept-encoding returns a non-deflated response req = h.get("/dumb") assert( !req['Content-Encoding'] || @@ -91,9 +124,9 @@ class HandlersTest < Test::Unit::TestCase # TODO: find out why this fails on win32 but nowhere else #def test_posting_fails_dirhandler - # req = Net::HTTP::Post.new("http://localhost:9998/files/rdoc/") + # req = Net::HTTP::Post.new("http://localhost:#{@port}/files/rdoc/") # req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';') - # res = hit [["http://localhost:9998/files/rdoc/",req]] + # res = hit [["http://localhost:#{@port}/files/rdoc/",req]] # check_status res, Net::HTTPNotFound #end @@ -101,4 +134,3 @@ class HandlersTest < Test::Unit::TestCase @config.listeners["127.0.0.1:#{@port}"].unregister("/") end end - diff --git a/test/test_http11.rb b/test/unit/test_http_parser.rb index 64fe19b..d8f3fe8 100644 --- a/test/test_http11.rb +++ b/test/unit/test_http_parser.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' include Mongrel @@ -34,7 +34,7 @@ class HttpParserTest < Test::Unit::TestCase assert parser.nread == 0, "Number read after reset should be 0" end - def test_parse_dumbfuck_headers + def test_parse_strange_headers parser = HttpParser.new req = {} should_be_good = "GET / HTTP/1.1\r\naaaaaaaaaaaaa:++++++++++\r\n\r\n" @@ -42,33 +42,14 @@ class HttpParserTest < Test::Unit::TestCase assert_equal should_be_good.length, nread assert parser.finished? assert !parser.error? - assert_equal "++++++++++", req["HTTP_AAAAAAAAAAAAA"] 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_ie6_urls - %w(/some/random/path" - /some/random/path> - /some/random/path< - /we/love/you/ie6?q=<""> - /url?<="&>=" - /mal"formed"? - ).each do |path| - parser = HttpParser.new - req = {} - sorta_safe = %(GET #{path} HTTP/1.1\r\n\r\n) - nread = parser.execute(req, sorta_safe, 0) - assert_equal sorta_safe.length, nread - assert parser.finished? - assert !parser.error? - end + 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 @@ -88,103 +69,6 @@ class HttpParserTest < Test::Unit::TestCase assert parser.error?, "Parser SHOULD have error" end - def test_parse_like_optimized_header - parser = HttpParser.new - req = {} - should_be_good = "GET / HTTP/1.1\r\nAuthorizationn: zz\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? - assert_equal "zz", req["HTTP_AUTHORIZATIONN"] - assert ! req["HTTP_AUTHORIZATION"] - end - - def test_parse_twin_lookalike_optimized_headers - parser = HttpParser.new - req = {} - should_be_good = "GET / HTTP/1.1\r\n" \ - "Accept-Encoding: abcdef\r\n" \ - "Accept-Language: zyxvut\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? - assert_equal "abcdef", req["HTTP_ACCEPT_ENCODING"] - assert_equal "zyxvut", req["HTTP_ACCEPT_LANGUAGE"] - end - - if RUBY_PLATFORM !~ /java/ - # as of now, the Java version does not have the same global-object - # reuse optimization the C version does - - def test_parse_optimized_headers_global_objects_used - parser = HttpParser.new - req = {} - should_be_good = "GET / HTTP/1.1\r\nHost: example.com\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? - assert_equal "example.com", req["HTTP_HOST"] - - frozen_host_a = nil - req.each { |k,v| k == "HTTP_HOST" && frozen_host_a = k } - - parser = HttpParser.new - req = {} - should_be_good = "GET / HTTP/1.1\r\nHost: example.com\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? - - frozen_host_b = nil - req.each { |k,v| k == "HTTP_HOST" && frozen_host_b = k } - assert_equal "HTTP_HOST", frozen_host_a - assert_equal "HTTP_HOST", frozen_host_b - assert_equal frozen_host_a.object_id, frozen_host_b.object_id - end - end - - def test_host_port_parsing - parser = HttpParser.new - req = {} - should_be_good = "GET / HTTP/1.1\r\nHost: example.com\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? - assert_equal "example.com", req["HTTP_HOST"] - assert_equal "example.com", req["SERVER_NAME"] - assert_equal "80", req["SERVER_PORT"] - - parser = HttpParser.new - req = {} - should_be_good = "GET / HTTP/1.1\r\nHost: example.com:123\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? - assert_equal "example.com:123", req["HTTP_HOST"] - assert_equal "example.com", req["SERVER_NAME"] - assert_equal "123", req["SERVER_PORT"] - - # null character in domain name is never actually valid, but if it - # becomes valid in Web 3.0, we'll be ready for it. - parser = HttpParser.new - req = {} - should_be_good = "GET / HTTP/1.1\r\nHost: example.com\0:123\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? - assert_equal "example.com\0:123", req["HTTP_HOST"] - assert_equal "example.com\0", req["SERVER_NAME"] - assert_equal "123", req["SERVER_PORT"] - end - def test_fragment_in_uri parser = HttpParser.new req = {} diff --git a/test/test_redirect_handler.rb b/test/unit/test_redirect_handler.rb index a4c5056..e990427 100644 --- a/test/test_redirect_handler.rb +++ b/test/unit/test_redirect_handler.rb @@ -4,16 +4,17 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' class RedirectHandlerTest < Test::Unit::TestCase def setup + @port = process_based_port redirect_test_io do - @server = Mongrel::HttpServer.new('127.0.0.1', process_based_port) + @server = Mongrel::HttpServer.new('127.0.0.1', @port) end @server.run - @client = Net::HTTP.new('127.0.0.1', process_based_port) + @client = Net::HTTP.new('127.0.0.1', @port) end def teardown diff --git a/test/test_request_progress.rb b/test/unit/test_request_progress.rb index 4be9d16..a100426 100644 --- a/test/test_request_progress.rb +++ b/test/unit/test_request_progress.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' class UploadBeginHandler < Mongrel::HttpHandler attr_reader :request_began, :request_progressed, :request_processed diff --git a/test/test_response.rb b/test/unit/test_response.rb index 123ed98..b49c9df 100644 --- a/test/test_response.rb +++ b/test/unit/test_response.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' include Mongrel diff --git a/test/test_stats.rb b/test/unit/test_stats.rb index 404870a..012c6a5 100644 --- a/test/test_stats.rb +++ b/test/unit/test_stats.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' class StatsTest < Test::Unit::TestCase diff --git a/test/test_uriclassifier.rb b/test/unit/test_uriclassifier.rb index 28af72c..a438065 100644 --- a/test/test_uriclassifier.rb +++ b/test/unit/test_uriclassifier.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' include Mongrel diff --git a/test/test_ws.rb b/test/unit/test_ws.rb index 237aa54..9de8a45 100644 --- a/test/test_ws.rb +++ b/test/unit/test_ws.rb @@ -4,7 +4,7 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'test/testhelp' +require 'test/test_helper' include Mongrel @@ -21,8 +21,8 @@ end class WebServerTest < Test::Unit::TestCase def setup - @port = process_based_port @valid_request = "GET / HTTP/1.1\r\nHost: www.zedshaw.com\r\nContent-Type: text/plain\r\n\r\n" + @port = process_based_port redirect_test_io do # We set num_processors=1 so that we can test the reaping code @@ -95,7 +95,7 @@ class WebServerTest < Test::Unit::TestCase def test_num_processors_overload redirect_test_io do - assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL do + assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do tests = [ Thread.new { do_test(@valid_request, 1) }, Thread.new { do_test(@valid_request, 10) }, |