nice_http 1.7.2 → 1.7.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +607 -581
- data/lib/nice_http/http_methods.rb +526 -526
- data/lib/nice_http/manage_request.rb +237 -237
- data/lib/nice_http/manage_response.rb +280 -280
- data/lib/nice_http/utils.rb +109 -109
- data/lib/nice_http.rb +414 -411
- metadata +2 -2
@@ -1,280 +1,280 @@
|
|
1
|
-
module NiceHttpManageResponse
|
2
|
-
|
3
|
-
######################################################
|
4
|
-
# private method to manage Response
|
5
|
-
# input:
|
6
|
-
# resp
|
7
|
-
# data
|
8
|
-
# output:
|
9
|
-
# @response updated
|
10
|
-
######################################################
|
11
|
-
def manage_response(resp, data)
|
12
|
-
require "json"
|
13
|
-
@prev_response = Hash.new() if @prev_response.nil?
|
14
|
-
begin
|
15
|
-
if @start_time.kind_of?(Time)
|
16
|
-
@response[:time_elapsed_total] = Time.now - @start_time
|
17
|
-
@start_time = nil
|
18
|
-
else
|
19
|
-
@response[:time_elapsed_total] = nil
|
20
|
-
end
|
21
|
-
if @start_time_net.kind_of?(Time)
|
22
|
-
@response[:time_elapsed] = Time.now - @start_time_net
|
23
|
-
@start_time_net = nil
|
24
|
-
else
|
25
|
-
@response[:time_elapsed] = nil
|
26
|
-
end
|
27
|
-
|
28
|
-
create_stats(resp) if @create_stats
|
29
|
-
|
30
|
-
begin
|
31
|
-
# this is to be able to access all keys as symbols
|
32
|
-
new_resp = Hash.new()
|
33
|
-
resp.each { |key, value|
|
34
|
-
if key.kind_of?(String)
|
35
|
-
new_resp[key.to_sym] = value
|
36
|
-
end
|
37
|
-
}
|
38
|
-
new_resp.each { |key, value|
|
39
|
-
resp[key] = value
|
40
|
-
}
|
41
|
-
rescue
|
42
|
-
end
|
43
|
-
#for mock_responses to be able to add outside of the header like content-type for example
|
44
|
-
if resp.kind_of?(Hash) and !resp.has_key?(:header)
|
45
|
-
resp[:header] = {}
|
46
|
-
end
|
47
|
-
|
48
|
-
#todo: check this. not sure if this is valid anymore since resp it will be a hash only when mock_response
|
49
|
-
if resp.kind_of?(Hash)
|
50
|
-
resp.each { |k, v|
|
51
|
-
if k != :code and k != :message and k != :data and k != :'set-cookie' and k != :header
|
52
|
-
resp[:header][k] = v
|
53
|
-
end
|
54
|
-
}
|
55
|
-
resp[:header].each { |k, v|
|
56
|
-
resp.delete(k) if resp.has_key?(k)
|
57
|
-
}
|
58
|
-
end
|
59
|
-
|
60
|
-
method_s = caller[0].to_s().scan(/:in `(.*)'/).join
|
61
|
-
if resp.header.kind_of?(Hash) and (resp.header["content-type"].to_s() == "application/x-deflate" or resp.header[:"content-type"].to_s() == "application/x-deflate")
|
62
|
-
data = Zlib::Inflate.inflate(data)
|
63
|
-
end
|
64
|
-
encoding_response = ""
|
65
|
-
if resp.header.kind_of?(Hash) and (resp.header["content-type"].to_s() != "" or resp.header[:"content-type"].to_s() != "")
|
66
|
-
encoding_response = resp.header["content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?("content-type")
|
67
|
-
encoding_response = resp.header[:"content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?(:"content-type")
|
68
|
-
end
|
69
|
-
if encoding_response.to_s() == ""
|
70
|
-
encoding_response = "UTF-8"
|
71
|
-
end
|
72
|
-
|
73
|
-
if encoding_response.to_s() != "" and encoding_response.to_s().upcase != "UTF-8"
|
74
|
-
data.encode!("UTF-8", encoding_response.to_s())
|
75
|
-
end
|
76
|
-
|
77
|
-
if encoding_response != "" and encoding_response.to_s().upcase != "UTF-8"
|
78
|
-
@response[:message] = resp.message.to_s().encode("UTF-8", encoding_response.to_s())
|
79
|
-
#todo: response data in here for example is convert into string, verify if that is correct or needs to maintain the original data type (hash, array...)
|
80
|
-
resp.each { |key, val| @response[key.to_sym] = val.to_s().encode("UTF-8", encoding_response.to_s()) }
|
81
|
-
else
|
82
|
-
@response[:message] = resp.message
|
83
|
-
resp.each { |key, val|
|
84
|
-
@response[key.to_sym] = val
|
85
|
-
}
|
86
|
-
end
|
87
|
-
|
88
|
-
if !defined?(Net::HTTP::Post::Multipart) or (defined?(Net::HTTP::Post::Multipart) and !data.kind_of?(Net::HTTP::Post::Multipart))
|
89
|
-
@response[:data] = data
|
90
|
-
else
|
91
|
-
@response[:data] = ""
|
92
|
-
end
|
93
|
-
|
94
|
-
@response[:code] = resp.code
|
95
|
-
message = "\nRESPONSE: \n " + @response[:code].to_s() + ":" + @response[:message].to_s()
|
96
|
-
if @debug or @prev_response[:'content-type'] != @response[:'content-type'] or @prev_response[:'content-length'] != @response[:'content-length'] or
|
97
|
-
@prev_response[:data] != @response[:data] or @prev_response[:code] != @response[:code] or @prev_response[:message] != @response[:message]
|
98
|
-
self.class.last_response = message if @debug
|
99
|
-
@response.each { |key, value|
|
100
|
-
if value.to_s() != ""
|
101
|
-
value_orig = value
|
102
|
-
if key.kind_of?(Symbol)
|
103
|
-
if key == :code or key == :data or key == :header or key == :message
|
104
|
-
if key == :data and !@response[:'content-type'].to_s.include?("text/html")
|
105
|
-
begin
|
106
|
-
JSON.parse(value_orig)
|
107
|
-
data_s = JSON.pretty_generate(JSON.parse(value_orig))
|
108
|
-
rescue
|
109
|
-
data_s = value_orig
|
110
|
-
end
|
111
|
-
if @debug
|
112
|
-
self.class.last_response += "\n " + key.to_s() + ": '" + data_s.gsub("<", "<") + "'\n"
|
113
|
-
end
|
114
|
-
if value_orig != value
|
115
|
-
message += "\n " + key.to_s() + ": '" + value.gsub("<", "<") + "'\n"
|
116
|
-
else
|
117
|
-
message += "\n " + key.to_s() + ": '" + data_s.gsub("<", "<") + "'\n"
|
118
|
-
end
|
119
|
-
else
|
120
|
-
if @debug
|
121
|
-
self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
122
|
-
message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
123
|
-
end
|
124
|
-
end
|
125
|
-
else
|
126
|
-
if @debug
|
127
|
-
self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
128
|
-
end
|
129
|
-
message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
130
|
-
end
|
131
|
-
elsif !@response.include?(key.to_sym)
|
132
|
-
if @debug
|
133
|
-
self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
134
|
-
end
|
135
|
-
message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
136
|
-
end
|
137
|
-
end
|
138
|
-
}
|
139
|
-
else
|
140
|
-
message += "\n Same as the last response."
|
141
|
-
end
|
142
|
-
@logger.info message
|
143
|
-
if @response.kind_of?(Hash)
|
144
|
-
if @response.keys.include?(:requestid)
|
145
|
-
@headers["requestId"] = @response[:requestid]
|
146
|
-
self.class.request_id = @response[:requestid]
|
147
|
-
@logger.info "requestId was found on the response header and it has been added to the headers for the next request"
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
if resp[:'set-cookie'].to_s() != ""
|
152
|
-
if resp.kind_of?(Hash) #mock_response
|
153
|
-
cookies_to_set = resp[:'set-cookie'].to_s().split(", ")
|
154
|
-
else #Net::Http
|
155
|
-
cookies_to_set = resp.get_fields("set-cookie")
|
156
|
-
end
|
157
|
-
cookies_to_set.each { |cookie|
|
158
|
-
cookie_pair = cookie.split("; ")[0].split("=")
|
159
|
-
cookie_path = cookie.scan(/; path=([^;]+)/i).join
|
160
|
-
@cookies[cookie_path] = Hash.new() unless @cookies.keys.include?(cookie_path)
|
161
|
-
@cookies[cookie_path][cookie_pair[0]] = cookie_pair[1]
|
162
|
-
}
|
163
|
-
|
164
|
-
@logger.info "set-cookie added to Cookie header as required"
|
165
|
-
|
166
|
-
if @headers.has_key?("X-CSRFToken")
|
167
|
-
csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join
|
168
|
-
if csrftoken.to_s() != ""
|
169
|
-
@headers["X-CSRFToken"] = csrftoken
|
170
|
-
@logger.info "X-CSRFToken exists on headers and has been overwritten"
|
171
|
-
end
|
172
|
-
else
|
173
|
-
csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join
|
174
|
-
if csrftoken.to_s() != ""
|
175
|
-
@headers["X-CSRFToken"] = csrftoken
|
176
|
-
@logger.info "X-CSRFToken added to header as required"
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
|
-
@prev_response = @response
|
181
|
-
rescue Exception => stack
|
182
|
-
@logger.fatal stack
|
183
|
-
@logger.fatal "manage_response Error on method #{method_s} "
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
private
|
188
|
-
|
189
|
-
def set_stats(hash)
|
190
|
-
unless hash.key?(:num_requests)
|
191
|
-
# to add to the end the previous keys so num_requests and time_elapsed come first
|
192
|
-
keys = hash.keys
|
193
|
-
hash.keys.each do |k|
|
194
|
-
hash.delete(k)
|
195
|
-
end
|
196
|
-
|
197
|
-
hash[:num_requests] = 0
|
198
|
-
hash[:time_elapsed] = {
|
199
|
-
total: 0,
|
200
|
-
maximum: 0,
|
201
|
-
minimum: 100000,
|
202
|
-
average: 0,
|
203
|
-
}
|
204
|
-
|
205
|
-
# to add to the end the previous keys so num_requests and time_elapsed come first
|
206
|
-
keys.each do |k|
|
207
|
-
hash[k] = {}
|
208
|
-
end
|
209
|
-
end
|
210
|
-
hash[:num_requests] += 1
|
211
|
-
hash[:time_elapsed][:total] += @response[:time_elapsed]
|
212
|
-
hash[:time_elapsed][:maximum] = @response[:time_elapsed] if @response[:time_elapsed] > hash[:time_elapsed][:maximum]
|
213
|
-
hash[:time_elapsed][:minimum] = @response[:time_elapsed] if @response[:time_elapsed] < hash[:time_elapsed][:minimum]
|
214
|
-
hash[:time_elapsed][:average] = hash[:time_elapsed][:total] / hash[:num_requests]
|
215
|
-
end
|
216
|
-
|
217
|
-
private
|
218
|
-
|
219
|
-
def create_stats(resp)
|
220
|
-
# all
|
221
|
-
set_stats(self.class.stats[:all])
|
222
|
-
# all method
|
223
|
-
unless self.class.stats[:all][:method].key?(@prev_request[:method])
|
224
|
-
self.class.stats[:all][:method][@prev_request[:method]] = {
|
225
|
-
response: {},
|
226
|
-
}
|
227
|
-
end
|
228
|
-
set_stats(self.class.stats[:all][:method][@prev_request[:method]])
|
229
|
-
# all method response
|
230
|
-
unless self.class.stats[:all][:method][@prev_request[:method]][:response].key?(resp.code)
|
231
|
-
self.class.stats[:all][:method][@prev_request[:method]][:response][resp.code] = {}
|
232
|
-
end
|
233
|
-
set_stats(self.class.stats[:all][:method][@prev_request[:method]][:response][resp.code])
|
234
|
-
|
235
|
-
# server
|
236
|
-
server = "#{@host}:#{@port}"
|
237
|
-
unless self.class.stats[:path].key?(server)
|
238
|
-
self.class.stats[:path][server] = {}
|
239
|
-
end
|
240
|
-
set_stats(self.class.stats[:path][server])
|
241
|
-
# server path
|
242
|
-
unless self.class.stats[:path][server].key?(@prev_request[:path])
|
243
|
-
self.class.stats[:path][server][@prev_request[:path]] = {method: {}}
|
244
|
-
end
|
245
|
-
set_stats(self.class.stats[:path][server][@prev_request[:path]])
|
246
|
-
# server path method
|
247
|
-
unless self.class.stats[:path][server][@prev_request[:path]][:method].key?(@prev_request[:method])
|
248
|
-
self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]] = {
|
249
|
-
response: {},
|
250
|
-
}
|
251
|
-
end
|
252
|
-
set_stats(self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]])
|
253
|
-
# server path method response
|
254
|
-
unless self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]][:response].key?(resp.code)
|
255
|
-
self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]][:response][resp.code] = {}
|
256
|
-
end
|
257
|
-
set_stats(self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]][:response][resp.code])
|
258
|
-
|
259
|
-
if @prev_request.key?(:name)
|
260
|
-
# name
|
261
|
-
unless self.class.stats[:name].key?(@prev_request[:name])
|
262
|
-
self.class.stats[:name][@prev_request[:name]] = {method: {}}
|
263
|
-
end
|
264
|
-
set_stats(self.class.stats[:name][@prev_request[:name]])
|
265
|
-
# name method
|
266
|
-
unless self.class.stats[:name][@prev_request[:name]][:method].key?(@prev_request[:method])
|
267
|
-
self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]] = {
|
268
|
-
response: {},
|
269
|
-
}
|
270
|
-
end
|
271
|
-
set_stats(self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]])
|
272
|
-
# name method response
|
273
|
-
unless self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]][:response].key?(resp.code)
|
274
|
-
self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]][:response][resp.code] = {}
|
275
|
-
end
|
276
|
-
set_stats(self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]][:response][resp.code])
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
end
|
1
|
+
module NiceHttpManageResponse
|
2
|
+
|
3
|
+
######################################################
|
4
|
+
# private method to manage Response
|
5
|
+
# input:
|
6
|
+
# resp
|
7
|
+
# data
|
8
|
+
# output:
|
9
|
+
# @response updated
|
10
|
+
######################################################
|
11
|
+
def manage_response(resp, data)
|
12
|
+
require "json"
|
13
|
+
@prev_response = Hash.new() if @prev_response.nil?
|
14
|
+
begin
|
15
|
+
if @start_time.kind_of?(Time)
|
16
|
+
@response[:time_elapsed_total] = Time.now - @start_time
|
17
|
+
@start_time = nil
|
18
|
+
else
|
19
|
+
@response[:time_elapsed_total] = nil
|
20
|
+
end
|
21
|
+
if @start_time_net.kind_of?(Time)
|
22
|
+
@response[:time_elapsed] = Time.now - @start_time_net
|
23
|
+
@start_time_net = nil
|
24
|
+
else
|
25
|
+
@response[:time_elapsed] = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
create_stats(resp) if @create_stats
|
29
|
+
|
30
|
+
begin
|
31
|
+
# this is to be able to access all keys as symbols
|
32
|
+
new_resp = Hash.new()
|
33
|
+
resp.each { |key, value|
|
34
|
+
if key.kind_of?(String)
|
35
|
+
new_resp[key.to_sym] = value
|
36
|
+
end
|
37
|
+
}
|
38
|
+
new_resp.each { |key, value|
|
39
|
+
resp[key] = value
|
40
|
+
}
|
41
|
+
rescue
|
42
|
+
end
|
43
|
+
#for mock_responses to be able to add outside of the header like content-type for example
|
44
|
+
if resp.kind_of?(Hash) and !resp.has_key?(:header)
|
45
|
+
resp[:header] = {}
|
46
|
+
end
|
47
|
+
|
48
|
+
#todo: check this. not sure if this is valid anymore since resp it will be a hash only when mock_response
|
49
|
+
if resp.kind_of?(Hash)
|
50
|
+
resp.each { |k, v|
|
51
|
+
if k != :code and k != :message and k != :data and k != :'set-cookie' and k != :header
|
52
|
+
resp[:header][k] = v
|
53
|
+
end
|
54
|
+
}
|
55
|
+
resp[:header].each { |k, v|
|
56
|
+
resp.delete(k) if resp.has_key?(k)
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
method_s = caller[0].to_s().scan(/:in `(.*)'/).join
|
61
|
+
if resp.header.kind_of?(Hash) and (resp.header["content-type"].to_s() == "application/x-deflate" or resp.header[:"content-type"].to_s() == "application/x-deflate")
|
62
|
+
data = Zlib::Inflate.inflate(data)
|
63
|
+
end
|
64
|
+
encoding_response = ""
|
65
|
+
if resp.header.kind_of?(Hash) and (resp.header["content-type"].to_s() != "" or resp.header[:"content-type"].to_s() != "")
|
66
|
+
encoding_response = resp.header["content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?("content-type")
|
67
|
+
encoding_response = resp.header[:"content-type"].scan(/;charset=(.*)/i).join if resp.header.has_key?(:"content-type")
|
68
|
+
end
|
69
|
+
if encoding_response.to_s() == ""
|
70
|
+
encoding_response = "UTF-8"
|
71
|
+
end
|
72
|
+
|
73
|
+
if encoding_response.to_s() != "" and encoding_response.to_s().upcase != "UTF-8"
|
74
|
+
data.encode!("UTF-8", encoding_response.to_s())
|
75
|
+
end
|
76
|
+
|
77
|
+
if encoding_response != "" and encoding_response.to_s().upcase != "UTF-8"
|
78
|
+
@response[:message] = resp.message.to_s().encode("UTF-8", encoding_response.to_s())
|
79
|
+
#todo: response data in here for example is convert into string, verify if that is correct or needs to maintain the original data type (hash, array...)
|
80
|
+
resp.each { |key, val| @response[key.to_sym] = val.to_s().encode("UTF-8", encoding_response.to_s()) }
|
81
|
+
else
|
82
|
+
@response[:message] = resp.message
|
83
|
+
resp.each { |key, val|
|
84
|
+
@response[key.to_sym] = val
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
if !defined?(Net::HTTP::Post::Multipart) or (defined?(Net::HTTP::Post::Multipart) and !data.kind_of?(Net::HTTP::Post::Multipart))
|
89
|
+
@response[:data] = data
|
90
|
+
else
|
91
|
+
@response[:data] = ""
|
92
|
+
end
|
93
|
+
|
94
|
+
@response[:code] = resp.code
|
95
|
+
message = "\nRESPONSE: \n " + @response[:code].to_s() + ":" + @response[:message].to_s()
|
96
|
+
if @debug or @prev_response[:'content-type'] != @response[:'content-type'] or @prev_response[:'content-length'] != @response[:'content-length'] or
|
97
|
+
@prev_response[:data] != @response[:data] or @prev_response[:code] != @response[:code] or @prev_response[:message] != @response[:message]
|
98
|
+
self.class.last_response = message if @debug
|
99
|
+
@response.each { |key, value|
|
100
|
+
if value.to_s() != ""
|
101
|
+
value_orig = value
|
102
|
+
if key.kind_of?(Symbol)
|
103
|
+
if key == :code or key == :data or key == :header or key == :message
|
104
|
+
if key == :data and !@response[:'content-type'].to_s.include?("text/html")
|
105
|
+
begin
|
106
|
+
JSON.parse(value_orig)
|
107
|
+
data_s = JSON.pretty_generate(JSON.parse(value_orig))
|
108
|
+
rescue
|
109
|
+
data_s = value_orig
|
110
|
+
end
|
111
|
+
if @debug
|
112
|
+
self.class.last_response += "\n " + key.to_s() + ": '" + data_s.gsub("<", "<") + "'\n"
|
113
|
+
end
|
114
|
+
if value_orig != value
|
115
|
+
message += "\n " + key.to_s() + ": '" + value.gsub("<", "<") + "'\n"
|
116
|
+
else
|
117
|
+
message += "\n " + key.to_s() + ": '" + data_s.gsub("<", "<") + "'\n"
|
118
|
+
end
|
119
|
+
else
|
120
|
+
if @debug
|
121
|
+
self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
122
|
+
message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
else
|
126
|
+
if @debug
|
127
|
+
self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
128
|
+
end
|
129
|
+
message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
130
|
+
end
|
131
|
+
elsif !@response.include?(key.to_sym)
|
132
|
+
if @debug
|
133
|
+
self.class.last_response += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
134
|
+
end
|
135
|
+
message += "\n " + key.to_s() + ": '" + value.to_s().gsub("<", "<") + "'"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
}
|
139
|
+
else
|
140
|
+
message += "\n Same as the last response."
|
141
|
+
end
|
142
|
+
@logger.info message
|
143
|
+
if @response.kind_of?(Hash)
|
144
|
+
if @response.keys.include?(:requestid)
|
145
|
+
@headers["requestId"] = @response[:requestid]
|
146
|
+
self.class.request_id = @response[:requestid]
|
147
|
+
@logger.info "requestId was found on the response header and it has been added to the headers for the next request"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
if resp[:'set-cookie'].to_s() != ""
|
152
|
+
if resp.kind_of?(Hash) #mock_response
|
153
|
+
cookies_to_set = resp[:'set-cookie'].to_s().split(", ")
|
154
|
+
else #Net::Http
|
155
|
+
cookies_to_set = resp.get_fields("set-cookie")
|
156
|
+
end
|
157
|
+
cookies_to_set.each { |cookie|
|
158
|
+
cookie_pair = cookie.split("; ")[0].split("=")
|
159
|
+
cookie_path = cookie.scan(/; path=([^;]+)/i).join
|
160
|
+
@cookies[cookie_path] = Hash.new() unless @cookies.keys.include?(cookie_path)
|
161
|
+
@cookies[cookie_path][cookie_pair[0]] = cookie_pair[1]
|
162
|
+
}
|
163
|
+
|
164
|
+
@logger.info "set-cookie added to Cookie header as required"
|
165
|
+
|
166
|
+
if @headers.has_key?("X-CSRFToken")
|
167
|
+
csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join
|
168
|
+
if csrftoken.to_s() != ""
|
169
|
+
@headers["X-CSRFToken"] = csrftoken
|
170
|
+
@logger.info "X-CSRFToken exists on headers and has been overwritten"
|
171
|
+
end
|
172
|
+
else
|
173
|
+
csrftoken = resp[:"set-cookie"].to_s().scan(/csrftoken=([\da-z]+);/).join
|
174
|
+
if csrftoken.to_s() != ""
|
175
|
+
@headers["X-CSRFToken"] = csrftoken
|
176
|
+
@logger.info "X-CSRFToken added to header as required"
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
@prev_response = @response
|
181
|
+
rescue Exception => stack
|
182
|
+
@logger.fatal stack
|
183
|
+
@logger.fatal "manage_response Error on method #{method_s} "
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
private
|
188
|
+
|
189
|
+
def set_stats(hash)
|
190
|
+
unless hash.key?(:num_requests)
|
191
|
+
# to add to the end the previous keys so num_requests and time_elapsed come first
|
192
|
+
keys = hash.keys
|
193
|
+
hash.keys.each do |k|
|
194
|
+
hash.delete(k)
|
195
|
+
end
|
196
|
+
|
197
|
+
hash[:num_requests] = 0
|
198
|
+
hash[:time_elapsed] = {
|
199
|
+
total: 0,
|
200
|
+
maximum: 0,
|
201
|
+
minimum: 100000,
|
202
|
+
average: 0,
|
203
|
+
}
|
204
|
+
|
205
|
+
# to add to the end the previous keys so num_requests and time_elapsed come first
|
206
|
+
keys.each do |k|
|
207
|
+
hash[k] = {}
|
208
|
+
end
|
209
|
+
end
|
210
|
+
hash[:num_requests] += 1
|
211
|
+
hash[:time_elapsed][:total] += @response[:time_elapsed]
|
212
|
+
hash[:time_elapsed][:maximum] = @response[:time_elapsed] if @response[:time_elapsed] > hash[:time_elapsed][:maximum]
|
213
|
+
hash[:time_elapsed][:minimum] = @response[:time_elapsed] if @response[:time_elapsed] < hash[:time_elapsed][:minimum]
|
214
|
+
hash[:time_elapsed][:average] = hash[:time_elapsed][:total] / hash[:num_requests]
|
215
|
+
end
|
216
|
+
|
217
|
+
private
|
218
|
+
|
219
|
+
def create_stats(resp)
|
220
|
+
# all
|
221
|
+
set_stats(self.class.stats[:all])
|
222
|
+
# all method
|
223
|
+
unless self.class.stats[:all][:method].key?(@prev_request[:method])
|
224
|
+
self.class.stats[:all][:method][@prev_request[:method]] = {
|
225
|
+
response: {},
|
226
|
+
}
|
227
|
+
end
|
228
|
+
set_stats(self.class.stats[:all][:method][@prev_request[:method]])
|
229
|
+
# all method response
|
230
|
+
unless self.class.stats[:all][:method][@prev_request[:method]][:response].key?(resp.code)
|
231
|
+
self.class.stats[:all][:method][@prev_request[:method]][:response][resp.code] = {}
|
232
|
+
end
|
233
|
+
set_stats(self.class.stats[:all][:method][@prev_request[:method]][:response][resp.code])
|
234
|
+
|
235
|
+
# server
|
236
|
+
server = "#{@host}:#{@port}"
|
237
|
+
unless self.class.stats[:path].key?(server)
|
238
|
+
self.class.stats[:path][server] = {}
|
239
|
+
end
|
240
|
+
set_stats(self.class.stats[:path][server])
|
241
|
+
# server path
|
242
|
+
unless self.class.stats[:path][server].key?(@prev_request[:path])
|
243
|
+
self.class.stats[:path][server][@prev_request[:path]] = {method: {}}
|
244
|
+
end
|
245
|
+
set_stats(self.class.stats[:path][server][@prev_request[:path]])
|
246
|
+
# server path method
|
247
|
+
unless self.class.stats[:path][server][@prev_request[:path]][:method].key?(@prev_request[:method])
|
248
|
+
self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]] = {
|
249
|
+
response: {},
|
250
|
+
}
|
251
|
+
end
|
252
|
+
set_stats(self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]])
|
253
|
+
# server path method response
|
254
|
+
unless self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]][:response].key?(resp.code)
|
255
|
+
self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]][:response][resp.code] = {}
|
256
|
+
end
|
257
|
+
set_stats(self.class.stats[:path][server][@prev_request[:path]][:method][@prev_request[:method]][:response][resp.code])
|
258
|
+
|
259
|
+
if @prev_request.key?(:name)
|
260
|
+
# name
|
261
|
+
unless self.class.stats[:name].key?(@prev_request[:name])
|
262
|
+
self.class.stats[:name][@prev_request[:name]] = {method: {}}
|
263
|
+
end
|
264
|
+
set_stats(self.class.stats[:name][@prev_request[:name]])
|
265
|
+
# name method
|
266
|
+
unless self.class.stats[:name][@prev_request[:name]][:method].key?(@prev_request[:method])
|
267
|
+
self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]] = {
|
268
|
+
response: {},
|
269
|
+
}
|
270
|
+
end
|
271
|
+
set_stats(self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]])
|
272
|
+
# name method response
|
273
|
+
unless self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]][:response].key?(resp.code)
|
274
|
+
self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]][:response][resp.code] = {}
|
275
|
+
end
|
276
|
+
set_stats(self.class.stats[:name][@prev_request[:name]][:method][@prev_request[:method]][:response][resp.code])
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
end
|