class Akita::HarLogger::Middleware

Logs HTTP request-response pairs to a HAR file.

Params:

app

the application to log.

out_file_name

the name of the HAR file to be produced. If the file exists, it will be overwritten.

Public Class Methods

new(app, out_file_name = nil) click to toggle source
# File lib/akita/har_logger.rb, line 28
def initialize(app, out_file_name = nil)
  @app = app

  if out_file_name == nil then
    out_file_name = HarLogger.default_file_name
  end

  @entry_queue = HarLogger.get_queue(out_file_name)
rescue => e
  HarLogger.logException("initializing middleware", e)
  raise
end

Public Instance Methods

call(env) click to toggle source
# File lib/akita/har_logger.rb, line 41
def call(env)
  start_time = Time.now

  # Read the request body here, in case there is non-Rack-compliant
  # middleware in the stack that closes the request-body stream on us.
  request_body = env['rack.input'].read
  env['rack.input'].rewind  # Be kind.

  status, headers, body = @app.call(env)
  end_time = Time.now

  wait_time_ms = ((end_time.to_f - start_time.to_f) * 1000).round

  # Patch env with our saved request body.
  saved_input = env['rack.input']
  env['rack.input'] = StringIO.new request_body

  @entry_queue << (HarEntry.new start_time, wait_time_ms, env, status,
                                headers, body)

  # Be kind and restore the original request-body stream.
  env['rack.input'] = saved_input

  [ status, headers, body ]
rescue => e
  HarLogger.logException("handling request", e)
  raise
end