Packages
diint_utilites_common_app
1.0.11
1.4.23
1.4.22
1.4.21
1.4.20
1.4.19
1.4.18
1.4.17
1.4.16
1.4.15
1.4.14
1.4.12
1.4.11
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.9
1.3.8
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.101
1.2.11
1.2.10
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.13
1.0.12
1.0.11
1.0.10
1.0.9
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
библиотеки для работы с ребитом и протоколы
Current section
Files
Jump to
Current section
Files
src/network/http_utilites.erl
%%%-------------------------------------------------------------------
%%% @author cheese
%%% @copyright (C) 2015, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 24. Nov 2015 8:22
%%%-------------------------------------------------------------------
-module(http_utilites).
-author("cheese").
%% API
-export([is_valid_client/2, get_current_user_ip/1, is_websocket_request/1, get_header_value/2]).
%% Internal
-export([get_client_ip/2, check_client_ip/2]).
get_current_user_ip(CowboyReq) ->
{{Client, _Port}, Req1} = cowboy_req:peer(CowboyReq),
{XRealIP, Req} = cowboy_req:header(<<"x-real-ip">>,Req1, <<"">>),
{get_client_ip(XRealIP, Client), Req}.
-spec(is_valid_client(any(), [binary()]) -> {ok, binary(), any()} | {error, binary(), any()}).
is_valid_client(CowboyReq, ValidIpList) ->
{RealClient, Req} = get_current_user_ip(CowboyReq),
case check_client_ip(RealClient, ValidIpList) of
{ok} ->
{ok, RealClient, Req};
{error, Reason} ->
{error, RealClient, Reason}
end.
%% Internal
get_client_ip(<<>>, Client) ->
Client;
get_client_ip(XRealIP, _Client) ->
socket_utilites:parse_ip_from_bytes(XRealIP).
check_client_ip(_ClientIP, []) ->
{error, access_denied};
check_client_ip(_ClientIP, [all | _OtherIP]) ->
{ok};
check_client_ip(ClientIP, [ClientIP | _OtherIP]) ->
{ok};
check_client_ip(ClientIP, [_IP | OtherIP]) ->
check_client_ip(ClientIP, OtherIP).
-spec(is_websocket_request(term()) -> {true | false, term()}).
is_websocket_request(Req) ->
{AllHeaders, Req2} = cowboy_req:headers(Req),
Value = get_header_value(<<"UPGRADE">>, AllHeaders),
case list_to_binary(string:to_upper(binary_to_list(Value))) of
<<"WEBSOCKET">> ->
{true, Req2};
_ ->
{false, Req2}
end.
-spec(get_header_value(binary(), [{term(), term()}]) -> binary()).
get_header_value(_Name, []) ->
<<>>;
get_header_value(Name, [{Key, Val} | Tail]) ->
KeyUpper = list_to_binary(string:to_upper(binary_to_list(Key))),
case bytes_extension:are_equal(KeyUpper, Name) of
true ->
Val;
_ ->
get_header_value(Name, Tail)
end.