|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
Static file serving middleware implementation. More...
#include <fcntl.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <unistd.h>#include <uv.h>#include "csilk/csilk.h"#include "csilk/core/internal.h"
Functions | |
| static const char * | get_mime_type (const char *path) |
| Internal helper to get MIME type from file path. | |
| static void | set_range_response (csilk_ctx_t *c, int fd, size_t size, const char *mime_type) |
| Setup a file response with optional 206 Partial Content for range requests. | |
| static void | static_work_cb (uv_work_t *req) |
| libuv work callback: resolve and open the requested static file. | |
| static void | file_work_cb (uv_work_t *req) |
| libuv work callback: open and stat a specific file. | |
| static void | static_after_work_cb (uv_work_t *req, int status) |
| libuv after-work callback: send the static file response. | |
| void | csilk_static (csilk_ctx_t *c, const char *root_dir) |
| Serve static files from a local directory (async, offloaded). | |
| void | csilk_file (csilk_ctx_t *c, const char *file_path) |
| Serve a specific file (async, offloaded). | |
Static file serving middleware implementation.
| void csilk_file | ( | csilk_ctx_t * | c, |
| const char * | file_path | ||
| ) |
Serve a specific file (async, offloaded).
Serve a specific file from the local filesystem.
Offloads file open/stat operations to the libuv thread pool. The response is sent via the normal _csilk_send_response() path using zero-copy.
| c | The request context. |
| file_path | Path to the file to serve. |
| void csilk_static | ( | csilk_ctx_t * | c, |
| const char * | root_dir | ||
| ) |
Serve static files from a local directory (async, offloaded).
Serve static files from a local directory.
Offloads file resolution, path traversal checks, and file open/stat operations to the libuv thread pool. The response is sent via the normal _csilk_send_response() path, which sends the file using platform-specific zero-copy mechanisms (e.g. sendfile on Linux).
The URL prefix is automatically stripped from the request path before resolving against root_dir — the "static_prefix" must be set via csilk_set(c, "static_prefix", prefix_string) before calling this function.
| c | The request context. |
| root_dir | Absolute or relative path to the static file root directory. Must not be NULL. |
|
static |
libuv work callback: open and stat a specific file.
Runs in the libuv thread pool. Opens the file specified by the "serve_file_path" context property and sets up a range response.
| req | libuv work request. req->data points to the csilk_ctx_t. |
|
static |
Internal helper to get MIME type from file path.
Maps the file extension to a MIME type string. Currently supports .html, .css, and .js. All other extensions (including files without an extension) return application/octet-stream.
| path | The file path to evaluate. |
|
static |
Setup a file response with optional 206 Partial Content for range requests.
Examines the incoming "Range" header. If absent or malformed, the entire file is served with a 200 OK status. If a valid "bytes=" range is present, a 206 Partial Content response is configured with the appropriate Content-Range header.
In both cases, the file descriptor, offset, and size are stored on the context for subsequent async sending.
| c | The request context. |
| fd | Open file descriptor for the requested file. |
| size | Total file size in bytes. |
| mime_type | The MIME type string for Content-Type header. |
|
static |
libuv after-work callback: send the static file response.
Runs on the event loop after static_work_cb completes. If the client connection is still alive (_internal_client is non-NULL), it triggers _csilk_send_response() to transmit the file (using sendfile or chunked I/O depending on the platform).
| req | libuv work request. req->data points to the csilk_ctx_t. |
| status | Work status — 0 on success, negative on cancellation. |
|
static |
libuv work callback: resolve and open the requested static file.
Runs on the libuv thread pool. Resolves the requested path relative to the configured root directory, performs a path traversal check (ensuring the resolved path stays within the root), opens the file, stats it, determines the MIME type, and configures the response (including any range handling). The actual file content is sent later in static_after_work_cb via _csilk_send_response.
| req | libuv work request. req->data points to the csilk_ctx_t. The context must have "static_root" and "static_prefix" set via csilk_set(). |