7#include <cjson/cJSON.h>
19 if (!req)
return NULL;
31 const char *value = request_header(req, name);
32 return value &&
strcasecmp(value,
"true") == 0;
44 if (header_is_true(req,
"HX-Request"))
45 return !header_is_true(req,
"HX-History-Restore-Request");
46 const char *
frame = request_header(req,
"Turbo-Frame");
59 const char *
frame = request_header(req,
"Turbo-Frame");
61 const char *
target = request_header(req,
"HX-Target");
81 bool ok = cwist_error_is_ok(&
err);
92 bool ok = cwist_error_is_ok(&
err);
111 bool ok = out !=
NULL;
114 ok = cwist_error_is_ok(&
err);
117 if (ok &&
html->data) {
119 ok = cwist_error_is_ok(&
err);
143 if (!
root)
return -1;
144 if (!response_accepts_html(res)) {
150 if (!
html)
return -1;
152 const char *data =
html->data ?
html->data :
"";
154 bool ok = cwist_error_is_ok(&
err);
159 return set_header(res,
"Content-Type",
"text/html; charset=utf-8");
182 if (!response_accepts_html(res)) {
198 if (
rc != 0)
return -1;
229 if (!response_accepts_html(res) || !element_has_id(
el)) {
248 if (!
html)
return -1;
250 bool ok = cwist_error_is_ok(&
err);
261static bool location_is_valid(
const char *
location) {
263 for (
const unsigned char *
p = (
const unsigned char *)
location; *
p;
p++) {
264 if (*
p < 0x20 || *
p == 0x7f)
return false;
284 if (!response_accepts_html(res) || !location_is_valid(
location))
return -1;
290 if (header_is_true(req,
"HX-Request")) {
292 rc = add_header(res,
"HX-Redirect",
location);
297 if (
rc != 0)
return -1;
300 bool ok = cwist_error_is_ok(&
err);
303 return add_header(res,
"Vary",
"HX-Request");
void cwist_html_element_add_attr(cwist_html_element_t *el, const char *key, const char *value)
Add an attribute to an element.
void cwist_html_element_destroy(cwist_html_element_t *el)
Destroy an HTML element and its children.
cwist_sstring * cwist_html_render(cwist_html_element_t *el)
Render the element tree to an HTML string.
struct cwist_html_component cwist_html_component_t
cwist_html_element_t * cwist_html_component_instantiate(cwist_html_component_t *comp, const void *props, cwist_html_element_t **children, size_t child_count)
Render one instance of a component.
cwist_sstring * cwist_sstring_create(void)
Create a new sstring instance.
cwist_error_t cwist_sstring_append(cwist_sstring *str, const char *data)
Append a C string.
cwist_error_t cwist_sstring_assign_len(cwist_sstring *str, const char *data, size_t len)
Assign raw bytes with length.
cwist_error_t cwist_sstring_assign(cwist_sstring *str, const char *data)
Assign a C string.
void cwist_sstring_destroy(cwist_sstring *str)
Destroy an sstring instance.
int cwist_http_response_add_oob(const cwist_http_request *req, cwist_http_response *res, cwist_html_element_t *el)
Append an out-of-band swapped element to the response body.
int cwist_http_response_html_redirect(const cwist_http_request *req, cwist_http_response *res, const char *location)
Redirect an HTML request to location, using htmx headers when applicable.
const char * cwist_http_request_fragment_target(const cwist_http_request *req)
Get the identifier of the fragment region the client wants updated.
int cwist_http_response_set_view(const cwist_http_request *req, cwist_http_response *res, cwist_html_element_t *content, cwist_html_component_t *layout, const void *layout_props)
Set the response body from content, wrapping it in layout for full pages.
bool cwist_http_request_wants_fragment(const cwist_http_request *req)
Determine whether the client requested an HTML fragment only.
int cwist_http_response_set_html(cwist_http_response *res, cwist_html_element_t *root, bool document)
Set the response body to the rendered HTML of root.
HTML page and fragment responses for HTML-over-the-wire handlers.
#define CWIST_HTML_FRAGMENT_VARY
Request headers that select between a page and a fragment.
char * cwist_http_header_get(cwist_http_header_node *head, const char *key)
Find a header value by key.
cwist_error_t cwist_http_header_add(cwist_http_header_node **head, const char *key, const char *value)
Add a header to the list.
size_t cwist_http_header_remove(cwist_http_header_node **head, const char *key)
Remove all headers matching a name, honoring per-node ownership (heap vs arena vs borrowed buffers).
Integrated error structure used throughout the system.
cwist_http_header_node * headers
HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
bool use_file_stream
Fast File Streaming.
cwist_http_status_t status_code
cwist_http_header_node * headers
bool is_ptr_body
Zero-Copy Pointer Body.
bool stream_mode
cwist_http_response_stream_begin() ran.
char * data
Access directly only when raw handling is necessary.
void cwist_error_dispose(cwist_error_t *err)
Release resources owned by an error value. Currently only CWIST_ERR_JSON owns memory (the cJSON paylo...