1#define _POSIX_C_SOURCE 200809L
11struct cwist_sse_stream {
35 bool ok = cwist_error_is_ok(&
err);
46static bool sse_append(
cwist_sstring *out,
const char *data,
size_t len) {
61static int append_field(
cwist_sstring *out,
const char *name,
const char *value) {
62 const char *
line = value ? value :
"";
66 if (len &&
line[len - 1] ==
'\r') len--;
67 if (!sse_append(out, name,
strlen(name)) || !sse_append(out,
":", 1) ||
68 (len && !sse_append(out,
line, len)) || !sse_append(out,
"\n", 1))
89static cwist_sstring *format_event(
const char *event,
const char *
id,
int retry_ms,
95 if (!
is_comment && event) failed |= append_field(
frame,
"event", event);
102 if (failed || !sse_append(
frame,
"\n", 1)) {
120 "text/event-stream; charset=utf-8")) ||
123 return sse_error(-1);
139 int retry_ms,
const char *data) {
140 if (!res || retry_ms < -1)
return sse_error(-1);
142 if (!
frame)
return sse_error(-1);
155 if (!res)
return sse_error(-1);
157 if (!
frame)
return sse_error(-1);
169 if (!event)
return sse_error(-1);
183static int sse_send_all(
int fd,
const char *data,
size_t len) {
187 if (
n <= 0)
return -1;
206 static const char headers[] =
207 "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream; charset=utf-8\r\nCache-Control: no-cache\r\nX-Accel-Buffering: no\r\nConnection: keep-alive\r\n\r\n";
209 sse_send_all(req->
client_fd, headers,
sizeof(headers) - 1))
212 if (!stream)
return NULL;
234 int retry_ms,
const char *data) {
235 if (!stream || retry_ms < -1)
return sse_error(-1);
237 if (!
frame)
return sse_error(-1);
239 int rc = stream->closed ? -1 : sse_send_all(stream->fd,
frame->data,
frame->size);
240 if (
rc) stream->closed = 1;
243 return sse_error(
rc);
257 if (!stream)
return sse_error(-1);
259 if (!
frame)
return sse_error(-1);
261 int rc = stream->closed ? -1 : sse_send_all(stream->fd,
frame->data,
frame->size);
262 if (
rc) stream->closed = 1;
265 return sse_error(
rc);
274 if (!event)
return sse_error(-1);
Memory allocation wrappers backed by libttak.
void cwist_free(void *ptr)
Free memory obtained via cwist_alloc/cwist_strdup/etc.
void * cwist_alloc(size_t size)
Allocate zeroed memory tracked by libttak.
Safe string type with helper methods.
cwist_error_t cwist_sstring_append_len(cwist_sstring *str, const char *data, size_t len)
Append raw bytes with length.
cwist_sstring * cwist_sstring_create(void)
Create a new sstring instance.
void cwist_sstring_destroy(cwist_sstring *str)
Destroy an sstring instance.
@ CWIST_ERR_INT16
used when checking common errcodes in Unix/Linux
cwist_error_t cwist_http_header_add(cwist_http_header_node **head, const char *key, const char *value)
Add a header to the list.
Server-Sent Events stream API.
struct cwist_sse_stream cwist_sse_stream_t
cwist_error_t cwist_sse_response_init(cwist_http_response *res)
Prepare a response buffer for a Server-Sent Events reply.
cwist_error_t cwist_sse_stream_comment(cwist_sse_stream_t *stream, const char *comment)
Send an SSE comment line on a live stream.
cwist_error_t cwist_sse_response_comment(cwist_http_response *res, const char *comment)
Append an SSE comment line to a response buffer.
cwist_sse_stream_t * cwist_sse_stream_open(cwist_http_request *req)
Upgrade a request connection to a live SSE stream.
cwist_error_t cwist_sse_response_event(cwist_http_response *res, const char *event, const char *id, int retry_ms, const char *data)
Append a formatted SSE event to a response buffer.
cwist_error_t cwist_sse_stream_send(cwist_sse_stream_t *stream, const char *event, const char *id, int retry_ms, const char *data)
Send a formatted SSE event on a live stream.
void cwist_sse_stream_close(cwist_sse_stream_t *stream)
Close a live SSE stream and release its resources.
cwist_error_t cwist_sse_response_write(cwist_http_response *res, const cwist_sse_event_t *event)
Append an SSE event described by a cwist_sse_event_t to a response.
cwist_error_t cwist_sse_stream_write(cwist_sse_stream_t *stream, const cwist_sse_event_t *event)
Send an SSE event described by a cwist_sse_event_t on a stream.
Integrated error structure used throughout the system.
HTTP Response Object. Supports standard string body or Zero-Copy pointer body.
cwist_http_header_node * headers
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...