nice_http 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,234 +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
- if content_type_included and (!headers_t["Content-Type"][/text\/xml/].nil? or
94
- !headers_t["Content-Type"]["application/soap+xml"].nil? or
95
- !headers_t["Content-Type"][/application\/jxml/].nil?)
96
- if arguments[0].include?(:values_for)
97
- arguments[0][:values_for].each { |key, value|
98
- data = NiceHttpUtils.set_value_xml_tag(key.to_s(), data, value.to_s(), true)
99
- }
100
- end
101
- elsif content_type_included and !headers_t["Content-Type"][/application\/json/].nil? and data.to_s() != ""
102
- require "json"
103
- if data.kind_of?(String)
104
- if arguments[0].include?(:values_for)
105
- arguments[0][:values_for].each { |key, value|
106
- data.gsub!(/"(#{key})":\s*"([^"]*)"/,'"\1": "'+value+'"') # "key":"value"
107
- data.gsub!(/(#{key}):\s*"([^"]*)"/,'\1: "'+value+'"') # key:"value"
108
- data.gsub!(/(#{key}):\s*'([^']*)'/,'\1: \''+value+"'") # key:'value'
109
- data.gsub!(/"(#{key})":\s*(\w+)/,'"\1": '+value) # "key":456
110
- data.gsub!(/(#{key}):\s*(\w+)/,'\1: '+value) # key:456
111
- }
112
- end
113
- elsif data.kind_of?(Hash)
114
- data_n = Hash.new()
115
- data.each { |key, value|
116
- data_n[key.to_s()] = value
117
- }
118
- if arguments[0].include?(:values_for)
119
- #req[:values_for][:loginName] or req[:values_for]["loginName"]
120
- new_values_hash = Hash.new()
121
- arguments[0][:values_for].each { |kv, vv|
122
- if data_n.keys.include?(kv.to_s())
123
- new_values_hash[kv.to_s()] = vv
124
- end
125
- }
126
- data_n.merge!(new_values_hash)
127
- end
128
- data = data_n.to_json()
129
- elsif data.kind_of?(Array)
130
- data_arr = Array.new()
131
- data.each_with_index { |row, indx|
132
- unless row.kind_of?(Hash)
133
- @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
134
- return :error, :error, :error
135
- end
136
- data_n = Hash.new()
137
- row.each { |key, value|
138
- data_n[key.to_s()] = value
139
- }
140
- if arguments[0].include?(:values_for)
141
- #req[:values_for][:loginName] or req[:values_for]["loginName"]
142
- new_values_hash = Hash.new()
143
- if arguments[0][:values_for].kind_of?(Hash) #values[:mykey][3]
144
- arguments[0][:values_for].each { |kv, vv|
145
- if data_n.keys.include?(kv.to_s()) and !vv[indx].nil?
146
- new_values_hash[kv.to_s()] = vv[indx]
147
- end
148
- }
149
- elsif arguments[0][:values_for].kind_of?(Array) #values[5][:mykey]
150
- if !arguments[0][:values_for][indx].nil?
151
- arguments[0][:values_for][indx].each { |kv, vv|
152
- if data_n.keys.include?(kv.to_s())
153
- new_values_hash[kv.to_s()] = vv
154
- end
155
- }
156
- end
157
- else
158
- @logger.fatal("Wrong format on request application/json when supplying values, the data is an array of Hashes but the values supplied are not")
159
- return :error, :error, :error
160
- end
161
- data_n.merge!(new_values_hash)
162
- end
163
- data_arr.push(data_n)
164
- }
165
- data = data_arr.to_json()
166
- else
167
- @logger.fatal("Wrong format on request application/json, be sure is a Hash, Array of Hashes or JSON string")
168
- return :error, :error, :error
169
- end
170
- elsif content_type_included and arguments[0].include?(:values_for)
171
- if arguments[0][:values_for].kind_of?(Hash) and arguments[0][:values_for].keys.size > 0
172
- 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"
173
- @logger.warn(":values_for key given without a valid content-type or data for request. No values modified on the request")
174
- end
175
- end
176
- end
177
- elsif arguments.size == 1 and arguments[0].kind_of?(String)
178
- #path=arguments[0].to_s()
179
- data = ""
180
- else
181
- @logger.fatal("Invalid number of arguments or wrong arguments in #{method_s}")
182
- return :error, :error, :error
183
- end
184
- 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
185
- encoding = "UTF-8"
186
- data_s = ""
187
- else
188
- encoding = data.to_s().scan(/encoding='(.*)'/i).join
189
- if encoding.to_s() == ""
190
- encoding = data.to_s().scan(/charset='(.*)'/i).join
191
- end
192
- if encoding.to_s() == "" and headers_t.include?("Content-Type")
193
- encoding = headers_t["Content-Type"].scan(/charset='?(.*)'?/i).join
194
- if encoding.to_s() == ""
195
- encoding = headers_t["Content-Type"].scan(/encoding='?(.*)'?/i).join
196
- end
197
- end
198
-
199
- begin
200
- data_s = JSON.pretty_generate(JSON.parse(data))
201
- rescue
202
- data_s = data
203
- end
204
- data_s = data_s.to_s().gsub("<", "&lt;")
205
- end
206
- if headers_t.keys.include?("Accept-Encoding")
207
- headers_t["Accept-Encoding"].gsub!("gzip", "") #removed so the response is in plain text
208
- end
209
-
210
- headers_ts = ""
211
- headers_t.each { |key, val| headers_ts += key.to_s + ":" + val.to_s() + ", " }
212
- message = "#{method_s} REQUEST: \npath= " + path.to_s() + "\n"
213
- message += "headers= " + headers_ts.to_s() + "\n"
214
- message += "data= " + data_s.to_s() + "\n"
215
- message = @message_server + "\n" + message
216
- if path.to_s().scan(/^https?:\/\//).size > 0 and path.to_s().scan(/^https?:\/\/#{@host}/).size == 0
217
- # the path is for another server than the current
218
- else
219
- self.class.last_request = message
220
- @logger.info(message)
221
- end
222
-
223
- if data.to_s() != "" and encoding.to_s().upcase != "UTF-8" and encoding != ""
224
- data = data.to_s().encode(encoding, "UTF-8")
225
- end
226
- return path, data, headers_t
227
- rescue Exception => stack
228
- @logger.fatal(stack)
229
- @logger.fatal("manage_request Error on method #{method_s} . path:#{path.to_s()}. data:#{data.to_s()}. headers:#{headers_t.to_s()}")
230
- return :error
231
- end
232
- end
233
-
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
+
234
243
  end