From 8a05380388835b0b134c42e4ddfc4793295c9386 Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Sat, 23 Jan 2021 23:32:42 +0100 Subject: [PATCH] refactor(emqx_coap): use url_string module to decode url encoded topic --- apps/emqx_coap/src/emqx_coap_ps_resource.erl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/emqx_coap/src/emqx_coap_ps_resource.erl b/apps/emqx_coap/src/emqx_coap_ps_resource.erl index 09d291bf5..d16c11695 100644 --- a/apps/emqx_coap/src/emqx_coap_ps_resource.erl +++ b/apps/emqx_coap/src/emqx_coap_ps_resource.erl @@ -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) ->