nice_http 1.5.2 → 1.6.0

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