nethttputils 0.3.0.0 → 0.3.1.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
2
  SHA1:
3
- metadata.gz: 9a9df743f10c31c23bbbcc076750dcea63f50133
4
- data.tar.gz: 503c63e39858d28602a042701187a4f08404d349
3
+ metadata.gz: d553d1c931adfe28123f8c57313a0817d88f1396
4
+ data.tar.gz: 15527b1fc760308058e0bf48d7cdff662d681ae6
5
5
  SHA512:
6
- metadata.gz: 6d88ded5499b3aaddfbd187eb185099d6de32d2e78f941ab6f95ed3db7a4a5f2d5fe4b134c267bd85cbc2199aba15fec6873f2fc8b13eed184e1d4fe566d1acf
7
- data.tar.gz: '006529bc8770f5c149572528fe0050a69755f0b2db8efbb76ec2e2fb324e198ce4f7cf176d2e04f9d6a461d1a3d07b74badc85ee96765c01889a86e7f22b2ddd'
6
+ metadata.gz: 4417fc854b1f76bdc96d06e5b44e5c0f4f1e709fadf2360b9e9059c42721121c78d48b9a4126a821400620a072035aaca7b88abd81320ae2fd55d9b397257769
7
+ data.tar.gz: c3ba3f3bb0efb29f88a2d42f792be19b2793ec51afec179dd36cf5403116a8bbb8dcc0511af637f9478eb29acde7ec566036a809295934df2dd85f6cf1e9cf66
@@ -30,15 +30,14 @@ module NetHTTPUtils
30
30
  gsub(/<[^>]*>/, "").split(?\n).map(&:strip).reject(&:empty?).join(?\n)
31
31
  end
32
32
 
33
- def start_http url, timeout = 30, max_start_http_retry_delay = 3600
34
- fail if url.is_a? URI::HTTP
33
+ def start_http url, max_start_http_retry_delay = 3600, timeout = 30
35
34
  uri = url
36
35
  uri = URI.parse begin
37
36
  URI url
38
37
  url
39
38
  rescue URI::InvalidURIError
40
39
  URI.escape url
41
- end
40
+ end unless url.is_a? URI::HTTP
42
41
  delay = 5
43
42
  begin
