nethttputils 0.3.1.0 → 0.3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/nethttputils.rb +60 -32
- data/nethttputils.gemspec +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dbd1ca6e3281c7f96be6d420512c69b4de18f5c42d06f2ac7b7641ed03a1b27e
|
4
|
+
data.tar.gz: 22dae6fd0d8304246db829fe7408b496047514731775169f3899aebbba2cbbf3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2a4f3d7378a86797bdb1aa0bac71c1c5a37953f1e95d79c9f4e29bcc1900b33eb078fea2d38d1b9bbb1937d947832e4c7c73220f1ebdd3d0b6a6dc152f6e90c
|
7
|
+
data.tar.gz: 3d090b3ffd767e60a692427db61c39276587bbe15c117623e212340866321bfa6d58bcc17227b47dfc60504c1ec57317020bedc8e4d7495cc779968876a4d302
|
data/lib/nethttputils.rb
CHANGED
@@ -95,6 +95,7 @@ module NetHTTPUtils
|
|
95
95
|
retry
|
96
96
|
end.tap do |http|
|
97
97
|
http.instance_variable_set :@uri, uri
|
98
|
+
http.instance_variable_set :@http, nil
|
98
99
|
http.define_singleton_method :read do |mtd = :GET, type = :form, form: {}, header: {}, auth: nil, timeout: 30,
|
99
100
|
max_read_retry_delay: 3600,
|
100
101
|
patch_request: nil,
|
@@ -135,7 +136,7 @@ module NetHTTPUtils
|
|
135
136
|
else ; raise "unknown content-type '#{type}'"
|
136
137
|
end
|
137
138
|
end
|
138
|
-
header.each{ |k, v| request[k.to_s] = v }
|
139
|
+
header.each{ |k, v| request[k.to_s] = v.is_a?(Array) ? v.first : v }
|
139
140
|
request["cookie"] = [*request["cookie"], cookies.map{ |k, v| "#{k}=#{v}" }].join "; " unless cookies.empty?
|
140
141
|
|
141
142
|
logger.info "> #{request.class} #{uri.host} #{request.path}"
|
@@ -161,7 +162,7 @@ module NetHTTPUtils
|
|
161
162
|
logger.debug stack.join " -> "
|
162
163
|
end
|
163
164
|
end
|
164
|
-
http =
|
165
|
+
http = instance_variable_get(:@http) || self
|
165
166
|
do_request = lambda do |request|
|
166
167
|
delay = 5
|
167
168
|
response = begin
|
@@ -205,7 +206,7 @@ module NetHTTPUtils
|
|
205
206
|
end
|
206
207
|
logger.debug "< header: #{response.to_hash}"
|
207
208
|
case response.code
|
208
|
-
when /\
|
209
|
+
when /\A30\d\z/
|
209
210
|
logger.info "redirect: #{response["location"]}"
|
210
211
|
new_uri = URI.join request.uri, URI.escape(response["location"])
|
211
212
|
new_host = new_uri.host
|
@@ -214,18 +215,21 @@ module NetHTTPUtils
|
|
214
215
|
http.use_ssl? != (new_uri.scheme == "https")
|
215
216
|
logger.debug "changing host from '#{http.address}' to '#{new_host}'"
|
216
217
|
# http.finish
|
217
|
-
http
|
218
|
+
instance_variable_set :@http, NetHTTPUtils.start_http(new_uri, max_start_http_retry_delay, timeout)
|
218
219
|
end
|
220
|
+
mtd = :GET
|
219
221
|
do_request.call prepare_request[new_uri]
|
220
222
|
when "404"
|
221
223
|
logger.error "404 at #{request.method} #{request.uri} with body: #{
|
222
|
-
if response.body
|
223
|
-
|
224
|
+
if !response.body
|
225
|
+
response.body.class
|
226
|
+
elsif response.body.is_a? Net::ReadAdapter
|
227
|
+
"<<impossible to reread Net::ReadAdapter -- check the IO you've used in block form>>"
|
224
228
|
elsif response.to_hash["content-type"] == ["image/png"]
|
225
229
|
response.to_hash["content-type"].to_s
|
226
230
|
else
|
227
231
|
response.body.tap do |body|
|
228
|
-
body.replace remove_tags body if body[/<html[> ]/]
|
232
|
+
body.replace NetHTTPUtils.remove_tags body if body[/<html[> ]/]
|
229
233
|
end.inspect
|
230
234
|
end
|
231
235
|
}"
|
@@ -235,9 +239,13 @@ module NetHTTPUtils
|
|
235
239
|
response
|
236
240
|
when /\A50\d\z/
|
237
241
|
logger.error "#{response.code} at #{request.method} #{request.uri} with body: #{
|
238
|
-
response.body
|
239
|
-
body.
|
240
|
-
|
242
|
+
if !response.body
|
243
|
+
response.body.class
|
244
|
+
else
|
245
|
+
response.body.tap do |body|
|
246
|
+
body.replace NetHTTPUtils.remove_tags body if body[/<html[> ]/]
|
247
|
+
end.inspect
|
248
|
+
end
|
241
249
|
}"
|
242
250
|
response
|
243
251
|
when /\A20/
|
@@ -250,13 +258,13 @@ module NetHTTPUtils
|
|
250
258
|
}"
|
251
259
|
logger.debug "< body: #{
|
252
260
|
response.body.tap do |body|
|
253
|
-
body.replace remove_tags body if body[/<html[> ]/]
|
261
|
+
body.replace NetHTTPUtils.remove_tags body if body[/<html[> ]/]
|
254
262
|
end.inspect
|
255
|
-
}"
|
263
|
+
}" if request.is_a? Net::HTTP::Get
|
256
264
|
response
|
257
265
|
end
|
258
266
|
end
|
259
|
-
response = do_request
|
267
|
+
response = do_request.call prepare_request[uri]
|
260
268
|
cookies.each{ |k, v| response.add_field "Set-Cookie", "#{k}=#{v};" }
|
261
269
|
logger.debug "< header: #{response.to_hash}"
|
262
270
|
(response.body || "").tap{ |r| r.instance_variable_set :@last_response, response }
|
@@ -271,25 +279,42 @@ module NetHTTPUtils
|
|
271
279
|
patch_request: nil, &block
|
272
280
|
http = start_http http, max_start_http_retry_delay, timeout unless http.is_a? Net::HTTP
|
273
281
|
path = http.instance_variable_get(:@uri).path
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
282
|
+
|
283
|
+
check_code = lambda do |body|
|
284
|
+
fail unless code = body.instance_variable_get(:@last_response).code
|
285
|
+
case code
|
286
|
+
# TODO: raise on 405
|
287
|
+
when /\A(20\d|3\d\d|405)\z/
|
288
|
+
nil
|
289
|
+
else
|
290
|
+
ct = body.instance_variable_get(:@last_response).to_hash.fetch("content-type")
|
291
|
+
raise Error.new(
|
292
|
+
(ct == ["image/png"] ? ct : body),
|
293
|
+
code.to_i
|
294
|
+
)
|
295
|
+
end
|
296
|
+
end
|
297
|
+
require "set"
|
298
|
+
@@_405 ||= Set.new
|
299
|
+
if mtd == :GET && !@@_405.include?(http.address)
|
300
|
+
body = request_data http, :HEAD, max_start_http_retry_delay: max_start_http_retry_delay, max_read_retry_delay: max_read_retry_delay
|
301
|
+
if "405" == body.instance_variable_get(:@last_response).code
|
302
|
+
@@_405.add http.address
|
303
|
+
else
|
304
|
+
check_code.call body
|
305
|
+
end
|
280
306
|
end
|
281
307
|
body = http.read mtd, type, form: form, header: header, auth: auth, timeout: timeout,
|
282
308
|
max_read_retry_delay: max_read_retry_delay,
|
283
309
|
patch_request: patch_request, &block
|
310
|
+
check_code.call body
|
311
|
+
|
284
312
|
last_response = body.instance_variable_get :@last_response
|
285
313
|
if last_response.to_hash["content-encoding"] == "gzip"
|
286
314
|
Zlib::GzipReader.new(StringIO.new(body)).read
|
287
315
|
else
|
288
316
|
body
|
289
317
|
end.tap do |string|
|
290
|
-
string.instance_variable_set :@code, last_response.code
|
291
|
-
string.instance_variable_set :@uri_path, path # useless?
|
292
|
-
string.instance_variable_set :@header, last_response.to_hash
|
293
318
|
end
|
294
319
|
# ensure
|
295
320
|
# response.instance_variable_get("@nethttputils_close").call if response
|
@@ -302,6 +327,7 @@ end
|
|
302
327
|
if $0 == __FILE__
|
303
328
|
STDOUT.sync = true
|
304
329
|
print "self testing... "
|
330
|
+
NetHTTPUtils.logger.level = Logger::DEBUG
|
305
331
|
require "pp"
|
306
332
|
|
307
333
|
|
@@ -312,17 +338,17 @@ if $0 == __FILE__
|
|
312
338
|
server = WEBrick::HTTPServer.new Port: 8000
|
313
339
|
stack = []
|
314
340
|
server.mount_proc ?/ do |req, res|
|
315
|
-
stack.push req.request_method
|
341
|
+
p stack.push req.request_method
|
316
342
|
end
|
317
343
|
t = Thread.new{ server.start }
|
318
344
|
NetHTTPUtils.start_http("http://localhost:8000/")
|
319
|
-
fail unless stack == %w{ }
|
345
|
+
fail stack.inspect unless stack == %w{ }
|
320
346
|
stack.clear
|
321
347
|
NetHTTPUtils.start_http("http://localhost:8000/").head("/")
|
322
|
-
fail unless stack == %w{ HEAD }
|
348
|
+
fail stack.inspect unless stack == %w{ HEAD }
|
323
349
|
stack.clear
|
324
350
|
NetHTTPUtils.request_data("http://localhost:8000/")
|
325
|
-
fail unless stack == %w{ HEAD GET }
|
351
|
+
fail stack.inspect unless stack == %w{ HEAD GET }
|
326
352
|
server.shutdown
|
327
353
|
|
328
354
|
# TODO: test that HEAD method request goes through redirects
|
@@ -338,11 +364,14 @@ if $0 == __FILE__
|
|
338
364
|
end
|
339
365
|
Thread.abort_on_exception = true
|
340
366
|
Thread.new{ server.start }
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
367
|
+
check = lambda do |path, headers, response|
|
368
|
+
fail response unless JSON.dump([path, headers]) == response
|
369
|
+
end
|
370
|
+
check["/", %w{ accept-encoding accept user-agent host connection }, NetHTTPUtils.request_data("http://localhost:8000/")]
|
371
|
+
check["/?1", %w{ accept-encoding accept user-agent host connection }, NetHTTPUtils.request_data("http://localhost:8000/?1")]
|
372
|
+
check["/?1=2", %w{ accept-encoding accept user-agent host connection }, NetHTTPUtils.request_data("http://localhost:8000/?1=2")]
|
373
|
+
check["/?1=3", %w{ accept-encoding accept user-agent host connection }, NetHTTPUtils.request_data("http://localhost:8000/?1=2&3=4", form: {1=>3})]
|
374
|
+
check["/", %w{ accept-encoding accept user-agent host content-type connection content-length }, NetHTTPUtils.request_data("http://localhost:8000/", :post, form: {1=>2})]
|
346
375
|
server.shutdown
|
347
376
|
|
348
377
|
|
@@ -359,7 +388,6 @@ if $0 == __FILE__
|
|
359
388
|
fail unless NetHTTPUtils.start_http("http://httpstat.us/500").read == "500 Internal Server Error"
|
360
389
|
fail unless NetHTTPUtils.start_http("http://httpstat.us/502").read == "502 Bad Gateway"
|
361
390
|
fail unless NetHTTPUtils.start_http("http://httpstat.us/503").read == "503 Service Unavailable"
|
362
|
-
NetHTTPUtils.logger.level = Logger::FATAL
|
363
391
|
[
|
364
392
|
["https://imgur.com/a/cccccc"],
|
365
393
|
["https://imgur.com/mM4Dh7Z"],
|
data/nethttputils.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "nethttputils"
|
3
|
-
spec.version = "0.3.
|
3
|
+
spec.version = "0.3.2.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.3.
|
4
|
+
version: 0.3.2.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:
|
11
|
+
date: 2019-01-11 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 --
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
version: '0'
|
52
52
|
requirements: []
|
53
53
|
rubyforge_project:
|
54
|
-
rubygems_version: 2.
|
54
|
+
rubygems_version: 2.7.6
|
55
55
|
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: this tool is like a pet that I adopted young and now I depend on, sorry
|