nethttputils 0.4.1.2 → 0.4.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a982e237dae69aa1f18645e6e63d231bb22d5e3
4
- data.tar.gz: 71f6c6e79050d656e9bb8c290caa11e013acf386
3
+ metadata.gz: 8a3bdaaec8992d13db534ec38ce63c28f303bc52
4
+ data.tar.gz: 70adf066cd7c2d63247da3955057b9872a3d1a4f
5
5
  SHA512:
6
- metadata.gz: e14f163d07b052595d502d07a3d104bcee3c6fbefc6169495f778e589dc80562817912f7fec3183741fa56379a7c6d6bd9fbd0ab3b8b05cb1bae9b2b671eacc5
7
- data.tar.gz: 6d8df37fb3fedc5ca757fe8345338421a55ff15283ad5f2456a4a1559de9bc6fe98551aabc56549fc27006a014e1d1702295f1bc19f8984d5eed1b7d82324579
6
+ metadata.gz: adbfb02313e49c395e8f44b25d2f5f7cbfaa749220d61d855e1e29f5b3c656797347a802acbda55b40afcceb05ffa65b77ae90339f59ac12f55a9a2b88737f23
7
+ data.tar.gz: 37f2b5b2ea2db8620558444a21eaeeac9b15d4dcaba06d3007e7b112db84d91869a0238560c3c834f725386b64a17a377e2e4bbe081002ae7b5861b3ef93ec24
data/lib/nethttputils.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require "net/http"
2
+ require "cgi"
2
3
  require "openssl"
3
4
 
4
5
  require "logger"
5
6
 
6
-
7
7
  module NetHTTPUtils
8
8
  class << self
9
9
  attr_accessor :logger
@@ -16,8 +16,10 @@ module NetHTTPUtils
16
16
 
17
17
  class Error < RuntimeError
18
18
  attr_reader :code
19
+ attr_reader :body
19
20
  def initialize body, code = nil
20
21
  @code = code
22
+ @body = body
21
23
  body = body[0...997] + "..." if body.size > 1000
22
24
  super "HTTP error ##{code.inspect} #{body}"
23
25
  end
@@ -40,7 +42,7 @@ module NetHTTPUtils
40
42
  URI url
41
43
  url
42
44
  rescue URI::InvalidURIError
43
- URI.escape url
45
+ CGI.escape url
44
46
  end unless url.is_a? URI::HTTP
45
47
  delay = 5
46
48
  begin
@@ -101,15 +103,17 @@ module NetHTTPUtils
101
103
  end
102
104
 
103
105
  private
104
- def read http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, timeout: nil, no_redirect: false, max_read_retry_delay: 3600, patch_request: nil, &block
106
+ def read http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, force_post: false, timeout: nil, no_redirect: false, max_read_retry_delay: 3600, patch_request: nil, &block
105
107
  timeout ||= 30
106
- logger = NetHTTPUtils.logger
108
+ logger = NetHTTPUtils.logger
109
+ logger.info [mtd, http].inspect
107
110
 
108
111
  uri = http.instance_variable_get :@uri
109
- logger.debug "Warning: query params included in `url` argument are discarded because `:form` isn't empty" if uri.query && !form.empty?
112
+ if %i{ HEAD GET }.include?(mtd = mtd.upcase) && !form.empty? # not .upcase! because it's not defined for Symbol
113
+ logger.debug "Warning: query params included in `url` argument are discarded because `:form` isn't empty" if uri.query
110
114
  # we can't just merge because URI fails to parse such queries as "/?1"
111
-
112
- uri.query = URI.encode_www_form form if %i{ HEAD GET }.include?(mtd = mtd.upcase) && !form.empty?
115
+ uri.query = URI.encode_www_form form
116
+ end
113
117
  cookies = {}
114
118
  prepare_request = lambda do |uri|
115
119
  case mtd.upcase
@@ -233,6 +237,7 @@ module NetHTTPUtils
233
237
  response.add_field "Set-Cookie", "#{k}=#{v}"
234
238
  end
235
239
 
240
+ logger.info "response.code = #{response.code}"
236
241
  case response.code
237
242
  when /\A20/
238
243
  response
@@ -250,8 +255,8 @@ module NetHTTPUtils
250
255
  # http.finish # why commented out?
251
256
  http = NetHTTPUtils.start_http new_uri, http.instance_variable_get(:@max_start_http_retry_delay), timeout, no_redirect
252
257
  end
253
- if request.method == "POST"
254
- logger.info "POST redirects to GET (RFC)"
258
+ if !force_post && request.method == "POST"
259
+ logger.info "POST redirects to GET (RFC)" # TODO: do it only on code 307; note that some servers still do 302
255
260
  mtd = :GET
256
261
  end
257
262
  do_request.call prepare_request[new_uri]
