|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
Gzip response compression middleware implementation. More...
#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <zlib.h>#include "csilk/csilk.h"#include "csilk/core/internal.h"
Data Structures | |
| struct | gzip_async_state_t |
| State for asynchronous gzip compression offloading. More... | |
Macros | |
| #define | CSILK_GZIP_CHUNK 16384 |
| #define | CSILK_GZIP_MIN_LENGTH 1024 |
Functions | |
| static void | gzip_work_cb (uv_work_t *req) |
| libuv work callback: perform gzip compression off the event loop. | |
| static void | gzip_after_work_cb (uv_work_t *req, int status) |
| libuv after-work callback: apply compressed body and send response. | |
| void | csilk_gzip_middleware (csilk_ctx_t *c) |
| Gzip response compression middleware (offloaded to thread pool). | |
Gzip response compression middleware implementation.
| struct gzip_async_state_t |
State for asynchronous gzip compression offloading.
Carries the compressed output buffer, capacity, actual compressed length, and the zlib return status between the work callback (running on the libuv thread pool) and the after-work callback (running on the event loop).
| Data Fields | ||
|---|---|---|
| size_t | compressed_len |
Actual compressed data length. |
| uint8_t * | dest |
Compressed output buffer. |
| size_t | dest_cap |
Capacity of the output buffer. |
| int | ret |
zlib return status. |
| #define CSILK_GZIP_CHUNK 16384 |
| #define CSILK_GZIP_MIN_LENGTH 1024 |
| void csilk_gzip_middleware | ( | csilk_ctx_t * | c | ) |
Gzip response compression middleware (offloaded to thread pool).
Gzip response compression middleware.
Calls csilk_next() first, then inspects the response. Compression is skipped if:
When eligible, the response body is made managed (copied if necessary) and compression is offloaded to the libuv thread pool via a work request.
| c | The request context. |
|
static |
libuv after-work callback: apply compressed body and send response.
Runs on the event loop after compression finishes. If compression succeeded (Z_STREAM_END), it replaces the original response body with the compressed data, sets Content-Encoding: gzip and Vary: Accept-Encoding headers, then sends the response. On failure the original uncompressed body is sent instead.
| 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: perform gzip compression off the event loop.
Initializes a zlib deflate stream with gzip wrapper (windowBits = 15 + 16), compresses the response body into a dynamically allocated buffer, then stores the result in the gzip_async_state_t attached to the context.
| req | libuv work request. req->data points to the csilk_ctx_t. |