# `Julep.Proxy`
[🔗](https://github.com/RiverFinancial/julep/blob/v0.1.0-rc.1/lib/julep/proxy.ex#L1)

HTTP and HTTPS proxies for tunnelling WebSocket connections.

To connect through a proxy, build one with `new/2` and pass it as the
`:proxy` option to `Julep.start_link/2`. Its credentials are left out of
`inspect/1`.

Julep never reads the environment itself. To respect `https_proxy`,
`no_proxy` and the like, call `resolve!/2` and pass on the result:

    Julep.start_link(MyApp.Ticker, url: url, proxy: Julep.Proxy.resolve!(url))

# `env`

```elixir
@type env() :: %{optional(String.t()) =&gt; String.t()}
```

# `resolution`

```elixir
@type resolution() :: {:ok, t()} | :none | :error
```

# `t`

```elixir
@type t() :: %Julep.Proxy{
  auth: {username :: String.t(), password :: String.t()} | nil,
  host: String.t(),
  port: :inet.port_number(),
  scheme: :http | :https,
  tls_opts: keyword()
}
```

# `new`

```elixir
@spec new(String.t(), [{:tls_opts, keyword()}]) :: {:ok, t()} | :error
```

Builds a proxy from a URL such as `"http://user:secret@proxy.internal:3128"`.

Returns `:error` unless the URL has an `http` or `https` scheme and a host.

## Options

  * `:tls_opts` - TLS options for the connection to an `https` proxy, merged
    over Mint's defaults. Ignored for an `http` proxy. Defaults to `[]`.

# `resolve`

```elixir
@spec resolve(String.t(), env: env(), tls_opts: keyword()) :: resolution()
```

Resolves the proxy for a `ws://` or `wss://` URL from the environment.

Returns `{:ok, proxy}` when a proxy applies, `:none` when the URL should be
reached directly, and `:error` when the variable that applies does not hold
an `http` or `https` proxy URL, as for `new/2`.

Follows the conventions of `curl` and most HTTP clients:

  * A `wss://` URL uses `https_proxy`, then `http_proxy`, then `all_proxy`. A
    `ws://` URL uses `http_proxy`, then `all_proxy`.
  * A lowercase variable wins over its uppercase form, and an empty value
    counts as unset.
  * `no_proxy` is a comma-separated list of hosts to reach directly, ignoring
    case. `*` matches every host. A name matches itself and its subdomains,
    with or without a leading `.` or `*.`. An IP address matches the same
    address however it is written, so `::1` matches `0:0:0:0:0:0:0:1`.
  * An address range, such as `10.0.0.0/8` or `fd00::/8`, matches a URL whose
    host is an address inside it. Names are never resolved to check them
    against a range.
  * A `no_proxy` entry with a port, such as `example.com:8443` or
    `[::1]:8443`, matches that port only.

## Options

  * `:env` - the environment to read, as a map of variable names to values.
    Defaults to `System.get_env/0`.
  * `:tls_opts` - TLS options for the resolved proxy, as for `new/2`.

# `resolve!`

```elixir
@spec resolve!(String.t(), env: env(), tls_opts: keyword()) :: t() | nil
```

Resolves the proxy for a URL from the environment, as `resolve/2` does.

Returns the proxy, or `nil` when the URL should be reached directly. Raises
where `resolve/2` returns `:error`.

Takes the options of `resolve/2`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
