From e763776a760800078d8f5b23d4ca2b467692e6a6 Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Tue, 16 Jun 2026 18:26:14 +0200 Subject: server: extract connection lifecycle, header parsing, and framing into server_http.c --- src/server.c | 381 +---------------------------------------------------------- 1 file changed, 1 insertion(+), 380 deletions(-) (limited to 'src/server.c') diff --git a/src/server.c b/src/server.c index 9c439f5..411ad3d 100644 --- a/src/server.c +++ b/src/server.c @@ -94,10 +94,6 @@ static model_slot model_slot_for_id(const char *id) { return MODEL_UNKNOWN; } -static int queue_write(client *c); -static void dispatch_request(client *c); -static void read_cb(aeEventLoop *loop, int fd, void *clientData, int mask); - static volatile sig_atomic_t g_stop_requested = 0; static volatile sig_atomic_t g_signal_wfd = -1; @@ -117,137 +113,6 @@ static void stop_signal_handler(int sig) { errno = saved; } -static int set_nonblock(int fd) { - int flags = fcntl(fd, F_GETFL, 0); - if (flags < 0) - return -1; - return fcntl(fd, F_SETFL, flags | O_NONBLOCK); -} - -static void http_request_free(http_request *r) { - free(r->body); - free(r->auth); - memset(r, 0, sizeof(*r)); -} - -static void client_free(client *c) { - if (!c) - return; - http_request_free(&c->req); - sbuf_free(&c->in); - sbuf_free(&c->out); - free(c); -} - -static void client_incref(client *c) { - pthread_mutex_lock(&c->srv->mu); - c->refcount++; - pthread_mutex_unlock(&c->srv->mu); -} - -static void client_decref(client *c) { - int free_now = 0; - pthread_mutex_lock(&c->srv->mu); - if (--c->refcount == 0) - free_now = 1; - pthread_mutex_unlock(&c->srv->mu); - if (free_now) - client_free(c); -} - -static void client_decref_n(client *c, int n) { - int free_now = 0; - if (n <= 0) - return; - pthread_mutex_lock(&c->srv->mu); - c->refcount -= n; - if (c->refcount <= 0) - free_now = 1; - pthread_mutex_unlock(&c->srv->mu); - if (free_now) - client_free(c); -} - -static void client_unlink(client *c) { - http_server *s = c->srv; - if (!c->linked) - return; - if (c->prev) - c->prev->next = c->next; - else - s->clients = c->next; - if (c->next) - c->next->prev = c->prev; - c->prev = c->next = NULL; - c->linked = 0; - if (s->n_clients > 0) - s->n_clients--; -} - -static int close_client_unlink(client *c) { - if (!c || c->cancelled) - return 0; - c->cancelled = 1; - if (c->fd >= 0) { - aeDeleteFileEvent(c->srv->loop, c->fd, AE_READABLE); - aeDeleteFileEvent(c->srv->loop, c->fd, AE_WRITABLE); - close(c->fd); - c->fd = -1; - } - client_unlink(c); - return 1; -} - -static void close_client(client *c) { - if (!close_client_unlink(c)) - return; - client_decref(c); -} - -static void append_http_response_ex(client *c, - int status, - const char *ctype, - const char *extra_headers, - const char *body, - size_t body_len) { - const char *reason = "OK"; - if (status == 204) - reason = "No Content"; - else if (status == 400) - reason = "Bad Request"; - else if (status == 401) - reason = "Unauthorized"; - else if (status == 404) - reason = "Not Found"; - else if (status == 405) - reason = "Method Not Allowed"; - else if (status == 422) - reason = "Unprocessable Entity"; - else if (status == 503) - reason = "Service Unavailable"; - else if (status >= 500) - reason = "Internal Server Error"; - - sbuf_printf(&c->out, - "HTTP/1.1 %d %s\r\n" - "Content-Length: %zu\r\n" - "Connection: %s\r\n", - status, reason, body_len, c->close_after_write ? "close" : "keep-alive"); - if (ctype) - sbuf_printf(&c->out, "Content-Type: %s\r\n", ctype); - - if (extra_headers) - sbuf_puts(&c->out, extra_headers); - sbuf_puts(&c->out, "\r\n"); - if (body_len) - sbuf_append(&c->out, body, body_len); -} - -static void -append_http_response(client *c, int status, const char *ctype, const char *body, size_t body_len) { - append_http_response_ex(c, status, ctype, NULL, body, body_len); -} - static bool auth_ok(http_server *s, const char *auth) { if (!s->api_key || !s->api_key[0]) return true; @@ -258,210 +123,6 @@ static bool auth_ok(http_server *s, const char *auth) { !strcmp(auth + sizeof(prefix) - 1, s->api_key); } -static ssize_t find_header_end(const char *p, size_t n) { - for (size_t i = 3; i < n; i++) { - if (p[i - 3] == '\r' && p[i - 2] == '\n' && p[i - 1] == '\r' && p[i] == '\n') - return (ssize_t)(i + 1); - } - for (size_t i = 1; i < n; i++) { - if (p[i - 1] == '\n' && p[i] == '\n') - return (ssize_t)(i + 1); - } - return -1; -} - -static char *header_value_dup(const char *h, size_t n, const char *name) { - size_t name_len = strlen(name); - const char *p = h, *end = h + n; - while (p < end) { - const char *line = p; - while (p < end && *p != '\n') - p++; - size_t len = (size_t)(p - line); - if (len && line[len - 1] == '\r') - len--; - if (len > name_len && !strncasecmp(line, name, name_len) && line[name_len] == ':') { - const char *v = line + name_len + 1; - while (v < line + len && isspace((unsigned char)*v)) - v++; - const char *e = line + len; - while (e > v && isspace((unsigned char)e[-1])) - e--; - return xstrndup(v, (size_t)(e - v)); - } - if (p < end) - p++; - } - return NULL; -} - -static int header_has_token(const char *value, const char *token) { - size_t token_len = strlen(token); - const char *p = value; - while (p && *p) { - while (*p == ',' || isspace((unsigned char)*p)) - p++; - const char *start = p; - while (*p && *p != ',') - p++; - const char *end = p; - while (end > start && isspace((unsigned char)end[-1])) - end--; - if ((size_t)(end - start) == token_len && !strncasecmp(start, token, token_len)) - return 1; - } - return 0; -} - -static int parse_request_headers(client *c) { - char line[512]; - size_t i = 0; - while (i < c->header_len && c->in.ptr[i] != '\n' && i + 1 < sizeof(line)) { - line[i] = c->in.ptr[i]; - i++; - } - line[i] = '\0'; - int fields = sscanf(line, "%7s %255s %15s", c->req.method, c->req.path, c->req.version); - if (fields < 2) - return -1; - if (fields < 3) - snprintf(c->req.version, sizeof(c->req.version), "HTTP/1.0"); - char *q = strchr(c->req.path, '?'); - if (q) - *q = '\0'; - - c->close_after_write = strcasecmp(c->req.version, "HTTP/1.1") != 0; - char *conn = header_value_dup(c->in.ptr, c->header_len, "Connection"); - if (conn) { - if (header_has_token(conn, "close")) - c->close_after_write = 1; - else if (header_has_token(conn, "keep-alive")) - c->close_after_write = 0; - free(conn); - } - - char *cl = header_value_dup(c->in.ptr, c->header_len, "Content-Length"); - if (cl) { - char *end = NULL; - errno = 0; - unsigned long long v = strtoull(cl, &end, 10); - if (errno || !end || *end || v > EMBED_HTTP_MAX_BODY) { - free(cl); - return -1; - } - c->content_length = (size_t)v; - free(cl); - } - c->req.auth = header_value_dup(c->in.ptr, c->header_len, "Authorization"); - return 0; -} - -static int complete_request(client *c) { - if (!c->header_done) { - ssize_t hend = find_header_end(c->in.ptr, c->in.len); - if (hend < 0) { - if (c->in.len > EMBED_HTTP_MAX_HEADER) - return -1; - return 0; - } - c->header_done = 1; - c->header_len = (size_t)hend; - if (parse_request_headers(c) != 0) - return -1; - } - if (c->content_length > EMBED_HTTP_MAX_BODY) - return -1; - if (c->in.len < c->header_len + c->content_length) - return 0; - c->req.body_len = c->content_length; - c->req.body = xmalloc(c->content_length + 1); - memcpy(c->req.body, c->in.ptr + c->header_len, c->content_length); - c->req.body[c->content_length] = '\0'; - return 1; -} - -static void send_bad_http_request(client *c) { - size_t len; - char *body = json_error_body("bad HTTP request", "invalid_request_error", &len); - c->close_after_write = 1; - append_http_response(c, 400, "application/json", body, len); - free(body); - aeDeleteFileEvent(c->srv->loop, c->fd, AE_READABLE); - if (queue_write(c) != AE_OK) - close_client(c); -} - -static void reset_client_for_next_request(client *c) { - size_t consumed = c->header_len + c->content_length; - if (consumed > c->in.len) - consumed = c->in.len; - size_t remain = c->in.len - consumed; - if (remain) - memmove(c->in.ptr, c->in.ptr + consumed, remain); - c->in.len = remain; - if (c->in.ptr) - c->in.ptr[remain] = '\0'; - - http_request_free(&c->req); - sbuf_clear(&c->out); - c->sent = 0; - c->header_done = 0; - c->header_len = 0; - c->content_length = 0; - c->close_after_write = 0; -} - -static void arm_next_request(client *c) { - reset_client_for_next_request(c); - if (c->cancelled || c->fd < 0) - return; - - if (c->in.len) { - int done = complete_request(c); - if (done < 0) { - send_bad_http_request(c); - return; - } - if (done > 0) { - dispatch_request(c); - return; - } - } - - if (aeCreateFileEvent(c->srv->loop, c->fd, AE_READABLE, read_cb, c) != AE_OK) - close_client(c); -} - -static void write_cb(aeEventLoop *loop, int fd, void *clientData, int mask) { - (void)loop; - (void)mask; - client *c = clientData; - while (c->sent < c->out.len) { - ssize_t n = write(fd, c->out.ptr + c->sent, c->out.len - c->sent); - if (n > 0) { - c->sent += (size_t)n; - c->last_active_ms = mstime(); - continue; - } - if (n < 0 && errno == EINTR) - continue; - if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) - return; - close_client(c); - return; - } - aeDeleteFileEvent(c->srv->loop, fd, AE_WRITABLE); - if (c->close_after_write) - close_client(c); - else - arm_next_request(c); -} - -static int queue_write(client *c) { - if (!c || c->cancelled || c->fd < 0) - return -1; - return aeCreateFileEvent(c->srv->loop, c->fd, AE_WRITABLE, write_cb, c); -} static void enqueue_done(job *j) { http_server *s = j->srv; @@ -616,13 +277,7 @@ static void completion_cb(aeEventLoop *loop, int fd, void *clientData, int mask) } } -static bool route_is_inference(const char *method, const char *path) { - return !strcmp(method, "POST") && - (!strcmp(path, "/v1/embeddings") || !strcmp(path, "/v1/contextualizedembeddings") || - !strcmp(path, "/v1/rerank")); -} - -static void dispatch_request(client *c) { +void dispatch_request(client *c) { aeDeleteFileEvent(c->srv->loop, c->fd, AE_READABLE); if (!strcmp(c->req.method, "OPTIONS")) { @@ -666,40 +321,6 @@ static void dispatch_request(client *c) { enqueue_job(j); } -static void read_cb(aeEventLoop *loop, int fd, void *clientData, int mask) { - (void)loop; - (void)mask; - client *c = clientData; - char tmp[EMBED_HTTP_IO_BUF]; - for (;;) { - ssize_t n = read(fd, tmp, sizeof(tmp)); - if (n > 0) { - sbuf_append(&c->in, tmp, (size_t)n); - c->last_active_ms = mstime(); - int done = complete_request(c); - if (done < 0) { - send_bad_http_request(c); - return; - } - if (done > 0) { - dispatch_request(c); - return; - } - continue; - } - if (n == 0) { - close_client(c); - return; - } - if (errno == EINTR) - continue; - if (errno == EAGAIN || errno == EWOULDBLOCK) - return; - close_client(c); - return; - } -} - static void accept_cb(aeEventLoop *loop, int fd, void *clientData, int mask) { (void)loop; (void)mask; -- cgit v1.3.1