From fb66920d5fd7fadfecb7b3ddca0ce4dbec22aae9 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 14 Dec 2016 19:56:14 +0100 Subject: [PATCH] net.c: Fix response code for unknown resources John Carter has pointed out that non-GET requests for a resource that does not exist caused a 4.05 response ("Method not allowed"). This fix corrects this behavior to return 4.04 in case a resource does not exist and, for .well-known/core, 4.05 if anything but a GET request was received. --- src/net.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/src/net.c b/src/net.c index a733a1a6..e08aaa43 100644 --- a/src/net.c +++ b/src/net.c @@ -1391,30 +1391,22 @@ handle_request(coap_context_t *context, coap_queue_t *node) { * be the well-known URI. In that case, we generate a default * response, otherwise, we return 4.04 */ - switch(node->pdu->hdr->code) { - - case COAP_REQUEST_GET: - if (is_wkc(key)) { /* GET request for .well-known/core */ + if (is_wkc(key)) { /* request for .well-known/core */ + if (node->pdu->hdr->code == COAP_REQUEST_GET) { /* GET */ info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN); response = coap_wellknown_response(context, node->pdu); - - } else { /* GET request for any another resource, return 4.04 */ - - debug("GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n", - key[0], key[1], key[2], key[3]); - response = - coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(404), - opt_filter); - } - break; - - default: /* any other request type */ - - debug("unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n", - key[0], key[1], key[2], key[3]); - + } else { + debug("method not allowed for .well-known/core\n"); response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405), opt_filter); + } + } else { /* request for any another resource, return 4.04 */ + + debug("request for unknown resource 0x%02x%02x%02x%02x, return 4.04\n", + key[0], key[1], key[2], key[3]); + response = + coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(404), + opt_filter); } if (response