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