nethttputils 0.3.1.0 → 0.3.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d553d1c931adfe28123f8c57313a0817d88f1396
4
- data.tar.gz: 15527b1fc760308058e0bf48d7cdff662d681ae6
2
+ SHA256:
3
+ metadata.gz: dbd1ca6e3281c7f96be6d420512c69b4de18f5c42d06f2ac7b7641ed03a1b27e
4
+ data.tar.gz: 22dae6fd0d8304246db829fe7408b496047514731775169f3899aebbba2cbbf3
5
5
  SHA512:
6
- metadata.gz: 4417fc854b1f76bdc96d06e5b44e5c0f4f1e709fadf2360b9e9059c42721121c78d48b9a4126a821400620a072035aaca7b88abd81320ae2fd55d9b397257769
7
- data.tar.gz: c3ba3f3bb0efb29f88a2d42f792be19b2793ec51afec179dd36cf5403116a8bbb8dcc0511af637f9478eb29acde7ec566036a809295934df2dd85f6cf1e9cf66
6
+ metadata.gz: a2a4f3d7378a86797bdb1aa0bac71c1c5a37953f1e95d79c9f4e29bcc1900b33eb078fea2d38d1b9bbb1937d947832e4c7c73220f1ebdd3d0b6a6dc152f6e90c
7
+ data.tar.gz: 3d090b3ffd767e60a692427db61c39276587bbe15c117623e212340866321bfa6d58bcc17227b47dfc60504c1ec57317020bedc8e4d7495cc779968876a4d302
@@ -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 = NetHTTPUtils.start_http url, max_start_http_retry_delay, timeout
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 /\A3\d\d\z/
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 = NetHTTPUtils.start_http new_uri, max_start_http_retry_delay, timeout
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.is_a? Net::ReadAdapter
223
- "impossible to reread Net::ReadAdapter -- check the IO you've used in block form"
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.tap do |body|
239
- body.replace remove_tags body if body[/<html[> ]/]
240
- end.inspect
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[prepare_request[uri]]
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
- if mtd == :GET
275
- head = http.head path, header
276
- raise Error.new(
277
- (head.to_hash["content-type"] == ["image/png"] ? head.to_hash["content-type"] : head.body),
278
- head.code.to_i
279
- ) unless head.code[/\A(20\d|3\d\d)\z/]
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
- fail unless JSON.dump(["/", %w{ accept-encoding accept user-agent host connection }]) == NetHTTPUtils.request_data("http://localhost:8000/")
342
- fail unless JSON.dump(["/?1", %w{ accept-encoding accept user-agent host connection }]) == NetHTTPUtils.request_data("http://localhost:8000/?1")
343
- fail unless JSON.dump(["/?1=2", %w{ accept-encoding accept user-agent host connection }]) == NetHTTPUtils.request_data("http://localhost:8000/?1=2")
344
- fail unless JSON.dump(["/?1=3", %w{ accept-encoding accept user-agent host connection }]) == NetHTTPUtils.request_data("http://localhost:8000/?1=2&3=4", form: {1=>3})
345
- fail unless JSON.dump(["/", %w{ accept-encoding accept user-agent host content-type connection content-length }]) == NetHTTPUtils.request_data("http://localhost:8000/", :post, form: {1=>2})
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"],
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "nethttputils"
3
- spec.version = "0.3.1.0"
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.1.0
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: 2018-12-30 00:00:00.000000000 Z
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.5.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