nethttputils 0.3.2.1 → 0.3.2.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
- SHA256:
3
- metadata.gz: be5755b14b4796da9b7dcf94680280a6fc9b4a0ade93a3d28e2220790f777943
4
- data.tar.gz: a5aeb671b813d2dd6ec7c1a835a228ce60bdc8f82a1ae42473fc3c11a626cfc4
2
+ SHA1:
3
+ metadata.gz: ec41b21545731116bc0725bf44119fef0426b335
4
+ data.tar.gz: 2d1d9a34358628a0710f57ebdb49d2680ba17333
5
5
  SHA512:
6
- metadata.gz: 292acc0a8a6caa59aba16fe703ccbceeebf872e6f7bbe6ce4aaa0a04e7b89565442ccd372d3745fc7309071ed66fdc93bb884217f30796963cdd2019e43f11db
7
- data.tar.gz: '08f624ffd64a57f9e46f8883e3bca9856f5bf44bee3868892fa555beae265c5b8d4b781bb6377da94997b6237a1969daf22a515dbe73e06eeda6244d33ef710e'
6
+ metadata.gz: 6d41944dc91ce2825d8b8095091f94792e4c1760f554ae5914320fa545063311a221222261aee19f85a5f34c97a16dec1176e034a5bff3753a061b0554d6b0a2
7
+ data.tar.gz: 884d389aebd0be39b04c9dd811276d5d3e3c8410c3e21e32d971ce99dc9fcb7935cb91a5f9cc85874cf0d4d9606797b3b2ea72a5caa34114cf2b0bf2a2ead125
@@ -95,14 +95,15 @@ 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
99
- http.define_singleton_method :read do |mtd = :GET, type = :form, form: {}, header: {}, auth: nil, timeout: 30,
100
- max_read_retry_delay: 3600,
101
- patch_request: nil,
102
- &block|
98
+ http.instance_variable_set :@max_start_http_retry_delay, max_start_http_retry_delay
99
+ end
100
+ end
103
101
 
102
+ 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
104
  logger = NetHTTPUtils.logger
105
105
 
106
+ uri = http.instance_variable_get :@uri
106
107
  logger.warn "Warning: query params included in `url` argument are discarded because `:form` isn't empty" if uri.query && !form.empty?
107
108
  # we can't just merge because URI fails to parse such queries as "/?1"
108
109
 
@@ -142,15 +143,17 @@ module NetHTTPUtils
142
143
  logger.info "> #{request.class} #{uri.host} #{request.path}"
143
144
  next unless logger.debug?
144
145
  logger.debug "content-length: #{request.content_length.to_i}, content-type: #{request.content_type}" unless %i{ HEAD GET }.include? mtd
146
+ logger.debug "query: #{uri.query.inspect}"
147
+ logger.debug "content-type: #{request.content_type.inspect}"
145
148
  curl_form = case request.content_type
146
149
  when "application/json" ; "-d #{JSON.dump form} "
147
150
  when "multipart/form-data" ; form.map{ |k, v| "-F \"#{k}=#{v.respond_to?(:to_path) ? "@#{v.to_path}" : v}\" " }.join
148
151
  when "application/x-www-form-urlencoded" ; "-d \"#{URI.encode_www_form form}\" "
149
152
  else %i{ HEAD GET }.include?(mtd) ? "" : fail("unknown content-type '#{request.content_type}'")
150
153
  end
151
- logger.debug "curl -vsSL --compressed -o /dev/null #{
154
+ logger.debug "curl -vsSL --compressed -o /dev/null #{"-X HEAD " if request.is_a? Net::HTTP::Head}#{
152
155
  request.each_header.map{ |k, v| "-H \"#{k}: #{v}\" " unless k == "host" }.join
153
- }#{curl_form}'#{url.gsub "&", "\\\\&"}#{"?#{uri.query}" if uri.query && uri.query.empty?}'"
156
+ }#{curl_form}'#{uri.scheme}://#{uri.host}#{uri.path}#{"?#{uri.query}" if uri.query && !uri.query.empty?}'"
154
157
  logger.debug "> header: #{request.each_header.to_a}"
155
158
  logger.debug "> body: #{request.body.inspect.tap{ |body| body[997..-1] = "..." if body.size > 500 }}"
156
159
  # TODO this is buggy -- mixes lines from different files into one line
@@ -162,7 +165,6 @@ module NetHTTPUtils
162
165
  logger.debug stack.join " -> "
163
166
  end
164
167
  end
165
- http = instance_variable_get(:@http) || self
166
168
  do_request = lambda do |request|
167
169
  delay = 5
168
170
  response = begin
@@ -204,7 +206,6 @@ module NetHTTPUtils
204
206
  logger.debug "set-cookie: #{k}=#{v[/[^;]+/]}"
205
207
  cookies.store k, v[/[^;]+/]
206
208
  end
207
- logger.debug "< header: #{response.to_hash}"
208
209
  case response.code
209
210
  when /\A30\d\z/
210
211
  logger.info "redirect: #{response["location"]}"
@@ -215,9 +216,12 @@ module NetHTTPUtils
215
216
  http.use_ssl? != (new_uri.scheme == "https")
