Skip to content

Commit

Permalink
Validate CoAP request before processing
Browse files Browse the repository at this point in the history
- validation is executed before processing
- some data parsed during the validation is stored and passed to processing
to avoid duplicate parsing
  • Loading branch information
jkralik authored Jan 23, 2024
1 parent c98b759 commit a882b59
Show file tree
Hide file tree
Showing 10 changed files with 807 additions and 369 deletions.
31 changes: 24 additions & 7 deletions api/oc_rep_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,25 +571,42 @@ oc_rep_decoder_get_type(void)
return g_rep_decoder.type;
}

bool
oc_rep_decoder_set_by_content_format(oc_content_format_t content_format)
static bool
rep_decoder_set_or_check_content_format(oc_content_format_t content_format,
bool check)
{
if (content_format == APPLICATION_CBOR ||
content_format == APPLICATION_VND_OCF_CBOR ||
content_format == APPLICATION_NOT_DEFINED) {
oc_rep_decoder_set_type(OC_REP_CBOR_DECODER);
if (content_format == APPLICATION_NOT_DEFINED ||
content_format == APPLICATION_CBOR ||
content_format == APPLICATION_VND_OCF_CBOR) {
if (!check) {
oc_rep_decoder_set_type(OC_REP_CBOR_DECODER);
}
return true;
}
#ifdef OC_JSON_ENCODER
if (content_format == APPLICATION_JSON ||
content_format == APPLICATION_TD_JSON) {
oc_rep_decoder_set_type(OC_REP_JSON_DECODER);
if (!check) {
oc_rep_decoder_set_type(OC_REP_JSON_DECODER);
}
return true;
}
#endif /* OC_JSON_ENCODER */
return false;
}

bool
oc_rep_decoder_set_by_content_format(oc_content_format_t content_format)
{
return rep_decoder_set_or_check_content_format(content_format, false);
}

bool
oc_rep_decoder_is_supported_content_format(oc_content_format_t content_format)
{
return rep_decoder_set_or_check_content_format(content_format, true);
}

int
oc_rep_parse_payload(const uint8_t *payload, size_t payload_size,
oc_rep_parse_result_t *result)
Expand Down
10 changes: 10 additions & 0 deletions api/oc_rep_decode_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ oc_rep_decoder_type_t oc_rep_decoder_get_type(void);
*/
bool oc_rep_decoder_set_by_content_format(oc_content_format_t content_format);

/**
* @brief Check if the decoder type is supported for the content format.
*
* @param content_format the content format
* @return true if the decoder type is supported
* @return false otherwise
*/
bool oc_rep_decoder_is_supported_content_format(
oc_content_format_t content_format);

#ifdef __cplusplus
}
#endif
Expand Down
36 changes: 22 additions & 14 deletions api/oc_resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,31 @@ oc_resource_get_method_handler(const oc_resource_t *resource,
oc_request_handler_t *handler)
{
assert(resource != NULL);
if (method == OC_GET) {
*handler = resource->get_handler;
return true;
const oc_request_handler_t *h = NULL;
switch (method) {
case OC_FETCH:
// TODO: implement fetch
break;
case OC_GET:
h = &resource->get_handler;
break;
case OC_POST:
h = &resource->post_handler;
break;
case OC_PUT:
h = &resource->put_handler;
break;
case OC_DELETE:
h = &resource->delete_handler;
break;
}
if (method == OC_POST) {
*handler = resource->post_handler;
return true;
if (h == NULL || h->cb == NULL) {
return false;
}
if (method == OC_PUT) {
*handler = resource->put_handler;
return true;
if (handler) {
*handler = *h;
}
if (method == OC_DELETE) {
*handler = resource->delete_handler;
return true;
}
return false;
return true;
}

bool
Expand Down
3 changes: 2 additions & 1 deletion api/oc_resource_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ bool oc_resource_supports_interface(const oc_resource_t *resource,
*/
bool oc_resource_get_method_handler(const oc_resource_t *resource,
oc_method_t method,
oc_request_handler_t *handler) OC_NONNULL();
oc_request_handler_t *handler)
OC_NONNULL(1);

/**
* @brief Callback invoked for each resource iterated by oc_resources_iterate.
Expand Down
Loading

0 comments on commit a882b59

Please sign in to comment.