44
43
  Net::HTTP.start(
@@ -95,7 +94,7 @@ module NetHTTPUtils
95
94
  sleep delay
96
95
  retry
97
96
  end.tap do |http|
98
- http.instance_variable_set "@uri", uri
97
+ http.instance_variable_set :@uri, uri
99
98
  http.define_singleton_method :read do |mtd = :GET, type = :form, form: {}, header: {}, auth: nil, timeout: 30,
100
99
  max_read_retry_delay: 3600,
101
100
  patch_request: nil,
@@ -106,10 +105,11 @@ module NetHTTPUtils
106
105
  logger.warn "Warning: query params included in `url` argument are discarded because `:form` isn't empty" if uri.query && !form.empty?
107
106
  # we can't just merge because URI fails to parse such queries as "/?1"
108
107
 
109
- uri.query = URI.encode_www_form form if :GET == (mtd = mtd.upcase) && !form.empty?
108
+ uri.query = URI.encode_www_form form if %i{ HEAD GET }.include?(mtd = mtd.upcase) && !form.empty?
110
109
  cookies = {}
111
- prepare_request = lambda do |uri, mtd = :GET, form = {}|
110
+ prepare_request = lambda do |uri|
112
111
  case mtd.upcase
112
+ when :HEAD ; Net::HTTP::Head
113
113
  when :GET ; Net::HTTP::Get
114
114
  when :POST ; Net::HTTP::Post
115
115
  when :PUT ; Net::HTTP::Put
@@ -140,12 +140,12 @@ module NetHTTPUtils
140
140
 
141
141
  logger.info "> #{request.class} #{uri.host} #{request.path}"
142
142
  next unless logger.debug?
143
- logger.debug "content-type: #{request.content_type}" unless mtd == :GET
143
+ logger.debug "content-length: #{request.content_length.to_i}, content-type: #{request.content_type}" unless %i{ HEAD GET }.include? mtd
144
144
  curl_form = case request.content_type
145
145
  when "application/json" ; "-d #{JSON.dump form} "
146
146
  when "multipart/form-data" ; form.map{ |k, v| "-F \"#{k}=#{v.respond_to?(:to_path) ? "@#{v.to_path}" : v}\" " }.join
147
147
  when "application/x-www-form-urlencoded" ; "-d \"#{URI.encode_www_form form}\" "
148
- else ; mtd == :GET ? "" : fail("unknown content-type '#{request.content_type}'")
148
+ else %i{ HEAD GET }.include?(mtd) ? "" : fail("unknown content-type '#{request.content_type}'")
149
149
  end
150
150
  logger.debug "curl -vsSL --compressed -o /dev/null #{
151
151
  request.each_header.map{ |k, v| "-H \"#{k}: #{v}\" " unless k == "host" }.join
@@ -161,7 +161,7 @@ module NetHTTPUtils
161
161
  logger.debug stack.join " -> "
162
162
  end
163
163
  end
164
- http = NetHTTPUtils.start_http url, timeout, max_start_http_retry_delay
164
+ http = NetHTTPUtils.start_http url, max_start_http_retry_delay, timeout
165
165
  do_request = lambda do |request|
166
166
  delay = 5
167
167
  response = begin
@@ -214,7 +214,7 @@ module NetHTTPUtils
214
214
  http.use_ssl? != (new_uri.scheme == "https")
215
215
  logger.debug "changing host from '#{http.address}' to '#{new_host}'"
216
216
  # http.finish
217
- http = start_http new_uri, timeout, max_start_http_retry_delay
217
+ http = NetHTTPUtils.start_http new_uri, max_start_http_retry_delay, timeout
218
218
  end
219
219
  do_request.call prepare_request[new_uri]
220
220
  when "404"
@@ -256,10 +256,10 @@ module NetHTTPUtils
256
256
  response
257
257
  end
258
258
  end
259
- do_request[prepare_request[uri, mtd, form]].tap do |response|
260
- cookies.each{ |k, v| response.add_field "Set-Cookie", "#{k}=#{v};" }
261
- logger.debug "< header: #{response.to_hash}"
262
- end.body
259
+ response = do_request[prepare_request[uri]]
260
+ cookies.each{ |k, v| response.add_field "Set-Cookie", "#{k}=#{v};" }
261
+ logger.debug "< header: #{response.to_hash}"
262
+ (response.body || "").tap{ |r| r.instance_variable_set :@last_response, response }
263
263
 
264
264
  end
265
265
  end
@@ -269,23 +269,27 @@ module NetHTTPUtils
269
269
  max_start_http_retry_delay: 3600,
270
270
  max_read_retry_delay: 3600,
271
271
  patch_request: nil, &block
272
- http = start_http http, timeout, max_start_http_retry_delay unless http.is_a? Net::HTTP
272
+ http = start_http http, max_start_http_retry_delay, timeout unless http.is_a? Net::HTTP
273
273
  path = http.instance_variable_get(:@uri).path
274
- head = http.head path
275
- raise Error.new(
276
- (head.to_hash["content-type"] == ["image/png"] ? head.to_hash["content-type"] : head.body),
277
- head.code.to_i
278
- ) unless head.code[/\A(20\d|3\d\d)\z/]
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/]
280
+ end
279
281
  body = http.read mtd, type, form: form, header: header, auth: auth, timeout: timeout,
280
282
  max_read_retry_delay: max_read_retry_delay,
281
283
  patch_request: patch_request, &block
282
- if head.to_hash["content-encoding"] == "gzip"
284
+ last_response = body.instance_variable_get :@last_response
285
+ if last_response.to_hash["content-encoding"] == "gzip"
283
286
  Zlib::GzipReader.new(StringIO.new(body)).read
284
287
  else
285
288
  body
286
289
  end.tap do |string|
287
- string.instance_variable_set :@uri_path, path
288
- string.instance_variable_set :@header, head.to_hash
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
289
293
  end
290
294
  # ensure
291
295
  # response.instance_variable_get("@nethttputils_close").call if response
@@ -310,7 +314,7 @@ if $0 == __FILE__
310
314
  server.mount_proc ?/ do |req, res|
311
315
  stack.push req.request_method
312
316
  end
313
- Thread.new{ server.start }
317
+ t = Thread.new{ server.start }
314
318
  NetHTTPUtils.start_http("http://localhost:8000/")
315
319
  fail unless stack == %w{ }
316
320
  stack.clear
@@ -321,6 +325,11 @@ if $0 == __FILE__
321
325
  fail unless stack == %w{ HEAD GET }
322
326
  server.shutdown
323
327
 
328
+ # TODO: test that HEAD method request goes through redirects
329
+ # TODO: test for `NetHTTPUtils.request_data "...", :head
330
+ # TODO: request the HEAD only if mtd == :GET
331
+
332
+ t.join # Address already in use - bind(2) for [::]:8000 (Errno::EADDRINUSE)
324
333
  server = WEBrick::HTTPServer.new Port: 8000
325
334
  server.mount_proc ?/ do |req, res|
326
335
  # pp req.dup.tap{ |_| _.instance_variable_set "@config", nil }
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "nethttputils"
3
- spec.version = "0.3.0.0"
3
+ spec.version = "0.3.1.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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nethttputils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.0
4
+ version: 0.3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon