nice_http 1.5.2 → 1.6.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 +366 -365
- data/lib/nice_http.rb +348 -340
- data/lib/nice_http/http_methods.rb +528 -528
- data/lib/nice_http/manage_request.rb +242 -233
- data/lib/nice_http/manage_response.rb +180 -180
- data/lib/nice_http/utils.rb +109 -109
- metadata +2 -2
data/lib/nice_http.rb
CHANGED
@@ -1,340 +1,348 @@
|
|
1
|
-
require "logger"
|
2
|
-
require "nice_hash"
|
3
|
-
require_relative "nice_http/utils"
|
4
|
-
require_relative "nice_http/manage_request"
|
5
|
-
require_relative "nice_http/manage_response"
|
6
|
-
require_relative "nice_http/http_methods"
|
7
|
-
|
8
|
-
######################################################
|
9
|
-
# Attributes you can access using NiceHttp.the_attribute:
|
10
|
-
# :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port,
|
11
|
-
# :last_request, :last_response, :request_id, :use_mocks, :connections,
|
12
|
-
# :active, :auto_redirect
|
13
|
-
#
|
14
|
-
# @attr [String] host The host to be accessed
|
15
|
-
# @attr [Integer] port The port number
|
16
|
-
# @attr [Boolean] ssl If you use ssl or not
|
17
|
-
# @attr [Hash] headers Contains the headers you will be using on your connection
|
18
|
-
# @attr [Boolean] debug In case true shows all the details of the communication with the host
|
19
|
-
# @attr [String, Symbol] log :fix_file, :no, :screen, :file, "path and file name".
|
20
|
-
# :fix_file will log the communication on nice_http.log. (default).
|
21
|
-
# :no will not generate any logs.
|
22
|
-
# :screen will print the logs on the screen.
|
23
|
-
# :file will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log.
|
24
|
-
# String the path and file name where the logs will be stored.
|
25
|
-
# @attr [String] proxy_host the proxy host to be used
|
26
|
-
# @attr [Integer] proxy_port the proxy port to be used
|
27
|
-
# @attr [String] last_request The last request with all the content sent
|
28
|
-
# @attr [String] last_response Only in case :debug is true, the last response with all the content
|
29
|
-
# @attr [String] request_id If the response includes a requestId, will be stored here
|
30
|
-
# @attr [Boolean] use_mocks If true, in case the request hash includes a :mock_response key, it will be used as the response instead
|
31
|
-
# @attr [Array] connections It will include all the active connections (NiceHttp instances)
|
32
|
-
# @attr [Integer] active Number of active connections
|
33
|
-
# @attr [Boolean] auto_redirect If true, NiceHttp will take care of the auto redirections when required by the responses
|
34
|
-
# @attr [Hash] response Contains the full response hash
|
35
|
-
# @attr [Integer] num_redirects Number of consecutive redirections managed
|
36
|
-
# @attr [Hash] headers The updated headers of the communication
|
37
|
-
# @attr [Hash] cookies Cookies set. The key is the path (String) where cookies are set and the value a Hash with pairs of cookie keys and values, example:
|
38
|
-
# { '/' => { "cfid" => "d95adfas2550255", "amddom.settings" => "doom" } }
|
39
|
-
# @attr [Logger] logger An instance of the Logger class where logs will be stored. You can access on anytime to store specific data, for example:
|
40
|
-
# my_http.logger.info "add this to the log file"
|
41
|
-
# @see https://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
include
|
47
|
-
include
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
message
|
58
|
-
message += "http
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
:
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
@
|
75
|
-
@
|
76
|
-
@
|
77
|
-
@
|
78
|
-
@
|
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
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
#
|
125
|
-
#
|
126
|
-
# @param args [
|
127
|
-
# @example
|
128
|
-
# http = NiceHttp.new(
|
129
|
-
# @
|
130
|
-
#
|
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
|
-
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
#
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
@
|
174
|
-
@
|
175
|
-
@
|
176
|
-
@
|
177
|
-
@
|
178
|
-
@
|
179
|
-
|
180
|
-
@
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
@
|
196
|
-
@
|
197
|
-
@
|
198
|
-
@
|
199
|
-
|
200
|
-
@
|
201
|
-
@
|
202
|
-
@
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
if
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
#only the first connection in the run will be deleting the log file
|
218
|
-
log_filename =
|
219
|
-
mode = "w" unless self.class.log_files.include?(log_filename)
|
220
|
-
f = File.new(log_filename, mode)
|
221
|
-
f.sync = true
|
222
|
-
@logger = Logger.new f
|
223
|
-
elsif @log == :
|
224
|
-
|
225
|
-
|
226
|
-
mode = "w" unless self.class.log_files.include?(log_filename)
|
227
|
-
f = File.new(log_filename, mode)
|
228
|
-
f.sync = true
|
229
|
-
@logger = Logger.new f
|
230
|
-
elsif @log == :
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
@
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
@
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
@http
|
272
|
-
@http.
|
273
|
-
@http.
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
self.
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
end
|
1
|
+
require "logger"
|
2
|
+
require "nice_hash"
|
3
|
+
require_relative "nice_http/utils"
|
4
|
+
require_relative "nice_http/manage_request"
|
5
|
+
require_relative "nice_http/manage_response"
|
6
|
+
require_relative "nice_http/http_methods"
|
7
|
+
|
8
|
+
######################################################
|
9
|
+
# Attributes you can access using NiceHttp.the_attribute:
|
10
|
+
# :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port,
|
11
|
+
# :last_request, :last_response, :request_id, :use_mocks, :connections,
|
12
|
+
# :active, :auto_redirect, :values_for
|
13
|
+
#
|
14
|
+
# @attr [String] host The host to be accessed
|
15
|
+
# @attr [Integer] port The port number
|
16
|
+
# @attr [Boolean] ssl If you use ssl or not
|
17
|
+
# @attr [Hash] headers Contains the headers you will be using on your connection
|
18
|
+
# @attr [Boolean] debug In case true shows all the details of the communication with the host
|
19
|
+
# @attr [String, Symbol] log :fix_file, :no, :screen, :file, "path and file name".
|
20
|
+
# :fix_file will log the communication on nice_http.log. (default).
|
21
|
+
# :no will not generate any logs.
|
22
|
+
# :screen will print the logs on the screen.
|
23
|
+
# :file will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log.
|
24
|
+
# String the path and file name where the logs will be stored.
|
25
|
+
# @attr [String] proxy_host the proxy host to be used
|
26
|
+
# @attr [Integer] proxy_port the proxy port to be used
|
27
|
+
# @attr [String] last_request The last request with all the content sent
|
28
|
+
# @attr [String] last_response Only in case :debug is true, the last response with all the content
|
29
|
+
# @attr [String] request_id If the response includes a requestId, will be stored here
|
30
|
+
# @attr [Boolean] use_mocks If true, in case the request hash includes a :mock_response key, it will be used as the response instead
|
31
|
+
# @attr [Array] connections It will include all the active connections (NiceHttp instances)
|
32
|
+
# @attr [Integer] active Number of active connections
|
33
|
+
# @attr [Boolean] auto_redirect If true, NiceHttp will take care of the auto redirections when required by the responses
|
34
|
+
# @attr [Hash] response Contains the full response hash
|
35
|
+
# @attr [Integer] num_redirects Number of consecutive redirections managed
|
36
|
+
# @attr [Hash] headers The updated headers of the communication
|
37
|
+
# @attr [Hash] cookies Cookies set. The key is the path (String) where cookies are set and the value a Hash with pairs of cookie keys and values, example:
|
38
|
+
# { '/' => { "cfid" => "d95adfas2550255", "amddom.settings" => "doom" } }
|
39
|
+
# @attr [Logger] logger An instance of the Logger class where logs will be stored. You can access on anytime to store specific data, for example:
|
40
|
+
# my_http.logger.info "add this to the log file"
|
41
|
+
# @see https://ruby-doc.org/stdlib-2.5.0/libdoc/logger/rdoc/Logger.html
|
42
|
+
# @attr [Hash] values_for The default values to set on the data in case not specified others
|
43
|
+
######################################################
|
44
|
+
class NiceHttp
|
45
|
+
|
46
|
+
include NiceHttpManageRequest
|
47
|
+
include NiceHttpManageResponse
|
48
|
+
include NiceHttpHttpMethods
|
49
|
+
|
50
|
+
Error = Class.new StandardError
|
51
|
+
|
52
|
+
InfoMissing = Class.new Error do
|
53
|
+
attr_reader :attribute
|
54
|
+
|
55
|
+
def initialize(attribute)
|
56
|
+
@attribute = attribute
|
57
|
+
message = "It was not possible to create the http connection!!!\n"
|
58
|
+
message += "Wrong #{attribute}, remember to supply http:// or https:// in case you specify an url to create the http connection, for example:\n"
|
59
|
+
message += "http = NiceHttp.new('http://example.com')"
|
60
|
+
super message
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class << self
|
65
|
+
attr_accessor :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port,
|
66
|
+
:last_request, :last_response, :request_id, :use_mocks, :connections,
|
67
|
+
:active, :auto_redirect, :log_files, :values_for
|
68
|
+
end
|
69
|
+
|
70
|
+
######################################################
|
71
|
+
# to reset to the original defaults
|
72
|
+
######################################################
|
73
|
+
def self.reset!
|
74
|
+
@host = nil
|
75
|
+
@port = 80
|
76
|
+
@ssl = false
|
77
|
+
@headers = {}
|
78
|
+
@values_for = {}
|
79
|
+
@debug = false
|
80
|
+
@log = :fix_file
|
81
|
+
@proxy_host = nil
|
82
|
+
@proxy_port = nil
|
83
|
+
@last_request = nil
|
84
|
+
@last_response = nil
|
85
|
+
@request_id = ""
|
86
|
+
@use_mocks = false
|
87
|
+
@connections = []
|
88
|
+
@active = 0
|
89
|
+
@auto_redirect = true
|
90
|
+
@log_files = []
|
91
|
+
end
|
92
|
+
reset!
|
93
|
+
|
94
|
+
######################################################
|
95
|
+
# If inheriting from NiceHttp class
|
96
|
+
######################################################
|
97
|
+
def self.inherited(subclass)
|
98
|
+
subclass.reset!
|
99
|
+
end
|
100
|
+
|
101
|
+
attr_reader :host, :port, :ssl, :debug, :log, :proxy_host, :proxy_port, :response, :num_redirects
|
102
|
+
attr_accessor :headers, :cookies, :use_mocks, :auto_redirect, :logger, :values_for
|
103
|
+
|
104
|
+
######################################################
|
105
|
+
# Change the default values for NiceHttp supplying a Hash
|
106
|
+
#
|
107
|
+
# @param par [Hash] keys: :host, :port, :ssl, :headers, :debug, :log, :proxy_host, :proxy_port, :use_mocks, :auto_redirect, :values_for
|
108
|
+
######################################################
|
109
|
+
def self.defaults=(par = {})
|
110
|
+
@host = par[:host] if par.key?(:host)
|
111
|
+
@port = par[:port] if par.key?(:port)
|
112
|
+
@ssl = par[:ssl] if par.key?(:ssl)
|
113
|
+
@headers = par[:headers].dup if par.key?(:headers)
|
114
|
+
@values_for = par[:values_for].dup if par.key?(:values_for)
|
115
|
+
@debug = par[:debug] if par.key?(:debug)
|
116
|
+
@log = par[:log] if par.key?(:log)
|
117
|
+
@proxy_host = par[:proxy_host] if par.key?(:proxy_host)
|
118
|
+
@proxy_port = par[:proxy_port] if par.key?(:proxy_port)
|
119
|
+
@use_mocks = par[:use_mocks] if par.key?(:use_mocks)
|
120
|
+
@auto_redirect = par[:auto_redirect] if par.key?(:auto_redirect)
|
121
|
+
end
|
122
|
+
|
123
|
+
######################################################
|
124
|
+
# Creates a new http connection.
|
125
|
+
#
|
126
|
+
# @param args [] If no parameter supplied, by default will access how is setup on defaults
|
127
|
+
# @example
|
128
|
+
# http = NiceHttp.new()
|
129
|
+
# @param args [String]. The url to create the connection.
|
130
|
+
# @example
|
131
|
+
# http = NiceHttp.new("https://www.example.com")
|
132
|
+
# @example
|
133
|
+
# http = NiceHttp.new("example.com:8999")
|
134
|
+
# @example
|
135
|
+
# http = NiceHttp.new("localhost:8322")
|
136
|
+
# @param args [Hash] containing these possible keys:
|
137
|
+
#
|
138
|
+
# host -- example.com. (default blank screen)
|
139
|
+
#
|
140
|
+
# port -- port for the connection. 80 (default)
|
141
|
+
#
|
142
|
+
# ssl -- true, false (default)
|
143
|
+
#
|
144
|
+
# headers -- hash with the headers
|
145
|
+
#
|
146
|
+
# values_for -- hash with the values_for
|
147
|
+
#
|
148
|
+
# debug -- true, false (default)
|
149
|
+
#
|
150
|
+
# log -- :no, :screen, :file, :fix_file (default).
|
151
|
+
#
|
152
|
+
# A string with a path can be supplied.
|
153
|
+
#
|
154
|
+
# If :fix_file: nice_http.log
|
155
|
+
#
|
156
|
+
# In case :file it will be generated a log file with name: nice_http_YY-mm-dd-HHMMSS.log
|
157
|
+
#
|
158
|
+
# proxy_host
|
159
|
+
#
|
160
|
+
# proxy_port
|
161
|
+
# @example
|
162
|
+
# http2 = NiceHttp.new( host: "reqres.in", port: 443, ssl: true )
|
163
|
+
# @example
|
164
|
+
# my_server = {host: "example.com",
|
165
|
+
# port: 80,
|
166
|
+
# headers: {"api-key": "zdDDdjkck"}
|
167
|
+
# }
|
168
|
+
# http3 = NiceHttp.new my_server
|
169
|
+
######################################################
|
170
|
+
def initialize(args = {})
|
171
|
+
require "net/http"
|
172
|
+
require "net/https"
|
173
|
+
@host = self.class.host
|
174
|
+
@port = self.class.port
|
175
|
+
@prepath = ''
|
176
|
+
@ssl = self.class.ssl
|
177
|
+
@headers = self.class.headers.dup
|
178
|
+
@values_for = self.class.values_for.dup
|
179
|
+
@debug = self.class.debug
|
180
|
+
@log = self.class.log
|
181
|
+
@proxy_host = self.class.proxy_host
|
182
|
+
@proxy_port = self.class.proxy_port
|
183
|
+
@use_mocks = self.class.use_mocks
|
184
|
+
@auto_redirect = false #set it up at the end of initialize
|
185
|
+
auto_redirect = self.class.auto_redirect
|
186
|
+
@num_redirects = 0
|
187
|
+
|
188
|
+
#todo: set only the cookies for the current domain
|
189
|
+
#key: path, value: hash with key is the name of the cookie and value the value
|
190
|
+
# we set the default value for non existing keys to empty Hash {} so in case of merge there is no problem
|
191
|
+
@cookies = Hash.new { |h, k| h[k] = {} }
|
192
|
+
|
193
|
+
if args.is_a?(String)
|
194
|
+
uri = URI.parse(args)
|
195
|
+
@host = uri.host unless uri.host.nil?
|
196
|
+
@port = uri.port unless uri.port.nil?
|
197
|
+
@ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
|
198
|
+
@prepath = uri.path unless uri.path=='/'
|
199
|
+
elsif args.is_a?(Hash) && !args.keys.empty?
|
200
|
+
@host = args[:host] if args.keys.include?(:host)
|
201
|
+
@port = args[:port] if args.keys.include?(:port)
|
202
|
+
@ssl = args[:ssl] if args.keys.include?(:ssl)
|
203
|
+
@headers = args[:headers].dup if args.keys.include?(:headers)
|
204
|
+
@values_for = args[:values_for].dup if args.keys.include?(:values_for)
|
205
|
+
@debug = args[:debug] if args.keys.include?(:debug)
|
206
|
+
@log = args[:log] if args.keys.include?(:log)
|
207
|
+
@proxy_host = args[:proxy_host] if args.keys.include?(:proxy_host)
|
208
|
+
@proxy_port = args[:proxy_port] if args.keys.include?(:proxy_port)
|
209
|
+
@use_mocks = args[:use_mocks] if args.keys.include?(:use_mocks)
|
210
|
+
auto_redirect = args[:auto_redirect] if args.keys.include?(:auto_redirect)
|
211
|
+
end
|
212
|
+
|
213
|
+
begin
|
214
|
+
mode = "a"
|
215
|
+
log_filename =''
|
216
|
+
if @log.kind_of?(String)
|
217
|
+
#only the first connection in the run will be deleting the log file
|
218
|
+
log_filename = @log
|
219
|
+
mode = "w" unless self.class.log_files.include?(log_filename)
|
220
|
+
f = File.new(log_filename, mode)
|
221
|
+
f.sync = true
|
222
|
+
@logger = Logger.new f
|
223
|
+
elsif @log == :fix_file
|
224
|
+
#only the first connection in the run will be deleting the log file
|
225
|
+
log_filename = 'nice_http.log'
|
226
|
+
mode = "w" unless self.class.log_files.include?(log_filename)
|
227
|
+
f = File.new(log_filename, mode)
|
228
|
+
f.sync = true
|
229
|
+
@logger = Logger.new f
|
230
|
+
elsif @log == :file
|
231
|
+
log_filename = "nice_http_#{Time.now.strftime("%Y-%m-%d-%H%M%S")}.log"
|
232
|
+
#only the first connection in the run will be deleting the log file
|
233
|
+
mode = "w" unless self.class.log_files.include?(log_filename)
|
234
|
+
f = File.new(log_filename, mode)
|
235
|
+
f.sync = true
|
236
|
+
@logger = Logger.new f
|
237
|
+
elsif @log == :screen
|
238
|
+
@logger = Logger.new STDOUT
|
239
|
+
elsif @log == :no
|
240
|
+
@logger = Logger.new nil
|
241
|
+
else
|
242
|
+
raise InfoMissing, :log
|
243
|
+
end
|
244
|
+
@logger.level = Logger::INFO
|
245
|
+
self.class.log_files << log_filename if mode == 'w'
|
246
|
+
rescue Exception => stack
|
247
|
+
@logger = Logger.new nil
|
248
|
+
raise InfoMissing, :log
|
249
|
+
end
|
250
|
+
|
251
|
+
|
252
|
+
if @host.to_s != "" and (@host.start_with?("http:") or @host.start_with?("https:"))
|
253
|
+
uri = URI.parse(@host)
|
254
|
+
@host = uri.host unless uri.host.nil?
|
255
|
+
@port = uri.port unless uri.port.nil?
|
256
|
+
@ssl = true if !uri.scheme.nil? && (uri.scheme == "https")
|
257
|
+
@prepath = uri.path unless uri.path=='/'
|
258
|
+
end
|
259
|
+
|
260
|
+
raise InfoMissing, :port if @port.to_s == ""
|
261
|
+
raise InfoMissing, :host if @host.to_s == ""
|
262
|
+
raise InfoMissing, :ssl unless @ssl.is_a?(TrueClass) or @ssl.is_a?(FalseClass)
|
263
|
+
raise InfoMissing, :debug unless @debug.is_a?(TrueClass) or @debug.is_a?(FalseClass)
|
264
|
+
raise InfoMissing, :auto_redirect unless auto_redirect.is_a?(TrueClass) or auto_redirect.is_a?(FalseClass)
|
265
|
+
raise InfoMissing, :use_mocks unless @use_mocks.is_a?(TrueClass) or @use_mocks.is_a?(FalseClass)
|
266
|
+
raise InfoMissing, :headers unless @headers.is_a?(Hash)
|
267
|
+
raise InfoMissing, :values_for unless @values_for.is_a?(Hash)
|
268
|
+
|
269
|
+
begin
|
270
|
+
if !@proxy_host.nil? && !@proxy_port.nil?
|
271
|
+
@http = Net::HTTP::Proxy(@proxy_host, @proxy_port).new(@host, @port)
|
272
|
+
@http.use_ssl = @ssl
|
273
|
+
@http.set_debug_output $stderr if @debug
|
274
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
275
|
+
@http.start
|
276
|
+
else
|
277
|
+
@http = Net::HTTP.new(@host, @port)
|
278
|
+
@http.use_ssl = @ssl
|
279
|
+
@http.set_debug_output $stderr if @debug
|
280
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
281
|
+
@http.start
|
282
|
+
end
|
283
|
+
|
284
|
+
@message_server = "(#{self.object_id}):"
|
285
|
+
|
286
|
+
log_message = "(#{self.object_id}): Http connection created. host:#{@host}, port:#{@port}, ssl:#{@ssl}, mode:#{@mode}, proxy_host: #{@proxy_host.to_s()}, proxy_port: #{@proxy_port.to_s()} "
|
287
|
+
|
288
|
+
@logger.info(log_message)
|
289
|
+
@message_server += " Http connection: "
|
290
|
+
if @ssl
|
291
|
+
@message_server += "https://"
|
292
|
+
else
|
293
|
+
@message_server += "http://"
|
294
|
+
end
|
295
|
+
@message_server += "#{@host}:#{@port}"
|
296
|
+
if @proxy_host.to_s != ""
|
297
|
+
@message_server += " proxy:#{@proxy_host}:#{@proxy_port}"
|
298
|
+
end
|
299
|
+
@auto_redirect = auto_redirect
|
300
|
+
|
301
|
+
self.class.active += 1
|
302
|
+
self.class.connections.push(self)
|
303
|
+
rescue Exception => stack
|
304
|
+
puts stack
|
305
|
+
@logger.fatal stack
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
######################################################
|
310
|
+
# Close HTTP connection
|
311
|
+
######################################################
|
312
|
+
def close
|
313
|
+
begin
|
314
|
+
pos = 0
|
315
|
+
found = false
|
316
|
+
self.class.connections.each { |conn|
|
317
|
+
if conn.object_id == self.object_id
|
318
|
+
found = true
|
319
|
+
break
|
320
|
+
else
|
321
|
+
pos += 1
|
322
|
+
end
|
323
|
+
}
|
324
|
+
if found
|
325
|
+
self.class.connections.delete_at(pos)
|
326
|
+
end
|
327
|
+
|
328
|
+
unless @closed
|
329
|
+
if !@http.nil?
|
330
|
+
@http.finish()
|
331
|
+
@http = nil
|
332
|
+
@logger.info "the HTTP connection was closed: #{@message_server}"
|
333
|
+
else
|
334
|
+
@http = nil
|
335
|
+
@logger.fatal "It was not possible to close the HTTP connection: #{@message_server}"
|
336
|
+
end
|
337
|
+
@closed = true
|
338
|
+
else
|
339
|
+
@logger.warn "It was not possible to close the HTTP connection, already closed: #{@message_server}"
|
340
|
+
end
|
341
|
+
rescue Exception => stack
|
342
|
+
@logger.fatal stack
|
343
|
+
end
|
344
|
+
self.class.active -= 1
|
345
|
+
end
|
346
|
+
|
347
|
+
private :manage_request, :manage_response
|
348
|
+
end
|