Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 1482

Show
Ignore:
Timestamp:
06/22/05 12:59:36 (3 years ago)
Author:
david
Message:

Removed the mutex from the WEBrick adapter under the production environment so concurrent requests can be served

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/cgi_process.rb

    r1336 r1482  
    125125    end 
    126126 
    127     def out 
     127    def out(output = $stdout) 
    128128      convert_content_type!(@headers) 
    129       $stdout.binmode if $stdout.respond_to?(:binmode) 
    130       $stdout.sync = false 
     129      output.binmode      if output.respond_to?(:binmode) 
     130      output.sync = false if output.respond_to?(:sync=) 
    131131       
    132132      begin 
    133         print @cgi.header(@headers
     133        output.write(@cgi.header(@headers)
    134134 
    135135        if @cgi.send(:env_table)['REQUEST_METHOD'] == 'HEAD' 
     
    138138          @body.call(self) 
    139139        else 
    140           print @body 
     140          output.write(@body) 
    141141        end 
    142142      rescue Errno::EPIPE => e 
  • trunk/railties/CHANGELOG

    r1480 r1482  
    11*SVN* 
     2 
     3* Removed the mutex from the WEBrick adapter under the production environment so concurrent requests can be served 
    24 
    35* Fixed that mailer generator generated fixtures/plural while units expected fixtures/singular #1457 [Scott Barron] 
  • trunk/railties/lib/dispatcher.rb

    r1278 r1482  
    2626class Dispatcher 
    2727  class << self 
    28     def dispatch(cgi = CGI.new, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS
     28    def dispatch(cgi = CGI.new, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout
    2929      begin 
    3030        request, response = ActionController::CgiRequest.new(cgi, session_options), ActionController::CgiResponse.new(cgi) 
    3131        prepare_application 
    32         ActionController::Routing::Routes.recognize!(request).process(request, response).out 
     32        ActionController::Routing::Routes.recognize!(request).process(request, response).out(output) 
    3333      rescue Object => exception 
    34         ActionController::Base.process_with_exception(request, response, exception).out 
     34        ActionController::Base.process_with_exception(request, response, exception).out(output) 
    3535      ensure 
    3636        reset_application 
  • trunk/railties/lib/webrick_server.rb

    r1433 r1482  
    88 
    99ABSOLUTE_RAILS_ROOT = File.expand_path(RAILS_ROOT) 
     10 
     11class CGI 
     12  def stdinput 
     13    @stdin || $stdin 
     14  end 
     15   
     16  def stdinput=(input) 
     17    @stdin = input 
     18  end 
     19 
     20  def env_table 
     21    @env_table || ENV 
     22  end 
     23 
     24  def env_table=(table) 
     25    @env_table = table 
     26  end 
     27end 
    1028 
    1129class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet 
     
    1937 
    2038    trap("INT") { server.shutdown } 
     39 
     40    require File.join(@server_options[:server_root], "..", "config", "environment") unless defined?(RAILS_ROOT) 
     41    require "dispatcher" 
     42 
    2143    server.start 
    2244  end 
     
    3254    begin 
    3355      unless handle_file(req, res) 
    34         REQUEST_MUTEX.lock 
     56        REQUEST_MUTEX.lock unless RAILS_ENV == 'production' 
    3557        unless handle_dispatch(req, res) 
    3658          raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." 
     
    3860      end 
    3961    ensure 
    40       REQUEST_MUTEX.unlock if REQUEST_MUTEX.locked? 
     62      unless RAILS_ENV == 'production' 
     63        REQUEST_MUTEX.unlock if REQUEST_MUTEX.locked? 
     64      end 
    4165    end 
    4266  end 
     
    4569    begin 
    4670      req = req.dup 
    47  
    4871      path = req.path.dup 
    4972 
     
    6487  end 
    6588 
    66   def handle_dispatch(req, res, origin = nil) 
    67     env = req.meta_vars.clone 
    68     env.delete "SCRIPT_NAME" 
    69     env["QUERY_STRING"] = req.request_uri.query 
    70     env["REQUEST_URI"] = origin if origin 
    71      
    72     data = nil 
    73     $old_stdin, $old_stdout = $stdin, $stdout 
    74     $stdin, $stdout = StringIO.new(req.body || ""), StringIO.new 
     89  def handle_dispatch(req, res, origin = nil)     
     90    data = StringIO.new 
     91    Dispatcher.dispatch(create_cgi(req, origin), ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, data) 
    7592 
    76     begin 
    77       require 'cgi' 
    78       CGI.send(:define_method, :env_table) { env } 
    79  
    80       load File.join(@server_options[:server_root], "dispatch.rb") 
    81  
    82       $stdout.rewind 
    83       data = $stdout.read 
    84     ensure 
    85       $stdin, $stdout = $old_stdin, $old_stdout 
    86     end 
    87  
    88     raw_header, body = *data.split(/^[\xd\xa]+/on, 2) 
    89     header = WEBrick::HTTPUtils::parse_header(raw_header) 
    90     if /^(\d+)/ =~ header['status'][0] 
    91       res.status = $1.to_i 
    92       header.delete('status') 
    93     end 
    94     res.cookies.concat header.delete('set-cookie') 
     93    header, body = extract_header_and_body(data) 
     94    assign_status(res, header) 
     95    res.cookies.concat(header.delete('set-cookie')) 
    9596    header.each { |key, val| res[key] = val.join(", ") } 
    9697     
     
    101102    return false 
    102103  end 
     104   
     105  private 
     106    def create_cgi(req, origin) 
     107      cgi = CGI.new 
     108      cgi.env_table = create_env_table(req, origin) 
     109      cgi.stdinput  = req.body || "" 
     110      return cgi 
     111    end 
     112 
     113    def create_env_table(req, origin) 
     114      env = req.meta_vars.clone 
     115      env.delete "SCRIPT_NAME" 
     116      env["QUERY_STRING"] = req.request_uri.query 
     117      env["REQUEST_URI"]  = origin if origin 
     118      return env 
     119    end 
     120     
     121    def extract_header_and_body(data) 
     122      data.rewind 
     123      data = data.read 
     124 
     125      raw_header, body = *data.split(/^[\xd\xa]+/on, 2) 
     126      header = WEBrick::HTTPUtils::parse_header(raw_header) 
     127       
     128      return header, body 
     129    end 
     130 
     131    def assign_status(res, header) 
     132      if /^(\d+)/ =~ header['status'][0] 
     133        res.status = $1.to_i 
     134        header.delete('status') 
     135      end 
     136    end 
    103137end