| 1 | /* |
| 2 | * sx_http.h -- the one network stack of plugin_ntfy. |
| 3 | * |
| 4 | * A tiny HTTP/1.1 client over plain TCP or TLS, written for exactly the two |
| 5 | * things an ntfy channel connector needs: |
| 6 | * |
| 7 | * - a buffered request/response (publish a notification, poll the bot API), |
| 8 | * - and a long-lived streaming read (the `<topic>/json` NDJSON subscription) |
| 9 | * that hands back one line at a time without ever buffering the whole |
| 10 | * stream. |
| 11 | * |
| 12 | * TLS is mbedTLS 3.6 with the client-only configuration in |
| 13 | * src/sx_ntfy_mbedtls_config.h. The plugin owns the socket and feeds mbedTLS |
| 14 | * through mbedtls_ssl_set_bio(), because the only way to put a hard deadline |
| 15 | * on every phase (DNS, connect, handshake, each read of a stream that may sit |
| 16 | * idle for minutes between keepalives) is to own the blocking. |
| 17 | * |
| 18 | * Trust: the certificate chain is verified against the bundled root store in |
| 19 | * src/ca_bundle_pem.h, with hostname verification on. A self-hosted server |
| 20 | * with a private CA is supported by an extra CA file and/or an SPKI pin |
| 21 | * allowlist. There is no "skip verification" default: verification failure |
| 22 | * aborts the connection. The single escape hatch is the environment variable |
| 23 | * NTFY_TLS_INSECURE=1, which is never settable from the plugin's own config |
| 24 | * file or from any HTTP request, and is shouted about in the log. |
| 25 | * |
| 26 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 27 | */ |
| 28 | #ifndef SX_NTFY_HTTP_H |
| 29 | #define SX_NTFY_HTTP_H |
| 30 | |
| 31 | #include <stddef.h> |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | #if defined(_WIN32) |
| 35 | # include <winsock2.h> |
| 36 | typedef SOCKET sx_sock_t; |
| 37 | # define SX_SOCK_INVALID INVALID_SOCKET |
| 38 | #else |
| 39 | # include <sys/types.h> |
| 40 | typedef int sx_sock_t; |
| 41 | # define SX_SOCK_INVALID (-1) |
| 42 | #endif |
| 43 | |
| 44 | /* mbedTLS is vendored in this repo (see vendor/mbedtls + src/mbedtls). */ |
| 45 | #include "mbedtls/ssl.h" |
| 46 | #include "mbedtls/entropy.h" |
| 47 | #include "mbedtls/ctr_drbg.h" |
| 48 | #include "mbedtls/x509_crt.h" |
| 49 | |
| 50 | #define SX_HTTP_BODY_CAP (4u * 1024u * 1024u) /* hard cap on a response body */ |
| 51 | #define SX_HTTP_LINE_CAP (1u * 1024u * 1024u) /* cap on one streamed NDJSON line */ |
| 52 | #define SX_HTTP_RBUF 16384 |
| 53 | |
| 54 | typedef struct { |
| 55 | char scheme[8]; |
| 56 | char host[256]; |
| 57 | int port; |
| 58 | char path[1024]; |
| 59 | int tls; /* 1 when scheme is https */ |
| 60 | } sx_url; |
| 61 | |
| 62 | /* Parses http(s)://host[:port][/path][?query]. Anything else is refused with a |
| 63 | * message in err. The path keeps the query string. Empty path becomes "/". */ |
| 64 | int sx_url_parse(const char *url, sx_url *out, char *err, size_t errsz); |
| 65 | |
| 66 | /* chunked-transfer state machine (see sx_body_byte) */ |
| 67 | typedef enum { |
| 68 | CHUNK_SIZE = 0, /* reading the hex chunk-size line */ |
| 69 | CHUNK_DATA, /* inside chunk bytes */ |
| 70 | CHUNK_DATA_CRLF, /* expecting the CRLF that closes a chunk */ |
| 71 | CHUNK_TRAILER /* after the 0-size chunk, until the blank line */ |
| 72 | } sx_chunk_state; |
| 73 | |
| 74 | typedef struct sx_conn { |
| 75 | sx_sock_t fd; |
| 76 | int tls; |
| 77 | |
| 78 | /* where this connection points: every request rebuilds its own Host |
| 79 | * header from these, so a caller never has to pass the url twice */ |
| 80 | char host[256]; |
| 81 | int port; |
| 82 | |
| 83 | /* TLS state (only allocated for https) */ |
| 84 | mbedtls_ssl_context *ssl; |
| 85 | mbedtls_ssl_config *conf; |
| 86 | mbedtls_x509_crt *ca; |
| 87 | mbedtls_entropy_context *ent; |
| 88 | mbedtls_ctr_drbg_context *drbg; |
| 89 | |
| 90 | long long deadline_ms; /* absolute monotonic deadline, ms */ |
| 91 | char peer_fp_sha256[80]; /* hex SPKI pin of the leaf, empty when plain */ |
| 92 | int peer_verified; /* 1 when the chain checked out on its own */ |
| 93 | int pin_accepted; /* 1 when a configured pin matched */ |
| 94 | |
| 95 | /* response framing */ |
| 96 | char rbuf[SX_HTTP_RBUF]; |
| 97 | size_t rn, rp; /* bytes present / read cursor */ |
| 98 | int chunked; |
| 99 | sx_chunk_state chunk_state; |
| 100 | long long content_left; |
| 101 | int headers_done; |
| 102 | int eof; |
| 103 | int status; |
| 104 | char status_text[64]; |
| 105 | |
| 106 | /* buffered body (only filled by sx_http_request) */ |
| 107 | char *body; |
| 108 | size_t body_len; |
| 109 | size_t body_cap; |
| 110 | } sx_conn; |
| 111 | |
| 112 | void sx_conn_init(sx_conn *c); |
| 113 | |
| 114 | /* Connects (and handshakes) to url. bearer may be NULL; when set it is sent |
| 115 | * verbatim as an `Authorization: <bearer>` value (ntfy accepts "Bearer tok" or |
| 116 | * "Basic <b64>"). pins is a comma/space separated list of lowercase or |
| 117 | * uppercase hex SHA-256 SPKI pins, or NULL/empty for none. insecure=1 disables |
| 118 | * verification (env-only). ca_file is an optional extra PEM bundle. |
| 119 | * timeout_ms bounds connect AND handshake. */ |
| 120 | int sx_conn_open(sx_conn *c, const sx_url *url, const char *bearer, |
| 121 | const char *pins, const char *ca_file, int insecure, |
| 122 | long long timeout_ms, char *err, size_t errsz); |
| 123 | |
| 124 | void sx_conn_close(sx_conn *c); |
| 125 | |
| 126 | /* One buffered request. hdrs is a NULL-terminated array of "Name: value" |
| 127 | * strings. On success *status is the HTTP status and *body_out (malloc'd, |
| 128 | * NUL-terminated) holds the body. Caller frees *body_out. */ |
| 129 | int sx_http_request(sx_conn *c, const char *method, const char *path, |
| 130 | const char *const *hdrs, const char *body, size_t body_len, |
| 131 | int *status, char **body_out, size_t *body_out_len, |
| 132 | char *err, size_t errsz); |
| 133 | |
| 134 | /* Starts a streaming response: sends the request, reads status + headers, |
| 135 | * leaves the body for sx_http_readline(). */ |
| 136 | int sx_http_open_stream(sx_conn *c, const char *method, const char *path, |
| 137 | const char *const *hdrs, int *status, |
| 138 | char *err, size_t errsz); |
| 139 | |
| 140 | /* Reads one newline-delimited record. Returns 1 (line, NUL-terminated, |
| 141 | * trailing CR stripped), 0 (clean EOF) or -1 (timeout or transport error). |
| 142 | * timeout_ms bounds this single wait: an idle subscription legitimately |
| 143 | * produces nothing for a minute at a time. */ |
| 144 | int sx_http_readline(sx_conn *c, char *line, size_t capsz, long long timeout_ms); |
| 145 | |
| 146 | /* Monotonic milliseconds. */ |
| 147 | long long sx_now_ms(void); |
| 148 | |
| 149 | /* Hex SHA-256 of an arbitrary buffer, lowercase, 64 chars + NUL. */ |
| 150 | void sx_sha256_hex(const void *data, size_t len, char out[65]); |
| 151 | |
| 152 | /* Percent-encodes a path/topic segment (everything but [A-Za-z0-9._~-]). */ |
| 153 | void sx_url_encode(const char *in, char *out, size_t capsz); |
| 154 | |
| 155 | #endif /* SX_NTFY_HTTP_H */ |