11#if PC_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 pc_cloudevents_build_json(
char *buf,
size_t cap,
const CloudEvent *ce)
28 if (!buf || cap == 0 || !ce)
33 if (!ce_present(ce->id) || !ce_present(ce->source) || !ce_present(ce->type))
40 w.kv_str(
"specversion",
"1.0");
41 w.kv_str(
"id", ce->id);
42 w.kv_str(
"source", ce->source);
43 w.kv_str(
"type", ce->type);
44 if (ce_present(ce->subject))
46 w.kv_str(
"subject", ce->subject);
50 if (ce->data_json && ce->data_json[0] !=
'\0')
52 w.kv_str(CE_KEY_DATACONTENTTYPE, ce_present(ce->datacontenttype) ? ce->datacontenttype :
"application/json");
56 else if (ce->data_str)
58 if (ce_present(ce->datacontenttype))
60 w.kv_str(CE_KEY_DATACONTENTTYPE, ce->datacontenttype);
62 w.kv_str(CE_KEY_DATA, ce->data_str);
64 else if (ce_present(ce->datacontenttype))
66 w.kv_str(CE_KEY_DATACONTENTTYPE, ce->datacontenttype);
70 return w.ok() ? strnlen(buf, cap) : 0;
73bool pc_cloudevents_from_headers(
const HttpReq *req, CloudEvent *out)
79 memset(out, 0,
sizeof(*out));
85 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.