Karta.NetGuard.HTTP (Karta v1.0.0)

One HTTP request to a URL that someone else gave Karta (a webhook's URL), through Karta.NetGuard:

  • Where. The URL's host is resolved once by Karta.NetGuard.resolve/2 (an address in the URL is checked the same way); refused addresses are never connected to. Karta connects to the checked addresses themselves, in order, until one accepts the connection: no second lookup, so a name that resolves to another address the next time (DNS rebinding) changes nothing. The request still names the host: the Host header, and for https the TLS server name (SNI) and the name the certificate must be valid for.
  • TLS. For https, TLS 1.2 or 1.3 and a certificate that chains to a trusted root (the operating system's, or :cacerts) and is valid for the host (RFC 6125, wildcards allowed).
  • HTTP. HTTP/1.1, one request per connection. A redirect is an answer like any other: it is never followed.
  • Limits. The whole request, every address and every read, must end within :timeout (default 15 s). At most :max_body bytes of the answer's body are read (default 16 KiB); the rest is not read, and the answer says truncated: true.

Returns {:ok, %{status:, headers:, body:, truncated:, address:}} (the address connected to, as text) or {:error, reason} with the reason in words.

Options: :timeout, :max_body, :cacerts, and :dns, :allowed_networks, :blocked_networks for Karta.NetGuard.

Summary

Functions

Checks that Karta may connect to the URL's host: an address in the URL is checked, and a name is resolved and every address checked (Karta.NetGuard), as a request to it would be. The lookup happens now, so a name that later resolves elsewhere is still checked then.

Checks a URL that is to be stored for later requests: http or https, a host, no user name or password, no fragment. Its host is not resolved: that happens at each request.

Sends one request. See the module doc.

Types

response()

@type response() :: %{
  status: 100..599,
  headers: [{String.t(), String.t()}],
  body: binary(),
  truncated: boolean(),
  address: String.t()
}

Functions

check_destination(url)

@spec check_destination(String.t()) :: :ok | {:error, String.t()}

Checks that Karta may connect to the URL's host: an address in the URL is checked, and a name is resolved and every address checked (Karta.NetGuard), as a request to it would be. The lookup happens now, so a name that later resolves elsewhere is still checked then.

check_url(url)

@spec check_url(String.t()) :: :ok | {:error, String.t()}

Checks a URL that is to be stored for later requests: http or https, a host, no user name or password, no fragment. Its host is not resolved: that happens at each request.

request(method, url, headers, body, opts \\ [])

@spec request(String.t(), String.t(), [{String.t(), String.t()}], iodata(), keyword()) ::
  {:ok, response()} | {:error, String.t()}

Sends one request. See the module doc.