Skip to content

Commit

Permalink
Implement uri-queries for .well-known/core resources
Browse files Browse the repository at this point in the history
  • Loading branch information
gotthardp committed Nov 1, 2015
1 parent 26d09aa commit 1fd56e2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -8,9 +8,8 @@ which aims to be conformant with:
- CoRE link format [RFC 6690](https://tools.ietf.org/rfc/rfc6690.txt)

The following features are not (yet) implemented:
- DTLS
- Proxying
- Uri Query
- DTLS transport
- Proxying and virtual servers (Uri-Host, Uri-Port, Proxy-Uri and Proxy-Scheme options)

It was tested with the following CoAP implementations:
- C [libcoap](https://www.libcoap.net/) (develop branch)
Expand Down Expand Up @@ -48,6 +47,7 @@ The tool accepts the following arguments:
Run the example simply by:

$ ./coap-client.sh coap://127.0.0.1/.well-known/core
$ ./coap-client.sh coap://127.0.0.1/.well-known/core?rt=core.ps
$ ./coap-client.sh coap://127.0.0.1/resource
$ ./coap-client.sh coap://127.0.0.1/resource -s 1000
$ ./coap-client.sh -m put coap://127.0.0.1/resource -e data
Expand Down
3 changes: 2 additions & 1 deletion src/coap_responder_sup.erl
Expand Up @@ -26,8 +26,9 @@ get_responder(SupPid, Request) ->

start_responder(SupPid, #coap_message{method=Method, options=Options}) ->
Uri = proplists:get_value(uri_path, Options, []),
Query = proplists:get_value(uri_query, Options, []),
supervisor:start_child(SupPid,
{{Method, Uri},
{{Method, Uri, Query},
{coap_responder, start_link, [self(), Uri]},
temporary, 5000, worker, []}).

Expand Down
40 changes: 33 additions & 7 deletions src/coap_server_content.erl
Expand Up @@ -7,7 +7,7 @@
% Copyright (c) 2015 Petr Gotthard <petr.gotthard@centrum.cz>
%

% provides the .well-known/core resource and (in future) content queries
% provides the .well-known/core resource and content queries
-module(coap_server_content).
-behaviour(coap_resource).

Expand Down Expand Up @@ -36,14 +36,40 @@ coap_unobserve(_State) -> ok.
handle_info(_Message, State) -> {noreply, State}.
coap_ack(_Ref, State) -> {ok, State}.

% uri-query processing

filter(Links, []) ->
Links;
filter(Links, [Search | Query]) ->
case binary:split(Search, <<$=>>) of
[Name, Value] ->
ok;
_Else -> ok
end,
filter(Links, Query).
filter(
case binary:split(Search, <<$=>>) of
[Name0, Value0] ->
Name = list_to_atom(binary_to_list(Name0)),
Value = wildcard_value(Value0),
lists:filter(
fun (Link) -> match_link(Link, Name, Value) end,
Links);
_Else ->
Links
end,
Query).

wildcard_value(<<>>) ->
{global, <<>>};
wildcard_value(Value) ->
case binary:last(Value) of
$* -> {prefix, binary:part(Value, 0, byte_size(Value)-1)};
_Else -> {global, Value}
end.

match_link({_Type, _Uri, Attrs}, Name, Value0) ->
lists:any(
fun (AttrVal) ->
case Value0 of
{prefix, Value} -> binary:part(AttrVal, 0, byte_size(Value)) =:= Value;
{global, Value} -> AttrVal =:= Value
end
end,
proplists:get_all_values(Name, Attrs)).

% end of file

0 comments on commit 1fd56e2

Please sign in to comment.