nethttputils 0.4.1.1 → 0.4.3.0

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: 3c68a49af2dbbca3cacf81cd5518df2ce6f2cb72
4
- data.tar.gz: bae1db040743d7f9ea9ac17d7aeafba778f227c5
3
+ metadata.gz: ecb7a8e00f832f9c1fa1f79610772f8f955a177f
4
+ data.tar.gz: ee617bf653fca7736e95e257929ebb7a597872a1
5
5
  SHA512:
6
- metadata.gz: 8ba54aa8a5fe007911b8af3117061b1a279182eb27cbdcd2be0a6293428d4408356b2b8238c7342284e93da9ee20f62d169b79346f1f30019677c24c0ca5c9af
7
- data.tar.gz: 23e5657489821b849a611a3692d14335b46c1f367dfff1c130d862a1390eb5234742110cdda9d69bb06f9206ae98ed0fdf6dfa6d66fc7056bf45dacd62a0ca13
6
+ metadata.gz: e3632e1d20d3d853f2b7542364785ce5ba4ca8b4ff25aa2304856061bbe133cdd145ac18843a1bd52af275939e9f8354223c0d37dac6d64df5791dc2f21fc9db
7
+ data.tar.gz: 90e9b8a75802bd2d48cb727cf5c28ac4aa1c481e4af0afdc90d4e5b0f251e36293867819ef788774db550b92c0796482024913d313b7475e82737440c2685197
data/lib/nethttputils.rb CHANGED
@@ -33,7 +33,8 @@ module NetHTTPUtils
33
33
  gsub(/<[^>]*>/, "").split(?\n).map(&:strip).reject(&:empty?).join(?\n)
34
34
  end
35
35
 
36
- def start_http url, max_start_http_retry_delay = 3600, timeout = 30, proxy = nil
36
+ def start_http url, max_start_http_retry_delay = 3600, timeout = nil, no_redirect = false, proxy = nil
37
+ timeout ||= 30
37
38
  uri = url
38
39
  uri = URI.parse begin
39
40
  URI url
@@ -100,14 +101,17 @@ module NetHTTPUtils
100
101
  end
101
102
 
102
103
  private
103
- def read http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, timeout: 30, max_read_retry_delay: 3600, patch_request: nil, &block
104
- logger = NetHTTPUtils.logger
104
+ 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
+ timeout ||= 30
106
+ logger = NetHTTPUtils.logger
107
+ logger.info [mtd, http].inspect
105
108
 
106
109
  uri = http.instance_variable_get :@uri
107
- logger.debug "Warning: query params included in `url` argument are discarded because `:form` isn't empty" if uri.query && !form.empty?
110
+ if %i{ HEAD GET }.include?(mtd = mtd.upcase) && !form.empty? # not .upcase! because it's not defined for Symbol
111
+ logger.debug "Warning: query params included in `url` argument are discarded because `:form` isn't empty" if uri.query
108
112
  # we can't just merge because URI fails to parse such queries as "/?1"
109
-
110
- uri.query = URI.encode_www_form form if %i{ HEAD GET }.include?(mtd = mtd.upcase) && !form.empty?
113
+ uri.query = URI.encode_www_form form
114
+ end
111
115
  cookies = {}
112
116
  prepare_request = lambda do |uri|
113
117
  case mtd.upcase
@@ -126,15 +130,20 @@ module NetHTTPUtils
126
130
  request.basic_auth *auth if auth
127
131
  if (mtd == :POST || mtd == :PATCH) && !form.empty?
128
132
  case type
129
- when :json ; request.body = JSON.dump form
130
- request.content_type = "application/json"
131
- when :form ; if form.any?{ |k, v| v.respond_to? :to_path }
133
+ when :json
134
+ request.body = JSON.dump form
135
+ request.content_type = "application/json"
136
+ when :multipart
137
+ request.set_form form, "multipart/form-data"
138
+ when :form
139
+ if form.any?{ |k, v| v.respond_to? :to_path }
132
140
  request.set_form form, "multipart/form-data"
133
141
  else
134
142
  request.set_form_data form
135
143
  request.content_type = "application/x-www-form-urlencoded;charset=UTF-8"
136
144
  end
137
- else ; raise "unknown content-type '#{type}'"
145
+ else
146
+ raise "unknown content-type '#{type}'"
138
147
  end
139
148
  end
140
149
  header.each{ |k, v| request[k.to_s] = v.is_a?(Array) ? v.first : v }
@@ -177,7 +186,7 @@ module NetHTTPUtils
177
186
  rescue EOFError => e
178
187
  raise unless e.backtrace.empty?
179
188
  # https://bugs.ruby-lang.org/issues/13018
180
- # https://blog.kalina.tech/2019/04/exception-without-backtrace-in-ruby.html?spref=reddit
189
+ # https://blog.kalina.tech/2019/04/exception-without-backtrace-in-ruby.html
181
190
  raise EOFError_from_rbuf_fill.new "probably the old Ruby empty backtrace EOFError exception from net/protocol.rb"
182
191
  end
183
192
  # response.instance_variable_set "@nethttputils_close", http.method(:finish)
@@ -226,8 +235,12 @@ module NetHTTPUtils
226
235
  response.add_field "Set-Cookie", "#{k}=#{v}"
227
236
  end
228
237
 
238
+ logger.info "response.code = #{response.code}"
229
239
  case response.code
240
+ when /\A20/
241
+ response
230
242
  when /\A30\d\z/
243
+ next response if no_redirect
231
244
  logger.info "redirect: #{response["location"]}"
232
245
  require "addressable"
233
246
  new_uri = URI.join request.uri.to_s, Addressable::URI.escape(response["location"])
@@ -238,10 +251,10 @@ module NetHTTPUtils
238
251
  http.use_ssl? != (new_uri.scheme == "https")
239
252
  logger.debug "changing host from '#{http.address}' to '#{new_host}'"
240
253
  # http.finish # why commented out?
241
- http = NetHTTPUtils.start_http new_uri, http.instance_variable_get(:@max_start_http_retry_delay), timeout
254
+ http = NetHTTPUtils.start_http new_uri, http.instance_variable_get(:@max_start_http_retry_delay), timeout, no_redirect
242
255
  end
243
- if request.method == "POST"
244
- logger.info "POST redirects to GET (RFC)"
256
+ if !force_post && request.method == "POST"
257
+ logger.info "POST redirects to GET (RFC)" # TODO: do it only on code 307; note that some servers still do 302
245
258
  mtd = :GET
246
259
  end
247
260
  do_request.call prepare_request[new_uri]
@@ -274,11 +287,10 @@ module NetHTTPUtils
274
287
  end
275
288
  }"
276
289
  response
277
- when /\A20/
278
- response
279
290
  else
280
- logger.warn "code #{response.code} at #{request.method} #{request.uri} from #{
281
- [__FILE__, caller.map{ |i| i[/(?<=:)\d+/] }].join ?:
291
+ logger.warn "code #{response.code} from #{request.method} #{request.uri} at #{
292
+ caller_path, caller_locs = caller_locations.chunk(&:path).first
293
+ [caller_path, caller_locs.map(&:lineno).chunk(&:itself).map(&:first)].join ":"
282
294
  }"
283
295
  logger.debug "< body: #{
284
296
  response.body.tap do |body|
@@ -296,12 +308,11 @@ module NetHTTPUtils
296
308
 
297
309
  require "set"
298
310
  @@_405 ||= Set.new
299
- def request_data http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, proxy: nil,
300
- timeout: 30,
301
- max_start_http_retry_delay: 3600,
302
- max_read_retry_delay: 3600,
311
+ def request_data http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, proxy: nil, force_post: false, no_redirect: false, head: false,
312
+ timeout: nil, max_start_http_retry_delay: 3600, max_read_retry_delay: 3600,
303
313
  patch_request: nil, &block
