Skip to content

Commit

Permalink
move uri-related calls from kz_util to kz_http_util (2600hz#6135)
Browse files Browse the repository at this point in the history
* move uri-related calls from kz_util to kz_http_util

* separate legacy from rfc implementations

* update usages of urldecode
  • Loading branch information
jamesaimonetti authored and lazedo committed Nov 5, 2019
1 parent fc2bc9e commit 44cf600
Show file tree
Hide file tree
Showing 30 changed files with 265 additions and 216 deletions.
5 changes: 2 additions & 3 deletions applications/crossbar/src/cb_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,11 @@ method(#cb_context{method=M}) -> M.
pretty_print(#cb_context{pretty_print = PrettyPrint}) -> PrettyPrint.

-spec path_token(binary()) -> binary().
path_token(Token) ->
kz_util:uri_decode(Token).
path_token(Token) -> kz_http_util:urldecode(Token).

-spec path_tokens(context()) -> kz_term:ne_binaries().
path_tokens(#cb_context{raw_path=Path}) ->
[path_token(kz_util:uri_decode(Token))
[path_token(Token)
|| Token <- binary:split(Path, <<"/">>, ['global', 'trim'])
].

Expand Down
2 changes: 1 addition & 1 deletion applications/crossbar/src/cb_mobile_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ req_uri(ExplodedPath) ->
case kapps_config:get_binary(?MOD_CONFIG_CAT, <<"url">>) of
'undefined' -> 'undefined';
Url ->
Uri = kz_util:uri(Url, ExplodedPath),
Uri = kz_http_util:uri(Url, ExplodedPath),
kz_term:to_list(Uri)
end.

Expand Down
2 changes: 1 addition & 1 deletion applications/crossbar/src/crossbar_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ apply_response_map_item({Key, ExistingKey}, J, JObj) ->

-spec get_path(cowboy_req:req() | kz_term:ne_binary(), kz_term:ne_binary()) -> kz_term:ne_binary().
get_path(<<_/binary>> = RawPath, Relative) ->
kz_util:resolve_uri(RawPath, Relative);
kz_http_util:resolve_uri(RawPath, Relative);
get_path(Req, Relative) ->
get_path(cowboy_req:path(Req), Relative).

Expand Down
2 changes: 1 addition & 1 deletion applications/crossbar/src/modules/cb_port_requests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ find_template(ResellerId, 'undefined') ->
{'ok', Template} = kz_pdf:find_template(ResellerId, <<"loa">>),
Template;
find_template(ResellerId, CarrierName) ->
EncodedCarrierName = kz_term:to_lower_binary(kz_util:uri_encode(CarrierName)),
EncodedCarrierName = kz_term:to_lower_binary(kz_http_util:urlencode(CarrierName)),
TemplateName = <<EncodedCarrierName/binary, ".tmpl">>,
lager:debug("looking for carrier template ~s or plain template for reseller ~s"
,[TemplateName, ResellerId]),
Expand Down
7 changes: 3 additions & 4 deletions applications/crossbar/src/modules/cb_quickcall.erl
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ originate_quickcall(Endpoints, Call, Context) ->
originate(Request, Context, cb_context:req_verb(Context), CallTimeoutS).

originate(Request, Context, ?HTTP_GET, _CallTimeoutS) ->
kz_amqp_worker:cast(Request, fun kapi_resource:publish_originate_req/1),
_ = kz_amqp_worker:cast(Request, fun kapi_resource:publish_originate_req/1),
JObj = kz_json:normalize(kz_api:remove_defaults(Request)),
crossbar_util:response_202(<<"quickcall initiated">>, JObj, cb_context:set_resp_data(Context, Request));
originate(Request, Context, ?HTTP_POST, CallTimeoutS) ->
Expand Down Expand Up @@ -378,14 +378,14 @@ get_media(Context) ->
get_cid_name(Context, Default) ->
case cb_context:req_value(Context, <<"cid-name">>, Default) of
'undefined' -> 'undefined';
CIDName -> kz_util:uri_decode(CIDName)
CIDName -> kz_http_util:urldecode(CIDName)
end.

-spec get_cid_number(cb_context:context(), kz_term:api_binary()) -> kz_term:api_binary().
get_cid_number(Context, Default) ->
case cb_context:req_value(Context, <<"cid-number">>, Default) of
'undefined' -> 'undefined';
CIDNumber -> kz_util:uri_decode(CIDNumber)
CIDNumber -> kz_http_util:urldecode(CIDNumber)
end.

-spec maybe_retain_cid(cb_context:context()) -> kz_term:proplist().
Expand All @@ -394,4 +394,3 @@ maybe_retain_cid(Context) ->
'undefined' -> [{<<"Retain-CID">>, <<"false">>}];
_Found -> [{<<"Retain-CID">>, <<"true">>}]
end.

2 changes: 1 addition & 1 deletion applications/crossbar/src/modules/phonebook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ handle_resp(Type, Error) ->
-spec phonebook_uri(iolist()) -> string().
phonebook_uri(ExplodedPath) ->
Url = kapps_config:get_binary(?MOD_CONFIG_CAT, <<"phonebook_url">>),
Uri = kz_util:uri(Url, ExplodedPath),
Uri = kz_http_util:uri(Url, ExplodedPath),
kz_term:to_list(Uri).

-spec req_headers(kz_term:ne_binary()) -> kz_term:proplist().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ xml_member_to_participant([#xmlElement{name='uuid'
}
|XmlElements
], Participant) ->
CallId = kz_util:uri_decode(xml_text_to_binary(UUID)),
CallId = kz_http_util:urldecode(xml_text_to_binary(UUID)),
lager:debug("uuid ~s callid ~s", [xml_text_to_binary(UUID), CallId]),
xml_member_to_participant(XmlElements
,Participant#participant{uuid=kz_term:to_binary(CallId)}
Expand Down
2 changes: 1 addition & 1 deletion applications/media_mgr/src/media_store_proxy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ setup_context(Req, #media_store_path{att=Attachment}=Path) ->

-spec decode_url(kz_term:ne_binary()) -> media_store_path() | 'error'.
decode_url(Url) ->
try binary_to_term(base64:decode(kz_util:uri_decode(Url))) of
try binary_to_term(base64:decode(kz_http_util:urldecode(Url))) of
{Db, Id, Attachment, Options} ->
#media_store_path{db = Db
,id = Id
Expand Down
2 changes: 1 addition & 1 deletion applications/notify/src/notify_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ category_to_file(_) ->
qr_code_image('undefined') -> 'undefined';
qr_code_image(Text) ->
lager:debug("create qr code for ~s", [Text]),
CHL = kz_util:uri_encode(Text),
CHL = kz_http_util:urlencode(Text),
Url = <<"https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=", CHL/binary, "&choe=UTF-8">>,

case kz_http:get(kz_term:to_list(Url)) of
Expand Down
2 changes: 1 addition & 1 deletion applications/tasks/src/modules/kt_compaction_reporter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,4 @@ save_compaction_stats(#{'id' := Id

-spec normalize_db(kz_term:ne_binary()) -> kz_term:ne_binary().
normalize_db(Db) ->
kz_util:uri_decode(Db).
kz_http_util:urldecode(Db).
2 changes: 1 addition & 1 deletion applications/tasks/src/modules/kt_compactor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ get_db_disk_and_data_fold(Conn, UnencDb, State, _ChunkSize) ->
,{[db_and_sizes()], non_neg_integer()}
) -> {[db_and_sizes()], pos_integer()}.
do_get_db_disk_and_data_fold(Conn, UnencDb, {Acc, Counter}) ->
EncDb = kz_util:uri_encode(UnencDb),
EncDb = kz_http_util:urlencode(UnencDb),

case kt_compactor_worker:get_db_disk_and_data(Conn, EncDb) of
'not_found' ->
Expand Down
66 changes: 0 additions & 66 deletions core/kazoo/src/kz_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@
,format_resource_selectors_db/1
]).

-export([uri_encode/1
,uri_decode/1
,resolve_uri/2
]).

-export([uri/2]).

-export([pretty_print_bytes/1, pretty_print_bytes/2
,bin_usage/0, mem_usage/0
]).
Expand Down Expand Up @@ -60,10 +53,6 @@

-export([kz_log_md_clear/0, kz_log_md_put/2]).

-ifdef(TEST).
-export([resolve_uri_path/2]).
-endif.

-deprecated({'log_stacktrace', 0, 'next_major_release'}).
-deprecated({'log_stacktrace', 2, 'next_major_release'}).

Expand Down Expand Up @@ -517,61 +506,6 @@ get_event_type(JObj) ->
,kz_json:get_value(<<"Event-Name">>, JObj)
}.

-spec uri_decode(kz_term:text()) -> kz_term:text().
uri_decode(Binary) when is_binary(Binary) ->
kz_term:to_binary(http_uri:decode(kz_term:to_list(Binary)));
uri_decode(String) when is_list(String) ->
http_uri:decode(String);
uri_decode(Atom) when is_atom(Atom) ->
kz_term:to_atom(http_uri:decode(kz_term:to_list(Atom)), 'true').

-spec uri_encode(kz_term:text()) -> kz_term:text().
uri_encode(Binary) when is_binary(Binary) ->
kz_term:to_binary(http_uri:encode(kz_term:to_list(Binary)));
uri_encode(String) when is_list(String) ->
http_uri:encode(String);
uri_encode(Atom) when is_atom(Atom) ->
kz_term:to_atom(http_uri:encode(kz_term:to_list(Atom)), 'true').

-spec resolve_uri(nonempty_string() | kz_term:ne_binary(), nonempty_string() | kz_term:api_ne_binary()) -> kz_term:ne_binary().
resolve_uri(Raw, 'undefined') -> kz_term:to_binary(Raw);
resolve_uri(_Raw, <<"http", _/binary>> = Abs) -> Abs;
resolve_uri(<<_/binary>> = RawPath, <<_/binary>> = Relative) ->
Path = resolve_uri_path(RawPath, Relative),
kz_binary:join(Path, <<"/">>);
resolve_uri(RawPath, Relative) ->
resolve_uri(kz_term:to_binary(RawPath), kz_term:to_binary(Relative)).

-spec resolve_uri_path(kz_term:ne_binary(), kz_term:ne_binary()) -> kz_term:ne_binaries().
resolve_uri_path(RawPath, Relative) ->
PathTokensRev = lists:reverse(binary:split(RawPath, <<"/">>, ['global'])),
UrlTokens = binary:split(Relative, <<"/">>, ['global']),
lists:reverse(
lists:foldl(fun resolve_uri_fold/2, PathTokensRev, UrlTokens)
).

-spec resolve_uri_fold(kz_term:ne_binary(), kz_term:ne_binaries()) -> kz_term:ne_binaries().
resolve_uri_fold(<<"..">>, []) -> [];
resolve_uri_fold(<<"..">>, [_ | PathTokens]) -> PathTokens;
resolve_uri_fold(<<".">>, PathTokens) -> PathTokens;
resolve_uri_fold(<<>>, PathTokens) -> PathTokens;
resolve_uri_fold(Segment, [<<>>|DirTokens]) -> [Segment|DirTokens];
resolve_uri_fold(Segment, [LastToken|DirTokens]=PathTokens) ->
case filename:extension(LastToken) of
<<>> ->
%% no extension, append Segment to Tokens
[Segment | PathTokens];
_Ext ->
%% Extension found, append Segment to DirTokens
[Segment|DirTokens]
end.

-spec uri(kz_term:ne_binary(), kz_term:ne_binaries()) -> kz_term:ne_binary().
uri(BaseUrl, Tokens) ->
[Pro, Url] = binary:split(BaseUrl, <<"://">>),
Uri = filename:join([Url | Tokens]),
<<Pro/binary, "://", Uri/binary>>.

%%------------------------------------------------------------------------------
%% @doc Fetch and cache the kazoo version from the VERSION file in kazoo's root folder/
%% @end
Expand Down
28 changes: 0 additions & 28 deletions core/kazoo/test/kz_util_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,6 @@ put_callid_test_() ->
,?_assert(is_integer(begin kz_util:set_startup(), kz_util:startup() end))
].


uri_test_() ->
[?_assertEqual(<<"http://test.com/path1/path2">>, kz_util:uri(<<"http://test.com">>, [<<"path1">>, <<"path2">>]))
,?_assertEqual(<<"http://192.168.0.1:8888/path1/path2">>, kz_util:uri(<<"http://192.168.0.1:8888/">>, [<<"path1">>, <<"path2">>]))
,?_assertEqual(<<"http://test.com/path1/path2">>, kz_util:uri(<<"http://test.com/">>, [<<"path1/">>, <<"path2/">>]))
].

spawns_test_() ->
[?_assert(is_pid(kz_util:spawn(fun () -> io:format("x") end)))
,?_assert(is_pid(kz_util:spawn(fun (X) -> io:format("~p",[X]) end, [x])))
Expand All @@ -130,27 +123,6 @@ spawns_test_() ->
,?_assertMatch({_,_}, kz_util:spawn_monitor(fun (X) -> io:format("~p",[X]) end, [x]))
].

resolve_uri_test_() ->
RawPath = <<"http://pivot/script.php">>,
Relative = <<"script2.php">>,
[?_assertEqual(<<"http://pivot/script2.php">>, kz_util:resolve_uri(RawPath, Relative))
,?_assertEqual(<<"http://pivot/script2.php">>, kz_util:resolve_uri(RawPath, <<"/", Relative/binary>>))
,?_assertEqual(Relative, kz_util:resolve_uri(Relative, undefined))
,?_assertEqual(RawPath, kz_util:resolve_uri(Relative, RawPath))
,?_assertEqual(Relative, kz_util:resolve_uri(kz_term:to_list(Relative), undefined))
,?_assertEqual(RawPath, kz_util:resolve_uri(kz_term:to_list(Relative), RawPath))
,?_assertEqual(RawPath, kz_util:resolve_uri(Relative, kz_term:to_list(RawPath)))
,?_assertEqual(<<"http://host/d1/d2/a">>, kz_util:resolve_uri(<<"http://host/d1/d2/d3/file.ext">>, <<"../.././a">>))
].

resolve_uri_path_test_() ->
RawPath = <<"http://pivot/script.php">>,
Relative = <<"script2.php">>,
RawPathList = [<<"http:">>, <<>>, <<"pivot">>, <<"script2.php">>],
[?_assertEqual(RawPathList, kz_util:resolve_uri_path(RawPath, Relative))
,?_assertEqual(RawPathList, kz_util:resolve_uri_path(RawPath, <<"/", Relative/binary>>))
].

account_formats_test_() ->
AccountId = <<A:2/binary, B:2/binary, Rest:28/binary>> = kz_binary:rand_hex(16),
AccountDbUn = list_to_binary(["account/", A, "/", B, "/", Rest]),
Expand Down
6 changes: 3 additions & 3 deletions core/kazoo_attachments/src/kz_att_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ format_url_from_metadata(Map, {DbName, DocId, AName}, Format, JObj) ->
Args = [{<<"attachment">>, AName}
,{<<"id">>, DocId}
,{<<"account_id">>, kz_doc:account_id(JObj, <<"unknown_account">>)}
,{<<"db">>, kz_util:uri_decode(DbName)}
,{<<"db">>, kz_http_util:urldecode(DbName)}
],
format_url(Map, kz_doc:public_fields(JObj), Args, Format).

Expand Down Expand Up @@ -107,14 +107,14 @@ format_url_field(JObj, Args, #{<<"arg">> := Arg}, Fields) ->
format_url_field(_JObj, Args, {'arg', Arg}, Fields) ->
case props:get_value(Arg, Args) of
'undefined' -> Fields;
V -> [kz_util:uri_encode(V) | Fields]
V -> [kz_http_util:urlencode(V) | Fields]
end;
format_url_field(JObj, Args, #{<<"field">> := Field}, Fields) ->
format_url_field(JObj, Args, {'field', Field}, Fields);
format_url_field(JObj, _Args, {'field', Field}, Fields) ->
case kz_json:get_ne_binary_value(Field, JObj) of
'undefined' -> Fields;
V -> [kz_util:uri_encode(V) | Fields]
V -> [kz_http_util:urlencode(V) | Fields]
end;
format_url_field(JObj, Args, #{<<"const">> := Field}, Fields) ->
format_url_field(JObj, Args, {'const', Field}, Fields);
Expand Down
12 changes: 6 additions & 6 deletions core/kazoo_auth/src/kz_auth_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ jwt_assertion(Map) ->
jwt_request(#{auth_url := URL
,jwt := JWT
}=Map) ->
GrantType = kz_term:to_list(kz_util:uri_encode(?OAUTH_GRANT_TYPE)),
GrantType = kz_term:to_list(kz_http_util:urlencode(?OAUTH_GRANT_TYPE)),
Headers = [{"Content-Type","application/x-www-form-urlencoded"}
,{"User-Agent", "Kazoo"}
],
Fields = [{"assertion", kz_term:to_list(kz_util:uri_encode(JWT))}
Fields = [{"assertion", kz_term:to_list(kz_http_util:urlencode(JWT))}
,{"grant_type", GrantType}
],
Body = string:join(lists:append(lists:map(fun({K,V}) -> [string:join([K,V], "=") ] end, Fields)),"&"),
Expand Down Expand Up @@ -322,14 +322,14 @@ add_claim(<<"exp">>) -> {'exp', kz_time:current_unix_tstamp()+(2 * ?MILLISECONDS
request(URL, Token) ->
request('get', URL, <<>>, Token).

-spec request(atom(), kz_term:ne_binary(), binary(), map()) -> {'ok', binary()} | {'error', any()}.
-spec request(kz_http:method(), kz_term:ne_binary(), kz_http:http_body(), map()) -> {'ok', binary()} | {'error', any()}.
request(Verb, URL, Body, #{token := #{authorization := Authorization}}) ->
Options = [{'headers_as_is', 'true'}
,{'ssl', [{'versions', ['tlsv1.2']}]}
],
{'ok',{_,_, Host, _, _, _}} = http_uri:parse(URL),
Headers = [{<<"host">>, Host}
,{<<"Content-Type">>, <<"application/json">>}
{'ok', {_,_, Host, _, _, _}} = http_uri:parse(URL),
Headers = [{"host", Host}
,{"Content-Type", "application/json"}
,{"Authorization", kz_term:to_list(Authorization)}
],
case kz_http:req(Verb, URL, Headers, Body, Options) of
Expand Down
4 changes: 2 additions & 2 deletions core/kazoo_auth/src/mfa_provider/kz_mfa_duo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ authenticate(Claims, JObj) ->
,props:get_ne_binary_value(<<"mfa_resp">>, Claims)
).

-spec maybe_sign_or_verify(map() | mfa_error(), kz_term:api_ne_binary()) -> mfa_result().
-spec maybe_sign_or_verify(map() | {'error', mfa_error()}, kz_term:api_ne_binary()) -> mfa_result().
maybe_sign_or_verify(#{}=Identity, 'undefined') ->
sign_request(Identity);
maybe_sign_or_verify(#{}=Identity, SigResponse) ->
Expand Down Expand Up @@ -244,7 +244,7 @@ is_user_name(Maps) ->
%% @doc
%% @end
%%------------------------------------------------------------------------------
-spec map_config(kz_term:proplist(), kz_json:object()) -> map() | mfa_error().
-spec map_config(kz_term:proplist(), kz_json:object()) -> map() | {'error', mfa_error()}.
map_config(Claims, JObj) ->
Identity = maps:from_list(
[{<<"user_name">>, props:get_value(<<"owner_id">>, Claims)}
Expand Down
4 changes: 2 additions & 2 deletions core/kazoo_cnam/src/cnam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ lookup(<<_/binary>> = Number) ->
'false' -> knm_converters:normalize(Number);
'true' -> Number
end,
lookup(kz_json:set_values([{<<"phone_number">>, kz_util:uri_encode(Num)}
lookup(kz_json:set_values([{<<"phone_number">>, kz_http_util:urlencode(Num)}
,{<<"Caller-ID-Number">>, Num}
]
,kz_json:new()
Expand All @@ -98,7 +98,7 @@ lookup(JObj) ->

-spec set_phone_number(kz_term:ne_binary(), kz_json:object()) -> kz_json:object().
set_phone_number(Num, JObj) ->
kz_json:set_value(<<"phone_number">>, kz_util:uri_encode(Num), JObj).
kz_json:set_value(<<"phone_number">>, kz_http_util:urlencode(Num), JObj).

-spec update_request(kz_json:object(), kz_term:api_binary(), boolean()) -> kz_json:object().
update_request(JObj, 'undefined', _) -> JObj;
Expand Down
4 changes: 2 additions & 2 deletions core/kazoo_data/src/kz_datamgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,7 @@ do_refresh_views(DbName) ->
Updated = case view_definitions(DbName, Classification) of
[] -> 'false';
Views ->
Database = kz_util:uri_encode(kz_util:uri_decode(DbName)),
Database = kz_http_util:urlencode(kz_http_util:urldecode(DbName)),
db_view_update(Database, Views)
end,

Expand All @@ -1900,7 +1900,7 @@ do_refresh_views(DbName) ->

-spec view_definitions(kz_term:ne_binary(), atom() | kz_term:ne_binary()) -> views_listing().
view_definitions(DbName, Classification) ->
ViewOptions = [kz_util:uri_decode(DbName), kz_term:to_binary(Classification)],
ViewOptions = [kz_http_util:urldecode(DbName), kz_term:to_binary(Classification)],
case get_result_docs(?KZ_DATA_DB, <<"views/views_by_classification">>, ViewOptions) of
{'error', _} -> [];
{'ok', JObjs} ->
Expand Down
4 changes: 2 additions & 2 deletions core/kazoo_media/src/kz_media_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ proxy_uri(#media_store_path{db = Db
,StreamType
) ->
_ = maybe_prepare_proxy(StreamType, Store),
Path = kz_util:uri_encode(base64:encode(term_to_binary({Db, Id, Attachment, Options}))),
File = kz_util:uri_encode(Attachment),
Path = kz_http_util:urlencode(base64:encode(term_to_binary({Db, Id, Attachment, Options}))),
File = kz_http_util:urlencode(Attachment),
UrlParts = [kz_media_util:proxy_base_url(StreamType)
,StreamType
,Path
Expand Down
2 changes: 1 addition & 1 deletion core/kazoo_number_manager/src/carriers/knm_other.erl
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ get_blocks(?BLOCK_PHONEBOOK_URL, _Prefix, _Quantity, Options) ->
get_blocks(Url, Prefix, Quantity, Options) ->
Offset = props:get_binary_value('offset', Options, <<"0">>),
Limit = props:get_binary_value('blocks', Options, <<"0">>),
ReqBody = list_to_binary(["?prefix=", kz_util:uri_encode(Prefix)
ReqBody = list_to_binary(["?prefix=", kz_http_util:urlencode(Prefix)
,"&size=", kz_term:to_binary(Quantity)
,"&offset=", Offset
,"&limit=", Limit
Expand Down
Loading

0 comments on commit 44cf600

Please sign in to comment.