Class: HTTP::Wiretap

Inherits:
Object
  • Object
show all
Defined in:
lib/http/wiretap.rb

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.enabledObject

A flag stating if wiretap has started



23
24
25
# File 'lib/http/wiretap.rb', line 23

def self.enabled
  @enabled
end

.log_directoryObject

The root directory where logs are stored.



29
30
31
# File 'lib/http/wiretap.rb', line 29

def self.log_directory
  @log_directory || 'http-log'
end

.log_directory=(dir) ⇒ Object

Sets the root directory where logs are stored.



34
35
36
# File 'lib/http/wiretap.rb', line 34

def self.log_directory=(dir)
  @log_directory = dir
end

.log_request(http, request) ⇒ Fixnum

Logs a request’s headers and body to a file. The file will be written to:

<log_directory>/raw/<request_number>/request

A symbolic link will be made from that directory to the ‘host/` directory.

Parameters:

  • http (Net::HTTP)

    the object sending the request

  • request (Net::HTTP::Request)

    the request being logged

Returns:

  • (Fixnum)

    the sequential identifier for this request



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/http/wiretap.rb', line 79

def self.log_request(http, request)
  return unless @enabled
  
  # Retrieve the request identifier
  request_id = @next_id
  @next_id += 1
  
  # Create raw log directory
  raw_dir = File.expand_path("#{log_directory}/raw/#{request_id}")
  ::FileUtils.mkdir_p(raw_dir)
  
  # Write request to file
  File.open("#{raw_dir}/request", 'w') do |file|
    # Write method and path
    file.write("#{request.method} #{request.path} HTTP/1.1\r\n")
    
    # Write headers
    connection = *request.get_fields('Connection') || 'close'
    request.each_capitalized do |header_name, header_value|
      if header_name != 'Connection'
        file.write("#{header_name}: #{header_value}\r\n")
      end
    end
    file.write("Connection: #{connection}\r\n")
    file.write("Host: #{http.address}:#{http.port}\r\n")
    file.write("\r\n")
    
    # Write body
    file.write(request.body) unless request.body.nil?
  end
  
  # Link to host-based log
  host_request_name = "#{http.address}#{request.path}"
  host_request_id = @host_request_next_id[host_request_name] ||= 0
  @host_request_next_id[host_request_name] += 1
  host_dir = File.expand_path("#{log_directory}/host/#{host_request_name}/#{host_request_id}")
  @host_request_paths[request_id] = host_dir
  ::FileUtils.mkdir_p(host_dir)
  ::FileUtils.ln_sf("#{raw_dir}/request", "#{host_dir}/request")

  return request_id
end

.log_response(http, response, request_id) ⇒ Object

Logs a response’s headers and body to a file. The file will be written to:

<log_directory>/raw/<request_number>/response

A symbolic link will be made from that directory to the ‘host/` directory.

Parameters:

  • http (Net::HTTP)

    the object sending the request

  • request (Net::HTTP::Request)

    the request being logged

  • request_id (Fixnum)

    the sequential identifier for the request



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/http/wiretap.rb', line 131

def self.log_response(http, response, request_id)
  return unless @enabled

  # Create log directory
  raw_dir = File.expand_path("#{log_directory}/raw/#{request_id}")
  ::FileUtils.mkdir_p(raw_dir)
  
  # Write response to file
  File.open("#{raw_dir}/response", 'w') do |file|
    file.write("HTTP/#{response.http_version} #{response.code}\r\n")

    # Write headers
    response.each_capitalized do |header_name, header_value|
      file.write("#{header_name}: #{header_value}\r\n")
    end
    file.write("\r\n")
    
    # Write body
    file.write(response.body) unless response.body.nil?
  end

  # Link to host-based log
  host_dir = @host_request_paths[request_id]
  ::FileUtils.mkdir_p(host_dir)
  ::FileUtils.ln_sf("#{raw_dir}/response", "#{host_dir}/response")
end

.restartObject

Stops logging and then restarts.



64
65
66
67
# File 'lib/http/wiretap.rb', line 64

def self.restart()
  self.stop()
  self.start()
end

.startObject

Begins logging requests and responses.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/http/wiretap.rb', line 44

def self.start()
  # Reset the sequential request identifier
  @next_id = 0
  @host_request_paths = {}
  @host_request_next_id = {}
  
  # Enable logging
  @enabled = true
  
  # Clear log directory
  FileUtils.rm_rf(log_directory)
end

.stopObject

Stops logging requests and responses.



58
59
60
61
# File 'lib/http/wiretap.rb', line 58

def self.stop()
  # Disable logging
  @enabled = false
end