#ifndef SCORPIOX_HTTP_UTIL_H #define SCORPIOX_HTTP_UTIL_H /* * scorpiox-http-util.h — helpers every engine re-implemented. Vendored with the * transport; implemented in src/scorpiox_http_util.c. Binary-safe by design (B3). */ #include "scorpiox-http.h" #include #ifdef __cplusplus extern "C" { #endif /* ---- growable byte buffer (keeps the tail NUL-terminated for convenience) ---- */ typedef struct scorpiox_buf { unsigned char *p; size_t n, cap; } scorpiox_buf; int scorpiox_buf_add(scorpiox_buf *b, const void *d, size_t n); /* 0 ok, -1 OOM/overflow */ int scorpiox_buf_addc(scorpiox_buf *b, unsigned char c); int scorpiox_buf_adds(scorpiox_buf *b, const char *s); void scorpiox_buf_free(scorpiox_buf *b); char *scorpiox_strdup(const char *s); /* NULL-safe */ char *scorpiox_strndup(const char *s, size_t n); /* ---- request helpers ---- */ /* Build a "Name: value" line. Returns 0 / -1. name must be an RFC 9110 token, * value must carry no CR/LF/NUL or other control byte. */ int scorpiox_header_line(const char *name, const char *value, char *out, size_t n); /* Case-insensitive header lookup in a "Name: value" line table. * Returns the value start (leading spaces skipped) or NULL. */ const char *scorpiox_header_get(const scorpiox_http_request *rq, const char *name); /* RFC 9110 token check for a method. 1 = valid. */ int scorpiox_method_ok(const char *method); /* ---- response helpers ---- */ /* Case-insensitive lookup of the FINAL response headers already delivered to the * engine's collector (see scorpiox_collect). hdrs is a "name\0value\0" arena. * Returns the value or NULL. */ const char *scorpiox_hdrs_get(const scorpiox_buf *hdrs, const char *name); /* Content-Range: "bytes a-b/total"; "*" on either side means absent (-1). Returns 0 ok, -1 malformed. */ int scorpiox_content_range(const char *v, long long *a, long long *b, long long *total); /* Collector for "GET the whole body but bounded": fills body (malloc'd, caller frees * with free()) up to max bytes; stops the transfer when the limit is reached and * reports truncated. Use on_header/on_body wrappers below. */ typedef struct scorpiox_collect { scorpiox_buf body; /* bytes collected so far */ scorpiox_buf hdrs; /* "name\0value\0" arena of the final response */ long code; /* ":status" */ size_t max; /* 0 = unlimited */ int truncated; /* stopped early: max reached */ int stopped; /* a callback asked to stop */ } scorpiox_collect; void scorpiox_collect_init(scorpiox_collect *c, size_t max); int scorpiox_collect_header(void *user, const char *name, const char *value); /* 0=cont */ int scorpiox_collect_body(void *user, const void *data, size_t n); /* 0=cont */ void scorpiox_collect_free(scorpiox_collect *c); /* ---- bounded range read (B5): request bytes [start, end] of url ---- * On 206 the returned length is end-start+1 at most; a 200 (server ignored Range) * with start > 0 is reported in *status and the first chunk is NOT skipped here — * callers compare Content-Range. Returns 0 / -1 (transport). */ int scorpiox_http_range(const scorpiox_http *h, const char *url, long long start, long long end, scorpiox_collect *c, long *status, char **out_err); #ifdef __cplusplus } #endif #endif /* SCORPIOX_HTTP_UTIL_H */