Class: WEBrickNIO::HTTPServer::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/webricknio/httpserver.rb

Overview

Instances of this class are submitted to the threadpool to process incoming requests. It will very likely process the whole request in one shot but it might postpone the request too for a slow client

Instance Method Summary collapse

Constructor Details

#initialize(key, server, config, attachment) ⇒ RequestHandler

Returns a new instance of RequestHandler.



234
235
236
237
238
239
240
241
242
# File 'lib/webricknio/httpserver.rb', line 234

def initialize(key, server, config, attachment)
  @key = key
  @sock_channel = @key.channel
  @config = config
  @server = server
  @send_response = true
  @socket_id = -1
  @attachment = attachment
end

Instance Method Details

#callObject



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/webricknio/httpserver.rb', line 244

def call
  begin
    time1 = Time.now
    req = @attachment.request || ::WEBrickNIO::HTTPRequest.new(@config)
    res = @attachment.response || ::WEBrickNIO::HTTPResponse.new(@config)

    @socket_id = @sock_channel.socket.object_id

    socket = @sock_channel.socket

    if req.in_progress?
      @server.logger.debug "resuming request in progress"
      req.resume
    else
      # fresh request
      req.parse(@sock_channel)
    end

    if req.in_progress?
      @server.logger.debug "request in progress"
      @attachment.request = req if @attachment.request.nil?
      @attachment.response = res if @attachment.response.nil?
      @send_response = false
      return # goes to "ensure" first
    end

    res.request_method = req.request_method
    res.request_uri = req.request_uri
    res.request_http_version = req.http_version
    res.keep_alive = req.keep_alive?

    if req.unparsed_uri == "*"
      if req.request_method == "OPTIONS"
        do_OPTIONS(req, res)
        raise ::WEBrick::HTTPStatus::OK
      end
      raise ::WEBrick::HTTPStatus::NotFound, "`#{req.unparsed_uri}' not found."
    end

    servlet, options, script_name, path_info = @server.search_servlet(req.path)
    raise ::WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." unless servlet
    req.script_name = script_name
    req.path_info = path_info
    si = servlet.get_instance(self, *options)
    #@server.logger.debug(format("%s is invoked.", si.class.name))
    @server.access_log(@config, req, res)

    si.service(req, res)

  rescue ::WEBrick::HTTPStatus::EOFError => ex
    @send_response = false
    @attachment.cleanup
    @server.logger.debug(ex.message)
  rescue ::WEBrick::HTTPStatus::Error => ex
    @server.logger.error(ex)
    res.set_error(ex)
  rescue ::WEBrick::HTTPStatus::Status => ex
    res.status = ex.code
  rescue StandardError => ex
    @server.logger.error(ex)
    res.set_error(ex)
  rescue Exception => ex
    if ex.is_a?(Exception)
      @server.logger.error(ex)
    else
      message = ""
      if ex.respond_to?(:java_class) && ex.respond_to?(:stack_trace)
        @server.logger.error("error")
        ex.print_stack_trace
      else
        message = ex.inspect
      end
      @server.logger.error(message)
    end
    res.set_error("500")
  ensure
    begin
      time3 = Time.now
      @attachment.unlock
      if @send_response
        @attachment.cleanup
        res.send_response(@sock_channel)
        @server.logger.debug "time taken to send response #{Time.now - time3}"
      end
      if (!req.keep_alive? || !res.keep_alive? || (!@send_response && !req.in_progress?))
        @server.logger.debug("closing socket. req.keep alive: #{req.keep_alive}, resp.keep alive: #{res.keep_alive}, send_response: #{@send_response}, socket id: #{@socket_id}")
        @sock_channel.close
        @key.cancel
      end
    rescue Exception => ex
      if ex.is_a?(Exception)
        @server.logger.error(ex)
      else
        message = ""
        if ex.respond_to?(:java_class) && ex.respond_to?(:stack_trace)
          @server.logger.error("error")
          ex.print_stack_trace
        else
          message = ex.inspect
        end
        @server.logger.error(message)
      end
    end
    time2 = Time.now
    @server.logger.info "total request time: #{time2 - time1} sec" if @send_response
  end
end