nice_http 1.8.10 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +53 -3
- data/lib/nice_http/add_stats.rb +85 -0
- data/lib/nice_http/close.rb +39 -0
- data/lib/nice_http/defaults.rb +30 -0
- data/lib/nice_http/inherited.rb +8 -0
- data/lib/nice_http/initialize.rb +253 -0
- data/lib/nice_http/manage/create_stats.rb +64 -0
- data/lib/nice_http/{manage_request.rb → manage/request.rb} +16 -9
- data/lib/nice_http/{manage_response.rb → manage/response.rb} +7 -111
- data/lib/nice_http/manage/set_stats.rb +43 -0
- data/lib/nice_http/manage/wait_async_operation.rb +43 -0
- data/lib/nice_http/methods/delete.rb +108 -0
- data/lib/nice_http/methods/get.rb +159 -0
- data/lib/nice_http/methods/head.rb +72 -0
- data/lib/nice_http/methods/patch.rb +103 -0
- data/lib/nice_http/methods/post.rb +122 -0
- data/lib/nice_http/methods/put.rb +87 -0
- data/lib/nice_http/methods/send_request.rb +47 -0
- data/lib/nice_http/reset.rb +54 -0
- data/lib/nice_http/save_stats.rb +37 -0
- data/lib/nice_http/utils/basic_authentication.rb +18 -0
- data/lib/nice_http/{utils.rb → utils/get_value_xml_tag.rb} +0 -45
- data/lib/nice_http/utils/set_value_xml_tag.rb +30 -0
- data/lib/nice_http.rb +33 -475
- metadata +24 -6
- data/lib/nice_http/http_methods.rb +0 -686
data/lib/nice_http.rb
CHANGED
@@ -1,15 +1,34 @@
|
|
1
1
|
require "logger"
|
2
2
|
require "nice_hash"
|
3
|
-
require_relative "nice_http/
|
4
|
-
require_relative "nice_http/
|
5
|
-
require_relative "nice_http/
|
6
|
-
require_relative "nice_http/
|
3
|
+
require_relative "nice_http/methods/get"
|
4
|
+
require_relative "nice_http/methods/post"
|
5
|
+
require_relative "nice_http/methods/head"
|
6
|
+
require_relative "nice_http/methods/put"
|
7
|
+
require_relative "nice_http/methods/delete"
|
8
|
+
require_relative "nice_http/methods/patch"
|
9
|
+
require_relative "nice_http/methods/send_request"
|
10
|
+
require_relative "nice_http/manage/create_stats"
|
11
|
+
require_relative "nice_http/manage/request"
|
12
|
+
require_relative "nice_http/manage/response"
|
13
|
+
require_relative "nice_http/manage/set_stats"
|
14
|
+
require_relative "nice_http/manage/wait_async_operation"
|
15
|
+
require_relative "nice_http/utils/basic_authentication"
|
16
|
+
require_relative "nice_http/utils/get_value_xml_tag"
|
17
|
+
require_relative "nice_http/utils/set_value_xml_tag"
|
18
|
+
require_relative "nice_http/reset"
|
19
|
+
require_relative "nice_http/add_stats"
|
20
|
+
require_relative "nice_http/defaults"
|
21
|
+
require_relative "nice_http/inherited"
|
22
|
+
require_relative "nice_http/save_stats"
|
23
|
+
require_relative "nice_http/close"
|
24
|
+
require_relative "nice_http/initialize"
|
7
25
|
|
8
26
|
######################################################
|
9
27
|
# Attributes you can access using NiceHttp.the_attribute:
|
10
28
|
# :host, :port, :ssl, :timeout, :headers, :debug, :log, :log_headers, :proxy_host, :proxy_port,
|
11
29
|
# :last_request, :last_response, :request_id, :use_mocks, :connections,
|
12
|
-
# :active, :auto_redirect, :values_for, :create_stats, :stats, :capture, :captured, :request, :requests
|
30
|
+
# :active, :auto_redirect, :values_for, :create_stats, :stats, :capture, :captured, :request, :requests,
|
31
|
+
# :async_wait_seconds, :async_header, :async_completed, :async_resource, :async_status
|
13
32
|
#
|
14
33
|
# @attr [String] host The host to be accessed
|
15
34
|
# @attr [Integer] port The port number
|
@@ -50,6 +69,11 @@ require_relative "nice_http/http_methods"
|
|
50
69
|
# @attr [Hash] stats It contains detailed stats of the http communication
|
51
70
|
# @attr [Boolean] capture If true, NiceHttp will store all requests and responses on NiceHttp.captured as strings
|
52
71
|
# @attr [Array] captured It contains all the http requests and responses if NiceHttp.capture is set to true
|
72
|
+
# @attr [Integer] async_wait_seconds Number of seconds to wait until the async request is completed
|
73
|
+
# @attr [String] async_header The header to check if the async request is completed
|
74
|
+
# @attr [String] async_completed The value of the async_header to check if the async request is completed
|
75
|
+
# @attr [String] async_resource The resource to check if the async request is completed
|
76
|
+
# @attr [String] async_status The status to check if the async request is completed
|
53
77
|
######################################################
|
54
78
|
class NiceHttp
|
55
79
|
include NiceHttpManageRequest
|
@@ -74,7 +98,8 @@ class NiceHttp
|
|
74
98
|
class << self
|
75
99
|
attr_accessor :host, :port, :ssl, :timeout, :headers, :debug, :log_path, :log, :proxy_host, :proxy_port, :log_headers,
|
76
100
|
:last_request, :last_response, :request, :request_id, :use_mocks, :connections,
|
77
|
-
:active, :auto_redirect, :log_files, :values_for, :create_stats, :stats, :capture, :captured, :requests
|
101
|
+
:active, :auto_redirect, :log_files, :values_for, :create_stats, :stats, :capture, :captured, :requests,
|
102
|
+
:async_wait_seconds, :async_header, :async_completed, :async_resource, :async_status
|
78
103
|
end
|
79
104
|
|
80
105
|
at_exit do
|
@@ -83,478 +108,11 @@ class NiceHttp
|
|
83
108
|
end
|
84
109
|
end
|
85
110
|
|
86
|
-
######################################################
|
87
|
-
# to reset to the original defaults
|
88
|
-
######################################################
|
89
|
-
def self.reset!
|
90
|
-
@host = nil
|
91
|
-
@port = 80
|
92
|
-
@ssl = false
|
93
|
-
@timeout = nil
|
94
|
-
@headers = {}
|
95
|
-
@values_for = {}
|
96
|
-
@debug = false
|
97
|
-
@log = :fix_file
|
98
|
-
@log_path = ''
|
99
|
-
@log_headers = :all
|
100
|
-
@proxy_host = nil
|
101
|
-
@proxy_port = nil
|
102
|
-
@last_request = nil
|
103
|
-
@request = nil
|
104
|
-
@requests = nil
|
105
|
-
@last_response = nil
|
106
|
-
@request_id = ""
|
107
|
-
@use_mocks = false
|
108
|
-
@connections = []
|
109
|
-
@active = 0
|
110
|
-
@auto_redirect = true
|
111
|
-
@log_files = {}
|
112
|
-
@create_stats = false
|
113
|
-
@stats = {
|
114
|
-
all: {
|
115
|
-
num_requests: 0,
|
116
|
-
started: nil,
|
117
|
-
finished: nil,
|
118
|
-
real_time_elapsed: 0,
|
119
|
-
time_elapsed: {
|
120
|
-
total: 0,
|
121
|
-
maximum: 0,
|
122
|
-
minimum: 1000000,
|
123
|
-
average: 0,
|
124
|
-
},
|
125
|
-
method: {},
|
126
|
-
},
|
127
|
-
path: {},
|
128
|
-
name: {},
|
129
|
-
}
|
130
|
-
@capture = false
|
131
|
-
@captured = []
|
132
|
-
end
|
133
111
|
reset!
|
134
112
|
|
135
|
-
######################################################
|
136
|
-
# If inheriting from NiceHttp class
|
137
|
-
######################################################
|
138
|
-
def self.inherited(subclass)
|
139
|
-
subclass.reset!
|
140
|
-
end
|
141
|
-
|
142
113
|
attr_reader :host, :port, :ssl, :timeout, :debug, :log, :log_path, :proxy_host, :proxy_port, :response, :num_redirects
|
143
|
-
attr_accessor :headers, :cookies, :use_mocks, :auto_redirect, :logger, :values_for, :log_headers
|
144
|
-
|
145
|
-
######################################################
|
146
|
-
# Change the default values for NiceHttp supplying a Hash
|
147
|
-
#
|
148
|
-
# @param par [Hash] keys: :host, :port, :ssl, :timeout, :headers, :debug, :log, :log_path, :proxy_host, :proxy_port, :use_mocks, :auto_redirect, :values_for, :create_stats, :log_headers, :capture
|
149
|
-
######################################################
|
150
|
-
def self.defaults=(par = {})
|
151
|
-
@host = par[:host] if par.key?(:host)
|
152
|
-
@port = par[:port] if par.key?(:port)
|
153
|
-
@ssl = par[:ssl] if par.key?(:ssl)
|
154
|
-
@timeout = par[:timeout] if par.key?(:timeout)
|
155
|
-
@headers = par[:headers].dup if par.key?(:headers)
|
156
|
-
@values_for = par[:values_for].dup if par.key?(:values_for)
|
157
|
-
@debug = par[:debug] if par.key?(:debug)
|
158
|
-
@log_path = par[:log_path] if par.key?(:log_path)
|
159
|
-
@log = par[:log] if par.key?(:log)
|
160
|
-
@log_headers = par[:log_headers] if par.key?(:log_headers)
|
161
|
-
@proxy_host = par[:proxy_host] if par.key?(:proxy_host)
|
162
|
-
@proxy_port = par[:proxy_port] if par.key?(:proxy_port)
|
163
|
-
@use_mocks = par[:use_mocks] if par.key?(:use_mocks)
|
164
|
-
@auto_redirect = par[:auto_redirect] if par.key?(:auto_redirect)
|
165
|
-
@create_stats = par[:create_stats] if par.key?(:create_stats)
|
166
|
-
@capture = par[:capture] if par.key?(:capture)
|
167
|
-
end
|
168
|
-
|
169
|
-
######################################################
|
170
|
-
# To add specific stats
|
171
|
-
# The stats will be added to NiceHttp.stats[:specific]
|
172
|
-
#
|
173
|
-
# @param name [Symbol] name to group your specific stats
|
174
|
-
# @param state [Symbol] state of the name supplied to group your specific stats
|
175
|
-
# @param started [Time] when the process you want the stats started
|
176
|
-
# @param finished [Time] when the process you want the stats finished
|
177
|
-
# @param item [Object] (Optional) The item to be added to :items key to store all items in an array
|
178
|
-
#
|
179
|
-
# @example
|
180
|
-
# started = Time.now
|
181
|
-
# @http.send_request Requests::Customer.add_customer
|
182
|
-
# 30.times do
|
183
|
-
# resp = @http.get(Requests::Customer.get_customer)
|
184
|
-
# break if resp.code == 200
|
185
|
-
# sleep 0.5
|
186
|
-
# end
|
187
|
-
# NiceHttp.add_stats(:customer, :create, started, Time.now)
|
188
|
-
######################################################
|
189
|
-
def self.add_stats(name, state, started, finished, item = nil)
|
190
|
-
self.stats[:specific] ||= {}
|
191
|
-
self.stats[:specific][name] ||= { num: 0, started: started, finished: started, real_time_elapsed: 0, time_elapsed: { total: 0, maximum: 0, minimum: 100000, average: 0 } }
|
192
|
-
self.stats[:specific][name][:num] += 1
|
193
|
-
|
194
|
-
if started < self.stats[:specific][name][:finished]
|
195
|
-
self.stats[:specific][name][:real_time_elapsed] += (finished - self.stats[:specific][name][:finished])
|
196
|
-
else
|
197
|
-
self.stats[:specific][name][:real_time_elapsed] += (finished - started)
|
198
|
-
end
|
199
|
-
self.stats[:specific][name][:finished] = finished
|
200
|
-
|
201
|
-
time_elapsed = self.stats[:specific][name][:time_elapsed]
|
202
|
-
time_elapsed[:total] += finished - started
|
203
|
-
if time_elapsed[:maximum] < (finished - started)
|
204
|
-
time_elapsed[:maximum] = (finished - started)
|
205
|
-
if !item.nil?
|
206
|
-
time_elapsed[:item_maximum] = item
|
207
|
-
elsif Thread.current.name.to_s != ""
|
208
|
-
time_elapsed[:item_maximum] = Thread.current.name
|
209
|
-
end
|
210
|
-
end
|
211
|
-
if time_elapsed[:minimum] > (finished - started)
|
212
|
-
time_elapsed[:minimum] = (finished - started)
|
213
|
-
if !item.nil?
|
214
|
-
time_elapsed[:item_minimum] = item
|
215
|
-
elsif Thread.current.name.to_s != ""
|
216
|
-
time_elapsed[:item_minimum] = Thread.current.name
|
217
|
-
end
|
218
|
-
end
|
219
|
-
time_elapsed[:average] = time_elapsed[:total] / self.stats[:specific][name][:num]
|
220
|
-
|
221
|
-
self.stats[:specific][name][state] ||= { num: 0, started: started, finished: started, real_time_elapsed: 0, time_elapsed: { total: 0, maximum: 0, minimum: 1000, average: 0 }, items: [] }
|
222
|
-
self.stats[:specific][name][state][:num] += 1
|
223
|
-
if started < self.stats[:specific][name][state][:finished]
|
224
|
-
self.stats[:specific][name][state][:real_time_elapsed] += (finished - self.stats[:specific][name][state][:finished])
|
225
|
-
else
|
226
|
-
self.stats[:specific][name][state][:real_time_elapsed] += (finished - started)
|
227
|
-
end
|
228
|
-
|
229
|
-
self.stats[:specific][name][state][:finished] = finished
|
230
|
-
|
231
|
-
self.stats[:specific][name][state][:items] << item unless item.nil? or self.stats[:specific][name][state][:items].include?(item)
|
232
|
-
time_elapsed = self.stats[:specific][name][state][:time_elapsed]
|
233
|
-
time_elapsed[:total] += finished - started
|
234
|
-
if time_elapsed[:maximum] < (finished - started)
|
235
|
-
time_elapsed[:maximum] = (finished - started)
|
236
|
-
if !item.nil?
|
237
|
-
time_elapsed[:item_maximum] = item
|
238
|
-
elsif Thread.current.name.to_s != ""
|
239
|
-
time_elapsed[:item_maximum] = Thread.current.name
|
240
|
-
end
|
241
|
-
end
|
242
|
-
if time_elapsed[:minimum] > (finished - started)
|
243
|
-
time_elapsed[:minimum] = (finished - started)
|
244
|
-
if !item.nil?
|
245
|
-
time_elapsed[:item_minimum] = item
|
246
|
-
elsif Thread.current.name.to_s != ""
|
247
|
-
time_elapsed[:item_minimum] = Thread.current.name
|
248
|
-
end
|
249
|
-
end
|
250
|
-
time_elapsed[:average] = time_elapsed[:total] / self.stats[:specific][name][state][:num]
|
251
|
-
end
|
252
|
-
|
253
|
-
######################################################
|
254
|
-
# It will save the NiceHttp.stats on different files, each key of the hash in a different file.
|
255
|
-
#
|
256
|
-
# @param file_name [String] path and file name to be used to store the stats.
|
257
|
-
# In case no one supplied it will be used the value in NiceHttp.log and it will be saved on YAML format.
|
258
|
-
# In case extension is .yaml will be saved on YAML format.
|
259
|
-
# In case extension is .json will be saved on JSON format.
|
260
|
-
#
|
261
|
-
# @example
|
262
|
-
# NiceHttp.save_stats
|
263
|
-
# NiceHttp.save_stats('./stats/my_stats.yaml')
|
264
|
-
# NiceHttp.save_stats('./stats/my_stats.json')
|
265
|
-
######################################################
|
266
|
-
def self.save_stats(file_name = "")
|
267
|
-
if file_name == ""
|
268
|
-
if self.log.is_a?(String)
|
269
|
-
file_name = self.log
|
270
|
-
else
|
271
|
-
file_name = "./#{self.log_path}nice_http.log"
|
272
|
-
end
|
273
|
-
end
|
274
|
-
require "fileutils"
|
275
|
-
FileUtils.mkdir_p File.dirname(file_name)
|
276
|
-
if file_name.match?(/\.json$/)
|
277
|
-
require "json"
|
278
|
-
self.stats.keys.each do |key|
|
279
|
-
File.open("#{file_name.gsub(/.json$/, "_stats_")}#{key}.json", "w") { |file| file.write(self.stats[key].to_json) }
|
280
|
-
end
|
281
|
-
else
|
282
|
-
require "yaml"
|
283
|
-
self.stats.keys.each do |key|
|
284
|
-
File.open("#{file_name.gsub(/.\w+$/, "_stats_")}#{key}.yaml", "w") { |file| file.write(self.stats[key].to_yaml) }
|
285
|
-
end
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
######################################################
|
290
|
-
# Creates a new http connection.
|
291
|
-
#
|
292
|
-
# @param args [] If no parameter supplied, by default will access how is setup on defaults
|
293
|
-
# @example
|
294
|
-
# http = NiceHttp.new()
|
295
|
-
# @param args [String]. The url to create the connection.
|
296
|
-
# @example
|
297
|
-
# http = NiceHttp.new("https://www.example.com")
|
298
|
-
# @example
|
299
|
-
# http = NiceHttp.new("example.com:8999")
|
300
|
-
# @example
|
301
|
-
# http = NiceHttp.new("localhost:8322")
|
302
|
-
# @param args [Hash] containing these possible keys:
|
303
|
-
# host -- example.com. (default blank screen)
|
304
|
-
# port -- port for the connection. 80 (default)
|
305
|
-
# ssl -- true, false (default)
|
306
|
-
# timeout -- integer or nil (default)
|
307
|
-
# headers -- hash with the headers
|
308
|
-
# values_for -- hash with the values_for
|
309
|
-
# debug -- true, false (default)
|
310
|
-
# log_path -- string with path for the logs, empty string (default)
|
311
|
-
# log -- :no, :screen, :file, :fix_file (default).
|
312
|
-
# log_headers -- :all, :none, :partial (default).
|
313
|
-
# A string with a path can be supplied.
|
314
|
-
# If :fix_file: nice_http.log
|
315
|
-
# In case :file it will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log
|
316
|
-
# proxy_host
|
317
|
-
# proxy_port
|
318
|
-
# @example
|
319
|
-
# http2 = NiceHttp.new( host: "reqres.in", port: 443, ssl: true )
|
320
|
-
# @example
|
321
|
-
# my_server = {host: "example.com",
|
322
|
-
# port: 80,
|
323
|
-
# headers: {"api-key": "zdDDdjkck"}
|
324
|
-
# }
|
325
|
-
# http3 = NiceHttp.new my_server
|
326
|
-
######################################################
|
327
|
-
def initialize(args = {})
|
328
|
-
require "net/http"
|
329
|
-
require "net/https"
|
330
|
-
@host = self.class.host
|
331
|
-
@port = self.class.port
|
332
|
-
@prepath = ""
|
333
|
-
@ssl = self.class.ssl
|
334
|
-
@timeout = self.class.timeout
|
335
|
-
@headers = self.class.headers.dup
|
336
|
-
@values_for = self.class.values_for.dup
|
337
|
-
@debug = self.class.debug
|
338
|
-
@log = self.class.log
|
339
|
-
@log_path = self.class.log_path
|
340
|
-
@log_headers = self.class.log_headers
|
341
|
-
@proxy_host = self.class.proxy_host
|
342
|
-
@proxy_port = self.class.proxy_port
|
343
|
-
@use_mocks = self.class.use_mocks
|
344
|
-
@auto_redirect = false #set it up at the end of initialize
|
345
|
-
auto_redirect = self.class.auto_redirect
|
346
|
-
@num_redirects = 0
|
347
|
-
@create_stats = self.class.create_stats
|
348
|
-
@capture = self.class.capture
|
349
|
-
|
350
|
-
#todo: set only the cookies for the current domain
|
351
|
-
#key: path, value: hash with key is the name of the cookie and value the value
|
352
|
-
# we set the default value for non existing keys to empty Hash {} so in case of merge there is no problem
|
353
|
-
@cookies = Hash.new { |h, k| h[k] = {} }
|
354
|
-
|
355
|
-
if args.is_a?(String)
|
356
|
-
uri = URI.parse(args)
|
357
|
-
@host = uri.host unless uri.host.nil?
|
358
|
-
@port = uri.port unless uri.port.nil?
|
359
|
-
@ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
|
360
|
-
@prepath = uri.path unless uri.path == "/"
|
361
|
-
elsif args.is_a?(Hash) && !args.keys.empty?
|
362
|
-
@host = args[:host] if args.keys.include?(:host)
|
363
|
-
@port = args[:port] if args.keys.include?(:port)
|
364
|
-
@ssl = args[:ssl] if args.keys.include?(:ssl)
|
365
|
-
@timeout = args[:timeout] if args.keys.include?(:timeout)
|
366
|
-
@headers = args[:headers].dup if args.keys.include?(:headers)
|
367
|
-
@values_for = args[:values_for].dup if args.keys.include?(:values_for)
|
368
|
-
@debug = args[:debug] if args.keys.include?(:debug)
|
369
|
-
@log = args[:log] if args.keys.include?(:log)
|
370
|
-
@log_path = args[:log_path] if args.keys.include?(:log_path)
|
371
|
-
@log_headers = args[:log_headers] if args.keys.include?(:log_headers)
|
372
|
-
@proxy_host = args[:proxy_host] if args.keys.include?(:proxy_host)
|
373
|
-
@proxy_port = args[:proxy_port] if args.keys.include?(:proxy_port)
|
374
|
-
@use_mocks = args[:use_mocks] if args.keys.include?(:use_mocks)
|
375
|
-
auto_redirect = args[:auto_redirect] if args.keys.include?(:auto_redirect)
|
376
|
-
end
|
377
|
-
|
378
|
-
log_filename = ""
|
379
|
-
if @log.kind_of?(String) or @log == :fix_file or @log == :file or @log == :file_run
|
380
|
-
if @log.kind_of?(String)
|
381
|
-
log_filename = @log.dup
|
382
|
-
unless log_filename.start_with?(".")
|
383
|
-
if caller.first.start_with?(Dir.pwd)
|
384
|
-
folder = File.dirname(caller.first.scan(/(.+):\d/).join)
|
385
|
-
else
|
386
|
-
folder = File.dirname("#{Dir.pwd}/#{caller.first.scan(/(.+):\d/).join}")
|
387
|
-
end
|
388
|
-
folder += "/" unless log_filename.start_with?("/") or log_filename.match?(/^\w+:/)
|
389
|
-
log_filename = folder + log_filename
|
390
|
-
end
|
391
|
-
require "fileutils"
|
392
|
-
FileUtils.mkdir_p File.dirname(log_filename)
|
393
|
-
unless Dir.exist?(File.dirname(log_filename))
|
394
|
-
@logger = Logger.new nil
|
395
|
-
raise InfoMissing, :log, "Wrong directory specified for logs.\n"
|
396
|
-
end
|
397
|
-
elsif @log == :fix_file
|
398
|
-
log_filename = "nice_http.log"
|
399
|
-
elsif @log == :file
|
400
|
-
log_filename = "nice_http_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.log"
|
401
|
-
elsif @log == :file_run
|
402
|
-
log_filename = "#{caller.first.scan(/(.+):\d/).join}.log"
|
403
|
-
end
|
404
|
-
if Thread.current.name.to_s != ""
|
405
|
-
log_filename.gsub!(/\.log$/, "_#{Thread.current.name}.log")
|
406
|
-
end
|
407
|
-
unless @log_path.to_s == ''
|
408
|
-
log_filename.gsub!(Dir.pwd,'.')
|
409
|
-
dpath = @log_path.split("/")
|
410
|
-
dfile = log_filename.split("/")
|
411
|
-
log_filenamepath = ''
|
412
|
-
dfile.each_with_index do |d,i|
|
413
|
-
if d==dpath[i]
|
414
|
-
log_filenamepath<<"#{d}/"
|
415
|
-
else
|
416
|
-
log_filename = @log_path + "#{log_filename.gsub(/^#{log_filenamepath}/,'')}"
|
417
|
-
break
|
418
|
-
end
|
419
|
-
end
|
420
|
-
log_filename = "./#{log_filename}" unless log_filename[0..1]=='./'
|
421
|
-
log_filename = ".#{log_filename}" unless log_filename[0]=='.'
|
422
|
-
|
423
|
-
unless File.exist?(log_filename)
|
424
|
-
require 'fileutils'
|
425
|
-
FileUtils.mkdir_p(File.dirname(log_filename))
|
426
|
-
end
|
427
|
-
end
|
428
|
-
|
429
|
-
if self.class.log_files.key?(log_filename) and File.exist?(log_filename)
|
430
|
-
@logger = self.class.log_files[log_filename]
|
431
|
-
else
|
432
|
-
begin
|
433
|
-
f = File.new(log_filename, "w")
|
434
|
-
f.sync = true
|
435
|
-
@logger = Logger.new f
|
436
|
-
rescue Exception => stack
|
437
|
-
@logger = Logger.new nil
|
438
|
-
raise InfoMissing, :log
|
439
|
-
end
|
440
|
-
self.class.log_files[log_filename] = @logger
|
441
|
-
end
|
442
|
-
elsif @log == :screen
|
443
|
-
@logger = Logger.new STDOUT
|
444
|
-
elsif @log == :no
|
445
|
-
@logger = Logger.new nil
|
446
|
-
else
|
447
|
-
raise InfoMissing, :log
|
448
|
-
end
|
449
|
-
@logger.level = Logger::INFO
|
450
|
-
|
451
|
-
if @host.to_s != "" and (@host.start_with?("http:") or @host.start_with?("https:"))
|
452
|
-
uri = URI.parse(@host)
|
453
|
-
@host = uri.host unless uri.host.nil?
|
454
|
-
@port = uri.port unless uri.port.nil?
|
455
|
-
@ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
|
456
|
-
@prepath = uri.path unless uri.path == "/"
|
457
|
-
end
|
458
|
-
raise InfoMissing, :port if @port.to_s == ""
|
459
|
-
raise InfoMissing, :host if @host.to_s == ""
|
460
|
-
raise InfoMissing, :ssl unless @ssl.is_a?(TrueClass) or @ssl.is_a?(FalseClass)
|
461
|
-
raise InfoMissing, :timeout unless @timeout.is_a?(Integer) or @timeout.nil?
|
462
|
-
raise InfoMissing, :debug unless @debug.is_a?(TrueClass) or @debug.is_a?(FalseClass)
|
463
|
-
raise InfoMissing, :auto_redirect unless auto_redirect.is_a?(TrueClass) or auto_redirect.is_a?(FalseClass)
|
464
|
-
raise InfoMissing, :use_mocks unless @use_mocks.is_a?(TrueClass) or @use_mocks.is_a?(FalseClass)
|
465
|
-
raise InfoMissing, :headers unless @headers.is_a?(Hash)
|
466
|
-
raise InfoMissing, :values_for unless @values_for.is_a?(Hash)
|
467
|
-
raise InfoMissing, :log_headers unless [:all, :none, :partial].include?(@log_headers)
|
468
|
-
|
469
|
-
begin
|
470
|
-
if !@proxy_host.nil? && !@proxy_port.nil?
|
471
|
-
@http = Net::HTTP::Proxy(@proxy_host, @proxy_port).new(@host, @port)
|
472
|
-
@http.use_ssl = @ssl
|
473
|
-
@http.set_debug_output $stderr if @debug
|
474
|
-
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
475
|
-
unless @timeout.nil?
|
476
|
-
@http.open_timeout = @timeout
|
477
|
-
@http.read_timeout = @timeout
|
478
|
-
end
|
479
|
-
@http.start
|
480
|
-
else
|
481
|
-
@http = Net::HTTP.new(@host, @port)
|
482
|
-
@http.use_ssl = @ssl
|
483
|
-
@http.set_debug_output $stderr if @debug
|
484
|
-
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
485
|
-
unless @timeout.nil?
|
486
|
-
@http.open_timeout = @timeout
|
487
|
-
@http.read_timeout = @timeout
|
488
|
-
end
|
489
|
-
@http.start
|
490
|
-
end
|
491
|
-
|
492
|
-
@message_server = "(#{self.object_id}):"
|
493
|
-
|
494
|
-
log_message = "(#{self.object_id}): Http connection created. host:#{@host}, port:#{@port}, ssl:#{@ssl}, timeout:#{@timeout}, mode:#{@mode}, proxy_host: #{@proxy_host.to_s()}, proxy_port: #{@proxy_port.to_s()} "
|
495
|
-
|
496
|
-
@logger.info(log_message)
|
497
|
-
@message_server += " Http connection: "
|
498
|
-
if @ssl
|
499
|
-
@message_server += "https://"
|
500
|
-
else
|
501
|
-
@message_server += "http://"
|
502
|
-
end
|
503
|
-
@message_server += "#{@host}:#{@port}"
|
504
|
-
if @proxy_host.to_s != ""
|
505
|
-
@message_server += " proxy:#{@proxy_host}:#{@proxy_port}"
|
506
|
-
end
|
507
|
-
@auto_redirect = auto_redirect
|
508
|
-
# for the case we have headers following nice_hash implementation
|
509
|
-
@headers_orig = @headers.dup
|
510
|
-
@headers = @headers.generate
|
511
|
-
|
512
|
-
self.class.active += 1
|
513
|
-
self.class.connections.push(self)
|
514
|
-
rescue Exception => stack
|
515
|
-
puts stack
|
516
|
-
@logger.fatal stack
|
517
|
-
raise stack
|
518
|
-
end
|
519
|
-
end
|
520
|
-
|
521
|
-
######################################################
|
522
|
-
# Close HTTP connection
|
523
|
-
######################################################
|
524
|
-
def close
|
525
|
-
begin
|
526
|
-
pos = 0
|
527
|
-
found = false
|
528
|
-
self.class.connections.each { |conn|
|
529
|
-
if conn.object_id == self.object_id
|
530
|
-
found = true
|
531
|
-
break
|
532
|
-
else
|
533
|
-
pos += 1
|
534
|
-
end
|
535
|
-
}
|
536
|
-
if found
|
537
|
-
self.class.connections.delete_at(pos)
|
538
|
-
end
|
539
|
-
|
540
|
-
unless @closed
|
541
|
-
if !@http.nil?
|
542
|
-
@http.finish()
|
543
|
-
@http = nil
|
544
|
-
@logger.info "the HTTP connection was closed: #{@message_server}"
|
545
|
-
else
|
546
|
-
@http = nil
|
547
|
-
@logger.fatal "It was not possible to close the HTTP connection: #{@message_server}"
|
548
|
-
end
|
549
|
-
@closed = true
|
550
|
-
else
|
551
|
-
@logger.warn "It was not possible to close the HTTP connection, already closed: #{@message_server}"
|
552
|
-
end
|
553
|
-
rescue Exception => stack
|
554
|
-
@logger.fatal stack
|
555
|
-
end
|
556
|
-
self.class.active -= 1
|
557
|
-
end
|
114
|
+
attr_accessor :headers, :cookies, :use_mocks, :auto_redirect, :logger, :values_for, :log_headers,
|
115
|
+
:async_wait_seconds, :async_header, :async_completed, :async_resource, :async_status
|
558
116
|
|
559
117
|
private :manage_request, :manage_response
|
560
118
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nice_http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario Ruiz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-10-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nice_hash
|
@@ -64,10 +64,28 @@ files:
|
|
64
64
|
- LICENSE
|
65
65
|
- README.md
|
66
66
|
- lib/nice_http.rb
|
67
|
-
- lib/nice_http/
|
68
|
-
- lib/nice_http/
|
69
|
-
- lib/nice_http/
|
70
|
-
- lib/nice_http/
|
67
|
+
- lib/nice_http/add_stats.rb
|
68
|
+
- lib/nice_http/close.rb
|
69
|
+
- lib/nice_http/defaults.rb
|
70
|
+
- lib/nice_http/inherited.rb
|
71
|
+
- lib/nice_http/initialize.rb
|
72
|
+
- lib/nice_http/manage/create_stats.rb
|
73
|
+
- lib/nice_http/manage/request.rb
|
74
|
+
- lib/nice_http/manage/response.rb
|
75
|
+
- lib/nice_http/manage/set_stats.rb
|
76
|
+
- lib/nice_http/manage/wait_async_operation.rb
|
77
|
+
- lib/nice_http/methods/delete.rb
|
78
|
+
- lib/nice_http/methods/get.rb
|
79
|
+
- lib/nice_http/methods/head.rb
|
80
|
+
- lib/nice_http/methods/patch.rb
|
81
|
+
- lib/nice_http/methods/post.rb
|
82
|
+
- lib/nice_http/methods/put.rb
|
83
|
+
- lib/nice_http/methods/send_request.rb
|
84
|
+
- lib/nice_http/reset.rb
|
85
|
+
- lib/nice_http/save_stats.rb
|
86
|
+
- lib/nice_http/utils/basic_authentication.rb
|
87
|
+
- lib/nice_http/utils/get_value_xml_tag.rb
|
88
|
+
- lib/nice_http/utils/set_value_xml_tag.rb
|
71
89
|
homepage: https://github.com/MarioRuiz/nice_http
|
72
90
|
licenses:
|
73
91
|
- MIT
|