nethttputils 0.2.3.0 → 0.2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nethttputils.rb +48 -16
- data/nethttputils.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dcab77cae6a0a44cfd6fe0414a365763f114e95
|
4
|
+
data.tar.gz: f20d494f49424479fcf8c2534b3bc6d19be6b583
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 485981929b85428261d1e019d837c9915ee608bfd3e6e80c8af3988d55ddfa4d710a5c8ba4025ba850b268ea6bf67bddc0cb4df154d8f5a4a35f9830b62e7b80
|
7
|
+
data.tar.gz: 58b3cc0fb11c0557a8d0f59809f89fea37c971b6749ceee90f5cc6522d21bcc54c3f913ad99b02f9d2307d7d124699c00ed30a797f659a43f88f69c76a4b209a
|
data/lib/nethttputils.rb
CHANGED
@@ -58,24 +58,33 @@ module NetHTTPUtils
|
|
58
58
|
# pp Object.instance_method(:method).bind(request).call(:set_form).source_location
|
59
59
|
if (mtd == :POST || mtd == :PATCH) && !form.empty?
|
60
60
|
case type
|
61
|
+
when :json ; request.body = JSON.dump form
|
62
|
+
request.content_type = "application/json"
|
61
63
|
when :form ; if form.any?{ |k, v| v.respond_to? :to_path }
|
62
64
|
request.set_form form, "multipart/form-data"
|
63
65
|
else
|
64
66
|
request.set_form_data form
|
65
67
|
request.content_type = "application/x-www-form-urlencoded;charset=UTF-8"
|
66
68
|
end
|
67
|
-
when :json ; request.body = JSON.dump form
|
68
|
-
request.content_type = "application/json"
|
69
69
|
else ; raise "unknown content-type '#{type}'"
|
70
70
|
end
|
71
71
|
end
|
72
72
|
header.each{ |k, v| request[k.to_s] = v }
|
73
73
|
|
74
|
-
logger.info "> #{request} #{request.path}"
|
74
|
+
logger.info "> #{request.class} #{uri.host} #{request.path}"
|
75
75
|
next unless logger.debug?
|
76
|
-
logger.debug "
|
76
|
+
logger.debug "content-type: #{request.content_type}" unless mtd == :GET
|
77
|
+
curl_form = case request.content_type
|
78
|
+
when "application/json" ; "-d #{JSON.dump form} "
|
79
|
+
when "multipart/form-data" ; form.map{ |k, v| "-F \"#{k}=#{v.respond_to?(:to_path) ? "@#{v.to_path}" : v}\" " }.join
|
80
|
+
when "application/x-www-form-urlencoded" ; "-d \"#{URI.encode_www_form form}\" "
|
81
|
+
else ; mtd == :GET ? "" : fail("unknown content-type '#{request.content_type}'")
|
82
|
+
end
|
83
|
+
logger.debug "curl -vsSL -o /dev/null #{
|
84
|
+
request.each_header.map{ |k, v| "-H \"#{k}: #{v}\" " unless k == "host" }.join
|
85
|
+
}#{curl_form}#{url.gsub "&", "\\\\&"}"
|
77
86
|
logger.debug "> header: #{request.each_header.to_a}"
|
78
|
-
logger.debug "> body: #{request.body.inspect.tap{ |body| body[
|
87
|
+
logger.debug "> body: #{request.body.inspect.tap{ |body| body[997..-1] = "..." if body.size > 500 }}"
|
79
88
|
stack = caller.reverse.map do |level|
|
80
89
|
/((?:[^\/:]+\/)?[^\/:]+):([^:]+)/.match(level).captures
|
81
90
|
end.chunk(&:first).map do |file, group|
|
@@ -96,7 +105,18 @@ module NetHTTPUtils
|
|
96
105
|
) do |http|
|
97
106
|
# http.open_timeout = timeout # seems like when opening hangs, this line in unreachable
|
98
107
|
# http.read_timeout = timeout
|
99
|
-
http.set_debug_output
|
108
|
+
http.set_debug_output( Object.new.tap do |obj|
|
109
|
+
obj.instance_eval do
|
110
|
+
def << msg
|
111
|
+
@@buffer ||= "[Net::HTTP debug] "
|
112
|
+
@@buffer.concat msg
|
113
|
+
@@buffer = @@buffer[0...997] + "..." if @@buffer.size > 500
|
114
|
+
return unless @@buffer.end_with? ?\n
|
115
|
+
NetHTTPUtils.logger.debug @@buffer.sub ?\n, " "
|
116
|
+
@@buffer = nil
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end ) if logger.level == Logger::DEBUG # use `logger.debug?`?
|
100
120
|
http
|
101
121
|
end
|
102
122
|
rescue Errno::ECONNREFUSED => e
|
@@ -175,9 +195,15 @@ module NetHTTPUtils
|
|
175
195
|
do_request.call prepare_request[new_uri]
|
176
196
|
when "404"
|
177
197
|
logger.error "404 at #{request.method} #{request.uri} with body: #{
|
178
|
-
response.body.is_a?
|
179
|
-
|
180
|
-
|
198
|
+
if response.body.is_a? Net::ReadAdapter
|
199
|
+
"impossible to reread Net::ReadAdapter -- check the IO you've used in block form"
|
200
|
+
elsif response.to_hash["content-type"] == ["image/png"]
|
201
|
+
response.to_hash["content-type"].to_s
|
202
|
+
else
|
203
|
+
response.body.tap do |body|
|
204
|
+
body.replace remove_tags body if body[/<html[> ]/]
|
205
|
+
end.inspect
|
206
|
+
end
|
181
207
|
}"
|
182
208
|
response
|
183
209
|
when "429"
|
@@ -209,13 +235,16 @@ module NetHTTPUtils
|
|
209
235
|
end
|
210
236
|
do_request[prepare_request[uri]].tap do |response|
|
211
237
|
cookies.each{ |k, v| response.add_field "Set-Cookie", "#{k}=#{v};" }
|
212
|
-
logger.debug response.to_hash
|
238
|
+
logger.debug "< header: #{response.to_hash}"
|
213
239
|
end
|
214
240
|
end
|
215
241
|
|
216
242
|
def request_data *args, &block
|
217
243
|
response = get_response *args, &block
|
218
|
-
raise Error.new
|
244
|
+
raise Error.new(
|
245
|
+
(response.to_hash["content-type"] == ["image/png"] ? response.to_hash["content-type"] : response.body),
|
246
|
+
response.code.to_i
|
247
|
+
) unless response.code[/\A(20\d|3\d\d)\z/]
|
219
248
|
if response["content-encoding"] == "gzip"
|
220
249
|
Zlib::GzipReader.new(StringIO.new(response.body)).read
|
221
250
|
else
|
@@ -265,15 +294,18 @@ if $0 == __FILE__
|
|
265
294
|
fail unless NetHTTPUtils.get_response("http://httpstat.us/404").body == "404 Not Found"
|
266
295
|
fail unless NetHTTPUtils.get_response("http://httpstat.us/500").body == "500 Internal Server Error"
|
267
296
|
fail unless NetHTTPUtils.get_response("http://httpstat.us/503").body == "503 Service Unavailable"
|
268
|
-
|
269
|
-
|
270
|
-
https://imgur.com/
|
271
|
-
|
297
|
+
NetHTTPUtils.logger.level = Logger::FATAL
|
298
|
+
[
|
299
|
+
["https://imgur.com/a/cccccc"],
|
300
|
+
["https://imgur.com/mM4Dh7Z"],
|
301
|
+
["https://i.redd.it/si758zk7r5xz.jpg", "HTTP error #404 [\"image/png\"]"],
|
302
|
+
].each do |url, expectation|
|
272
303
|
begin
|
273
304
|
puts NetHTTPUtils.remove_tags NetHTTPUtils.request_data url
|
274
305
|
fail
|
275
306
|
rescue NetHTTPUtils::Error => e
|
276
|
-
raise unless e.code == 404
|
307
|
+
raise e.code.inspect unless e.code == 404
|
308
|
+
raise e.to_s if e.to_s != expectation if expectation
|
277
309
|
end
|
278
310
|
end
|
279
311
|
%w{
|
data/nethttputils.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "nethttputils"
|
3
|
-
spec.version = "0.2.
|
3
|
+
spec.version = "0.2.4.0"
|
4
4
|
spec.summary = "this tool is like a pet that I adopted young and now I depend on, sorry"
|
5
5
|
spec.description = <<-EOF
|
6
6
|
Back in 2015 I was a guy automating things at my job and two scripts had a common need --
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nethttputils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Maslov aka Nakilon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
Back in 2015 I was a guy automating things at my job and two scripts had a common need --
|