/* sprs — seekable HTTP range byte stream (C99). See SPEC.md. */ #ifndef SPRS_H #define SPRS_H #include #include #include "sprs_backend.h" #ifdef __cplusplus extern "C" { #endif #ifndef SPRS_API # if defined(__GNUC__) || defined(__clang__) # define SPRS_API __attribute__((visibility("default"))) # else # define SPRS_API # endif #endif enum { SPRS_OK = 0, SPRS_ERR_TRANSPORT = 1, SPRS_ERR_TIMEOUT = 2, SPRS_ERR_HTTP = 3, SPRS_ERR_NOT_FOUND = 4, SPRS_ERR_NO_RANGES = 5, SPRS_ERR_RANGE_NOT_SATISFIABLE = 6, SPRS_ERR_LENGTH_UNKNOWN = 7, SPRS_ERR_TRUNCATED = 8, SPRS_ERR_SEEK_RANGE = 9, SPRS_ERR_NO_IO = 10, SPRS_ERR_INVALID_ARG = 11, SPRS_ERR_OOM = 12, SPRS_ERR_DNS = 13, SPRS_ERR_REFUSED = 14 }; enum { SPRS_STRATEGY_PER_REQUEST = 0, /* Spec A */ SPRS_STRATEGY_LONG_LIVED = 1, /* Spec B — default */ SPRS_STRATEGY_AUTO = 2 }; typedef struct sprs sprs_t; /* ---- lifecycle ---- */ SPRS_API sprs_t *sprs_open(const sprs_backend *be, const char *url); SPRS_API sprs_t *sprs_open_with_headers(const sprs_backend *be, const char *url, const char *const *hdrs, int n); SPRS_API void sprs_close(sprs_t *s); /* ---- stream ops ---- */ SPRS_API int sprs_length(sprs_t *s, int64_t *out); /* lazy HEAD if needed */ SPRS_API int64_t sprs_position(const sprs_t *s); SPRS_API int sprs_seek(sprs_t *s, int64_t offset, int whence); /* SEEK_SET/CUR/END */ SPRS_API int sprs_read(sprs_t *s, unsigned char *dst, size_t n, size_t *got); SPRS_API int sprs_read_at(sprs_t *s, int64_t off, unsigned char *dst, size_t n, size_t *got); SPRS_API int sprs_eof(const sprs_t *s); /* ---- behaviour knobs ---- */ SPRS_API int sprs_set_cache_size(sprs_t *s, size_t bytes); /* 0 = off */ SPRS_API int sprs_set_seek_threshold(sprs_t *s, int64_t bytes); /* default 1 MiB */ SPRS_API int sprs_set_large_read_bypass(sprs_t *s, size_t bytes); /* default = cache */ SPRS_API int sprs_set_strategy(sprs_t *s, int strategy); SPRS_API int sprs_set_timeout_ms(sprs_t *s, int ms); /* advisory; backend */ SPRS_API int sprs_set_max_retries(sprs_t *s, int n); SPRS_API int sprs_set_require_accept_ranges(sprs_t *s, int yes); /* ---- server capability ---- */ SPRS_API int sprs_supports_ranges(const sprs_t *s); /* 1/0/-1 unknown */ SPRS_API int sprs_server_accept_ranges(const sprs_t *s); SPRS_API const char *sprs_last_content_range(const sprs_t *s); /* ---- observability ---- */ SPRS_API int64_t sprs_stat_requests(const sprs_t *s); SPRS_API int64_t sprs_stat_bytes_fetched(const sprs_t *s); SPRS_API int64_t sprs_stat_bytes_delivered(const sprs_t *s); SPRS_API int64_t sprs_stat_bytes_discarded(const sprs_t *s); SPRS_API int64_t sprs_stat_cache_hits(const sprs_t *s); SPRS_API int64_t sprs_stat_cache_misses(const sprs_t *s); SPRS_API int64_t sprs_stat_reopens(const sprs_t *s); SPRS_API void sprs_stat_reset(sprs_t *s); SPRS_API int sprs_last_http_code(const sprs_t *s); SPRS_API int sprs_last_error(const sprs_t *s); SPRS_API const char *sprs_strerror(int err); SPRS_API const char *sprs_last_error_msg(const sprs_t *s); SPRS_API const char *sprs_result_json(sprs_t *s); /* vendored yyjson; static per handle */ SPRS_API int sprs_warning_count(const sprs_t *s); SPRS_API const char *sprs_warning(const sprs_t *s, int i); /* ---- fake backend constructors (tests / CLI) ---- */ SPRS_API sprs_backend sprs_backend_fake(const unsigned char *data, size_t len); SPRS_API sprs_backend sprs_backend_fake_mode(const unsigned char *data, size_t len, int mode); SPRS_API void sprs_backend_fake_free(sprs_backend *be); /* Fake adversarial modes (bitmask) */ enum { SPRS_FAKE_OK = 0, SPRS_FAKE_NO_ACCEPT_RANGES = 1 << 0, /* omit Accept-Ranges; still 206 */ SPRS_FAKE_LIE_ACCEPT_RANGES = 1 << 1, /* claim bytes but return 200 full */ SPRS_FAKE_416_EVERYTHING = 1 << 2, SPRS_FAKE_1BYTE_READS = 1 << 3, /* short-read: 1 byte per read */ SPRS_FAKE_LIE_LENGTH_BIG = 1 << 4, /* Content-Range total too big */ SPRS_FAKE_LIE_LENGTH_SMALL = 1 << 5, SPRS_FAKE_NO_CONTENT_LENGTH = 1 << 6, /* chunked-like: no CL, CR total=* */ SPRS_FAKE_DROP_MID_BODY = 1 << 7, /* truncate after 50% of requested */ SPRS_FAKE_TIMEOUT = 1 << 8, SPRS_FAKE_DNS = 1 << 9, SPRS_FAKE_REFUSED = 1 << 10, SPRS_FAKE_404 = 1 << 11, SPRS_FAKE_MALFORMED_OK = 1 << 12, /* if Range lacks bytes=, send whole file (nginx) */ SPRS_FAKE_IGNORE_RANGE = 1 << 13 /* always 200 full body (python http.server) */ }; #ifdef __cplusplus } #endif #endif