diff --git a/sys/net/application_layer/gcoap/forward_proxy.c b/sys/net/application_layer/gcoap/forward_proxy.c index de27cc0186..169e46c926 100644 --- a/sys/net/application_layer/gcoap/forward_proxy.c +++ b/sys/net/application_layer/gcoap/forward_proxy.c @@ -202,8 +202,19 @@ static void _forward_resp_handler(const gcoap_request_memo_t *memo, /* check if we can just send 2.03 Valid instead */ if ((cep->req_etag_len == coap_opt_get_opaque(pdu, COAP_OPT_ETAG, &resp_etag)) && (memcmp(cep->req_etag, resp_etag, cep->req_etag_len) == 0)) { + uint32_t max_age; + + if (coap_opt_get_uint(pdu, COAP_OPT_MAX_AGE, &max_age) < 0) { + /* use default, + * see https://datatracker.ietf.org/doc/html/rfc7252#section-5.10.5 */ + max_age = 60U; + } gcoap_resp_init(pdu, (uint8_t *)pdu->hdr, buf_len, COAP_CODE_VALID); coap_opt_add_opaque(pdu, COAP_OPT_ETAG, cep->req_etag, cep->req_etag_len); + if (max_age != 60U) { + /* only include Max-Age option if it is not the default value */ + coap_opt_add_uint(pdu, COAP_OPT_MAX_AGE, max_age); + } coap_opt_finish(pdu, COAP_OPT_FINISH_NONE); } } diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index 381735199b..e19687653c 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -1327,7 +1327,7 @@ static ssize_t _cache_check(const uint8_t *buf, size_t len, uint8_t *start = coap_find_option(&req, COAP_OPT_ETAG); /* option length must always be <= COAP_ETAG_LENGTH_MAX = 8 < 12, so the length * is encoded in the first byte, see also RFC 7252, section 3.1 */ - *start &= 0x0f; + *start &= 0xf0; /* first if around here should make sure we are <= 8 < 0xf, so we don't need to * bitmask resp_etag_len */ *start |= (uint8_t)resp_etag_len;