refactor(emqx_coap): use url_string module to decode url encoded topic

This commit is contained in:
Zaiming Shi 2021-01-23 23:32:42 +01:00
parent c1b1ddbb6f
commit 8a05380388
1 changed files with 9 additions and 1 deletions

View File

@ -241,7 +241,7 @@ handle_received_publish(Topic, MaxAge, Format, Payload) ->
handle_received_create(TopicPrefix, MaxAge, Payload) ->
case core_link:decode(Payload) of
[{rootless, [Topic], [{ct, CT}]}] when is_binary(Topic), Topic =/= <<>> ->
TrueTopic = http_uri:decode(Topic),
TrueTopic = percent_decode(Topic),
?LOG(debug, "decoded link-format payload, the Topic=~p, CT=~p~n", [TrueTopic, CT]),
LocPath = concatenate_location_path([<<"ps">>, TopicPrefix, TrueTopic]),
FullTopic = binary:part(LocPath, 4, byte_size(LocPath)-4),
@ -259,6 +259,14 @@ handle_received_create(TopicPrefix, MaxAge, Payload) ->
{error, bad_request}
end.
%% http_uri:decode/1 is deprecated in OTP-23
%% its equivalent uri_string:percent_decode however is not available before OTP 23
-if(?OTP_RELEASE >= 23).
percent_decode(Topic) -> uri_string:percent_decode(Topic).
-else.
percent_decode(Topic) -> http_uri:decode(Topic).
-endif.
%% When topic is timeout, server should return nocontent here,
%% but gen_coap only receive return value of #coap_content from coap_get, so temporarily we can't give the Code 2.07 {ok, nocontent} out.TBC!!!
return_resource(Topic, Payload, MaxAge, TimeStamp, Content) ->