Class: AutoResp::HAR

Inherits:
Object
  • Object
show all
Defined in:
lib/ar/har.rb

Class Method Summary collapse

Class Method Details

.cache_data(req, res) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/ar/har.rb', line 121

def cache_data( req, res )
  {
    #FIXME
    beforeRequest: nil,
    afterRequest: nil
  }
end


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ar/har.rb', line 74

def cookie_data( cookie )
  {
    name:     cookie.name,
    value:    cookie.value,
    path:     cookie.path,
    domain:   cookie.domain,
    expires:  cookie.expires,
    #FIXME: check httponly
    httpOnly: nil,
    secure:   cookie.secure,
    comment: nil
  }
end

.header_data(header) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/ar/har.rb', line 88

def header_data( header )
  name, value = *header
  value = value.join("\n") if value.is_a?(Array)
  {
    name:   name,
    value:  value,
    comment: nil
  }
end

.request_data(req) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ar/har.rb', line 58

def request_data( req )
  {
    method:       req.request_method,
    url:          req.unparsed_uri,
    httpVersion:  req.http_version,
    cookies:      req.cookies.map {|c| cookie_data( c ) },
    headers:      req.header.map { |h| header_data( h ) },
    queryString:  CGI.parse(req.query_string || ''),
    #TODO: parse post data
    postData: {},
    headersSize:  0,
    bodySize:     (req.body ? req.content_length : 0),
    comment:      nil
  }
end

.response_data(res) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ar/har.rb', line 98

def response_data( res )
  body = res.body
  body.utf8!
  body = '**raw data**' unless body.valid_encoding?
  {
    status:       res.status,
    statusText:   res.message,

    httpVersion:  res.http_version,
    cookies:      res.cookies.map {|c| 
      cookie_data( WEBrick::Cookie.parse_set_cookie c) },
    headers:      res.header.map {|h| header_data(h) },
    content:      body,

    #FIXME
    redirectURL:  '',
    headerSize:   160,

    bodySize:     res.content_length,
    comment: nil
  }
end

.session_to_entry(session) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ar/har.rb', line 29

def session_to_entry( session )
  req, res, info = *session
  {
    pageref:   nil,
    startedDateTime:  info[:start],
    time:     info[:end].to_i - info[:start].to_i,
    request:  request_data( req ),
    response: response_data( res ),
    cache:    cache_data( req, res ),

    #FIXME: add timing feature
    timings: {
      blocked:  0,
      dns:      -1,
      connect:  15,
      send:     20,
      wait:     38,
      receive:  12,
      ssl:      -1,
      comment: ""
    },

    serverIPAddress: req.host,
    #NOT implemented
    connection: '',
    comment:    info[:matched] ? 'x-auto-response' : nil
  }
end

.sessions_to_har(sessions) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ar/har.rb', line 11

def sessions_to_har( sessions )
  har = {
    :log => {
      :version => '1.2',
      :creator => {
        :name     => 'auto_response',
        :version  => AutoResp::VERSION,
        :comment  => 'proxied by auto_response'
      },
      :pages => []
    }
  }
  har[:log][:entries] = sessions.map do |session|
    session_to_entry session
  end
  JSON.fast_generate har
end