11#if DETWS_ENABLE_CLOUDEVENTS
16static bool ce_present(
const char *s)
18 return s !=
nullptr && s[0] !=
'\0';
23static const char *
const CE_KEY_DATACONTENTTYPE =
"datacontenttype";
24static const char *
const CE_KEY_DATA =
"data";
26size_t cloudevents_build_json(
char *buf,
size_t cap,
const CloudEvent *ce)
28 if (!buf || cap == 0 || !ce)
31 if (!ce_present(ce->id) || !ce_present(ce->source) || !ce_present(ce->type))
36 w.kv_str(
"specversion",
"1.0");
37 w.kv_str(
"id", ce->id);
38 w.kv_str(
"source", ce->source);
39 w.kv_str(
"type", ce->type);
40 if (ce_present(ce->subject))
41 w.kv_str(
"subject", ce->subject);
44 if (ce->data_json && ce->data_json[0] !=
'\0')
46 w.kv_str(CE_KEY_DATACONTENTTYPE, ce_present(ce->datacontenttype) ? ce->datacontenttype :
"application/json");
50 else if (ce->data_str)
52 if (ce_present(ce->datacontenttype))
53 w.kv_str(CE_KEY_DATACONTENTTYPE, ce->datacontenttype);
54 w.kv_str(CE_KEY_DATA, ce->data_str);
56 else if (ce_present(ce->datacontenttype))
58 w.kv_str(CE_KEY_DATACONTENTTYPE, ce->datacontenttype);
62 return w.ok() ? strnlen(buf, cap) : 0;
65bool cloudevents_from_headers(
const HttpReq *req, CloudEvent *out)
69 memset(out, 0,
sizeof(*out));
75 return ce_present(out->id) && ce_present(out->source) && ce_present(out->type);
Builds a JSON document into a fixed caller buffer, no heap.
CloudEvents v1.0 (CNCF) event envelope - structured JSON build + binary (ce-* header) read....
const char * http_get_header(const HttpReq *req, const char *key)
Look up a header value by name (case-insensitive).
Layer 6 (Presentation) - zero-heap JSON: a bounded writer and top-level reader.
Fully-parsed HTTP/1.1 request.