#ifndef SCORPIOX_IO_H #define SCORPIOX_IO_H /* * scorpiox-io.h — pluggable IO backend for scorpiox C libraries. * * C libraries own all logic: URLs, request bodies, JSON parsing, state. * The backend owns only transport (HTTP) — libcurl on Linux, WinHTTP on * Windows, URLSession on iOS/macOS, or a mock in unit tests. * * Contract: * - http_get/http_post: 0 = request completed (*http_code set, *out_body * set to a malloc'd NUL-terminated body), -1 = transport failure * (*out_err set to a malloc'd message, *out_body stays NULL). * - Buffers allocated by the backend are freed with the backend's free_buf. * - The backend MUST be synchronous (it blocks until the response is in). * - Call ssms_client_set_io() ONCE before any high-level call. * * Headers (X-Bot-Password / Authorization) are appended by the C core to * the content_type string as extra HTTP header lines — backends must treat * content_type as a newline-separated "Name: value" header list. */ #include #include typedef struct scorpiox_io { void *ctx; /* extra_headers: "Name: value" lines joined by \n; may be NULL. */ int (*http_get) (void *ctx, const char *url, const char *extra_headers, char **out_body, long *http_code, char **out_err); /* extra_headers: NULL-terminated "Name: value" lines joined by \n (may carry the Content-Type line); may be NULL. */ int (*http_post)(void *ctx, const char *url, const char *body, const char *extra_headers, char **out_body, long *http_code, char **out_err); /* Optional: PUT/DELETE for REST-style routes (may be NULL in a backend). */ int (*http_put) (void *ctx, const char *url, const char *body, const char *extra_headers, char **out_body, long *http_code, char **out_err); int (*http_delete)(void *ctx, const char *url, char **out_body, long *http_code, char **out_err); void (*free_buf)(void *ctx, void *ptr); } scorpiox_io; /* * Portable RFC 3986 percent-encode (no libcurl needed). * Returns a malloc'd NUL-terminated string; caller frees with free(). * Returns NULL on allocation failure or NULL input. */ static inline char *scorpiox_percent_encode(const char *s) { if (!s) return 0; static const char hex[] = "0123456789ABCDEF"; size_t len = 0; for (const char *p = s; *p; p++) { char c = *p; int unreserved = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~'; len += unreserved ? 1 : 3; } char *out = (char *)malloc(len + 1); if (!out) return 0; char *o = out; for (const char *p = s; *p; p++) { char c = *p; int unreserved = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~'; if (unreserved) *o++ = c; else { *o++ = '%'; *o++ = hex[(unsigned char)c >> 4]; *o++ = hex[(unsigned char)c & 15]; } } *o = 0; return out; } #endif /* SCORPIOX_IO_H */