xonecas.io
7814
Ta

tiny-c-agent / files

tiny-c-agent / src / api / http.h

2.1 KB
1// Internal HTTP streaming transport shared by the streaming API adapters.
2//
3// Performs a POST and splits the server-sent event body into `data:` payloads.
4// The caller owns the URL, body, and header list.
5
6#ifndef TINY_API_HTTP_H
7#define TINY_API_HTTP_H
8
9#include <stdbool.h>
10#include <stddef.h>
11
12#include <curl/curl.h>
13
14// Called for each SSE `data:` payload. See tiny_sse_fn.
15typedef void (*tiny_http_data_fn)(const char *data, size_t len, void *user);
16
17typedef struct {
18    const char *url;
19    const char *body;
20    struct curl_slist *headers;   // caller builds and frees
21    const volatile bool *cancel;  // may be NULL
22    tiny_http_data_fn on_data;
23    void *user;
24
25    // Set by the call.
26    long status;   // HTTP status, 0 when none arrived
27    bool aborted;  // the cancel flag was observed
28    char *error;   // malloc'd message on transport or HTTP error, else NULL
29} tiny_http_request;
30
31// Runs the request. Returns 0 when the transfer completed (the status may still
32// be an error, with error set), and -1 on a transport failure (also with error
33// set).
34int tiny_http_stream(tiny_http_request *req);
35
36typedef enum {
37    TINY_API_OK,               // the caller should finalize
38    TINY_API_ABORTED,          // the cancel flag was observed
39    TINY_API_STREAM_ERROR,     // the stream reported an error (see stream_error)
40    TINY_API_TRANSPORT_ERROR,  // the transfer failed; *err is set
41} tiny_api_result;
42
43// Builds the standard headers (Content-Type, Accept, an optional auth line
44// "auth_prefix + api_key", and the NULL-terminated `extra` headers) and runs a
45// streaming POST. `stream_error` is the adapter's recorded stream error, or
46// NULL. Takes ownership of url and body.
47tiny_api_result tiny_api_post(const char *url, const char *body,
48                              const char *auth_prefix, const char *api_key,
49                              const char *const *extra,
50                              const volatile bool *cancel,
51                              tiny_http_data_fn on_data, void *user,
52                              const char *stream_error, char **err);
53
54#endif