Module: ClaudeAgentSDK::CLIInstaller::Http

Defined in:
lib/claude_agent_sdk/cli_installer.rb

Overview

HTTPS GET with bounded redirects, timeouts, a response-size cap for text and chunked streaming for the binary. Knows nothing about releases; the specs stub .fetch_text / .download_to wholesale so no HTTP stubbing library is needed.

Constant Summary collapse

MAX_REDIRECTS =
5
OPEN_TIMEOUT_SECONDS =
10
READ_TIMEOUT_SECONDS =
60

Class Method Summary collapse

Class Method Details

.download_to(url, path, max_bytes: nil) ⇒ Object

Streams the response to path in chunks — the CLI binary is ~280MB and must never be materialized in memory. O_EXCL: path must not exist, so a pre-planted file or symlink is never written through. max_bytes (the manifest's declared size, when it has one) aborts a response that runs long instead of filling the disk before the checksum gets a chance to reject it.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/claude_agent_sdk/cli_installer.rb', line 142

def download_to(url, path, max_bytes: nil)
  with_response(url) do |response|
    written = 0
    File.open(path, File::WRONLY | File::CREAT | File::EXCL | File::BINARY, 0o600) do |file|
      response.read_body do |chunk|
        written += chunk.bytesize
        raise CLIInstallError, "Download from #{url} exceeds the expected #{max_bytes} bytes" if over?(written, max_bytes)

        file.write(chunk)
      end
    end
  end
  path
end

.fetch_text(url, limit:) ⇒ Object

limit bounds how many bytes are buffered: the caller knows the expected shape of the response, and an unbounded read of a misrouted (or hostile) endpoint is an easy way to exhaust memory.



125
126
127
128
129
130
131
132
133
134
# File 'lib/claude_agent_sdk/cli_installer.rb', line 125

def fetch_text(url, limit:)
  with_response(url) do |response|
    body = +''
    response.read_body do |chunk|
      body << chunk
      raise CLIInstallError, "Response from #{url} exceeds the #{limit}-byte limit" if body.bytesize > limit
    end
    body
  end
end