mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-05 08:12:31 -04:00
Fixing /ip to use client's ip when available
This commit is contained in:
parent
944b685306
commit
9552b529c4
2 changed files with 28 additions and 1 deletions
|
@ -10,8 +10,21 @@ defmodule HTTParrot.IPHandler do
|
||||||
|
|
||||||
def get_json(req, state) do
|
def get_json(req, state) do
|
||||||
{{ip, _port}, req} = :cowboy_req.peer(req)
|
{{ip, _port}, req} = :cowboy_req.peer(req)
|
||||||
|
{:ok, forwarded_for, _} = :cowboy_req.parse_header(<<"x-forwarded-for">>, req)
|
||||||
|
case forwarded_for do
|
||||||
|
:undefined ->
|
||||||
|
{response(ip), req, state}
|
||||||
|
_ ->
|
||||||
|
{response(to_tuple(forwarded_for)), req, state}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
{response(ip), req, state}
|
defp to_tuple(client_ip) do
|
||||||
|
client_ip
|
||||||
|
|> hd
|
||||||
|
|> String.split(".")
|
||||||
|
|> Enum.map(fn(x) -> String.to_integer(x) end)
|
||||||
|
|> List.to_tuple
|
||||||
end
|
end
|
||||||
|
|
||||||
defp response(:local) do
|
defp response(:local) do
|
||||||
|
|
|
@ -13,6 +13,7 @@ defmodule HTTParrot.IPHandlerTest do
|
||||||
test "returns prettified json with origin" do
|
test "returns prettified json with origin" do
|
||||||
ip = {127, 1, 2, 3}
|
ip = {127, 1, 2, 3}
|
||||||
expect(:cowboy_req, :peer, 1, {{ip, :host}, :req2})
|
expect(:cowboy_req, :peer, 1, {{ip, :host}, :req2})
|
||||||
|
expect(:cowboy_req, :parse_header, 2, {:ok, :undefined, :req2})
|
||||||
expect(JSX, :encode!, [{[[origin: "127.1.2.3"]], :json}])
|
expect(JSX, :encode!, [{[[origin: "127.1.2.3"]], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :state) == {:json, :req2, :state}
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
@ -21,8 +22,21 @@ defmodule HTTParrot.IPHandlerTest do
|
||||||
assert validate JSX
|
assert validate JSX
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "returns prettified json with client ip when available" do
|
||||||
|
ip = {127, 1, 2, 3}
|
||||||
|
expect(:cowboy_req, :peer, 1, {{ip, :host}, :req2})
|
||||||
|
expect(:cowboy_req, :parse_header, 2, {:ok, ["190.1.2.3"], :req2})
|
||||||
|
expect(JSX, :encode!, [{[[origin: "190.1.2.3"]], :json}])
|
||||||
|
|
||||||
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
|
||||||
|
assert validate :cowboy_req
|
||||||
|
assert validate JSX
|
||||||
|
end
|
||||||
|
|
||||||
test "returns empty when running over unix sockets" do
|
test "returns empty when running over unix sockets" do
|
||||||
expect(:cowboy_req, :peer, 1, {{:local, ""}, :req2})
|
expect(:cowboy_req, :peer, 1, {{:local, ""}, :req2})
|
||||||
|
expect(:cowboy_req, :parse_header, 2, {:ok, :undefined, :req2})
|
||||||
expect(JSX, :encode!, [{[[origin: ""]], :json}])
|
expect(JSX, :encode!, [{[[origin: ""]], :json}])
|
||||||
|
|
||||||
assert get_json(:req1, :state) == {:json, :req2, :state}
|
assert get_json(:req1, :state) == {:json, :req2, :state}
|
||||||
|
|
Loading…
Add table
Reference in a new issue