Class: OpenASN::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/openasn/http_client.rb

Overview

Minimal stdlib HTTP client for the updater and Tier B executor.

  • Always sends a descriptive User-Agent (some endpoints 403 UA-less clients; it's also basic politeness toward the volunteer-run sources this gem depends on).
  • Follows redirects across hosts — GitHub release downloads ALWAYS redirect to objects.githubusercontent.com / release-assets.…; if your egress is allowlisted, those hosts must be on the list too.
  • Supports conditional GET via ETag (returns :not_modified).
  • Never talks to api.github.com (60 req/hr unauthenticated limit); releases/download// asset URLs redirect fine without auth.

Defined Under Namespace

Classes: Response

Constant Summary collapse

MAX_REDIRECTS =
5
OPEN_TIMEOUT =
10
READ_TIMEOUT =

artifacts are ~6MB; Apple's relay CSV ~10MB

120

Instance Method Summary collapse

Constructor Details

#initialize(user_agent:, logger:) ⇒ HttpClient

Returns a new instance of HttpClient.



25
26
27
28
# File 'lib/openasn/http_client.rb', line 25

def initialize(user_agent:, logger:)
  @user_agent = user_agent
  @logger = logger
end

Instance Method Details

#get(url, etag: nil) ⇒ Object

-> Response | :not_modified. Raises on HTTP errors / timeouts.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/openasn/http_client.rb', line 31

def get(url, etag: nil)
  headers = { "User-Agent" => @user_agent, "Accept-Encoding" => "identity" }
  headers["If-None-Match"] = etag if etag

  response = request(Net::HTTP::Get, url, headers, MAX_REDIRECTS)
  case response
  when Net::HTTPNotModified then :not_modified
  when Net::HTTPSuccess then Response.new(body: response.body, etag: response["etag"])
  else raise UpdateError, "HTTP #{response.code} for #{url}"
  end
end

#post_form(url, form) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/openasn/http_client.rb', line 43

def post_form(url, form)
  headers = { "User-Agent" => @user_agent,
              "Accept-Encoding" => "identity",
              "Content-Type" => "application/x-www-form-urlencoded" }
  response = request(Net::HTTP::Post, url, headers, MAX_REDIRECTS, URI.encode_www_form(form))
  case response
  when Net::HTTPSuccess then Response.new(body: response.body, etag: response["etag"])
  else raise UpdateError, "HTTP #{response.code} for #{url}"
  end
end