/* * sx_http.h -- the one network stack of plugin_ntfy. * * A tiny HTTP/1.1 client over plain TCP or TLS, written for exactly the two * things an ntfy channel connector needs: * * - a buffered request/response (publish a notification, poll the bot API), * - and a long-lived streaming read (the `/json` NDJSON subscription) * that hands back one line at a time without ever buffering the whole * stream. * * TLS is mbedTLS 3.6 with the client-only configuration in * src/sx_ntfy_mbedtls_config.h. The plugin owns the socket and feeds mbedTLS * through mbedtls_ssl_set_bio(), because the only way to put a hard deadline * on every phase (DNS, connect, handshake, each read of a stream that may sit * idle for minutes between keepalives) is to own the blocking. * * Trust: the certificate chain is verified against the bundled root store in * src/ca_bundle_pem.h, with hostname verification on. A self-hosted server * with a private CA is supported by an extra CA file and/or an SPKI pin * allowlist. There is no "skip verification" default: verification failure * aborts the connection. The single escape hatch is the environment variable * NTFY_TLS_INSECURE=1, which is never settable from the plugin's own config * file or from any HTTP request, and is shouted about in the log. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_NTFY_HTTP_H #define SX_NTFY_HTTP_H #include #include #if defined(_WIN32) # include typedef SOCKET sx_sock_t; # define SX_SOCK_INVALID INVALID_SOCKET #else # include typedef int sx_sock_t; # define SX_SOCK_INVALID (-1) #endif /* mbedTLS is vendored in this repo (see vendor/mbedtls + src/mbedtls). */ #include "mbedtls/ssl.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/x509_crt.h" #define SX_HTTP_BODY_CAP (4u * 1024u * 1024u) /* hard cap on a response body */ #define SX_HTTP_LINE_CAP (1u * 1024u * 1024u) /* cap on one streamed NDJSON line */ #define SX_HTTP_RBUF 16384 typedef struct { char scheme[8]; char host[256]; int port; char path[1024]; int tls; /* 1 when scheme is https */ } sx_url; /* Parses http(s)://host[:port][/path][?query]. Anything else is refused with a * message in err. The path keeps the query string. Empty path becomes "/". */ int sx_url_parse(const char *url, sx_url *out, char *err, size_t errsz); /* chunked-transfer state machine (see sx_body_byte) */ typedef enum { CHUNK_SIZE = 0, /* reading the hex chunk-size line */ CHUNK_DATA, /* inside chunk bytes */ CHUNK_DATA_CRLF, /* expecting the CRLF that closes a chunk */ CHUNK_TRAILER /* after the 0-size chunk, until the blank line */ } sx_chunk_state; typedef struct sx_conn { sx_sock_t fd; int tls; /* where this connection points: every request rebuilds its own Host * header from these, so a caller never has to pass the url twice */ char host[256]; int port; /* TLS state (only allocated for https) */ mbedtls_ssl_context *ssl; mbedtls_ssl_config *conf; mbedtls_x509_crt *ca; mbedtls_entropy_context *ent; mbedtls_ctr_drbg_context *drbg; long long deadline_ms; /* absolute monotonic deadline, ms */ char peer_fp_sha256[80]; /* hex SPKI pin of the leaf, empty when plain */ int peer_verified; /* 1 when the chain checked out on its own */ int pin_accepted; /* 1 when a configured pin matched */ /* response framing */ char rbuf[SX_HTTP_RBUF]; size_t rn, rp; /* bytes present / read cursor */ int chunked; sx_chunk_state chunk_state; long long content_left; int headers_done; int eof; int status; char status_text[64]; /* buffered body (only filled by sx_http_request) */ char *body; size_t body_len; size_t body_cap; } sx_conn; void sx_conn_init(sx_conn *c); /* Connects (and handshakes) to url. bearer may be NULL; when set it is sent * verbatim as an `Authorization: ` value (ntfy accepts "Bearer tok" or * "Basic "). pins is a comma/space separated list of lowercase or * uppercase hex SHA-256 SPKI pins, or NULL/empty for none. insecure=1 disables * verification (env-only). ca_file is an optional extra PEM bundle. * timeout_ms bounds connect AND handshake. */ int sx_conn_open(sx_conn *c, const sx_url *url, const char *bearer, const char *pins, const char *ca_file, int insecure, long long timeout_ms, char *err, size_t errsz); void sx_conn_close(sx_conn *c); /* One buffered request. hdrs is a NULL-terminated array of "Name: value" * strings. On success *status is the HTTP status and *body_out (malloc'd, * NUL-terminated) holds the body. Caller frees *body_out. */ int sx_http_request(sx_conn *c, const char *method, const char *path, const char *const *hdrs, const char *body, size_t body_len, int *status, char **body_out, size_t *body_out_len, char *err, size_t errsz); /* Starts a streaming response: sends the request, reads status + headers, * leaves the body for sx_http_readline(). */ int sx_http_open_stream(sx_conn *c, const char *method, const char *path, const char *const *hdrs, int *status, char *err, size_t errsz); /* Reads one newline-delimited record. Returns 1 (line, NUL-terminated, * trailing CR stripped), 0 (clean EOF) or -1 (timeout or transport error). * timeout_ms bounds this single wait: an idle subscription legitimately * produces nothing for a minute at a time. */ int sx_http_readline(sx_conn *c, char *line, size_t capsz, long long timeout_ms); /* Monotonic milliseconds. */ long long sx_now_ms(void); /* Hex SHA-256 of an arbitrary buffer, lowercase, 64 chars + NUL. */ void sx_sha256_hex(const void *data, size_t len, char out[65]); /* Percent-encodes a path/topic segment (everything but [A-Za-z0-9._~-]). */ void sx_url_encode(const char *in, char *out, size_t capsz); #endif /* SX_NTFY_HTTP_H */