← Back to the chapter · Next detail →

Technical detail · CoAP Messages: Parsing and Validation

CoAP option length, ordering and payload edge cases

The sensor request places options after its four-byte header and two Token bytes. From that boundary onward, each declared length determines where the parser may read the next header.

Advance over the whole option value before reading another option header. If a complete datagram ends with option header B7 followed by only six value bytes, the option is incomplete: its declared length is seven. That is evidence of a length failure, not a Token mismatch.

An option header encodes the delta from the preceding option number and the length of its value. Options appear in nondecreasing option-number order. Uri-Path (11) followed by another Uri-Path (11) and Content-Format (12) gives deltas 11, 0, 1. Reversing Content-Format and Uri-Path would require delta −1, which cannot be encoded. Keep repeated path segments in their path order while sorting by option number. Content-Format identifies the data format of the payload.

When a payload is present, recognize its 0xFF marker at an option boundary. A byte inside a declared option value is still part of that value. These short option headers pack delta and length into one byte; larger values need extended encoding, where additional bytes carry delta or length information.

← Back to the chapter · Next detail →