Class: Akita::HarLogger::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/akita/har_logger.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(app, out_file_name = nil) ⇒ Middleware

Returns a new instance of Middleware.



28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Method Details

#call(env) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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