7814
Ta
tiny-c-agent / files
tiny-c-agent / src / api / http.c
5.8 KB
1#include "http.h" 2 3#include <stdio.h> 4#include <stdlib.h> 5#include <string.h> 6 7#include <yyjson.h> 8 9#include "sse.h" 10#include "util.h" 11 12typedef struct { 13 tiny_sse sse; 14 char *raw; 15 size_t raw_len; 16 size_t raw_cap; 17 long status; 18 const volatile bool *cancel; 19 tiny_http_data_fn on_data; 20 void *user; 21} http_state; 22 23static void raw_add(http_state *st, const char *p, size_t n) { 24 if (st->raw_len + n + 1 > st->raw_cap) { 25 size_t cap = st->raw_cap ? st->raw_cap : 256; 26 while (cap < st->raw_len + n + 1) cap *= 2; 27 st->raw = realloc(st->raw, cap); 28 if (!st->raw) abort(); 29 st->raw_cap = cap; 30 } 31 memcpy(st->raw + st->raw_len, p, n); 32 st->raw_len += n; 33 st->raw[st->raw_len] = '\0'; 34} 35 36static size_t on_write(char *ptr, size_t size, size_t nmemb, void *ud) { 37 http_state *st = ud; 38 size_t n = size * nmemb; 39 if (st->cancel && *st->cancel) return 0; 40 if (st->status >= 400) raw_add(st, ptr, n); 41 else tiny_sse_feed(&st->sse, ptr, n, st->on_data, st->user); 42 return n; 43} 44 45static size_t on_header(char *ptr, size_t size, size_t nmemb, void *ud) { 46 http_state *st = ud; 47 size_t n = size * nmemb; 48 if (n >= 5 && memcmp(ptr, "HTTP/", 5) == 0) { 49 const char *sp = memchr(ptr, ' ', n); 50 if (sp) st->status = atoi(sp + 1); 51 } 52 return n; 53} 54 55static int on_xfer(void *ud, curl_off_t dlt, curl_off_t dln, curl_off_t ult, curl_off_t uln) { 56 (void)dlt; 57 (void)dln; 58 (void)ult; 59 (void)uln; 60 http_state *st = ud; 61 return (st->cancel && *st->cancel) ? 1 : 0; 62} 63 64// "provider: HTTP <status>: <best message from the body>". 65static char *http_error(long status, const char *body, size_t len) { 66 const char *msg = NULL; 67 yyjson_doc *doc = body ? yyjson_read(body, len, 0) : NULL; 68 if (doc) { 69 yyjson_val *root = yyjson_doc_get_root(doc); 70 yyjson_val *err = yyjson_obj_get(root, "error"); 71 if (yyjson_is_obj(err)) { 72 yyjson_val *m = yyjson_obj_get(err, "message"); 73 if (yyjson_is_str(m)) msg = yyjson_get_str(m); 74 } else if (yyjson_is_str(err)) { 75 msg = yyjson_get_str(err); 76 } else { 77 yyjson_val *m = yyjson_obj_get(root, "message"); 78 if (yyjson_is_str(m)) msg = yyjson_get_str(m); 79 } 80 } 81 char detail[512]; 82 snprintf(detail, sizeof detail, "provider: HTTP %ld: %s", status, 83 msg ? msg : (body ? body : "")); 84 if (doc) yyjson_doc_free(doc); 85 return xstrdup(detail); 86} 87 88int tiny_http_stream(tiny_http_request *req) { 89 http_state st = {0}; 90 st.cancel = req->cancel; 91 st.on_data = req->on_data; 92 st.user = req->user; 93 tiny_sse_init(&st.sse); 94 95 CURL *curl = curl_easy_init(); 96 if (!curl) { 97 tiny_sse_free(&st.sse); 98 req->error = xstrdup("provider: cannot init curl"); 99 return -1; 100 } 101 curl_easy_setopt(curl, CURLOPT_URL, req->url); 102 curl_easy_setopt(curl, CURLOPT_POST, 1L); 103 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req->body); 104 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(req->body)); 105 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, req->headers); 106 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, on_write); 107 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &st); 108 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, on_header); 109 curl_easy_setopt(curl, CURLOPT_HEADERDATA, &st); 110 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, on_xfer); 111 curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &st); 112 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); 113 curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); 114 curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""); 115 116 CURLcode res = curl_easy_perform(curl); 117 curl_easy_cleanup(curl); 118 119 req->status = st.status; 120 req->aborted = (req->cancel && *req->cancel) || res == CURLE_ABORTED_BY_CALLBACK; 121 int rc = 0; 122 if (req->aborted) { 123 // The caller turns this into an "aborted" stop reason. 124 } else if (res != CURLE_OK) { 125 tiny_set_error(&req->error, "provider: %s", curl_easy_strerror(res)); 126 rc = -1; 127 } else if (st.status >= 400) { 128 req->error = http_error(st.status, st.raw, st.raw_len); 129 } 130 131 tiny_sse_free(&st.sse); 132 free(st.raw); 133 return rc; 134} 135 136tiny_api_result tiny_api_post(const char *url, const char *body, 137 const char *auth_prefix, const char *api_key, 138 const char *const *extra, 139 const volatile bool *cancel, 140 tiny_http_data_fn on_data, void *user, 141 const char *stream_error, char **err) { 142 struct curl_slist *headers = NULL; 143 headers = curl_slist_append(headers, "Content-Type: application/json"); 144 headers = curl_slist_append(headers, "Accept: text/event-stream"); 145 char *auth = NULL; 146 if (auth_prefix && api_key && api_key[0]) { 147 size_t n = strlen(auth_prefix) + strlen(api_key) + 1; 148 auth = xmalloc(n); 149 snprintf(auth, n, "%s%s", auth_prefix, api_key); 150 headers = curl_slist_append(headers, auth); 151 } 152 for (size_t i = 0; extra && extra[i]; i++) 153 headers = curl_slist_append(headers, extra[i]); 154 155 tiny_http_request req = { 156 .url = url, 157 .body = body, 158 .headers = headers, 159 .cancel = cancel, 160 .on_data = on_data, 161 .user = user, 162 }; 163 tiny_http_stream(&req); 164 165 tiny_api_result result; 166 if (req.aborted) { 167 result = TINY_API_ABORTED; 168 } else if (req.error) { 169 free(*err); 170 *err = req.error; 171 req.error = NULL; 172 result = TINY_API_TRANSPORT_ERROR; 173 } else if (stream_error) { 174 result = TINY_API_STREAM_ERROR; 175 } else { 176 result = TINY_API_OK; 177 } 178 179 curl_slist_free_all(headers); 180 free(auth); 181 free(req.error); 182 free((char *)url); 183 free((char *)body); 184 return result; 185}