Module: Kotoshu::Integrity::NetHTTP

Defined in:
lib/kotoshu/integrity/net_http.rb

Overview

Thin wrapper around Net::HTTP so manifest fetches are testable without the network. Returns response body as a String on 2xx, nil on 404/410 (so callers can treat “manifest not published yet” as graceful degradation), and raises on other errors.

Defined Under Namespace

Classes: HttpError, TooManyRedirects

Class Method Summary collapse

Class Method Details

.get(url, redirect_limit: 3) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kotoshu/integrity/net_http.rb', line 14

def get(url, redirect_limit: 3)
  uri = URI(url)
  raise ArgumentError, "Only http/https supported: #{url}" unless
    uri.scheme == "http" || uri.scheme == "https"

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == "https")
  http.open_timeout = 10
  http.read_timeout = 30

  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  case response
  when Net::HTTPSuccess
    response.body
  when Net::HTTPNotFound, Net::HTTPGone
    nil
  when Net::HTTPRedirection
    raise TooManyRedirects if redirect_limit.zero?

    get(response["location"], redirect_limit: redirect_limit - 1)
  else
    raise HttpError, "GET #{url} failed: #{response.code} #{response.message}"
  end
end