|
cwist
Modern C web framework with libttak runtime
|
#include <cwist/net/http/sse.h>#include <cwist/core/mem/alloc.h>#include <cwist/core/sstring/sstring.h>#include <errno.h>#include <pthread.h>#include <stdio.h>#include <string.h>#include <sys/socket.h>
Go to the source code of this file.
Macros | |
| #define | _POSIX_C_SOURCE 200809L |
Functions | |
| 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_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_response_comment (cwist_http_response *res, const char *comment) |
| Append an SSE comment line to a response buffer. | |
| 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_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_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. | |
| 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_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. | |
| 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_comment | ( | cwist_http_response * | res, |
| const char * | comment | ||
| ) |
Append an SSE comment line to a response buffer.
| res | Response whose body receives the comment; must not be NULL. |
| comment | Comment text. |
res is NULL or the comment frame could not be built or appended. Definition at line 154 of file sse.c.
References cwist_http_response::body, cap, cwist_sstring_append_len(), cwist_sstring_destroy(), and cwist_sstring::data.
| 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.
| res | Response whose body receives the frame; must not be NULL. |
| event | Event type field, optional. |
| id | Event id field, optional. |
| retry_ms | Reconnection delay in milliseconds; -1 to omit. |
| data | Event payload data. |
res is NULL, retry_ms is less than -1, or the frame could not be built or appended. Definition at line 138 of file sse.c.
References cwist_http_response::body, cap, cwist_sstring_append_len(), cwist_sstring_destroy(), and cwist_sstring::data.
Referenced by cwist_sse_response_write().
| cwist_error_t cwist_sse_response_init | ( | cwist_http_response * | res | ) |
Prepare a response buffer for a Server-Sent Events reply.
Adds the Content-Type, Cache-Control, and X-Accel-Buffering headers required for SSE and enables keep-alive.
| res | Response to initialize; must not be NULL. |
res is NULL or a header could not be added. Definition at line 117 of file sse.c.
References cwist_http_header_add(), cwist_http_response::headers, and cwist_http_response::keep_alive.
| 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.
| res | Response whose body receives the frame. |
| event | Event descriptor; must not be NULL. |
Definition at line 168 of file sse.c.
References cwist_sse_response_event(), cwist_sse_event::data, cwist_sse_event::event, cwist_sse_event::id, and cwist_sse_event::retry_ms.
| void cwist_sse_stream_close | ( | cwist_sse_stream_t * | stream | ) |
Close a live SSE stream and release its resources.
Destroys the stream mutex and frees the stream; the underlying socket remains owned by the upgraded request.
| stream | Stream to close; NULL is silently ignored. |
Definition at line 285 of file sse.c.
References cap, and cwist_free().
| cwist_error_t cwist_sse_stream_comment | ( | cwist_sse_stream_t * | stream, |
| const char * | comment | ||
| ) |
Send an SSE comment line on a live stream.
Serializes access to the stream with its mutex; a failed send marks the stream as closed.
| stream | Stream to write to; must not be NULL. |
| comment | Comment text. |
Definition at line 256 of file sse.c.
References cap, and cwist_sstring_destroy().
| cwist_sse_stream_t * cwist_sse_stream_open | ( | cwist_http_request * | req | ) |
Upgrade a request connection to a live SSE stream.
Sends the SSE HTTP response headers and marks the request as upgraded, transferring ownership of the connection to the returned stream (release with cwist_sse_stream_close()).
| req | Request to upgrade; must not be NULL and must not already be upgraded. |
Definition at line 205 of file sse.c.
References cap, cwist_http_request::client_fd, cwist_alloc(), and cwist_http_request::upgraded.
| 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.
Serializes access to the stream with its mutex; a failed send marks the stream as closed.
| stream | Stream to write to; must not be NULL. |
| event | Event type field, optional. |
| id | Event id field, optional. |
| retry_ms | Reconnection delay in milliseconds; -1 to omit. |
| data | Event payload data. |
Definition at line 233 of file sse.c.
References cap, and cwist_sstring_destroy().
Referenced by cwist_sse_stream_write().
| 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.
| stream | Stream to write to. |
| event | Event descriptor; must not be NULL. |
Definition at line 273 of file sse.c.
References cwist_sse_stream_send(), cwist_sse_event::data, cwist_sse_event::event, cwist_sse_event::id, and cwist_sse_event::retry_ms.