216
217
  logger.debug "changing host from '#{http.address}' to '#{new_host}'"
217
218
  # http.finish
218
- instance_variable_set :@http, NetHTTPUtils.start_http(new_uri, max_start_http_retry_delay, timeout)
219
+ http = NetHTTPUtils.start_http new_uri, http.instance_variable_get(:@max_start_http_retry_delay), timeout
220
+ end
221
+ if request.method == "POST"
222
+ logger.info "POST redirects to GET (RFC)"
223
+ mtd = :GET
219
224
  end
220
- mtd = :GET
221
225
  do_request.call prepare_request[new_uri]
222
226
  when "404"
223
227
  logger.error "404 at #{request.method} #{request.uri} with body: #{
@@ -251,9 +255,7 @@ module NetHTTPUtils
251
255
  when /\A20/
252
256
  response
253
257
  else
254
- logger.warn "code #{response.code} at #{request.method} #{request.uri}#{
255
- " and so #{url}" if request.uri.to_s != url
256
- } from #{
258
+ logger.warn "code #{response.code} at #{request.method} #{request.uri} from #{
257
259
  [__FILE__, caller.map{ |i| i[/(?<=:)\d+/] }].join ?:
258
260
  }"
259
261
  logger.debug "< body: #{
@@ -268,10 +270,8 @@ module NetHTTPUtils
268
270
  cookies.each{ |k, v| response.add_field "Set-Cookie", "#{k}=#{v};" }
269
271
  logger.debug "< header: #{response.to_hash}"
270
272
  (response.body || "").tap{ |r| r.instance_variable_set :@last_response, response }
271
-
272
- end
273
- end
274
273
  end
274
+ public
275
275
 
276
276
  def request_data http, mtd = :GET, type = :form, form: {}, header: {}, auth: nil, timeout: 30,
277
277
  max_start_http_retry_delay: 3600,
@@ -297,7 +297,7 @@ module NetHTTPUtils
297
297
  require "set"
298
298
  @@_405 ||= Set.new
299
299
  if mtd == :GET && !@@_405.include?(http.address)
300
- body = request_data http, :HEAD, auth: auth,
300
+ body = request_data http, :HEAD, header: header, auth: auth,
301
301
  max_start_http_retry_delay: max_start_http_retry_delay,
302
302
  max_read_retry_delay: max_read_retry_delay
303
303
  if "405" == body.instance_variable_get(:@last_response).code
@@ -306,7 +306,7 @@ module NetHTTPUtils
306
306
  check_code.call body
307
307
  end
308
308
  end
309
- body = http.read mtd, type, form: form, header: header, auth: auth, timeout: timeout,
309
+ body = read http, mtd, type, form: form, header: header, auth: auth, timeout: timeout,
310
310
  max_read_retry_delay: max_read_retry_delay,
311
311
  patch_request: patch_request, &block
312
312
  check_code.call body
@@ -332,6 +332,7 @@ if $0 == __FILE__
332
332
  NetHTTPUtils.logger.level = Logger::DEBUG
333
333
  require "pp"
334
334
 
335
+ NetHTTPUtils.request_data("https://goo.gl/ySqUb5") # this will fail if domain redirects are broken
335
336
 
336
337
  require "webrick"
337
338
  require "json"
@@ -385,11 +386,11 @@ if $0 == __FILE__
385
386
  raise unless e.code == code
386
387
  end
387
388
  end
388
- fail unless NetHTTPUtils.start_http("http://httpstat.us/400").read == "400 Bad Request"
389
- fail unless NetHTTPUtils.start_http("http://httpstat.us/404").read == "404 Not Found"
390
- fail unless NetHTTPUtils.start_http("http://httpstat.us/500").read == "500 Internal Server Error"
391
- fail unless NetHTTPUtils.start_http("http://httpstat.us/502").read == "502 Bad Gateway"
392
- fail unless NetHTTPUtils.start_http("http://httpstat.us/503").read == "503 Service Unavailable"
389
+ fail unless NetHTTPUtils.method(:read).call(NetHTTPUtils.start_http("http://httpstat.us/400")) == "400 Bad Request"
390
+ fail unless NetHTTPUtils.method(:read).call(NetHTTPUtils.start_http("http://httpstat.us/404")) == "404 Not Found"
391
+ fail unless NetHTTPUtils.method(:read).call(NetHTTPUtils.start_http("http://httpstat.us/500")) == "500 Internal Server Error"
392
+ fail unless NetHTTPUtils.method(:read).call(NetHTTPUtils.start_http("http://httpstat.us/502")) == "502 Bad Gateway"
393
+ fail unless NetHTTPUtils.method(:read).call(NetHTTPUtils.start_http("http://httpstat.us/503")) == "503 Service Unavailable"
393
394
  [
394
395
  ["https://imgur.com/a/cccccc"],
395
396
  ["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.2.1"
3
+ spec.version = "0.3.2.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.3.2.1
4
+ version: 0.3.2.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: 2019-01-14 00:00:00.000000000 Z
11
+ date: 2019-01-20 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.7.6
54
+ rubygems_version: 2.5.2
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