304
- http = start_http http, max_start_http_retry_delay, timeout, *proxy unless http.is_a? Net::HTTP
314
+ timeout ||= 30
315
+ http = start_http http, max_start_http_retry_delay, timeout, no_redirect, *proxy unless http.is_a? Net::HTTP
305
316
  path = http.instance_variable_get(:@uri).path
306
317
 
307
318
  check_code = lambda do |body|
@@ -318,7 +329,7 @@ module NetHTTPUtils
318
329
  )
319
330
  end
320
331
  end
321
- if mtd == :GET && !@@_405.include?(http.address)
332
+ if head && mtd == :GET && !@@_405.include?(http.address)
322
333
  body = begin
323
334
  request_data http, :HEAD, form: form, header: header, auth: auth,
324
335
  max_start_http_retry_delay: max_start_http_retry_delay,
@@ -332,7 +343,8 @@ module NetHTTPUtils
332
343
  check_code.call body
333
344
  end
334
345
  end
335
- body = read http, mtd, type, form: form, header: header, auth: auth, timeout: timeout,
346
+ body = read http, mtd, type, form: form, header: header, auth: auth, force_post: force_post,
347
+ timeout: timeout, no_redirect: no_redirect,
336
348
  max_read_retry_delay: max_read_retry_delay,
337
349
  patch_request: patch_request, &block
338
350
  check_code.call body
@@ -342,7 +354,6 @@ module NetHTTPUtils
342
354
  Zlib::GzipReader.new(StringIO.new(body)).read
343
355
  else
344
356
  body
345
- end.tap do |string|
346
357
  end
347
358
  # ensure
348
359
  # response.instance_variable_get("@nethttputils_close").call if response
@@ -406,6 +417,7 @@ if $0 == __FILE__
406
417
  server.shutdown
407
418
  t.join
408
419
 
420
+ # HEAD should raise on 404 but not in two other cases
409
421
  [
410
422
  [WEBrick::HTTPStatus::NotFound, 404],
411
423
  [WEBrick::HTTPStatus::BadRequest],
@@ -417,7 +429,7 @@ if $0 == __FILE__
417
429
  end
418
430
  t = Thread.new{ server.start }
419
431
  begin
420
- NetHTTPUtils.request_data "http://localhost:8000/"
432
+ NetHTTPUtils.request_data "http://localhost:8000/", head: true
421
433
  NetHTTPUtils.class_variable_get(:@@_405).clear
422
434
  fail if should_raise
423
435
  rescue NetHTTPUtils::Error => e
@@ -442,7 +454,7 @@ if $0 == __FILE__
442
454
  NetHTTPUtils.request_data("http://localhost:8000/", :head)
443
455
  fail stack.inspect unless stack == %w{ HEAD }
444
456
  stack.clear
445
- NetHTTPUtils.request_data("http://localhost:8000/")
457
+ NetHTTPUtils.request_data("http://localhost:8000/", head: true)
446
458
  fail stack.inspect unless stack == %w{ HEAD GET }
447
459
  server.shutdown
448
460
  t.join
@@ -509,7 +521,7 @@ if $0 == __FILE__
509
521
  http://www.aeronautica.difesa.it/organizzazione/REPARTI/divolo/PublishingImages/6%C2%B0%20Stormo/2013-decollo%20al%20tramonto%20REX%201280.jpg
510
522
  }.each do |url|
511
523
  begin
512
- NetHTTPUtils.request_data url, timeout: 5, max_read_retry_delay: -1
524
+ NetHTTPUtils.request_data url, timeout: 5, max_read_retry_delay: -1, head: true
513
525
  fail
514
526
  rescue Net::ReadTimeout
515
527
  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.1"
3
+ spec.version = "0.4.3.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.4.1.1
4
+ version: 0.4.3.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: 2020-12-07 00:00:00.000000000 Z
11
+ date: 2021-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable