Class: Mongrel::HttpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/pdtp/common/http_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, num_processors = 950, throttle = 0, timeout = 60) ⇒ HttpServer

Returns a new instance of HttpServer.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pdtp/common/http_server.rb', line 81

def initialize(host, port, num_processors = 950, throttle = 0, timeout = 60)
  @socket = nil
  @classifier = Mongrel::URIClassifier.new
  @host = host
  @port = port
  @workers = ThreadGroup.new
  @timeout = timeout
  @num_processors = num_processors
  @death_time = 60
  self.class.const_set(:Instance,self)
end

Instance Method Details

#dispatch_to_handlers(handlers, request, response) ⇒ Object



144
145
146
147
148
149
# File 'lib/pdtp/common/http_server.rb', line 144

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/pdtp/common/http_server.rb', line 111

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 unless response.pending
    else
      response.close_connection_after_writing
    end
  else
    # Didn't find it, return a stock 404 response.
    client.send_data(Const::ERROR_404_RESPONSE)
    client.close_connection_after_writing
  end
end

#runObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pdtp/common/http_server.rb', line 93

def run
  trap('INT') { raise StopServer }
  trap('TERM') { raise StopServer }
  @acceptor = Thread.new do
    EventMachine.run do
      begin
        run_evented
      rescue StopServer
        EventMachine.stop_event_loop
      end
    end
  end
end

#run_eventedObject



107
108
109
# File 'lib/pdtp/common/http_server.rb', line 107

def run_evented
  EventMachine.start_server @host, @port, Mongrel::Protocol
end