From 5d600bfd240a09df159b83460b81626f71ce5029 Mon Sep 17 00:00:00 2001 From: zedshaw Date: Mon, 3 Apr 2006 15:22:17 +0000 Subject: Minor tweaks to the handlers and http parser to deal with more bad input possibilities. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@139 19e92222-5c0b-0410-8929-a290d50e31e9 --- examples/random_thrash.rb | 19 +++++++++++++++++++ ext/http11/http11.c | 14 ++++++++++++-- lib/mongrel/handlers.rb | 4 ++-- lib/mongrel/rails.rb | 3 ++- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 examples/random_thrash.rb diff --git a/examples/random_thrash.rb b/examples/random_thrash.rb new file mode 100644 index 0000000..fe9311c --- /dev/null +++ b/examples/random_thrash.rb @@ -0,0 +1,19 @@ +require 'socket' +devrand = open("/dev/random","r") + +loop do + s = TCPSocket.new(ARGV[0],ARGV[1]) + s.write("GET / HTTP/1.1\r\n") + total = 0 + begin + loop do + r = devrand.read(10) + n = s.write(r) + total += n + end + rescue Object + STDERR.puts "#$!: #{total}" + end + s.close + sleep 1 +end diff --git a/ext/http11/http11.c b/ext/http11/http11.c index d89f0d4..55ecc0f 100644 --- a/ext/http11/http11.c +++ b/ext/http11/http11.c @@ -124,10 +124,20 @@ void header_done(void *data, const char *at, size_t length) VALUE temp = Qnil; VALUE host = Qnil; VALUE port = Qnil; + VALUE ctype = Qnil; + VALUE clen = Qnil; char *colon = NULL; - rb_hash_aset(req, global_content_length, rb_hash_aref(req, global_http_content_length)); - rb_hash_aset(req, global_content_type, rb_hash_aref(req, global_http_content_type)); + clen = rb_hash_aref(req, global_http_content_length); + if(clen != Qnil) { + rb_hash_aset(req, global_content_length, clen); + } + + ctype = rb_hash_aref(req, global_http_content_type); + if(ctype != Qnil) { + rb_hash_aset(req, global_content_type, Qnil); + } + rb_hash_aset(req, global_gateway_interface, global_gateway_interface_value); if((temp = rb_hash_aref(req, global_http_host)) != Qnil) { // ruby better close strings off with a '\0' dammit diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb index b308194..6bc2a70 100644 --- a/lib/mongrel/handlers.rb +++ b/lib/mongrel/handlers.rb @@ -169,7 +169,7 @@ module Mongrel ext = req[dot_at .. -1] if MIME_TYPES[ext] stat = File.stat(req) - response.header[Const::CONTENT_TYPE] = MIME_TYPES[ext] + response.header[Const::CONTENT_TYPE] = MIME_TYPES[ext] || "text" # TODO: Confirm this works for rfc 1123 response.header[Const::LAST_MODIFIED] = HttpServer.httpdate(stat.mtime) # TODO that this is a valid way to calculate an etag @@ -187,7 +187,7 @@ module Mongrel else File.open(req, "rb") { |f| response.socket.write(f.read) } end - rescue EOFError,Errno::ECONNRESET,Errno::EPIPE + rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL # ignore these since it means the client closed off early end else diff --git a/lib/mongrel/rails.rb b/lib/mongrel/rails.rb index 66db691..611fcd0 100644 --- a/lib/mongrel/rails.rb +++ b/lib/mongrel/rails.rb @@ -132,7 +132,8 @@ module Mongrel $orig_dollar_quote = $".clone ENV['RAILS_ENV'] = ops[:environment] - require "#{ops[:cwd]}/config/environment" + env_location = "#{ops[:cwd]}/config/environment" + require env_location require 'dispatcher' require 'mongrel/rails' -- cgit v1.2.3-24-ge0c7