nethttputils 0.4.1.1 → 0.4.1.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: 3c68a49af2dbbca3cacf81cd5518df2ce6f2cb72
4
- data.tar.gz: bae1db040743d7f9ea9ac17d7aeafba778f227c5
3
+ metadata.gz: 3a982e237dae69aa1f18645e6e63d231bb22d5e3
4
+ data.tar.gz: 71f6c6e79050d656e9bb8c290caa11e013acf386
5
5
  SHA512:
6
- metadata.gz: 8ba54aa8a5fe007911b8af3117061b1a279182eb27cbdcd2be0a6293428d4408356b2b8238c7342284e93da9ee20f62d169b79346f1f30019677c24c0ca5c9af
7
- data.tar.gz: 23e5657489821b849a611a3692d14335b46c1f367dfff1c130d862a1390eb5234742110cdda9d69bb06f9206ae98ed0fdf6dfa6d66fc7056bf45dacd62a0ca13
6
+ metadata.gz: e14f163d07b052595d502d07a3d104bcee3c6fbefc6169495f778e589dc80562817912f7fec3183741fa56379a7c6d6bd9fbd0ab3b8b05cb1bae9b2b671eacc5
7
+ data.tar.gz: 6d8df37fb3fedc5ca757fe8345338421a55ff15283ad5f2456a4a1559de9bc6fe98551aabc56549fc27006a014e1d1702295f1bc19f8984d5eed1b7d82324579
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,7 +101,8 @@ 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
+ 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
105
+ timeout ||= 30
104
106
  logger = NetHTTPUtils.logger
105
107
 
106
108
  uri = http.instance_variable_get :@uri
@@ -126,15 +128,20 @@ module NetHTTPUtils
126
128
  request.basic_auth *auth if auth
127
129
  if (mtd == :POST || mtd == :PATCH) && !form.empty?
128
130
  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 }
131
+ when :json
132
+ request.body = JSON.dump form
133
+ request.content_type = "application/json"
134
+ when :multipart
135
+ request.set_form form, "multipart/form-data"
136
+ when :form
137
+ if form.any?{ |k, v| v.respond_to? :to_path }
132
138
  request.set_form form, "multipart/form-data"
133
139
  else
134
140
  request.set_form_data form
135
141
  request.content_type = "application/x-www-form-urlencoded;charset=UTF-8"
136
142
  end
137
- else ; raise "unknown content-type '#{type}'"
143
+ else
144
+ raise "unknown content-type '#{type}'"
138
145
  end
139
146
  end
140
147
  header.each{ |k, v| request[k.to_s] = v.is_a?(Array) ? v.first : v }
@@ -177,7 +184,7 @@ module NetHTTPUtils
177
184
  rescue EOFError => e
178
185
  raise unless e.backtrace.empty?
179
186
  # https://bugs.ruby-lang.org/issues/13018
180
- # https://blog.kalina.tech/2019/04/exception-without-backtrace-in-ruby.html?spref=reddit
187
+ # https://blog.kalina.tech/2019/04/exception-without-backtrace-in-ruby.html
181
188
  raise EOFError_from_rbuf_fill.new "probably the old Ruby empty backtrace EOFError exception from net/protocol.rb"
182
189
  end
183
190
  # response.instance_variable_set "@nethttputils_close", http.method(:finish)
@@ -227,7 +234,10 @@ module NetHTTPUtils
227
234
  end
228
235
 
229
236
  case response.code
237
+ when /\A20/
238
+ response
230
239
  when /\A30\d\z/
240
+ next response if no_redirect
231
241
  logger.info "redirect: #{response["location"]}"
232
242
  require "addressable"
233
243
  new_uri = URI.join request.uri.to_s, Addressable::URI.escape(response["location"])
@@ -238,7 +248,7 @@ module NetHTTPUtils
238
248
  http.use_ssl? != (new_uri.scheme == "https")
239
249
  logger.debug "changing host from '#{http.address}' to '#{new_host}'"
240
250
  # http.finish # why commented out?
241
- http = NetHTTPUtils.start_http new_uri, http.instance_variable_get(:@max_start_http_retry_delay), timeout
251
+ http = NetHTTPUtils.start_http new_uri, http.instance_variable_get(:@max_start_http_retry_delay), timeout, no_redirect
242
252
  end
243
253
  if request.method == "POST"
244
254
  logger.info "POST redirects to GET (RFC)"
@@ -274,8 +284,6 @@ module NetHTTPUtils
274
284
  end
275
285
  }"
276
286
  response
277
- when /\A20/
278
- response
279
287
  else
280
288
  logger.warn "code #{response.code} at #{request.method} #{request.uri} from #{
281
289
  [__FILE__, caller.map{ |i| i[/(?<=:)\d+/] }].join ?:
@@ -297,11 +305,12 @@ module NetHTTPUtils
297
305
  require "set"
298
306
  @@_405 ||= Set.new
299
307
  def request_data http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, proxy: nil,
300
- timeout: 30,
308
+ timeout: nil, no_redirect: no_redirect,
301
309
  max_start_http_retry_delay: 3600,
302
310
  max_read_retry_delay: 3600,
303
311
  patch_request: nil, &block
304
- http = start_http http, max_start_http_retry_delay, timeout, *proxy unless http.is_a? Net::HTTP
312
+ timeout ||= 30
313
+ http = start_http http, max_start_http_retry_delay, timeout, no_redirect, *proxy unless http.is_a? Net::HTTP
305
314
  path = http.instance_variable_get(:@uri).path
306
315
 
307
316
  check_code = lambda do |body|
@@ -332,7 +341,8 @@ module NetHTTPUtils
332
341
  check_code.call body
333
342
  end
334
343
  end
335
- body = read http, mtd, type, form: form, header: header, auth: auth, timeout: timeout,
344
+ body = read http, mtd, type, form: form, header: header, auth: auth,
345
+ timeout: timeout, no_redirect: no_redirect,
336
346
  max_read_retry_delay: max_read_retry_delay,
337
347
  patch_request: patch_request, &block
338
348
  check_code.call body
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.1.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.1
4
+ version: 0.4.1.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: 2020-12-07 00:00:00.000000000 Z
11
+ date: 2021-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable