Class: Mongrel::HttpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/packet_mongrel.rb,
lib/packet/packet_mongrel.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, num_processors = 950, x = 0, y = nil) ⇒ HttpServer

Deal with Mongrel 1.0.1 or earlier, as well as later.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/packet_mongrel.rb', line 69

def initialize(host, port, num_processors=950, x=0, y=nil) # Deal with Mongrel 1.0.1 or earlier, as well as later.
  @socket = nil
  @classifier = URIClassifier.new
  @host = host
  @port = port
  @workers = ThreadGroup.new
  if y
    @throttle = x
    @timeout = y || 60
  else
    @timeout = x
  end
  @num_processors = num_processors #num_processors is pointless for evented....
  @death_time = 60
  self.class.const_set(:Instance,self)
end

Instance Method Details

#dispatch_to_handlers(handlers, request, response) ⇒ Object



131
132
133
134
135
136
# File 'lib/packet_mongrel.rb', line 131

def dispatch_to_handlers(handlers,request,response)
  handlers.each do |handler|
    handler.process(request, response)
    break if response.done
  end
end

#process_http_request(params, linebuffer, client) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/packet_mongrel.rb', line 100

def process_http_request(params,linebuffer,client)
  if not params[Const::REQUEST_PATH]
    uri = URI.parse(params[Const::REQUEST_URI])
    params[Const::REQUEST_PATH] = uri.request_uri
  end

  raise "No REQUEST PATH" if not params[Const::REQUEST_PATH]

  script_name, path_info, handlers = @classifier.resolve(params[Const::REQUEST_PATH])

  if handlers
    notifiers = handlers.select { |h| h.request_notify }
    request = HttpRequest.new(params, linebuffer, notifiers)

    # request is good so far, continue processing the response
    response = HttpResponse.new(client)

    # Process each handler in registered order until we run out or one finalizes the response.
    dispatch_to_handlers(handlers,request,response)

    # And finally, if nobody closed the response off, we finalize it.
    unless response.done
      response.finished
    end
  else
    # Didn't find it, return a stock 404 response.
    client.send_data(Const::ERROR_404_RESPONSE)
    client.close_connection
  end
end

#runObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/packet_mongrel.rb', line 86

def run
  trap('INT') { raise StopServer }
  trap('TERM') { raise StopServer }
  @acceptor = Thread.new do
    Packet::Reactor.run do |t_reactor|
      begin
        t_reactor.start_server(@host,@port,MongrelProtocol)
      rescue StopServer
        t_reactor.start_server
      end
    end
  end
end