Skip to content

Commit

Permalink
Merge pull request #771 from mrdeep1/server_post
Browse files Browse the repository at this point in the history
coap-server: Add in POST support for unknown request handler
  • Loading branch information
obgm authored Dec 22, 2021
2 parents fce44ba + e8e33f0 commit 2a329e1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
52 changes: 41 additions & 11 deletions examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1330,12 +1330,12 @@ hnd_get(coap_resource_t *resource,
}

/*
* Regular PUT handler - used by resources created by the
* Unknown Resource PUT handler
* Regular PUT or POST handler - used by resources created by the
* Unknown Resource PUT/POST handler
*/

static void
hnd_put(coap_resource_t *resource,
hnd_put_post(coap_resource_t *resource,
coap_session_t *session,
const coap_pdu_t *request,
const coap_string_t *query COAP_UNUSED,
Expand Down Expand Up @@ -1371,7 +1371,7 @@ hnd_put(coap_resource_t *resource,
}
if (i == dynamic_count) {
if (dynamic_count >= support_dynamic) {
/* Should have been caught in hnd_unknown_put() */
/* Should have been caught hnd_put_post_unknown() */
coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_ACCEPTABLE);
coap_delete_string(uri_path);
return;
Expand All @@ -1384,7 +1384,6 @@ hnd_put(coap_resource_t *resource,
dynamic_entry[i].value = NULL;
dynamic_entry[i].resource = resource;
dynamic_entry[i].created = 1;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
if ((option = coap_check_option(request, COAP_OPTION_CONTENT_FORMAT,
&opt_iter)) != NULL) {
dynamic_entry[i].media_type =
Expand Down Expand Up @@ -1414,7 +1413,6 @@ hnd_put(coap_resource_t *resource,
} else {
/* Need to do this as coap_get_uri_path() created it */
coap_delete_string(uri_path);
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
}

resource_entry = &dynamic_entry[i];
Expand Down Expand Up @@ -1513,8 +1511,32 @@ hnd_put(coap_resource_t *resource,
resource_entry->value = transient_value;

if (resource_entry->created) {
coap_pdu_code_t code = coap_pdu_get_code(request);

resource_entry->created = 0;
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);
if (code == COAP_REQUEST_CODE_POST) {
/* Add in Location-Path / Location-Query Options */
coap_option_iterator_init(request, &opt_iter, COAP_OPT_ALL);
while ((option = coap_option_next(&opt_iter))) {
switch (opt_iter.number) {
case COAP_OPTION_URI_PATH:
if (!coap_add_option(response, COAP_OPTION_LOCATION_PATH,
coap_opt_length(option),
coap_opt_value(option)))
goto fail;
break;
case COAP_OPTION_URI_QUERY:
if (!coap_add_option(response, COAP_OPTION_LOCATION_QUERY,
coap_opt_length(option),
coap_opt_value(option)))
goto fail;
break;
default:
break;
}
}
}
}
else {
coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
Expand All @@ -1531,14 +1553,19 @@ hnd_put(coap_resource_t *resource,
body.s,
release_resource_data, resource_entry->value);
}
return;

fail:
coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
return;
}

/*
* Unknown Resource PUT handler
*/

static void
hnd_unknown_put(coap_resource_t *resource COAP_UNUSED,
hnd_put_post_unknown(coap_resource_t *resource COAP_UNUSED,
coap_session_t *session,
const coap_pdu_t *request,
const coap_string_t *query,
Expand Down Expand Up @@ -1567,15 +1594,16 @@ hnd_unknown_put(coap_resource_t *resource COAP_UNUSED,
r = coap_resource_init((coap_str_const_t*)uri_path,
COAP_RESOURCE_FLAGS_RELEASE_URI | resource_flags);
coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"Dynamic\""), 0);
coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put);
coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put_post);
coap_register_request_handler(r, COAP_REQUEST_POST, hnd_put_post);
coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete);
/* We possibly want to Observe the GETs */
coap_resource_set_get_observable(r, 1);
coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get);
coap_add_resource(coap_session_get_context(session), r);

/* Do the PUT for this first call */
hnd_put(r, session, request, query, response);
/* Do the PUT/POST for this first call */
hnd_put_post(r, session, request, query, response);
}

#if SERVER_CAN_PROXY
Expand Down Expand Up @@ -1780,7 +1808,9 @@ init_resources(coap_context_t *ctx) {

if (support_dynamic > 0) {
/* Create a resource to handle PUTs to unknown URIs */
r = coap_resource_unknown_init(hnd_unknown_put);
r = coap_resource_unknown_init(hnd_put_post_unknown);
/* Add in handling POST as well */
coap_register_handler(r, COAP_REQUEST_POST, hnd_put_post_unknown);
coap_add_resource(ctx, r);
}

Expand Down
4 changes: 2 additions & 2 deletions man/coap_resource.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ check_url_fn(coap_string_t *uri_path, uint8_t code) {
/* Unknown Resource PUT handler */

static void
hnd_unknown_put(coap_resource_t *resource, coap_session_t *session,
hnd_put_unknown(coap_resource_t *resource, coap_session_t *session,
const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
/* Remove (void) definition if variable is used */
(void)resource;
Expand Down Expand Up @@ -461,7 +461,7 @@ init_resources(coap_context_t *ctx) {
coap_resource_t *r;

/* Create a resource to handle PUTs to unknown URIs */
r = coap_resource_unknown_init(hnd_unknown_put);
r = coap_resource_unknown_init(hnd_put_unknown);
/*
* Additional handlers can be added - for example
* coap_register_request_handler(r, COAP_REQUEST_POST, hnd_post_unknown);
Expand Down

0 comments on commit 2a329e1

Please sign in to comment.