Skip to content

Commit 7288272

Browse files
authored
Merge pull request #7 from slab/bhan-2-remove-http-poison
[2] make HTTPoison an optional dependency
2 parents 9af5fbb + 65db4dd commit 7288272

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ See the [Documentation][docs] on HexDocs.
2828

2929
## Installation
3030

31-
Add `safeurl` to your project dependencies in `mix.exs`:
31+
To get started, add `safeurl` to your project dependencies in `mix.exs`. Optionally, you may
32+
also add `HTTPoison` to your dependencies for making requests directly through SafeURL:
3233

3334
```elixir
3435
def deps do
35-
[{:safeurl, "~> 0.2.0"}]
36+
[
37+
{:safeurl, "~> 0.2.0"},
38+
{:httpoison, "~> 1.8"}, # Optional
39+
]
3640
end
3741
```
3842

@@ -47,9 +51,9 @@ end
4751
CIDR ranges to the blocklist, or alternatively allow specific CIDR ranges to which the
4852
application is allowed to make requests.
4953

50-
You can use `allowed?/2` or `validate/2` to check if a URL is safe to call, or just call
51-
it directly via `get/4` which will validate it automatically before calling, and return an
52-
error if it is not.
54+
You can use `allowed?/2` or `validate/2` to check if a URL is safe to call, or if you have
55+
the `HTTPoison` application available, just call it directly via `get/4` which will validate
56+
it automatically before calling, and return an error if it is not.
5357

5458

5559
### Examples
@@ -67,6 +71,8 @@ iex> SafeURL.validate("http://230.10.10.10/")
6771
iex> SafeURL.validate("http://230.10.10.10/", block_reserved: false)
6872
:ok
6973

74+
# When HTTPoison is available:
75+
7076
iex> SafeURL.get("https://10.0.0.1/ssrf.txt")
7177
{:error, :restricted}
7278

lib/safeurl.ex

+11-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ defmodule SafeURL do
88
allowed to make requests.
99
1010
You can use `allowed?/2` or `validate/2` to check if a
11-
URL is safe to call, or just call it directly via `get/4`
12-
which will validate it automatically before calling, and
13-
return an error if it is not.
11+
URL is safe to call. If the `HTTPoison` application is
12+
available, you can also call `get/4` directly which will
13+
validate the host before making an HTTP request.
1414
1515
1616
## Examples
@@ -27,6 +27,8 @@ defmodule SafeURL do
2727
iex> SafeURL.validate("http://230.10.10.10/", block_reserved: false)
2828
:ok
2929
30+
# If HTTPoison is available:
31+
3032
iex> SafeURL.get("https://10.0.0.1/ssrf.txt")
3133
{:error, :restricted}
3234
@@ -190,7 +192,8 @@ defmodule SafeURL do
190192
return `{:error, :restricted}`.
191193
192194
`headers` and `httpoison_options` will be passed directly to
193-
`HTTPoison` when the request is executed.
195+
`HTTPoison` when the request is executed. This function will
196+
raise if `HTTPoison` if not available.
194197
195198
See `allowed?/2` for more details on URL validation.
196199
@@ -211,8 +214,11 @@ defmodule SafeURL do
211214
212215
"""
213216
@spec get(binary(), Keyword.t(), HTTPoison.headers(), Keyword.t()) ::
214-
{:ok, HTTPoison.Response.t()} | {:error, :restricted}
217+
{:ok, HTTPoison.Response.t()} | {:error, :restricted} | no_return()
215218
def get(url, options \\ [], headers \\ [], httpoison_options \\ []) do
219+
unless function_exported?(HTTPoison, :get, 3) do
220+
raise "HTTPoison.get/3 not available"
221+
end
216222
with :ok <- validate(url, options) do
217223
HTTPoison.get(url, headers, httpoison_options)
218224
end

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ defmodule SafeURL.MixProject do
5757
# Dependencies
5858
defp deps do
5959
[
60-
{:httpoison, "~> 1.8"},
60+
{:httpoison, "~> 1.8", optional: true},
6161
{:inet_cidr, "~> 1.0"},
6262
{:dns, "~> 2.2"},
6363
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},

0 commit comments

Comments
 (0)