repo_name string | dataset string | owner string | lang string | func_name string | code string | docstring string | url string | sha string |
|---|---|---|---|---|---|---|---|---|
zap | github_2023 | zigzap | c | http1_on_path | static int http1_on_path(http1_parser_s *parser, char *path, size_t len) {
http1_pr2handle(parser2http(parser)).path = fiobj_str_new(path, len);
parser2http(parser)->header_size += len;
return 0;
} | /** called when a request path (excluding query) is parsed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L589-L593 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_query | static int http1_on_query(http1_parser_s *parser, char *query, size_t len) {
http1_pr2handle(parser2http(parser)).query = fiobj_str_new(query, len);
parser2http(parser)->header_size += len;
return 0;
} | /** called when a request path (excluding query) is parsed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L596-L600 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_version | static int http1_on_version(http1_parser_s *parser, char *version, size_t len) {
http1_pr2handle(parser2http(parser)).version = fiobj_str_new(version, len);
parser2http(parser)->header_size += len;
/* start counting - occurs on the first line of both requests and responses */
#if FIO_HTTP_EXACT_LOGGING
clock_gettime(CLOCK_REALTIME,
&http1_pr2handle(parser2http(parser)).received_at);
#else
http1_pr2handle(parser2http(parser)).received_at = fio_last_tick();
#endif
return 0;
} | /** called when a the HTTP/1.x version is parsed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L602-L613 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_header | static int http1_on_header(http1_parser_s *parser, char *name, size_t name_len,
char *data, size_t data_len) {
FIOBJ sym;
FIOBJ obj;
if (!http1_pr2handle(parser2http(parser)).headers) {
FIO_LOG_ERROR("(http1 parse ordering error) missing HashMap for header "
"%s: %s",
name, data);
http_send_error2(500, parser2http(parser)->p.uuid,
parser2http(parser)->p.settings);
return -1;
}
parser2http(parser)->header_size += name_len + data_len;
if (parser2http(parser)->header_size >=
parser2http(parser)->max_header_size ||
fiobj_hash_count(http1_pr2handle(parser2http(parser)).headers) >
HTTP_MAX_HEADER_COUNT) {
if (parser2http(parser)->p.settings->log) {
FIO_LOG_WARNING("(HTTP) security alert - header flood detected.");
}
http_send_error(&http1_pr2handle(parser2http(parser)), 413);
return -1;
}
sym = fiobj_str_new(name, name_len);
obj = fiobj_str_new(data, data_len);
set_header_add(http1_pr2handle(parser2http(parser)).headers, sym, obj);
fiobj_free(sym);
return 0;
} | /** called when a header is parsed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L615-L643 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_body_chunk | static int http1_on_body_chunk(http1_parser_s *parser, char *data,
size_t data_len) {
if (parser->state.content_length >
(ssize_t)parser2http(parser)->p.settings->max_body_size ||
parser->state.read >
(ssize_t)parser2http(parser)->p.settings->max_body_size) {
http_send_error(&http1_pr2handle(parser2http(parser)), 413);
return -1; /* test every time, in case of chunked data */
}
if (!parser->state.read) {
if (parser->state.content_length > 0 &&
parser->state.content_length <= HTTP_MAX_HEADER_LENGTH) {
http1_pr2handle(parser2http(parser)).body = fiobj_data_newstr();
} else {
http1_pr2handle(parser2http(parser)).body = fiobj_data_newtmpfile();
}
}
fiobj_data_write(http1_pr2handle(parser2http(parser)).body, data, data_len);
return 0;
} | /** called when a body chunk is parsed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L645-L664 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_error | static int http1_on_error(http1_parser_s *parser) {
if (parser2http(parser)->close)
return -1;
FIO_LOG_DEBUG("HTTP parser error.");
fio_close(parser2http(parser)->p.uuid);
return -1;
} | /** called when a protocol error occurred. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L667-L673 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_consume_data | static inline void http1_consume_data(intptr_t uuid, http1pr_s *p) {
if (fio_pending(uuid) > 4) {
goto throttle;
}
ssize_t i = 0;
size_t org_len = p->buf_len;
int pipeline_limit = 8;
if (!p->buf_len)
return;
do {
i = http1_parse(&p->parser, p->buf + (org_len - p->buf_len), p->buf_len);
p->buf_len -= i;
--pipeline_limit;
} while (i && p->buf_len && pipeline_limit && !p->stop);
if (p->buf_len && org_len != p->buf_len) {
memmove(p->buf, p->buf + (org_len - p->buf_len), p->buf_len);
}
if (p->buf_len == HTTP_MAX_HEADER_LENGTH) {
/* no room to read... parser not consuming data */
if (p->request.method)
http_send_error(&p->request, 413);
else {
p->request.method = fiobj_str_tmp();
http_send_error(&p->request, 413);
}
}
if (!pipeline_limit) {
fio_force_event(uuid, FIO_EVENT_ON_DATA);
}
return;
throttle:
/* throttle busy clients (slowloris) */
p->stop |= 4;
fio_suspend(uuid);
FIO_LOG_DEBUG("(HTTP/1,1) throttling client at %.*s",
(int)fio_peer_addr(uuid).len, fio_peer_addr(uuid).data);
} | /* *****************************************************************************
Connection Callbacks
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L679-L719 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_data | static void http1_on_data(intptr_t uuid, fio_protocol_s *protocol) {
http1pr_s *p = (http1pr_s *)protocol;
if (p->stop) {
fio_suspend(uuid);
return;
}
ssize_t i = 0;
if (HTTP_MAX_HEADER_LENGTH - p->buf_len)
i = fio_read(uuid, p->buf + p->buf_len,
HTTP_MAX_HEADER_LENGTH - p->buf_len);
if (i > 0) {
p->buf_len += i;
}
http1_consume_data(uuid, p);
} | /** called when a data is available, but will not run concurrently */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L722-L736 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_close | static void http1_on_close(intptr_t uuid, fio_protocol_s *protocol) {
http1_destroy(protocol);
(void)uuid;
} | /** called when the connection was closed, but will not run concurrently */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L739-L742 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_ready | static void http1_on_ready(intptr_t uuid, fio_protocol_s *protocol) {
/* resume slow clients from suspension */
http1pr_s *p = (http1pr_s *)protocol;
if (p->stop & 4) {
p->stop ^= 4; /* flip back the bit, so it's zero */
fio_force_event(uuid, FIO_EVENT_ON_DATA);
}
(void)protocol;
} | /** called when the connection was closed, but will not run concurrently */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L745-L753 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_on_data_first_time | static void http1_on_data_first_time(intptr_t uuid, fio_protocol_s *protocol) {
http1pr_s *p = (http1pr_s *)protocol;
ssize_t i;
i = fio_read(uuid, p->buf + p->buf_len, HTTP_MAX_HEADER_LENGTH - p->buf_len);
if (i <= 0)
return;
p->buf_len += i;
/* ensure future reads skip this first time HTTP/2.0 test */
p->p.protocol.on_data = http1_on_data;
if (i >= 24 && !memcmp(p->buf, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", 24)) {
FIO_LOG_WARNING("client claimed unsupported HTTP/2 prior knowledge.");
fio_close(uuid);
return;
}
/* Finish handling the same way as the normal `on_data` */
http1_consume_data(uuid, p);
} | /** called when a data is available for the first time */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L756-L776 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1_destroy | void http1_destroy(fio_protocol_s *pr) {
http1pr_s *p = (http1pr_s *)pr;
http1_pr2handle(p).status = 0;
http_s_destroy(&http1_pr2handle(p), 0);
fio_free(p);
// FIO_LOG_DEBUG("Deallocated HTTP/1.1 protocol at. %p", (void *)p);
} | /** Manually destroys the HTTP1 protocol object. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L816-L822 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http1pr_status2str | static fio_str_info_s http1pr_status2str(uintptr_t status) {
static fio_str_info_s status2str[] = {
HTTP_SET_STATUS_STR(100, "Continue"),
HTTP_SET_STATUS_STR(101, "Switching Protocols"),
HTTP_SET_STATUS_STR(102, "Processing"),
HTTP_SET_STATUS_STR(103, "Early Hints"),
HTTP_SET_STATUS_STR(200, "OK"),
HTTP_SET_STATUS_STR(201, "Created"),
HTTP_SET_STATUS_STR(202, "Accepted"),
HTTP_SET_STATUS_STR(203, "Non-Authoritative Information"),
HTTP_SET_STATUS_STR(204, "No Content"),
HTTP_SET_STATUS_STR(205, "Reset Content"),
HTTP_SET_STATUS_STR(206, "Partial Content"),
HTTP_SET_STATUS_STR(207, "Multi-Status"),
HTTP_SET_STATUS_STR(208, "Already Reported"),
HTTP_SET_STATUS_STR(226, "IM Used"),
HTTP_SET_STATUS_STR(300, "Multiple Choices"),
HTTP_SET_STATUS_STR(301, "Moved Permanently"),
HTTP_SET_STATUS_STR(302, "Found"),
HTTP_SET_STATUS_STR(303, "See Other"),
HTTP_SET_STATUS_STR(304, "Not Modified"),
HTTP_SET_STATUS_STR(305, "Use Proxy"),
HTTP_SET_STATUS_STR(306, "(Unused), "),
HTTP_SET_STATUS_STR(307, "Temporary Redirect"),
HTTP_SET_STATUS_STR(308, "Permanent Redirect"),
HTTP_SET_STATUS_STR(400, "Bad Request"),
HTTP_SET_STATUS_STR(403, "Forbidden"),
HTTP_SET_STATUS_STR(404, "Not Found"),
HTTP_SET_STATUS_STR(401, "Unauthorized"),
HTTP_SET_STATUS_STR(402, "Payment Required"),
HTTP_SET_STATUS_STR(405, "Method Not Allowed"),
HTTP_SET_STATUS_STR(406, "Not Acceptable"),
HTTP_SET_STATUS_STR(407, "Proxy Authentication Required"),
HTTP_SET_STATUS_STR(408, "Request Timeout"),
HTTP_SET_STATUS_STR(409, "Conflict"),
HTTP_SET_STATUS_STR(410, "Gone"),
HTTP_SET_STATUS_STR(411, "Length Required"),
HTTP_SET_STATUS_STR(412, "Precondition Failed"),
HTTP_SET_STATUS_STR(413, "Payload Too Large"),
HTTP_SET_STATUS_STR(414, "URI Too Long"),
HTTP_SET_STATUS_STR(415, "Unsupported Media Type"),
HTTP_SET_STATUS_STR(416, "Range Not Satisfiable"),
HTTP_SET_STATUS_STR(417, "Expectation Failed"),
HTTP_SET_STATUS_STR(421, "Misdirected Request"),
HTTP_SET_STATUS_STR(422, "Unprocessable Entity"),
HTTP_SET_STATUS_STR(423, "Locked"),
HTTP_SET_STATUS_STR(424, "Failed Dependency"),
HTTP_SET_STATUS_STR(425, "Unassigned"),
HTTP_SET_STATUS_STR(426, "Upgrade Required"),
HTTP_SET_STATUS_STR(427, "Unassigned"),
HTTP_SET_STATUS_STR(428, "Precondition Required"),
HTTP_SET_STATUS_STR(429, "Too Many Requests"),
HTTP_SET_STATUS_STR(430, "Unassigned"),
HTTP_SET_STATUS_STR(431, "Request Header Fields Too Large"),
HTTP_SET_STATUS_STR(500, "Internal Server Error"),
HTTP_SET_STATUS_STR(501, "Not Implemented"),
HTTP_SET_STATUS_STR(502, "Bad Gateway"),
HTTP_SET_STATUS_STR(503, "Service Unavailable"),
HTTP_SET_STATUS_STR(504, "Gateway Timeout"),
HTTP_SET_STATUS_STR(505, "HTTP Version Not Supported"),
HTTP_SET_STATUS_STR(506, "Variant Also Negotiates"),
HTTP_SET_STATUS_STR(507, "Insufficient Storage"),
HTTP_SET_STATUS_STR(508, "Loop Detected"),
HTTP_SET_STATUS_STR(509, "Unassigned"),
HTTP_SET_STATUS_STR(510, "Not Extended"),
HTTP_SET_STATUS_STR(511, "Network Authentication Required"),
};
fio_str_info_s ret = (fio_str_info_s){.len = 0, .data = NULL};
if (status >= 100 &&
(status - 100) < sizeof(status2str) / sizeof(status2str[0]))
ret = status2str[status - 100];
if (!ret.data) {
ret = status2str[400];
}
return ret;
} | // #undef HTTP_SET_STATUS_STR
// clang-format on | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http1.c#L833-L908 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_on_request_handler______internal | void http_on_request_handler______internal(http_s *h,
http_settings_s *settings) {
if (!http_upgrade_hash)
http_upgrade_hash = fiobj_hash_string("upgrade", 7);
h->udata = settings->udata;
static uint64_t host_hash = 0;
if (!host_hash)
host_hash = fiobj_hash_string("host", 4);
if (1) {
/* test for Host header and avoid duplicates */
FIOBJ tmp = fiobj_hash_get2(h->headers, host_hash);
if (!tmp)
goto missing_host;
if (FIOBJ_TYPE_IS(tmp, FIOBJ_T_ARRAY)) {
fiobj_hash_set(h->headers, HTTP_HEADER_HOST, fiobj_ary_pop(tmp));
}
}
FIOBJ t = fiobj_hash_get2(h->headers, http_upgrade_hash);
if (t)
goto upgrade;
if (fiobj_iseq(
fiobj_hash_get2(h->headers, fiobj_obj2hash(HTTP_HEADER_ACCEPT)),
HTTP_HVALUE_SSE_MIME))
goto eventsource;
if (settings->public_folder) {
fio_str_info_s path_str = fiobj_obj2cstr(h->path);
if (!http_sendfile2(h, settings->public_folder,
settings->public_folder_length, path_str.data,
path_str.len)) {
return;
}
}
settings->on_request(h);
return;
upgrade:
if (1) {
fiobj_dup(t); /* allow upgrade name access after http_finish */
fio_str_info_s val = fiobj_obj2cstr(t);
if (val.data[0] == 'h' && val.data[1] == '2') {
http_send_error(h, 400);
} else {
settings->on_upgrade(h, val.data, val.len);
}
fiobj_free(t);
return;
}
eventsource:
settings->on_upgrade(h, (char *)"sse", 3);
return;
missing_host:
FIO_LOG_DEBUG("missing Host header");
http_send_error(h, 400);
return;
} | /** Use this function to handle HTTP requests.*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http_internal.c#L17-L75 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | http_send_error2 | int http_send_error2(size_t error, intptr_t uuid, http_settings_s *settings) {
if (!uuid || !settings || !error)
return -1;
fio_protocol_s *pr = http1_new(uuid, settings, NULL, 0);
http_s *r = fio_malloc(sizeof(*r));
FIO_ASSERT(pr, "Couldn't allocate response object for error report.")
http_s_new(r, (http_fio_protocol_s *)pr, http1_vtable());
int ret = http_send_error(r, error);
fio_close(uuid);
return ret;
} | /* *****************************************************************************
Internal helpers
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/http_internal.c#L96-L106 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | clear_subscriptions | static inline void clear_subscriptions(ws_s *ws) {
fio_lock(&ws->sub_lock);
while (fio_ls_any(&ws->subscriptions)) {
fio_unsubscribe(fio_ls_pop(&ws->subscriptions));
}
fio_unlock(&ws->sub_lock);
} | /* *****************************************************************************
Create/Destroy the websocket subscription objects
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L139-L145 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | websocket_on_unwrapped | static void websocket_on_unwrapped(void *ws_p, void *msg, uint64_t len,
char first, char last, char text,
unsigned char rsv) {
ws_s *ws = ws_p;
if (last && first) {
ws->on_message(ws, (fio_str_info_s){.data = msg, .len = len},
(uint8_t)text);
return;
}
if (first) {
ws->is_text = (uint8_t)text;
if (ws->msg == FIOBJ_INVALID)
ws->msg = fiobj_str_buf(len);
fiobj_str_resize(ws->msg, 0);
}
fiobj_str_write(ws->msg, msg, len);
if (last) {
ws->on_message(ws, fiobj_obj2cstr(ws->msg), ws->is_text);
}
(void)rsv;
} | /* *****************************************************************************
Callbacks - Required functions for websocket_parser.h
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L151-L172 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | websocket_optimize_free | static void websocket_optimize_free(fio_msg_s *msg, void *metadata) {
fiobj_free((FIOBJ)metadata);
(void)msg;
} | /* *****************************************************************************
Multi-client broadcast optimizations
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L409-L412 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | websocket_optimize4broadcasts | void websocket_optimize4broadcasts(intptr_t type, int enable) {
static intptr_t generic = 0;
static intptr_t text = 0;
static intptr_t binary = 0;
fio_msg_metadata_s (*callback)(fio_str_info_s, fio_str_info_s, uint8_t);
intptr_t *counter;
switch ((0 - type)) {
case (0 - WEBSOCKET_OPTIMIZE_PUBSUB):
counter = &generic;
callback = websocket_optimize_generic;
break;
case (0 - WEBSOCKET_OPTIMIZE_PUBSUB_TEXT):
counter = &text;
callback = websocket_optimize_text;
break;
case (0 - WEBSOCKET_OPTIMIZE_PUBSUB_BINARY):
counter = &binary;
callback = websocket_optimize_binary;
break;
default:
return;
}
if (enable) {
if (fio_atomic_add(counter, 1) == 1) {
fio_message_metadata_callback_set(callback, 1);
}
} else {
if (fio_atomic_sub(counter, 1) == 0) {
fio_message_metadata_callback_set(callback, 0);
}
}
} | /**
* Enables (or disables) broadcast optimizations.
*
* When using WebSocket pub/sub system is originally optimized for either
* non-direct transmission (messages are handled by callbacks) or direct
* transmission to 1-3 clients per channel (on average), meaning that the
* majority of the messages are meant for a single recipient (or multiple
* callback recipients) and only some are expected to be directly transmitted to
* a group.
*
* However, when most messages are intended for direct transmission to more than
* 3 clients (on average), certain optimizations can be made to improve memory
* consumption (minimize duplication or WebSocket network data).
*
* This function allows enablement (or disablement) of these optimizations.
* These optimizations include:
*
* * WEBSOCKET_OPTIMIZE_PUBSUB - optimize all direct transmission messages,
* best attempt to detect Text vs. Binary data.
* * WEBSOCKET_OPTIMIZE_PUBSUB_TEXT - optimize direct pub/sub text messages.
* * WEBSOCKET_OPTIMIZE_PUBSUB_BINARY - optimize direct pub/sub binary messages.
*
* Note: to disable an optimization it should be disabled the same amount of
* times it was enabled - multiple optimization enablements for the same type
* are merged, but reference counted (disabled when reference is zero).
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L488-L519 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | websocket_unsubscribe | void websocket_unsubscribe(ws_s *ws, uintptr_t subscription_id) {
fio_unsubscribe((subscription_s *)((fio_ls_s *)subscription_id)->obj);
fio_lock(&ws->sub_lock);
fio_ls_remove((fio_ls_s *)subscription_id);
fio_unlock(&ws->sub_lock);
(void)ws;
} | /**
* Unsubscribes from a channel.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L682-L689 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | websocket_uuid | intptr_t websocket_uuid(ws_s *ws) { return ws->fd; } | /** Returns the the process specific connection's UUID (see `libsock`). */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L699-L699 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | websocket_is_client | uint8_t websocket_is_client(ws_s *ws) { return ws->is_client; } | /**
* Returns 1 if the WebSocket connection is in Client mode (connected to a
* remote server) and 0 if the connection is in Server mode (a connection
* established using facil.io's HTTP server).
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L714-L714 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | websocket_write | int websocket_write(ws_s *ws, fio_str_info_s msg, uint8_t is_text) {
if (fio_is_valid(ws->fd)) {
websocket_write_impl(ws->fd, msg.data, msg.len, is_text, 1, 1,
ws->is_client);
return 0;
}
return -1;
} | /** Writes data to the websocket. Returns -1 on failure (0 on success). */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L717-L724 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | websocket_close | void websocket_close(ws_s *ws) {
fio_write2(ws->fd, .data.buffer = "\x88\x00", .length = 2,
.after.dealloc = FIO_DEALLOC_NOOP);
fio_close(ws->fd);
return;
} | /** Closes a websocket connection. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/http/websockets.c#L726-L731 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_memcpy | static inline void fio_memcpy(void *__restrict dest_, void *__restrict src_,
size_t units) {
#if __SIZEOF_INT128__ == 9 /* a 128bit type exists... but tests favor 64bit */
register __uint128_t *dest = dest_;
register __uint128_t *src = src_;
#elif SIZE_MAX == 0xFFFFFFFFFFFFFFFF /* 64 bit size_t */
register size_t *dest = dest_;
register size_t *src = src_;
units = units << 1;
#elif SIZE_MAX == 0xFFFFFFFF /* 32 bit size_t */
register size_t *dest = dest_;
register size_t *src = src_;
units = units << 2;
#else /* unknow... assume 16 bit? */
register size_t *dest = dest_;
register size_t *src = src_;
units = units << 3;
#endif
while (units >= 16) { /* unroll loop */
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest[4] = src[4];
dest[5] = src[5];
dest[6] = src[6];
dest[7] = src[7];
dest[8] = src[8];
dest[9] = src[9];
dest[10] = src[10];
dest[11] = src[11];
dest[12] = src[12];
dest[13] = src[13];
dest[14] = src[14];
dest[15] = src[15];
dest += 16;
src += 16;
units -= 16;
}
switch (units) {
case 15:
*(dest++) = *(src++); /* fallthrough */
case 14:
*(dest++) = *(src++); /* fallthrough */
case 13:
*(dest++) = *(src++); /* fallthrough */
case 12:
*(dest++) = *(src++); /* fallthrough */
case 11:
*(dest++) = *(src++); /* fallthrough */
case 10:
*(dest++) = *(src++); /* fallthrough */
case 9:
*(dest++) = *(src++); /* fallthrough */
case 8:
*(dest++) = *(src++); /* fallthrough */
case 7:
*(dest++) = *(src++); /* fallthrough */
case 6:
*(dest++) = *(src++); /* fallthrough */
case 5:
*(dest++) = *(src++); /* fallthrough */
case 4:
*(dest++) = *(src++); /* fallthrough */
case 3:
*(dest++) = *(src++); /* fallthrough */
case 2:
*(dest++) = *(src++); /* fallthrough */
case 1:
*(dest++) = *(src++);
}
} | /* *****************************************************************************
Memory Copying by 16 byte units
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L58-L129 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | spn_trylock | static inline int spn_trylock(spn_lock_i *lock) {
return SPN_LOCK_BUILTIN(lock, 1);
} | /** returns 1 and 0 if the lock was successfully aquired (TRUE == FAIL). */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L194-L196 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | spn_unlock | static inline __attribute__((unused)) int spn_unlock(spn_lock_i *lock) {
return SPN_LOCK_BUILTIN(lock, 0);
} | /** Releases a lock. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L199-L201 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | spn_is_locked | static inline __attribute__((unused)) int spn_is_locked(spn_lock_i *lock) {
__asm__ volatile("" ::: "memory");
return *lock;
} | /** returns a lock's state (non 0 == Busy). */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L204-L207 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | spn_lock | static inline __attribute__((unused)) void spn_lock(spn_lock_i *lock) {
while (spn_trylock(lock)) {
reschedule_thread();
}
} | /** Busy waits for the lock. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L210-L214 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | sys_free | static inline void sys_free(void *mem, size_t len) { munmap(mem, len); } | /* frees memory using `munmap`. requires exact, page aligned, `len` */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L265-L265 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | sys_round_size | static inline size_t sys_round_size(size_t size) {
return (size & (~4095)) + (4096 * (!!(size & 4095)));
} | /** Rounds up any size to the nearest page alignment (assumes 4096 bytes per
* page) */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L300-L302 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_malloc_after_fork | void fio_malloc_after_fork(void) {
arena_last_used = NULL;
if (!arenas) {
return;
}
memory.lock = SPN_LOCK_INIT;
for (size_t i = 0; i < memory.cores; ++i) {
arenas[i].lock = SPN_LOCK_INIT;
}
} | /** Clears any memory locks, in case of a system call to `fork`. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L371-L380 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | block_free | static inline void block_free(block_s *blk) {
if (spn_sub(&blk->ref, 1))
return;
if (spn_add(&memory.count, 1) >
(intptr_t)(FIO_MEM_MAX_BLOCKS_PER_CORE * memory.cores)) {
/* TODO: return memory to the system */
spn_sub(&memory.count, 1);
sys_free(blk, FIO_MEMORY_BLOCK_SIZE);
return;
}
memset(blk, 0, FIO_MEMORY_BLOCK_SIZE);
spn_lock(&memory.lock);
*(block_s **)blk = memory.available;
memory.available = (block_s *)blk;
spn_unlock(&memory.lock);
} | /* intializes the block header for an available block of memory. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L404-L420 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_mem_init | static void __attribute__((constructor)) fio_mem_init(void) {
if (arenas)
return;
#ifdef _SC_NPROCESSORS_ONLN
ssize_t cpu_count = sysconf(_SC_NPROCESSORS_ONLN);
#else
#warning Dynamic CPU core count is unavailable - assuming 8 cores for memory allocation pools.
ssize_t cpu_count = 8; /* fallback */
#endif
memory.cores = cpu_count;
memory.count = 0 - (intptr_t)cpu_count;
arenas = big_alloc(sizeof(*arenas) * cpu_count);
if (!arenas) {
perror("FATAL ERROR: Couldn't initialize memory allocator");
exit(errno);
}
size_t pre_pool = cpu_count > 32 ? 32 : cpu_count;
for (size_t i = 0; i < pre_pool; ++i) {
void *block = sys_alloc(FIO_MEMORY_BLOCK_SIZE, 0);
if (block) {
block_init(block);
block_free(block);
}
}
pthread_atfork(NULL, NULL, fio_malloc_after_fork);
} | /* *****************************************************************************
Library Initialization (initialize arenas and allocate a block for each CPU)
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/legacy/fio_mem.c#L520-L546 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_internal_reset | static inline void redis_internal_reset(struct redis_engine_internal_s *i) {
i->buf_pos = 0;
i->parser = (resp_parser_s){.obj_countdown = 0, .expecting = 0};
fiobj_free((FIOBJ)fio_ct_if(i->ary == FIOBJ_INVALID, (uintptr_t)i->str,
(uintptr_t)i->ary));
i->str = FIOBJ_INVALID;
i->ary = FIOBJ_INVALID;
i->ary_count = 0;
i->nesting = 0;
i->uuid = -1;
} | /* releases any resources used by an internal engine*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L72-L82 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_free | static inline void redis_free(redis_engine_s *r) {
if (fio_atomic_sub(&r->ref, 1))
return;
FIO_LOG_DEBUG("freeing redis engine for %s:%s", r->address, r->port);
redis_internal_reset(&r->pub_data);
redis_internal_reset(&r->sub_data);
fiobj_free(r->last_ch);
while (fio_ls_embd_any(&r->queue)) {
fio_free(
FIO_LS_EMBD_OBJ(redis_commands_s, node, fio_ls_embd_pop(&r->queue)));
}
fio_unsubscribe(r->publication_forwarder);
r->publication_forwarder = NULL;
fio_unsubscribe(r->cmd_forwarder);
r->cmd_forwarder = NULL;
fio_unsubscribe(r->cmd_reply);
r->cmd_reply = NULL;
fio_free(r);
} | /** cleans up and frees the engine data. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L85-L103 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj2resp___internal | inline static void fiobj2resp___internal(FIOBJ dest, FIOBJ obj) {
fio_str_info_s s;
switch (FIOBJ_TYPE(obj)) {
case FIOBJ_T_NULL:
fiobj_str_write(dest, "$-1\r\n", 5);
break;
case FIOBJ_T_ARRAY:
fiobj_str_write(dest, "*", 1);
fiobj_str_write_i(dest, fiobj_ary_count(obj));
fiobj_str_write(dest, "\r\n", 2);
break;
case FIOBJ_T_HASH:
fiobj_str_write(dest, "*", 1);
fiobj_str_write_i(dest, fiobj_hash_count(obj) * 2);
fiobj_str_write(dest, "\r\n", 2);
break;
case FIOBJ_T_TRUE:
fiobj_str_write(dest, "$4\r\ntrue\r\n", 10);
break;
case FIOBJ_T_FALSE:
fiobj_str_write(dest, "$4\r\nfalse\r\n", 11);
break;
#if 0
/* Numbers aren't as good for commands as one might think... */
case FIOBJ_T_NUMBER:
fiobj_str_write(dest, ":", 1);
fiobj_str_write_i(dest, fiobj_obj2num(obj));
fiobj_str_write(dest, "\r\n", 2);
break;
#else
case FIOBJ_T_NUMBER: /* overflow */
#endif
case FIOBJ_T_FLOAT: /* overflow */
case FIOBJ_T_UNKNOWN: /* overflow */
case FIOBJ_T_STRING: /* overflow */
case FIOBJ_T_DATA:
s = fiobj_obj2cstr(obj);
fiobj_str_write(dest, "$", 1);
fiobj_str_write_i(dest, s.len);
fiobj_str_write(dest, "\r\n", 2);
fiobj_str_write(dest, s.data, s.len);
fiobj_str_write(dest, "\r\n", 2);
break;
}
} | /* *****************************************************************************
Simple RESP formatting
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L109-L153 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj2resp | static FIOBJ fiobj2resp(FIOBJ dest, FIOBJ obj) {
fiobj_each2(obj, fiobj2resp_task, (void *)dest);
return dest;
} | /**
* Converts FIOBJ objects into a RESP string (client mode).
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L165-L168 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fiobj2resp_tmp | static inline FIOBJ fiobj2resp_tmp(FIOBJ obj) {
return fiobj2resp(fiobj_str_tmp(), obj);
} | /**
* Converts FIOBJ objects into a RESP string (client mode).
*
* Don't call `fiobj_free`, object will self-destruct.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L175-L177 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_parser_error | static int resp_on_parser_error(resp_parser_s *parser) {
struct redis_engine_internal_s *i = parser2data(parser);
FIO_LOG_ERROR("(redis) parser error - attempting to restart connection.\n");
fio_close(i->uuid);
return -1;
} | /* *****************************************************************************
RESP parser callbacks
***************************************************************************** */
/** a local static callback, called when a parser / protocol error occurs. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L184-L189 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_message | static int resp_on_message(resp_parser_s *parser) {
struct redis_engine_internal_s *i = parser2data(parser);
FIOBJ msg = i->ary ? i->ary : i->str;
i->on_message(i, msg);
/* cleanup */
fiobj_free(msg);
i->ary = FIOBJ_INVALID;
i->str = FIOBJ_INVALID;
return 0;
} | /** a local static callback, called when the RESP message is complete. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L192-L201 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_add_obj | static inline void resp_add_obj(struct redis_engine_internal_s *dest, FIOBJ o) {
if (dest->ary) {
fiobj_ary_push(dest->ary, o);
--dest->ary_count;
if (!dest->ary_count && dest->nesting) {
FIOBJ tmp = fiobj_ary_shift(dest->ary);
dest->ary_count = fiobj_obj2num(tmp);
fiobj_free(tmp);
dest->ary = fiobj_ary_shift(dest->ary);
--dest->nesting;
}
}
dest->str = o;
} | /** a local helper to add parsed objects to the data store. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L204-L217 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_number | static int resp_on_number(resp_parser_s *parser, int64_t num) {
struct redis_engine_internal_s *data = parser2data(parser);
resp_add_obj(data, fiobj_num_new(num));
return 0;
} | /** a local static callback, called when a Number object is parsed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L220-L224 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_okay | static int resp_on_okay(resp_parser_s *parser) {
struct redis_engine_internal_s *data = parser2data(parser);
resp_add_obj(data, fiobj_true());
return 0;
} | /** a local static callback, called when a OK message is received. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L226-L230 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_null | static int resp_on_null(resp_parser_s *parser) {
struct redis_engine_internal_s *data = parser2data(parser);
resp_add_obj(data, fiobj_null());
return 0;
} | /** a local static callback, called when NULL is received. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L232-L236 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_start_string | static int resp_on_start_string(resp_parser_s *parser, size_t str_len) {
struct redis_engine_internal_s *data = parser2data(parser);
resp_add_obj(data, fiobj_str_buf(str_len));
return 0;
} | /**
* a local static callback, called when a String should be allocated.
*
* `str_len` is the expected number of bytes that will fill the final string
* object, without any NUL byte marker (the string might be binary).
*
* If this function returns any value besides 0, parsing is stopped.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L246-L250 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_string_chunk | static int resp_on_string_chunk(resp_parser_s *parser, void *data, size_t len) {
struct redis_engine_internal_s *i = parser2data(parser);
fiobj_str_write(i->str, data, len);
return 0;
} | /** a local static callback, called as String objects are streamed. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L252-L256 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_end_string | static int resp_on_end_string(resp_parser_s *parser) {
return 0;
(void)parser;
} | /** a local static callback, called when a String object had finished
* streaming.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L260-L263 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_err_msg | static int resp_on_err_msg(resp_parser_s *parser, void *data, size_t len) {
struct redis_engine_internal_s *i = parser2data(parser);
resp_add_obj(i, fiobj_str_new(data, len));
return 0;
} | /** a local static callback, called an error message is received. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L266-L270 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_start_array | static int resp_on_start_array(resp_parser_s *parser, size_t array_len) {
struct redis_engine_internal_s *i = parser2data(parser);
if (i->ary) {
++i->nesting;
FIOBJ tmp = fiobj_ary_new2(array_len + 2);
fiobj_ary_push(tmp, fiobj_num_new(i->ary_count));
fiobj_ary_push(tmp, fiobj_num_new(i->ary));
i->ary = tmp;
} else {
i->ary = fiobj_ary_new2(array_len + 2);
}
i->ary_count = array_len;
return 0;
} | /**
* a local static callback, called when an Array should be allocated.
*
* `array_len` is the expected number of objects that will fill the Array
* object.
*
* There's no `resp_on_end_array` callback since the RESP protocol assumes the
* message is finished along with the Array (`resp_on_message` is called).
* However, just in case a non-conforming client/server sends nested Arrays,
* the callback should test against possible overflow or nested Array endings.
*
* If this function returns any value besides 0, parsing is stopped.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L285-L298 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_perform_callback | static void redis_perform_callback(void *e, void *cmd_) {
redis_commands_s *cmd = cmd_;
FIOBJ reply = (FIOBJ)cmd->node.next;
if (cmd->callback)
cmd->callback(e, reply, cmd->udata);
fiobj_free(reply);
FIO_LOG_DEBUG("Handled: %s\n", cmd->cmd);
fio_free(cmd);
} | /* *****************************************************************************
Publication and Command Handling
***************************************************************************** */
/* the deferred callback handler */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L305-L313 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_send_next_command_unsafe | static void redis_send_next_command_unsafe(redis_engine_s *r) {
if (!r->pub_sent && fio_ls_embd_any(&r->queue)) {
r->pub_sent = 1;
redis_commands_s *cmd =
FIO_LS_EMBD_OBJ(redis_commands_s, node, r->queue.next);
fio_write2(r->pub_data.uuid, .data.buffer = cmd->cmd,
.length = cmd->cmd_len, .after.dealloc = FIO_DEALLOC_NOOP);
FIO_LOG_DEBUG("(redis %d) Sending (%zu bytes):\n%s\n", (int)getpid(),
cmd->cmd_len, cmd->cmd);
}
} | /* send command within lock, to ensure flag integrity */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L316-L326 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_attach_cmd | static void redis_attach_cmd(redis_engine_s *r, redis_commands_s *cmd) {
fio_lock(&r->lock);
fio_ls_embd_push(&r->queue, &cmd->node);
redis_send_next_command_unsafe(r);
fio_unlock(&r->lock);
} | /* attach a command to the queue */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L329-L334 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_pub_message | static void resp_on_pub_message(struct redis_engine_internal_s *i, FIOBJ msg) {
redis_engine_s *r = pub2redis(i);
// #if DEBUG
if (FIO_LOG_LEVEL >= FIO_LOG_LEVEL_DEBUG) {
FIOBJ json = fiobj_obj2json(msg, 1);
FIO_LOG_DEBUG("Redis reply:\n%s\n", fiobj_obj2cstr(json).data);
fiobj_free(json);
}
// #endif
/* publishing / command parser */
fio_lock(&r->lock);
fio_ls_embd_s *node = fio_ls_embd_shift(&r->queue);
r->pub_sent = 0;
redis_send_next_command_unsafe(r);
fio_unlock(&r->lock);
if (!node) {
/* TODO: possible ping? from server?! not likely... */
FIO_LOG_WARNING("(redis %d) received a reply when no command was sent.",
(int)getpid());
return;
}
node->next = (void *)fiobj_dup(msg);
fio_defer(redis_perform_callback, &r->en,
FIO_LS_EMBD_OBJ(redis_commands_s, node, node));
} | /** a local static callback, called when the RESP message is complete. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L337-L361 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | resp_on_sub_message | static void resp_on_sub_message(struct redis_engine_internal_s *i, FIOBJ msg) {
redis_engine_s *r = sub2redis(i);
/* subscriotion parser */
if (FIOBJ_TYPE(msg) != FIOBJ_T_ARRAY) {
if (FIOBJ_TYPE(msg) != FIOBJ_T_STRING || fiobj_obj2cstr(msg).len != 4 ||
fiobj_obj2cstr(msg).data[0] != 'P') {
FIO_LOG_WARNING("(redis) unexpected data format in "
"subscription stream (%zu bytes):\n %s\n",
fiobj_obj2cstr(msg).len, fiobj_obj2cstr(msg).data);
}
} else {
// FIOBJ *ary = fiobj_ary2ptr(msg);
// for (size_t i = 0; i < fiobj_ary_count(msg); ++i) {
// fio_str_info_s tmp = fiobj_obj2cstr(ary[i]);
// fprintf(stderr, "(%lu) %s\n", (unsigned long)i, tmp.data);
// }
fio_str_info_s tmp = fiobj_obj2cstr(fiobj_ary_index(msg, 0));
if (tmp.len == 7) { /* "message" */
fiobj_free(r->last_ch);
r->last_ch = fiobj_dup(fiobj_ary_index(msg, 1));
fio_publish(.channel = fiobj_obj2cstr(r->last_ch),
.message = fiobj_obj2cstr(fiobj_ary_index(msg, 2)),
.engine = FIO_PUBSUB_CLUSTER);
} else if (tmp.len == 8) { /* "pmessage" */
if (!fiobj_iseq(r->last_ch, fiobj_ary_index(msg, 2)))
fio_publish(.channel = fiobj_obj2cstr(fiobj_ary_index(msg, 2)),
.message = fiobj_obj2cstr(fiobj_ary_index(msg, 3)),
.engine = FIO_PUBSUB_CLUSTER);
}
}
} | /* *****************************************************************************
Subscription Message Handling
***************************************************************************** */
/** a local static callback, called when the RESP message is complete. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L368-L398 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_data | static void redis_on_data(intptr_t uuid, fio_protocol_s *pr) {
struct redis_engine_internal_s *internal =
(struct redis_engine_internal_s *)pr;
uint8_t *buf;
if (internal->on_message == resp_on_sub_message) {
buf = sub2redis(pr)->buf + REDIS_READ_BUFFER;
} else {
buf = pub2redis(pr)->buf;
}
ssize_t i = fio_read(uuid, buf + internal->buf_pos,
REDIS_READ_BUFFER - internal->buf_pos);
if (i <= 0)
return;
internal->buf_pos += i;
i = resp_parse(&internal->parser, buf, internal->buf_pos);
if (i) {
memmove(buf, buf + internal->buf_pos - i, i);
}
internal->buf_pos = i;
} | /** Called when a data is available, but will not run concurrently */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L414-L434 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_close | static void redis_on_close(intptr_t uuid, fio_protocol_s *pr) {
struct redis_engine_internal_s *internal =
(struct redis_engine_internal_s *)pr;
redis_internal_reset(internal);
redis_engine_s *r;
if (internal->on_message == resp_on_sub_message) {
r = sub2redis(pr);
fiobj_free(r->last_ch);
r->last_ch = FIOBJ_INVALID;
if (r->flag) {
/* reconnection for subscription connection. */
if (uuid != -1) {
FIO_LOG_WARNING("(redis %d) subscription connection lost. "
"Reconnecting...",
(int)getpid());
}
fio_atomic_sub(&r->ref, 1);
defer_redis_connect(r, internal);
} else {
redis_free(r);
}
} else {
r = pub2redis(pr);
if (r->flag && uuid != -1) {
FIO_LOG_WARNING("(redis %d) publication connection lost. "
"Reconnecting...",
(int)getpid());
}
r->pub_sent = 0;
fio_close(r->sub_data.uuid);
redis_free(r);
}
(void)uuid;
} | /** Called when the connection was closed, but will not run concurrently */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L437-L470 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_shutdown | static uint8_t redis_on_shutdown(intptr_t uuid, fio_protocol_s *pr) {
fio_write2(uuid, .data.buffer = "*1\r\n$4\r\nQUIT\r\n", .length = 14,
.after.dealloc = FIO_DEALLOC_NOOP);
return 0;
(void)pr;
} | /** Called before the facil.io reactor is shut down. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L473-L478 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_sub_ping | static void redis_sub_ping(intptr_t uuid, fio_protocol_s *pr) {
fio_write2(uuid, .data.buffer = "*1\r\n$4\r\nPING\r\n", .length = 14,
.after.dealloc = FIO_DEALLOC_NOOP);
(void)pr;
} | /** Called on connection timeout. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L481-L485 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_pub_ping | static void redis_pub_ping(intptr_t uuid, fio_protocol_s *pr) {
redis_engine_s *r = pub2redis(pr);
if (fio_ls_embd_any(&r->queue)) {
FIO_LOG_WARNING("(redis) Redis server unresponsive, disconnecting.");
fio_close(uuid);
return;
}
redis_commands_s *cmd = fio_malloc(sizeof(*cmd) + 15);
*cmd = (redis_commands_s){.cmd_len = 14};
memcpy(cmd->cmd, "*1\r\n$4\r\nPING\r\n\0", 15);
redis_attach_cmd(r, cmd);
} | /** Called on connection timeout. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L488-L499 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_auth | static void redis_on_auth(fio_pubsub_engine_s *e, FIOBJ reply, void *udata) {
if (FIOBJ_TYPE_IS(reply, FIOBJ_T_TRUE)) {
fio_str_info_s s = fiobj_obj2cstr(reply);
FIO_LOG_WARNING("(redis) Authentication FAILED."
" %.*s",
(int)s.len, s.data);
}
(void)e;
(void)udata;
} | /* *****************************************************************************
Connecting to Redis
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L505-L514 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_subscribe_root | static void redis_on_subscribe_root(const fio_pubsub_engine_s *eng,
fio_str_info_s channel,
fio_match_fn match) {
redis_engine_s *r = (redis_engine_s *)eng;
if (r->sub_data.uuid != -1) {
FIOBJ cmd = fiobj_str_buf(96 + channel.len);
if (match == FIO_MATCH_GLOB)
fiobj_str_write(cmd, "*2\r\n$10\r\nPSUBSCRIBE\r\n$", 22);
else
fiobj_str_write(cmd, "*2\r\n$9\r\nSUBSCRIBE\r\n$", 20);
fiobj_str_write_i(cmd, channel.len);
fiobj_str_write(cmd, "\r\n", 2);
fiobj_str_write(cmd, channel.data, channel.len);
fiobj_str_write(cmd, "\r\n", 2);
// {
// fio_str_info_s s = fiobj_obj2cstr(cmd);
// fprintf(stderr, "(%d) Sending Subscription (%p):\n%s\n", getpid(),
// (void *)r->sub_data.uuid, s.data);
// }
fiobj_send_free(r->sub_data.uuid, cmd);
}
} | /* *****************************************************************************
Engine / Bridge Callbacks (Root Process)
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L589-L610 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_mock_subscribe_child | static void redis_on_mock_subscribe_child(const fio_pubsub_engine_s *eng,
fio_str_info_s channel,
fio_match_fn match) {
/* do nothing, root process is notified about (un)subscriptions by facil.io */
(void)eng;
(void)channel;
(void)match;
} | /* *****************************************************************************
Engine / Bridge Stub Callbacks (Child Process)
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L671-L678 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_internal_publish | static void redis_on_internal_publish(fio_msg_s *msg) {
if (msg->channel.len < 8)
return; /* internal error, unexpected data */
void *en = (void *)fio_str2u64(msg->channel.data);
if (en != msg->udata1)
return; /* should be delivered by a different engine */
/* step after the engine data */
msg->channel.len -= 8;
msg->channel.data += 8;
/* forward to publishing */
FIO_LOG_DEBUG("Forwarding to engine %p, on channel %s", msg->udata1,
msg->channel.data);
redis_on_publish_root(msg->udata1, msg->channel, msg->msg, msg->is_json);
} | /* *****************************************************************************
Root Publication Handler
***************************************************************************** */
/* listens to filter -1 and publishes and messages */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L701-L714 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_forward_reply | static void redis_forward_reply(fio_pubsub_engine_s *e, FIOBJ reply,
void *udata) {
uint8_t *data = udata;
fio_pubsub_engine_s *engine = (fio_pubsub_engine_s *)fio_str2u64(data + 0);
void *callback = (void *)fio_str2u64(data + 8);
if (engine != e || !callback) {
FIO_LOG_DEBUG("Redis reply not forwarded (callback: %p)", callback);
return;
}
int32_t pid = (int32_t)fio_str2u32(data + 24);
FIOBJ rp = fiobj_obj2json(reply, 0);
fio_publish(.filter = (-10 - (int32_t)pid), .channel.data = (char *)data,
.channel.len = 28, .message = fiobj_obj2cstr(rp), .is_json = 1);
fiobj_free(rp);
} | /* *****************************************************************************
Sending commands using the Root connection
***************************************************************************** */
/* callback from the Redis reply */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L721-L735 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_internal_cmd | static void redis_on_internal_cmd(fio_msg_s *msg) {
// void*(void *)fio_str2u64(msg->msg.data);
fio_pubsub_engine_s *engine =
(fio_pubsub_engine_s *)fio_str2u64(msg->channel.data + 0);
if (engine != msg->udata1) {
return;
}
redis_commands_s *cmd = fio_malloc(sizeof(*cmd) + msg->msg.len + 1 + 28);
FIO_ASSERT_ALLOC(cmd);
*cmd = (redis_commands_s){.callback = redis_forward_reply,
.udata = (cmd->cmd + msg->msg.len + 1),
.cmd_len = msg->msg.len};
memcpy(cmd->cmd, msg->msg.data, msg->msg.len);
memcpy(cmd->cmd + msg->msg.len + 1, msg->channel.data, 28);
redis_attach_cmd((redis_engine_s *)engine, cmd);
// fprintf(stderr, " *** Attached CMD (%d) ***\n%s\n", getpid(), cmd->cmd);
} | /* listens to channel -2 for commands that need to be sent (only ROOT) */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L738-L754 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_internal_reply | static void redis_on_internal_reply(fio_msg_s *msg) {
fio_pubsub_engine_s *engine =
(fio_pubsub_engine_s *)fio_str2u64(msg->channel.data + 0);
if (engine != msg->udata1) {
FIO_LOG_DEBUG("Redis reply not forwarded (engine mismatch: %p != %p)",
(void *)engine, msg->udata1);
return;
}
FIOBJ reply;
fiobj_json2obj(&reply, msg->msg.data, msg->msg.len);
void (*callback)(fio_pubsub_engine_s *, FIOBJ, void *) = (void (*)(
fio_pubsub_engine_s *, FIOBJ, void *))fio_str2u64(msg->channel.data + 8);
void *udata = (void *)fio_str2u64(msg->channel.data + 16);
callback(engine, reply, udata);
fiobj_free(reply);
} | /* Listens on filter `-10 -getpid()` for incoming reply data */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L757-L772 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_engine_send | intptr_t redis_engine_send(fio_pubsub_engine_s *engine, FIOBJ command,
void (*callback)(fio_pubsub_engine_s *e, FIOBJ reply,
void *udata),
void *udata) {
if ((uintptr_t)engine < 4) {
FIO_LOG_WARNING("(redis send) trying to use one of the core engines");
return -1;
}
// if(fio_is_master()) {
// FIOBJ resp = fiobj2resp_tmp(fio_str_info_s obj1, FIOBJ obj2);
// TODO...
// } else {
/* forward publication request to Root */
fio_str_s tmp = FIO_STR_INIT;
fio_str_info_s ti = fio_str_resize(&tmp, 28);
/* combine metadata */
fio_u2str64(ti.data + 0, (uint64_t)engine);
fio_u2str64(ti.data + 8, (uint64_t)callback);
fio_u2str64(ti.data + 16, (uint64_t)udata);
fio_u2str32(ti.data + 24, (uint32_t)getpid());
FIOBJ cmd = fiobj2resp_tmp(command);
fio_publish(.filter = -2, .channel = ti, .message = fiobj_obj2cstr(cmd),
.engine = FIO_PUBSUB_ROOT, .is_json = 0);
fio_str_free(&tmp);
// }
return 0;
} | /* publishes a Redis command to Root's filter -2 */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L775-L801 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_on_facil_start | static void redis_on_facil_start(void *r_) {
redis_engine_s *r = r_;
r->flag = 1;
if (!fio_is_valid(r->sub_data.uuid)) {
defer_redis_connect(r, &r->sub_data);
}
} | /* *****************************************************************************
Redis Engine Creation
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L807-L813 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | redis_engine_destroy | void redis_engine_destroy(fio_pubsub_engine_s *engine) {
redis_engine_s *r = (redis_engine_s *)engine;
r->flag = 0;
fio_pubsub_detach(&r->en);
fio_state_callback_remove(FIO_CALL_IN_CHILD, redis_on_engine_fork, r);
fio_state_callback_remove(FIO_CALL_ON_SHUTDOWN, redis_on_facil_shutdown, r);
fio_state_callback_remove(FIO_CALL_PRE_START, redis_on_facil_start, r);
FIO_LOG_DEBUG("Redis engine destroyed %p", (void *)r);
redis_free(r);
} | /* *****************************************************************************
Redis Engine Destruction
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/redis/redis_engine.c#L945-L954 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | alpn_add | FIO_FUNC inline void alpn_add(
fio_tls_s *tls, const char *protocol_name,
void (*on_selected)(intptr_t uuid, void *udata_connection, void *udata_tls),
void *udata_tls, void (*on_cleanup)(void *udata_tls)) {
alpn_s tmp = {
.name = FIO_STR_INIT_STATIC(protocol_name),
.on_selected = on_selected,
.udata_tls = udata_tls,
.on_cleanup = on_cleanup,
};
if (fio_str_len(&tmp.name) > 255) {
FIO_LOG_ERROR("ALPN protocol names are limited to 255 bytes.");
return;
}
alpn_list_overwrite(&tls->alpn, fio_str_hash(&tmp.name), tmp, NULL);
tmp.on_cleanup = NULL;
fio_alpn_destroy(&tmp);
} | /** Adds an ALPN data object to the ALPN "list" (set) */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L168-L185 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | alpn_select | FIO_FUNC inline void alpn_select(alpn_s *alpn, intptr_t uuid,
void *udata_connection) {
if (!alpn || !alpn->on_selected)
return;
alpn_task_s *t = fio_malloc(sizeof(*t));
*t = (alpn_task_s){
.alpn = *alpn,
.uuid = uuid,
.udata_connection = udata_connection,
};
/* move task out of the socket's lock */
fio_defer(alpn_select___task, t, NULL);
} | /** Schedules the ALPN protocol callback. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L209-L221 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_destroy_context | static void fio_tls_destroy_context(fio_tls_s *tls) {
/* TODO: Library specific implementation */
FIO_LOG_DEBUG("destroyed TLS context %p", (void *)tls);
} | /* *****************************************************************************
SSL/TLS Context (re)-building - TODO: add implementation details
***************************************************************************** */
/** Called when the library specific data for the context should be destroyed */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L228-L231 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_build_context | static void fio_tls_build_context(fio_tls_s *tls) {
fio_tls_destroy_context(tls);
/* TODO: Library specific implementation */
/* Certificates */
FIO_ARY_FOR(&tls->sni, pos) {
fio_str_info_s k = fio_str_info(&pos->private_key);
fio_str_info_s p = fio_str_info(&pos->public_key);
fio_str_info_s pw = fio_str_info(&pos->password);
if (p.len && k.len) {
/* TODO: attache certificate */
(void)pw;
} else {
/* TODO: self signed certificate */
}
}
/* ALPN Protocols */
FIO_SET_FOR_LOOP(&tls->alpn, pos) {
fio_str_info_s name = fio_str_info(&pos->obj.name);
(void)name;
// map to pos->callback;
}
/* Peer Verification / Trust */
if (trust_ary_count(&tls->trust)) {
/* TODO: enable peer verification */
/* TODO: Add each ceriticate in the PEM to the trust "store" */
FIO_ARY_FOR(&tls->trust, pos) {
fio_str_info_s pem = fio_str_info(&pos->pem);
(void)pem;
}
}
FIO_LOG_DEBUG("(re)built TLS context %p", (void *)tls);
} | /** Called when the library specific data for the context should be built */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L234-L270 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_read | static ssize_t fio_tls_read(intptr_t uuid, void *udata, void *buf,
size_t count) {
ssize_t ret = read(fio_uuid2fd(uuid), buf, count);
if (ret > 0) {
FIO_LOG_DEBUG("Read %zd bytes from %p", ret, (void *)uuid);
}
return ret;
(void)udata;
} | /**
* Implement reading from a file descriptor. Should behave like the file
* system `read` call, including the setup or errno to EAGAIN / EWOULDBLOCK.
*
* Note: facil.io library functions MUST NEVER be called by any r/w hook, or a
* deadlock might occur.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L293-L301 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_flush | static ssize_t fio_tls_flush(intptr_t uuid, void *udata) {
buffer_s *buffer = udata;
if (!buffer->len) {
FIO_LOG_DEBUG("Flush empty for %p", (void *)uuid);
return 0;
}
ssize_t r = write(fio_uuid2fd(uuid), buffer->buffer, buffer->len);
if (r < 0)
return -1;
if (r == 0) {
errno = ECONNRESET;
return -1;
}
size_t len = buffer->len - r;
if (len)
memmove(buffer->buffer, buffer->buffer + r, len);
buffer->len = len;
FIO_LOG_DEBUG("Sent %zd bytes to %p", r, (void *)uuid);
return r;
} | /**
* When implemented, this function will be called to flush any data remaining
* in the internal buffer.
*
* The function should return the number of bytes remaining in the internal
* buffer (0 is a valid response) or -1 (on error).
*
* Note: facil.io library functions MUST NEVER be called by any r/w hook, or a
* deadlock might occur.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L313-L332 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_write | static ssize_t fio_tls_write(intptr_t uuid, void *udata, const void *buf,
size_t count) {
buffer_s *buffer = udata;
size_t can_copy = TLS_BUFFER_LENGTH - buffer->len;
if (can_copy > count)
can_copy = count;
if (!can_copy)
goto would_block;
memcpy(buffer->buffer + buffer->len, buf, can_copy);
buffer->len += can_copy;
FIO_LOG_DEBUG("Copied %zu bytes to %p", can_copy, (void *)uuid);
fio_tls_flush(uuid, udata);
return can_copy;
would_block:
errno = EWOULDBLOCK;
return -1;
} | /**
* Implement writing to a file descriptor. Should behave like the file system
* `write` call.
*
* If an internal buffer is implemented and it is full, errno should be set to
* EWOULDBLOCK and the function should return -1.
*
* Note: facil.io library functions MUST NEVER be called by any r/w hook, or a
* deadlock might occur.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L344-L360 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_before_close | static ssize_t fio_tls_before_close(intptr_t uuid, void *udata) {
FIO_LOG_DEBUG("The `before_close` callback was called for %p", (void *)uuid);
return 1;
(void)udata;
} | /**
* The `close` callback should close the underlying socket / file descriptor.
*
* If the function returns a non-zero value, it will be called again after an
* attempt to flush the socket and any pending outgoing buffer.
*
* Note: facil.io library functions MUST NEVER be called by any r/w hook, or a
* deadlock might occur.
* */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L371-L375 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_cleanup | static void fio_tls_cleanup(void *udata) {
buffer_s *buffer = udata;
/* make sure the ALPN callback was called, just in case cleanup is required */
if (!buffer->alpn_ok) {
alpn_select(alpn_default(buffer->tls), -1, NULL /* ALPN udata */);
}
fio_tls_destroy(buffer->tls); /* manage reference count */
fio_free(udata);
} | /**
* Called to perform cleanup after the socket was closed.
* */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L379-L387 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_cert_add | int FIO_TLS_WEAK fio_tls_cert_add(fio_tls_s *tls, const char *server_name,
const char *cert, const char *key,
const char *pk_password) {
REQUIRE_LIBRARY();
cert_s c = {
.private_key = FIO_STR_INIT,
.public_key = FIO_STR_INIT,
.password = FIO_STR_INIT_STATIC2(pk_password,
(pk_password ? strlen(pk_password) : 0)),
};
if (key && cert) {
if (fio_str_readfile(&c.private_key, key, 0, 0).data == NULL)
goto file_missing;
if (fio_str_readfile(&c.public_key, cert, 0, 0).data == NULL)
goto file_missing;
cert_ary_push(&tls->sni, c);
} else if (server_name) {
/* Self-Signed TLS Certificates */
c.private_key = FIO_STR_INIT_STATIC(server_name);
cert_ary_push(&tls->sni, c);
}
fio_tls_cert_destroy(&c);
fio_tls_build_context(tls);
return 0;
file_missing:
FIO_LOG_FATAL("TLS certificate file missing for either %s or %s or both.",
key, cert);
return -1; // rene
} | /**
* Adds a certificate a new SSL/TLS context / settings object.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L494-L522 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_alpn_add | void FIO_TLS_WEAK fio_tls_alpn_add(
fio_tls_s *tls, const char *protocol_name,
void (*on_selected)(intptr_t uuid, void *udata_connection, void *udata_tls),
void *udata_tls, void (*on_cleanup)(void *udata_tls)) {
REQUIRE_LIBRARY();
alpn_add(tls, protocol_name, on_selected, udata_tls, on_cleanup);
fio_tls_build_context(tls);
} | /**
* Adds an ALPN protocol callback to the SSL/TLS context.
*
* The first protocol added will act as the default protocol to be selected.
*
* The callback should accept the `uuid`, the user data pointer passed to either
* `fio_tls_accept` or `fio_tls_connect` (here: `udata_connetcion`) and the user
* data pointer passed to the `fio_tls_alpn_add` function (`udata_tls`).
*
* The `on_cleanup` callback will be called when the TLS object is destroyed (or
* `fio_tls_alpn_add` is called again with the same protocol name). The
* `udata_tls` argumrnt will be passed along, as is, to the callback (if set).
*
* Except for the `tls` and `protocol_name` arguments, all arguments can be
* NULL.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L540-L547 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_alpn_count | uintptr_t FIO_TLS_WEAK fio_tls_alpn_count(fio_tls_s *tls) {
return tls ? alpn_list_count(&tls->alpn) : 0;
} | /**
* Returns the number of registered ALPN protocol names.
*
* This could be used when deciding if protocol selection should be delegated to
* the ALPN mechanism, or whether a protocol should be immediately assigned.
*
* If no ALPN protocols are registered, zero (0) is returned.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L557-L559 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_trust | int FIO_TLS_WEAK fio_tls_trust(fio_tls_s *tls, const char *public_cert_file) {
REQUIRE_LIBRARY();
trust_s c = {
.pem = FIO_STR_INIT,
};
if (!public_cert_file)
return 0;
if (fio_str_readfile(&c.pem, public_cert_file, 0, 0).data == NULL)
goto file_missing;
trust_ary_push(&tls->trust, c);
fio_tls_trust_destroy(&c);
fio_tls_build_context(tls);
return 0;
file_missing:
FIO_LOG_FATAL("TLS certificate file missing for %s ", public_cert_file);
return -1; // rene
} | /**
* Adds a certificate to the "trust" list, which automatically adds a peer
* verification requirement.
*
* fio_tls_trust(tls, "google-ca.pem" );
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L567-L583 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_accept | void FIO_TLS_WEAK fio_tls_accept(intptr_t uuid, fio_tls_s *tls, void *udata) {
REQUIRE_LIBRARY();
fio_tls_attach2uuid(uuid, tls, udata, 1);
} | /**
* Establishes an SSL/TLS connection as an SSL/TLS Server, using the specified
* context / settings object.
*
* The `uuid` should be a socket UUID that is already connected to a peer (i.e.,
* the result of `fio_accept`).
*
* The `udata` is an opaque user data pointer that is passed along to the
* protocol selected (if any protocols were added using `fio_tls_alpn_add`).
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L595-L598 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_connect | void FIO_TLS_WEAK fio_tls_connect(intptr_t uuid, fio_tls_s *tls, void *udata) {
REQUIRE_LIBRARY();
fio_tls_attach2uuid(uuid, tls, udata, 0);
} | /**
* Establishes an SSL/TLS connection as an SSL/TLS Client, using the specified
* context / settings object.
*
* The `uuid` should be a socket UUID that is already connected to a peer (i.e.,
* one received by a `fio_connect` specified callback `on_connect`).
*
* The `udata` is an opaque user data pointer that is passed along to the
* protocol selected (if any protocols were added using `fio_tls_alpn_add`).
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L610-L613 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_dup | void FIO_TLS_WEAK fio_tls_dup(fio_tls_s *tls) { fio_atomic_add(&tls->ref, 1); } | /**
* Increase the reference count for the TLS object.
*
* Decrease with `fio_tls_destroy`.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L620-L620 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_destroy | void FIO_TLS_WEAK fio_tls_destroy(fio_tls_s *tls) {
if (!tls)
return;
REQUIRE_LIBRARY();
if (fio_atomic_sub(&tls->ref, 1))
return;
fio_tls_destroy_context(tls);
alpn_list_free(&tls->alpn);
cert_ary_free(&tls->sni);
trust_ary_free(&tls->trust);
free(tls);
} | /**
* Destroys the SSL/TLS context / settings object and frees any related
* resources / memory.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_missing.c#L626-L637 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | alpn_add | FIO_FUNC inline void alpn_add(
fio_tls_s *tls, const char *protocol_name,
void (*on_selected)(intptr_t uuid, void *udata_connection, void *udata_tls),
void *udata_tls, void (*on_cleanup)(void *udata_tls)) {
alpn_s tmp = {
.name = FIO_STR_INIT_STATIC(protocol_name),
.on_selected = on_selected,
.udata_tls = udata_tls,
.on_cleanup = on_cleanup,
};
if (fio_str_len(&tmp.name) > 255) {
FIO_LOG_ERROR("ALPN protocol names are limited to 255 bytes.");
return;
}
alpn_list_overwrite(&tls->alpn, fio_str_hash(&tmp.name), tmp, NULL);
tmp.on_cleanup = NULL;
fio_alpn_destroy(&tmp);
} | /** Adds an ALPN data object to the ALPN "list" (set) */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L159-L176 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | alpn_select | FIO_FUNC inline void alpn_select(alpn_s *alpn, intptr_t uuid,
void *udata_connection) {
if (!alpn || !alpn->on_selected)
return;
alpn_task_s *t = fio_malloc(sizeof(*t));
*t = (alpn_task_s){
.alpn = *alpn,
.uuid = uuid,
.udata_connection = udata_connection,
};
fio_defer(alpn_select___task, t, NULL);
} | /** Schedules the ALPN protocol callback. */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L200-L211 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_destroy_context | static void fio_tls_destroy_context(fio_tls_s *tls) {
/* TODO: Library specific implementation */
SSL_CTX_free(tls->ctx);
free(tls->alpn_str);
tls->ctx = NULL;
tls->alpn_str = NULL;
tls->alpn_len = 0;
FIO_LOG_DEBUG("destroyed TLS context for OpenSSL %p", (void *)tls);
} | /** Called when the library specific data for the context should be destroyed */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L357-L366 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_build_context | static void fio_tls_build_context(fio_tls_s *tls) {
fio_tls_destroy_context(tls);
/* TODO: Library specific implementation */
/* create new context */
tls->ctx = SSL_CTX_new(TLS_method());
SSL_CTX_set_mode(tls->ctx, SSL_MODE_ENABLE_PARTIAL_WRITE);
/* see: https://caniuse.com/#search=tls */
SSL_CTX_set_min_proto_version(tls->ctx, TLS1_2_VERSION);
SSL_CTX_set_options(tls->ctx, SSL_OP_NO_COMPRESSION);
/* attach certificates */
FIO_ARY_FOR(&tls->sni, pos) {
fio_str_info_s keys[4] = {
fio_str_info(&pos->private_key), fio_str_info(&pos->public_key),
fio_str_info(&pos->password),
/* empty password slot for public key */
};
if (keys[0].len && keys[1].len) {
if (1) {
/* Extract private key from private key file */
BIO *bio = BIO_new_mem_buf(keys[0].data, keys[0].len);
if (bio) {
EVP_PKEY *k = PEM_read_bio_PrivateKey(
bio, NULL, fio_tls_pem_passwd_cb, keys + 2);
if (k) {
FIO_LOG_DEBUG("TLS read private key from PEM file.");
SSL_CTX_use_PrivateKey(tls->ctx, k);
}
BIO_free(bio);
}
}
/* Certificate Files loaded */
for (int ki = 0; ki < 2; ++ki) {
/* Extract as much data as possible from each file */
BIO *bio = BIO_new_mem_buf(keys[ki].data, keys[ki].len);
FIO_ASSERT(bio, "OpenSSL error allocating BIO.");
STACK_OF(X509_INFO) *inf = PEM_X509_INFO_read_bio(
bio, NULL, fio_tls_pem_passwd_cb, keys + ki + 2);
if (inf) {
for (int i = 0; i < sk_X509_INFO_num(inf); ++i) {
/* for each element in PEM */
X509_INFO *tmp = sk_X509_INFO_value(inf, i);
if (tmp->x509) {
FIO_LOG_DEBUG("TLS adding certificate from PEM file.");
SSL_CTX_use_certificate(tls->ctx, tmp->x509);
}
if (tmp->x_pkey) {
FIO_LOG_DEBUG("TLS adding private key from PEM file.");
SSL_CTX_use_PrivateKey(tls->ctx, tmp->x_pkey->dec_pkey);
}
}
sk_X509_INFO_pop_free(inf, X509_INFO_free);
} else {
/* TODO: attempt DER format? */
// X509 *c;
// EVP_PKEY *k;
// const uint8_t *pdata = (uint8_t *)&keys[ki].data;
// d2i_X509(&c, &pdata, keys[ki].len);
// pdata = (uint8_t *)&keys[ki].data;
// d2i_AutoPrivateKey(&k, &pdata, keys[ki].len);
}
BIO_free(bio);
}
} else if (keys[0].len) {
/* Self Signed Certificates, only if server name is provided. */
SSL_CTX_use_certificate(tls->ctx,
fio_tls_create_self_signed(keys[0].data));
SSL_CTX_use_PrivateKey(tls->ctx, fio_tls_pkey);
}
}
/* setup ALPN support */
if (1) {
size_t alpn_pos = 0;
/* looping twice is better than malloc fragmentation. */
FIO_SET_FOR_LOOP(&tls->alpn, pos) {
fio_str_info_s s = fio_str_info(&pos->obj.name);
if (!s.len)
continue;
alpn_pos += s.len + 1;
}
tls->alpn_str = malloc((alpn_pos | 15) + 1); /* round up to 16 + padding */
alpn_pos = 0;
FIO_SET_FOR_LOOP(&tls->alpn, pos) {
fio_str_info_s s = fio_str_info(&pos->obj.name);
if (!s.len)
continue;
tls->alpn_str[alpn_pos++] = (uint8_t)s.len;
memcpy(tls->alpn_str + alpn_pos, s.data, s.len);
alpn_pos += s.len;
}
tls->alpn_len = alpn_pos;
SSL_CTX_set_alpn_select_cb(tls->ctx, fio_tls_alpn_selector_cb, tls);
SSL_CTX_set_alpn_protos(tls->ctx, tls->alpn_str, tls->alpn_len);
}
/* Peer Verification / Trust */
if (trust_ary_count(&tls->trust)) {
/* TODO: enable peer verification */
X509_STORE *store = X509_STORE_new();
SSL_CTX_set_cert_store(tls->ctx, store);
SSL_CTX_set_verify(tls->ctx, SSL_VERIFY_PEER, NULL);
/* TODO: Add each ceriticate in the PEM to the trust "store" */
FIO_ARY_FOR(&tls->trust, pos) {
fio_str_info_s pem = fio_str_info(&pos->pem);
BIO *bio = BIO_new_mem_buf(pem.data, pem.len);
FIO_ASSERT(bio, "OpenSSL error allocating BIO.");
STACK_OF(X509_INFO) *inf = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
if (inf) {
for (int i = 0; i < sk_X509_INFO_num(inf); ++i) {
/* for each element in PEM */
X509_INFO *tmp = sk_X509_INFO_value(inf, i);
if (tmp->x509) {
FIO_LOG_DEBUG("TLS trusting certificate from PEM file.");
X509_STORE_add_cert(store, tmp->x509);
}
if (tmp->crl) {
X509_STORE_add_crl(store, tmp->crl);
}
}
sk_X509_INFO_pop_free(inf, X509_INFO_free);
}
BIO_free(bio);
}
}
FIO_LOG_DEBUG("(re)built TLS context for OpenSSL %p", (void *)tls);
} | /** Called when the library specific data for the context should be built */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L381-L509 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_delayed_close | static void fio_tls_delayed_close(void *uuid, void *ignr_) {
fio_close((intptr_t)uuid);
(void)ignr_;
} | /* *****************************************************************************
SSL/TLS RW Hooks
***************************************************************************** */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L515-L518 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_read | static ssize_t fio_tls_read(intptr_t uuid, void *udata, void *buf,
size_t count) {
fio_tls_connection_s *c = udata;
ssize_t ret = SSL_read(c->ssl, buf, count);
if (ret > 0)
return ret;
ret = SSL_get_error(c->ssl, ret);
switch (ret) {
case SSL_ERROR_SSL: /* overflow */
case SSL_ERROR_ZERO_RETURN:
return 0; /* EOF */
case SSL_ERROR_NONE: /* overflow */
case SSL_ERROR_WANT_CONNECT: /* overflow */
case SSL_ERROR_WANT_ACCEPT: /* overflow */
case SSL_ERROR_WANT_X509_LOOKUP: /* overflow */
#ifdef SSL_ERROR_WANT_ASYNC
case SSL_ERROR_WANT_ASYNC: /* overflow */
#endif
case SSL_ERROR_WANT_WRITE: /* overflow */
case SSL_ERROR_WANT_READ:
default:
break;
}
errno = EWOULDBLOCK;
return -1;
(void)uuid;
} | /* TODO: this is an example implementation - fix for specific library. */
/**
* Implement reading from a file descriptor. Should behave like the file
* system `read` call, including the setup or errno to EAGAIN / EWOULDBLOCK.
*
* Note: facil.io library functions MUST NEVER be called by any r/w hook, or a
* deadlock might occur.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L529-L555 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_flush | static ssize_t fio_tls_flush(intptr_t uuid, void *udata) {
(void)uuid;
(void)udata;
return 0;
} | /**
* When implemented, this function will be called to flush any data remaining
* in the internal buffer.
*
* The function should return the number of bytes remaining in the internal
* buffer (0 is a valid response) or -1 (on error).
*
* Note: facil.io library functions MUST NEVER be called by any r/w hook, or a
* deadlock might occur.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L567-L571 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_write | static ssize_t fio_tls_write(intptr_t uuid, void *udata, const void *buf,
size_t count) {
fio_tls_connection_s *c = udata;
ssize_t ret = SSL_write(c->ssl, buf, count);
if (ret > 0)
return ret;
ret = SSL_get_error(c->ssl, ret);
switch (ret) {
case SSL_ERROR_SSL: /* overflow */
case SSL_ERROR_ZERO_RETURN:
return 0; /* EOF */
case SSL_ERROR_NONE: /* overflow */
case SSL_ERROR_WANT_CONNECT: /* overflow */
case SSL_ERROR_WANT_ACCEPT: /* overflow */
case SSL_ERROR_WANT_X509_LOOKUP: /* overflow */
#ifdef SSL_ERROR_WANT_ASYNC
case SSL_ERROR_WANT_ASYNC: /* overflow */
#endif
case SSL_ERROR_WANT_WRITE: /* overflow */
case SSL_ERROR_WANT_READ:
default:
break;
}
errno = EWOULDBLOCK;
return -1;
(void)uuid;
} | /**
* Implement writing to a file descriptor. Should behave like the file system
* `write` call.
*
* If an internal buffer is implemented and it is full, errno should be set to
* EWOULDBLOCK and the function should return -1.
*
* Note: facil.io library functions MUST NEVER be called by any r/w hook, or a
* deadlock might occur.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L583-L609 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_before_close | static ssize_t fio_tls_before_close(intptr_t uuid, void *udata) {
fio_tls_connection_s *c = udata;
SSL_shutdown(c->ssl);
return 1;
(void)uuid;
} | /**
* The `close` callback should close the underlying socket / file descriptor.
*
* If the function returns a non-zero value, it will be called again after an
* attempt to flush the socket and any pending outgoing buffer.
*
* Note: facil.io library functions MUST NEVER be called by any r/w hook, or a
* deadlock might occur.
* */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L620-L625 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_cleanup | static void fio_tls_cleanup(void *udata) {
fio_tls_connection_s *c = udata;
if (!c->alpn_ok) {
alpn_select(alpn_default(c->tls), -1, c->alpn_arg);
}
SSL_free(c->ssl);
FIO_LOG_DEBUG("TLS cleanup for %p", (void *)c->uuid);
fio_tls_destroy(c->tls); /* manage reference count */
free(udata);
} | /**
* Called to perform cleanup after the socket was closed.
* */ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L629-L638 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_cert_add | int FIO_TLS_WEAK fio_tls_cert_add(fio_tls_s *tls, const char *server_name,
const char *cert, const char *key,
const char *pk_password) {
REQUIRE_LIBRARY();
cert_s c = {
.private_key = FIO_STR_INIT,
.public_key = FIO_STR_INIT,
.password = FIO_STR_INIT_STATIC2(pk_password,
(pk_password ? strlen(pk_password) : 0)),
};
if (key && cert) {
if (fio_str_readfile(&c.private_key, key, 0, 0).data == NULL)
goto file_missing;
if (fio_str_readfile(&c.public_key, cert, 0, 0).data == NULL)
goto file_missing;
cert_ary_push(&tls->sni, c);
} else if (server_name) {
/* Self-Signed TLS Certificates */
c.private_key = FIO_STR_INIT_STATIC(server_name);
cert_ary_push(&tls->sni, c);
}
fio_tls_cert_destroy(&c);
fio_tls_build_context(tls);
return 0;
file_missing:
FIO_LOG_FATAL("TLS certificate file missing for either %s or %s or both.",
key, cert);
return -1;
} | /**
* Adds a certificate a new SSL/TLS context / settings object.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L869-L897 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_alpn_add | void FIO_TLS_WEAK fio_tls_alpn_add(
fio_tls_s *tls, const char *protocol_name,
void (*on_selected)(intptr_t uuid, void *udata_connection, void *udata_tls),
void *udata_tls, void (*on_cleanup)(void *udata_tls)) {
REQUIRE_LIBRARY();
alpn_add(tls, protocol_name, on_selected, udata_tls, on_cleanup);
fio_tls_build_context(tls);
} | /**
* Adds an ALPN protocol callback to the SSL/TLS context.
*
* The first protocol added will act as the default protocol to be selected.
*
* The callback should accept the `uuid`, the user data pointer passed to
* either `fio_tls_accept` or `fio_tls_connect` (here: `udata_connetcion`) and
* the user data pointer passed to the `fio_tls_alpn_add` function
* (`udata_tls`).
*
* The `on_cleanup` callback will be called when the TLS object is destroyed
* (or `fio_tls_alpn_add` is called again with the same protocol name). The
* `udata_tls` argumrnt will be passed along, as is, to the callback (if set).
*
* Except for the `tls` and `protocol_name` arguments, all arguments can be
* NULL.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L916-L923 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
zap | github_2023 | zigzap | c | fio_tls_alpn_count | uintptr_t FIO_TLS_WEAK fio_tls_alpn_count(fio_tls_s *tls) {
return tls ? alpn_list_count(&tls->alpn) : 0;
} | /**
* Returns the number of registered ALPN protocol names.
*
* This could be used when deciding if protocol selection should be delegated
* to the ALPN mechanism, or whether a protocol should be immediately
* assigned.
*
* If no ALPN protocols are registered, zero (0) is returned.
*/ | https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/tls/fio_tls_openssl.c#L934-L936 | 675c65b509d48c21a8d1fa4c5ec53fc407643a3b |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.