@@ -285,8 +290,9 @@ module NetHTTPUtils
285
290
  }"
286
291
  response
287
292
  else
288
- logger.warn "code #{response.code} at #{request.method} #{request.uri} from #{
289
- [__FILE__, caller.map{ |i| i[/(?<=:)\d+/] }].join ?:
293
+ logger.warn "code #{response.code} from #{request.method} #{request.uri} at #{
294
+ caller_path, caller_locs = caller_locations.chunk(&:path).first
295
+ [caller_path, caller_locs.map(&:lineno).chunk(&:itself).map(&:first)].join ":"
290
296
  }"
291
297
  logger.debug "< body: #{
292
298
  response.body.tap do |body|
@@ -304,10 +310,8 @@ module NetHTTPUtils
304
310
 
305
311
  require "set"
306
312
  @@_405 ||= Set.new
307
- def request_data http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, proxy: nil,
308
- timeout: nil, no_redirect: no_redirect,
309
- max_start_http_retry_delay: 3600,
310
- max_read_retry_delay: 3600,
313
+ def request_data http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, proxy: nil, force_post: false, no_redirect: false, head: false,
314
+ timeout: nil, max_start_http_retry_delay: 3600, max_read_retry_delay: 3600,
311
315
  patch_request: nil, &block
312
316
  timeout ||= 30
313
317
  http = start_http http, max_start_http_retry_delay, timeout, no_redirect, *proxy unless http.is_a? Net::HTTP
@@ -327,7 +331,7 @@ module NetHTTPUtils
327
331
  )
328
332
  end
329
333
  end
330
- if mtd == :GET && !@@_405.include?(http.address)
334
+ if head && mtd == :GET && !@@_405.include?(http.address)
331
335
  body = begin
332
336
  request_data http, :HEAD, form: form, header: header, auth: auth,
333
337
  max_start_http_retry_delay: max_start_http_retry_delay,
@@ -341,7 +345,7 @@ module NetHTTPUtils
341
345
  check_code.call body
342
346
  end
343
347
  end
344
- body = read http, mtd, type, form: form, header: header, auth: auth,
348
+ body = read http, mtd, type, form: form, header: header, auth: auth, force_post: force_post,
345
349
  timeout: timeout, no_redirect: no_redirect,
346
350
  max_read_retry_delay: max_read_retry_delay,
347
351
  patch_request: patch_request, &block
@@ -352,7 +356,6 @@ module NetHTTPUtils
352
356
  Zlib::GzipReader.new(StringIO.new(body)).read
353
357
  else
354
358
  body
355
- end.tap do |string|
356
359
  end
357
360
  # ensure
358
361
  # response.instance_variable_get("@nethttputils_close").call if response
@@ -416,6 +419,7 @@ if $0 == __FILE__
416
419
  server.shutdown
417
420
  t.join
418
421
 
422
+ # HEAD should raise on 404 but not in two other cases
419
423
  [
420
424
  [WEBrick::HTTPStatus::NotFound, 404],
421
425
  [WEBrick::HTTPStatus::BadRequest],
@@ -427,7 +431,7 @@ if $0 == __FILE__
427
431
  end
428
432
  t = Thread.new{ server.start }
429
433
  begin
430
- NetHTTPUtils.request_data "http://localhost:8000/"
434
+ NetHTTPUtils.request_data "http://localhost:8000/", head: true
431
435
  NetHTTPUtils.class_variable_get(:@@_405).clear
432
436
  fail if should_raise
433
437
  rescue NetHTTPUtils::Error => e
@@ -452,7 +456,7 @@ if $0 == __FILE__
452
456
  NetHTTPUtils.request_data("http://localhost:8000/", :head)
453
457
  fail stack.inspect unless stack == %w{ HEAD }
454
458
  stack.clear
455
- NetHTTPUtils.request_data("http://localhost:8000/")
459
+ NetHTTPUtils.request_data("http://localhost:8000/", head: true)
456
460
  fail stack.inspect unless stack == %w{ HEAD GET }
457
461
  server.shutdown
458
462
  t.join
@@ -519,7 +523,7 @@ if $0 == __FILE__
519
523
  http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg
520
524
  }.each do |url|
521
525
  begin
522
- NetHTTPUtils.request_data url, timeout: 5, max_read_retry_delay: -1
526
+ NetHTTPUtils.request_data url, timeout: 5, max_read_retry_delay: -1, head: true
523
527
  fail
524
528
  rescue Net::ReadTimeout
525
529
  end
data/nethttputils.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "nethttputils"
3
- spec.version = "0.4.1.2"
3
+ spec.version = "0.4.3.2"
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.4.1.2
4
+ version: 0.4.3.2
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: 2021-06-28 00:00:00.000000000 Z
11
+ date: 2022-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable