DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
cloudevents.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file cloudevents.h
6 * @brief CloudEvents v1.0 (CNCF) event envelope - structured JSON build + binary
7 * (ce-* header) read. Zero-heap; builds on the JSON writer.
8 *
9 * CloudEvents is the application-layer metadata envelope that makes a device's
10 * events interoperable with serverless / event-mesh consumers. Two content modes:
11 *
12 * - **structured** - the whole event is one `application/cloudevents+json` body;
13 * build it with cloudevents_build_json() into a caller buffer.
14 * - **binary** - the event attributes ride as `ce-*` HTTP headers and the payload
15 * is the body; read an inbound binary event with cloudevents_from_headers().
16 *
17 * Emit a binary event from a handler by adding the `ce-id` / `ce-source` /
18 * `ce-type` / `ce-specversion` response headers yourself (and the data as the body)
19 * - no special API needed.
20 *
21 * @author Douglas Quigg (dstroy0)
22 * @date 2026
23 */
24
25#ifndef DETERMINISTICESPASYNCWEBSERVER_CLOUDEVENTS_H
26#define DETERMINISTICESPASYNCWEBSERVER_CLOUDEVENTS_H
27
28#include "ServerConfig.h"
29
30#if DETWS_ENABLE_CLOUDEVENTS
31
33#include <stddef.h>
34
35/**
36 * @brief A CloudEvents v1.0 event. The three required context attributes are
37 * @ref id, @ref source and @ref type (`specversion` is always "1.0").
38 *
39 * All strings are referenced, not copied. Exactly one of @ref data_json (a
40 * pre-formatted JSON value, emitted verbatim) or @ref data_str (a plain string,
41 * JSON-escaped) may be set; leave both null for an event with no data.
42 */
43struct CloudEvent
44{
45 const char *id; ///< required: unique id for this event (e.g. a counter / UUID)
46 const char *source; ///< required: the producer context URI-reference (e.g. "/devices/esp32-1")
47 const char *type; ///< required: the event type (e.g. "com.example.sensor.reading")
48 const char *subject; ///< optional: subject within the source, or null
49 const char *datacontenttype; ///< optional: media type of data (default "application/json" when data_json set)
50 const char *data_json; ///< optional: data as a pre-formatted JSON value (object/array/number/...)
51 const char *data_str; ///< optional: data as a plain string (emitted as a JSON string)
52};
53
54/**
55 * @brief Build a structured CloudEvents JSON envelope into @p buf.
56 *
57 * Emits `{"specversion":"1.0","id":...,"source":...,"type":...[,"subject":...]
58 * [,"datacontenttype":...][,"data":...]}`. Fails (returns 0) if a required
59 * attribute is missing/empty or the buffer overflows.
60 *
61 * @return number of bytes written (excluding the NUL), or 0 on error.
62 */
63size_t cloudevents_build_json(char *buf, size_t cap, const CloudEvent *ce);
64
65/**
66 * @brief Read an inbound binary-mode CloudEvent from a request's `ce-*` headers.
67 *
68 * Fills @p out from `ce-id` / `ce-source` / `ce-type` / `ce-subject` /
69 * `Content-Type` (the data is the request body). The pointers reference the
70 * request's header storage (valid for the duration of the request).
71 *
72 * @return true if the three required attributes (id, source, type) are present.
73 */
74bool cloudevents_from_headers(const HttpReq *req, CloudEvent *out);
75
76#endif // DETWS_ENABLE_CLOUDEVENTS
77
78#endif // DETERMINISTICESPASYNCWEBSERVER_CLOUDEVENTS_H
User-facing configuration for DeterministicESPAsyncWebServer.
Standalone HTTP/1.1 request parser - no transport dependency.
Fully-parsed HTTP/1.1 request.