nice_http 1.6.0 → 1.6.2

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.
@@ -1,243 +1,243 @@
1
- module NiceHttpManageRequest
2
-
3
- ######################################################
4
- # private method to manage Request
5
- # input:
6
- # 3 args: path, data, headers
7
- # 1 arg: Hash containg at least keys :path and :data
8
- # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
9
- # output:
10
- # path, data, headers
11
- ######################################################
12
- def manage_request(*arguments)
13
- require "json"
14
- begin
15
- content_type_included = false
16
- path = ""
17
- data = ""
18
-
19
- @response = Hash.new()
20
- headers_t = @headers.dup()
21
- cookies_to_set_str = ""
22
- if arguments.size == 3
23
- path = arguments[0]
24
- elsif arguments.size == 1 and arguments[0].kind_of?(Hash)
25
- path = arguments[0][:path]
26
- elsif arguments.size == 1 and arguments[0].kind_of?(String)
27
- path = arguments[0].to_s()
28
- end
29
- path = (@prepath + path).gsub('//','/') unless path.nil? or path.start_with?('http:') or path.start_with?('https:')
30
- @cookies.each { |cookie_path, cookies_hash|
31
- cookie_path = "" if cookie_path == "/"
32
- path_to_check = path
33
- if path == "/" or path[-1] != "/"
34
- path_to_check += "/"
35
- end
36
- if path_to_check.scan(/^#{cookie_path}\//).size > 0
37
- cookies_hash.each { |key, value|
38
- cookies_to_set_str += "#{key}=#{value}; "
39
- }
40
- end
41
- }
42
- headers_t["Cookie"] = cookies_to_set_str
43
-
44
- method_s = caller[0].to_s().scan(/:in `(.*)'/).join
45
-
46
- if arguments.size == 3
47
- data = arguments[1]
48
- if arguments[2].kind_of?(Hash)
49
- headers_t.merge!(arguments[2])
50
- end
51
- elsif arguments.size == 1 and arguments[0].kind_of?(Hash)
52
- if arguments[0][:data].nil?
53
- if arguments[0].keys.include?(:data)
54
- data = ""
55
- elsif arguments[0].keys.include?(:data_examples) and
56
- arguments[0][:data_examples].kind_of?(Array)
57
- data = arguments[0][:data_examples][0] #the first example by default
58
- else
59
- data = ""
60
- end
61
- else
62
- data = arguments[0][:data]
63
- end
64
- if arguments[0].include?(:headers)
65
- headers_t.merge!(arguments[0][:headers])
66
- end
67
-
68
- if headers_t["Content-Type"].to_s() == "" and headers_t["content-type"].to_s() == "" and
69
- headers_t[:"content-type"].to_s() == "" and headers_t[:"Content-Type"].to_s() == ""
70
- content_type_included = false
71
- elsif headers_t["content-type"].to_s() != ""
72
- content_type_included = true
73
- headers_t["Content-Type"] = headers_t["content-type"]
74
- elsif headers_t[:"content-type"].to_s() != ""
75
- content_type_included = true
76
- headers_t["Content-Type"] = headers_t[:"content-type"]
77
- headers_t.delete(:"content-type")
78
- elsif headers_t[:"Content-Type"].to_s() != ""
79
- content_type_included = true
80
- headers_t["Content-Type"] = headers_t[:"Content-Type"]
81
- headers_t.delete(:"Content-Type")
82
- elsif headers_t["Content-Type"].to_s() != ""
83
- content_type_included = true
84
- end
85
- if !content_type_included and data.kind_of?(Hash)
86
- headers_t["Content-Type"] = "application/json"
87
- content_type_included = true
88
- end
89
- # to be backwards compatible since before was :values
90
- if arguments[0].include?(:values) and !arguments[0].include?(:values_for)
91
- arguments[0][:values_for] = arguments[0][:values]
92
- end
93
-
94
- if @values_for.size>0
95
- if arguments[0][:values_for].nil?
96
- arguments[0][:values_for] = @values_for.dup
97
- else
98
- arguments[0][:values_for] = @values_for.merge(arguments[0][:values_for])
99
- end
100
- end
101
-
102
- if content_type_included and (!headers_t["Content-Type"][/text\/xml/].nil? or
103
- !headers_t["Content-Type"]["application/soap+xml"].nil? or
104
- !headers_t["Content-Type"][/application\/jxml/].nil?)
105
- if arguments[0].include?(:values_for)
106
- arguments[0][:values_for].each { |key, value|
107
- data = NiceHttpUtils.set_value_xml_tag(key.to_s(), data, value.to_s(), true)
108
- }
109
- end
110
- elsif content_type_included and !headers_t["Content-Type"][/application\/json/].nil? and data.to_s() != ""
111
- require "json"
112
- if data.kind_of?(String)
113
- if arguments[0].include?(:values_for)
114
- arguments[0][:values_for].each { |key, value|
115
- data.gsub!(/"(#{key})":\s*"([^"]*)"/,'"\1": "'+value+'"') # "key":"value"
116
- data.gsub!(/(#{key}):\s*"([^"]*)"/,'\1: "'+value+'"') # key:"value"
117
- data.gsub!(/(#{key}):\s*'([^']*)'/,'\1: \''+value+"'") # key:'value'
118
- data.gsub!(/"(#{key})":\s*(\w+)/,'"\1": '+value) # "key":456
119
- data.gsub!(/(#{key}):\s*(\w+)/,'\1: '+value) # key:456
120
- }
121
- end
122
- elsif data.kind_of?(Hash)
123
- data_n = Hash.new()
124
- data.each { |key, value|
125
- data_n[key.to_s()] = value
126
- }
127
- if arguments[0].include?(:values_for)
128
- #req[:values_for][:loginName] or req[:values_for]["loginName"]
129
- new_values_hash = Hash.new()
130
- arguments[0][:values_for].each { |kv, vv|
131
- if data_n.keys.include?(kv.to_s())
132
- new_values_hash[kv.to_s()] = vv
133
- end
134
- }
135
- data_n.merge!(new_values_hash)
136
- end
137
- data = data_n.to_json()
138
- elsif data.kind_of?(Array)
139
- data_arr = Array.new()
140
- data.each_with_index { |row, indx|
141
- unless row.kind_of?(Hash)
142
- @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
143
- return :error, :error, :error
144
- end
145
- data_n = Hash.new()
146
- row.each { |key, value|
147
- data_n[key.to_s()] = value
148
- }
149
- if arguments[0].include?(:values_for)
150
- #req[:values_for][:loginName] or req[:values_for]["loginName"]
151
- new_values_hash = Hash.new()
152
- if arguments[0][:values_for].kind_of?(Hash) #values[:mykey][3]
153
- arguments[0][:values_for].each { |kv, vv|
154
- if data_n.keys.include?(kv.to_s()) and !vv[indx].nil?
155
- new_values_hash[kv.to_s()] = vv[indx]
156
- end
157
- }
158
- elsif arguments[0][:values_for].kind_of?(Array) #values[5][:mykey]
159
- if !arguments[0][:values_for][indx].nil?
160
- arguments[0][:values_for][indx].each { |kv, vv|
161
- if data_n.keys.include?(kv.to_s())
162
- new_values_hash[kv.to_s()] = vv
163
- end
164
- }
165
- end
166
- else
167
- @logger.fatal("Wrong format on request application/json when supplying values, the data is an array of Hashes but the values supplied are not")
168
- return :error, :error, :error
169
- end
170
- data_n.merge!(new_values_hash)
171
- end
172
- data_arr.push(data_n)
173
- }
174
- data = data_arr.to_json()
175
- else
176
- @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
177
- return :error, :error, :error
178
- end
179
- elsif content_type_included and arguments[0].include?(:values_for)
180
- if arguments[0][:values_for].kind_of?(Hash) and arguments[0][:values_for].keys.size > 0
181
- if !headers_t.nil? and headers_t.kind_of?(Hash) and headers_t["Content-Type"] != "application/x-www-form-urlencoded" and headers_t["content-type"] != "application/x-www-form-urlencoded"
182
- @logger.warn(":values_for key given without a valid content-type or data for request. No values modified on the request")
183
- end
184
- end
185
- end
186
- elsif arguments.size == 1 and arguments[0].kind_of?(String)
187
- #path=arguments[0].to_s()
188
- data = ""
189
- else
190
- @logger.fatal("Invalid number of arguments or wrong arguments in #{method_s}")
191
- return :error, :error, :error
192
- end
193
- if headers_t.keys.include?("Content-Type") and !headers_t["Content-Type"]["multipart/form-data"].nil? and headers_t["Content-Type"] != ["multipart/form-data"] #only for the case raw multipart request
194
- encoding = "UTF-8"
195
- data_s = ""
196
- else
197
- encoding = data.to_s().scan(/encoding='(.*)'/i).join
198
- if encoding.to_s() == ""
199
- encoding = data.to_s().scan(/charset='(.*)'/i).join
200
- end
201
- if encoding.to_s() == "" and headers_t.include?("Content-Type")
202
- encoding = headers_t["Content-Type"].scan(/charset='?(.*)'?/i).join
203
- if encoding.to_s() == ""
204
- encoding = headers_t["Content-Type"].scan(/encoding='?(.*)'?/i).join
205
- end
206
- end
207
-
208
- begin
209
- data_s = JSON.pretty_generate(JSON.parse(data))
210
- rescue
211
- data_s = data
212
- end
213
- data_s = data_s.to_s().gsub("<", "&lt;")
214
- end
215
- if headers_t.keys.include?("Accept-Encoding")
216
- headers_t["Accept-Encoding"].gsub!("gzip", "") #removed so the response is in plain text
217
- end
218
-
219
- headers_ts = ""
220
- headers_t.each { |key, val| headers_ts += key.to_s + ":" + val.to_s() + ", " }
221
- message = "#{method_s} REQUEST: \npath= " + path.to_s() + "\n"
222
- message += "headers= " + headers_ts.to_s() + "\n"
223
- message += "data= " + data_s.to_s() + "\n"
224
- message = @message_server + "\n" + message
225
- if path.to_s().scan(/^https?:\/\//).size > 0 and path.to_s().scan(/^https?:\/\/#{@host}/).size == 0
226
- # the path is for another server than the current
227
- else
228
- self.class.last_request = message
229
- @logger.info(message)
230
- end
231
-
232
- if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
233
- data = data.to_s().encode(encoding, "UTF-8")
234
- end
235
- return path, data, headers_t
236
- rescue Exception => stack
237
- @logger.fatal(stack)
238
- @logger.fatal("manage_request Error on method #{method_s} . path:#{path.to_s()}. data:#{data.to_s()}. headers:#{headers_t.to_s()}")
239
- return :error
240
- end
241
- end
242
-
1
+ module NiceHttpManageRequest
2
+
3
+ ######################################################
4
+ # private method to manage Request
5
+ # input:
6
+ # 3 args: path, data, headers
7
+ # 1 arg: Hash containg at least keys :path and :data
8
+ # In case :data not supplied and :data_examples array supplied, it will be taken the first example as :data.
9
+ # output:
10
+ # path, data, headers
11
+ ######################################################
12
+ def manage_request(*arguments)
13
+ require "json"
14
+ begin
15
+ content_type_included = false
16
+ path = ""
17
+ data = ""
18
+
19
+ @response = Hash.new()
20
+ headers_t = @headers.dup()
21
+ cookies_to_set_str = ""
22
+ if arguments.size == 3
23
+ path = arguments[0]
24
+ elsif arguments.size == 1 and arguments[0].kind_of?(Hash)
25
+ path = arguments[0][:path]
26
+ elsif arguments.size == 1 and arguments[0].kind_of?(String)
27
+ path = arguments[0].to_s()
28
+ end
29
+ path = (@prepath + path).gsub('//','/') unless path.nil? or path.start_with?('http:') or path.start_with?('https:')
30
+ @cookies.each { |cookie_path, cookies_hash|
31
+ cookie_path = "" if cookie_path == "/"
32
+ path_to_check = path
33
+ if path == "/" or path[-1] != "/"
34
+ path_to_check += "/"
35
+ end
36
+ if path_to_check.scan(/^#{cookie_path}\//).size > 0
37
+ cookies_hash.each { |key, value|
38
+ cookies_to_set_str += "#{key}=#{value}; "
39
+ }
40
+ end
41
+ }
42
+ headers_t["Cookie"] = cookies_to_set_str
43
+
44
+ method_s = caller[0].to_s().scan(/:in `(.*)'/).join
45
+
46
+ if arguments.size == 3
47
+ data = arguments[1]
48
+ if arguments[2].kind_of?(Hash)
49
+ headers_t.merge!(arguments[2])
50
+ end
51
+ elsif arguments.size == 1 and arguments[0].kind_of?(Hash)
52
+ if arguments[0][:data].nil?
53
+ if arguments[0].keys.include?(:data)
54
+ data = ""
55
+ elsif arguments[0].keys.include?(:data_examples) and
56
+ arguments[0][:data_examples].kind_of?(Array)
57
+ data = arguments[0][:data_examples][0] #the first example by default
58
+ else
59
+ data = ""
60
+ end
61
+ else
62
+ data = arguments[0][:data]
63
+ end
64
+ if arguments[0].include?(:headers)
65
+ headers_t.merge!(arguments[0][:headers])
66
+ end
67
+
68
+ if headers_t["Content-Type"].to_s() == "" and headers_t["content-type"].to_s() == "" and
69
+ headers_t[:"content-type"].to_s() == "" and headers_t[:"Content-Type"].to_s() == ""
70
+ content_type_included = false
71
+ elsif headers_t["content-type"].to_s() != ""
72
+ content_type_included = true
73
+ headers_t["Content-Type"] = headers_t["content-type"]
74
+ elsif headers_t[:"content-type"].to_s() != ""
75
+ content_type_included = true
76
+ headers_t["Content-Type"] = headers_t[:"content-type"]
77
+ headers_t.delete(:"content-type")
78
+ elsif headers_t[:"Content-Type"].to_s() != ""
79
+ content_type_included = true
80
+ headers_t["Content-Type"] = headers_t[:"Content-Type"]
81
+ headers_t.delete(:"Content-Type")
82
+ elsif headers_t["Content-Type"].to_s() != ""
83
+ content_type_included = true
84
+ end
85
+ if !content_type_included and data.kind_of?(Hash)
86
+ headers_t["Content-Type"] = "application/json"
87
+ content_type_included = true
88
+ end
89
+ # to be backwards compatible since before was :values
90
+ if arguments[0].include?(:values) and !arguments[0].include?(:values_for)
91
+ arguments[0][:values_for] = arguments[0][:values]
92
+ end
93
+
94
+ if @values_for.size>0
95
+ if arguments[0][:values_for].nil?
96
+ arguments[0][:values_for] = @values_for.dup
97
+ else
98
+ arguments[0][:values_for] = @values_for.merge(arguments[0][:values_for])
99
+ end
100
+ end
101
+
102
+ if content_type_included and (!headers_t["Content-Type"][/text\/xml/].nil? or
103
+ !headers_t["Content-Type"]["application/soap+xml"].nil? or
104
+ !headers_t["Content-Type"][/application\/jxml/].nil?)
105
+ if arguments[0].include?(:values_for)
106
+ arguments[0][:values_for].each { |key, value|
107
+ data = NiceHttpUtils.set_value_xml_tag(key.to_s(), data, value.to_s(), true)
108
+ }
109
+ end
110
+ elsif content_type_included and !headers_t["Content-Type"][/application\/json/].nil? and data.to_s() != ""
111
+ require "json"
112
+ if data.kind_of?(String)
113
+ if arguments[0].include?(:values_for)
114
+ arguments[0][:values_for].each { |key, value|
115
+ data.gsub!(/"(#{key})":\s*"([^"]*)"/,'"\1": "'+value+'"') # "key":"value"
116
+ data.gsub!(/(#{key}):\s*"([^"]*)"/,'\1: "'+value+'"') # key:"value"
117
+ data.gsub!(/(#{key}):\s*'([^']*)'/,'\1: \''+value+"'") # key:'value'
118
+ data.gsub!(/"(#{key})":\s*(\w+)/,'"\1": '+value) # "key":456
119
+ data.gsub!(/(#{key}):\s*(\w+)/,'\1: '+value) # key:456
120
+ }
121
+ end
122
+ elsif data.kind_of?(Hash)
123
+ data_n = Hash.new()
124
+ data.each { |key, value|
125
+ data_n[key.to_s()] = value
126
+ }
127
+ if arguments[0].include?(:values_for)
128
+ #req[:values_for][:loginName] or req[:values_for]["loginName"]
129
+ new_values_hash = Hash.new()
130
+ arguments[0][:values_for].each { |kv, vv|
131
+ if data_n.keys.include?(kv.to_s())
132
+ new_values_hash[kv.to_s()] = vv
133
+ end
134
+ }
135
+ data_n.merge!(new_values_hash)
136
+ end
137
+ data = data_n.to_json()
138
+ elsif data.kind_of?(Array)
139
+ data_arr = Array.new()
140
+ data.each_with_index { |row, indx|
141
+ unless row.kind_of?(Hash)
142
+ @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
143
+ return :error, :error, :error
144
+ end
145
+ data_n = Hash.new()
146
+ row.each { |key, value|
147
+ data_n[key.to_s()] = value
148
+ }
149
+ if arguments[0].include?(:values_for)
150
+ #req[:values_for][:loginName] or req[:values_for]["loginName"]
151
+ new_values_hash = Hash.new()
152
+ if arguments[0][:values_for].kind_of?(Hash) #values[:mykey][3]
153
+ arguments[0][:values_for].each { |kv, vv|
154
+ if data_n.keys.include?(kv.to_s()) and !vv[indx].nil?
155
+ new_values_hash[kv.to_s()] = vv[indx]
156
+ end
157
+ }
158
+ elsif arguments[0][:values_for].kind_of?(Array) #values[5][:mykey]
159
+ if !arguments[0][:values_for][indx].nil?
160
+ arguments[0][:values_for][indx].each { |kv, vv|
161
+ if data_n.keys.include?(kv.to_s())
162
+ new_values_hash[kv.to_s()] = vv
163
+ end
164
+ }
165
+ end
166
+ else
167
+ @logger.fatal("Wrong format on request application/json when supplying values, the data is an array of Hashes but the values supplied are not")
168
+ return :error, :error, :error
169
+ end
170
+ data_n.merge!(new_values_hash)
171
+ end
172
+ data_arr.push(data_n)
173
+ }
174
+ data = data_arr.to_json()
175
+ else
176
+ @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
177
+ return :error, :error, :error
178
+ end
179
+ elsif content_type_included and arguments[0].include?(:values_for)
180
+ if arguments[0][:values_for].kind_of?(Hash) and arguments[0][:values_for].keys.size > 0
181
+ if !headers_t.nil? and headers_t.kind_of?(Hash) and headers_t["Content-Type"] != "application/x-www-form-urlencoded" and headers_t["content-type"] != "application/x-www-form-urlencoded"
182
+ @logger.warn(":values_for key given without a valid content-type or data for request. No values modified on the request")
183
+ end
184
+ end
185
+ end
186
+ elsif arguments.size == 1 and arguments[0].kind_of?(String)
187
+ #path=arguments[0].to_s()
188
+ data = ""
189
+ else
190
+ @logger.fatal("Invalid number of arguments or wrong arguments in #{method_s}")
191
+ return :error, :error, :error
192
+ end
193
+ if headers_t.keys.include?("Content-Type") and !headers_t["Content-Type"]["multipart/form-data"].nil? and headers_t["Content-Type"] != ["multipart/form-data"] #only for the case raw multipart request
194
+ encoding = "UTF-8"
195
+ data_s = ""
196
+ else
197
+ encoding = data.to_s().scan(/encoding='(.*)'/i).join
198
+ if encoding.to_s() == ""
199
+ encoding = data.to_s().scan(/charset='(.*)'/i).join
200
+ end
201
+ if encoding.to_s() == "" and headers_t.include?("Content-Type")
202
+ encoding = headers_t["Content-Type"].scan(/charset='?(.*)'?/i).join
203
+ if encoding.to_s() == ""
204
+ encoding = headers_t["Content-Type"].scan(/encoding='?(.*)'?/i).join
205
+ end
206
+ end
207
+
208
+ begin
209
+ data_s = JSON.pretty_generate(JSON.parse(data))
210
+ rescue
211
+ data_s = data
212
+ end
213
+ data_s = data_s.to_s().gsub("<", "&lt;")
214
+ end
215
+ if headers_t.keys.include?("Accept-Encoding")
216
+ headers_t["Accept-Encoding"].gsub!("gzip", "") #removed so the response is in plain text
217
+ end
218
+
219
+ headers_ts = ""
220
+ headers_t.each { |key, val| headers_ts += key.to_s + ":" + val.to_s() + ", " }
221
+ message = "#{method_s} REQUEST: \npath= " + path.to_s() + "\n"
222
+ message += "headers= " + headers_ts.to_s() + "\n"
223
+ message += "data= " + data_s.to_s() + "\n"
224
+ message = @message_server + "\n" + message
225
+ if path.to_s().scan(/^https?:\/\//).size > 0 and path.to_s().scan(/^https?:\/\/#{@host}/).size == 0
226
+ # the path is for another server than the current
227
+ else
228
+ self.class.last_request = message
229
+ @logger.info(message)
230
+ end
231
+
232
+ if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
233
+ data = data.to_s().encode(encoding, "UTF-8")
234
+ end
235
+ return path, data, headers_t
236
+ rescue Exception => stack
237
+ @logger.fatal(stack)
238
+ @logger.fatal("manage_request Error on method #{method_s} . path:#{path.to_s()}. data:#{data.to_s()}. headers:#{headers_t.to_s()}")
239
+ return :error
240
+ end
241
+ end
242
+
243
243
  end