ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
http_delivery.cpp
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 http_delivery.cpp
6 * @brief HTTP delivery optimizations (see http_delivery.h).
7 */
8
10#include "shared_primitives/strbuf.h" // pc_sb frame builder
11
12#if PC_ENABLE_HTTP_DELIVERY
13
14DeliveryVerdict pc_delivery_swr(uint32_t age_s, uint32_t max_age_s, uint32_t swr_s)
15{
16 if (age_s <= max_age_s)
17 {
18 return DeliveryVerdict::DELIVERY_FRESH;
19 }
20 uint64_t window = (uint64_t)max_age_s + swr_s;
21 if ((uint64_t)age_s <= window)
22 {
23 return DeliveryVerdict::DELIVERY_STALE_REVALIDATE;
24 }
25 return DeliveryVerdict::DELIVERY_EXPIRED;
26}
27
28size_t pc_delivery_cache_control(uint32_t max_age_s, uint32_t swr_s, char *out, size_t cap)
29{
30 if (!out || cap == 0)
31 {
32 return 0;
33 }
34 pc_sb b = {out, cap, 0, true};
35 pc_sb_put(&b, "public, max-age=");
36 pc_sb_u32(&b, max_age_s);
37 if (swr_s)
38 {
39 pc_sb_put(&b, ", stale-while-revalidate=");
40 pc_sb_u32(&b, swr_s);
41 }
42 if (!b.ok)
43 {
44 return 0;
45 }
46 out[b.len] = '\0';
47 return b.len;
48}
49
50size_t pc_delivery_sw_manifest(const char *const *paths, size_t n, const char *version, char *out, size_t cap)
51{
52 if (!out || cap == 0 || (n && !paths))
53 {
54 return 0;
55 }
56 pc_sb b2 = {out, cap, 0, true};
57 pc_sb_put(&b2, "{\"version\":");
58 pc_sb_json(&b2, version ? version : "");
59 pc_sb_put(&b2, ",\"precache\":[");
60 for (size_t i = 0; i < n; i++)
61 {
62 if (i)
63 {
64 pc_sb_put(&b2, ",");
65 }
66 pc_sb_json(&b2, paths[i]);
67 }
68 pc_sb_put(&b2, "]}");
69 if (!b2.ok)
70 {
71 return 0;
72 }
73 out[b2.len] = '\0';
74 return b2.len;
75}
76
77#endif // PC_ENABLE_HTTP_DELIVERY
HTTP delivery optimizations: stale-while-revalidate, Range/206 delta fetch, SW precache (PC_ENABLE_HT...
Bounded no-heap string builder that fails closed on overflow (one shared copy).
void pc_sb_json(pc_sb *b, const char *s)
Append s as a JSON string literal: double-quoted, with "</tt> and <tt>\</tt> backslash-escaped....
Definition strbuf.h:620
void pc_sb_put(pc_sb *b, const char *s)
Append NUL-terminated s; leaves the buffer untouched and clears ok if it would not fit.
Definition strbuf.h:60
void pc_sb_u32(pc_sb *b, uint32_t v)
Append v as decimal (no leading zeros; "0" for zero).
Definition strbuf.h:303
Bump-append target; ok latches false once an append would overflow cap.
Definition strbuf.h:30
size_t len
Definition strbuf.h:33
bool ok
Definition strbuf.h:34