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.
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
- # 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
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