Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
gzip.c File Reference

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"
Include dependency graph for gzip.c:

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).
 

Detailed Description

Gzip response compression middleware implementation.


Data Structure Documentation

◆ gzip_async_state_t

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.

Macro Definition Documentation

◆ CSILK_GZIP_CHUNK

#define CSILK_GZIP_CHUNK   16384

◆ CSILK_GZIP_MIN_LENGTH

#define CSILK_GZIP_MIN_LENGTH   1024

Function Documentation

◆ csilk_gzip_middleware()

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:

  • The response body is empty,
  • Content-Encoding is already set,
  • The Content-Type is binary / incompressible (image, video, audio, pdf, zip, gzip),
  • The client does not advertise gzip in Accept-Encoding,
  • The response body is smaller than CSILK_GZIP_MIN_LENGTH (1 KB).

When eligible, the response body is made managed (copied if necessary) and compression is offloaded to the libuv thread pool via a work request.

Parameters
cThe request context.
Note
This middleware must be registered AFTER handlers that produce the response body (since it calls csilk_next() first).
Warning
The response body is replaced in-place; subsequent middleware or cleanup code must not free the original pointer after compression.

◆ gzip_after_work_cb()

static void gzip_after_work_cb ( uv_work_t *  req,
int  status 
)
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.

Parameters
reqlibuv work request. req->data points to the csilk_ctx_t.
statusWork status — 0 on success, negative on cancellation.
Note
The original (uncompressed) response body is freed only if it was heap-managed (body_is_managed == 1).

◆ gzip_work_cb()

static void gzip_work_cb ( uv_work_t *  req)
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.

Parameters
reqlibuv work request. req->data points to the csilk_ctx_t.
Note
Runs on a libuv thread-pool thread. Must not touch non-thread-safe resources.