xs_curl.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
  2. #ifndef _XS_CURL_H
  3. #define _XS_CURL_H
  4. xs_dict *xs_http_request(const char *method, const char *url,
  5. const xs_dict *headers,
  6. const xs_str *body, int b_size, int *status,
  7. xs_str **payload, int *p_size, int timeout);
  8. int xs_smtp_request(const char *url, const char *user, const char *pass,
  9. const char *from, const char *to, const xs_str *body,
  10. int use_ssl);
  11. const char *xs_curl_strerr(int errnum);
  12. extern int xs_curl_force_http1;
  13. extern int xs_curl_force_ipv6;
  14. #ifdef XS_IMPLEMENTATION
  15. #include <curl/curl.h>
  16. int xs_curl_force_http1 = 0;
  17. int xs_curl_force_ipv6 = 0;
  18. static size_t _header_callback(char *buffer, size_t size,
  19. size_t nitems, xs_dict **userdata)
  20. {
  21. xs_dict *headers = *userdata;
  22. xs *l;
  23. /* get the line */
  24. l = xs_str_new(NULL);
  25. l = xs_append_m(l, buffer, size * nitems);
  26. l = xs_strip_i(l);
  27. /* only the HTTP/x 200 line and the last one doesn't have ': ' */
  28. if (xs_str_in(l, ": ") != -1) {
  29. xs *knv = xs_split_n(l, ": ", 1);
  30. xs_tolower_i((xs_str *)xs_list_get(knv, 0));
  31. headers = xs_dict_set(headers, xs_list_get(knv, 0), xs_list_get(knv, 1));
  32. }
  33. else
  34. if (xs_startswith(l, "HTTP/"))
  35. headers = xs_dict_set(headers, "_proto", l);
  36. *userdata = headers;
  37. return nitems * size;
  38. }
  39. struct _payload_data {
  40. char *data;
  41. int size;
  42. int offset;
  43. };
  44. static size_t _data_callback(void *buffer, size_t size,
  45. size_t nitems, struct _payload_data *pd)
  46. {
  47. size_t sz = size * nitems;
  48. /* open space */
  49. pd->size += sz;
  50. pd->data = xs_realloc(pd->data, _xs_blk_size(pd->size + 1));
  51. /* copy data */
  52. memcpy(pd->data + pd->offset, buffer, sz);
  53. pd->offset += sz;
  54. return sz;
  55. }
  56. static size_t _post_callback(char *buffer, size_t size,
  57. size_t nitems, struct _payload_data *pd)
  58. {
  59. /* size of data left */
  60. size_t sz = pd->size - pd->offset;
  61. /* if it's still bigger than the provided space, trim */
  62. if (sz > (size_t) (size * nitems))
  63. sz = size * nitems;
  64. memcpy(buffer, pd->data + pd->offset, sz);
  65. /* skip sent data */
  66. pd->offset += sz;
  67. return sz;
  68. }
  69. xs_dict *xs_http_request(const char *method, const char *url,
  70. const xs_dict *headers,
  71. const xs_str *body, int b_size, int *status,
  72. xs_str **payload, int *p_size, int timeout)
  73. /* does an HTTP request */
  74. {
  75. xs_dict *response;
  76. CURL *curl;
  77. struct curl_slist *list = NULL;
  78. const xs_str *k;
  79. const xs_val *v;
  80. long lstatus = 0;
  81. struct _payload_data pd;
  82. response = xs_dict_new();
  83. curl = curl_easy_init();
  84. curl_easy_setopt(curl, CURLOPT_URL, url);
  85. if (timeout <= 0)
  86. timeout = 8;
  87. curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long) timeout);
  88. if (xs_curl_force_http1) {
  89. /* force HTTP/1.1 */
  90. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  91. }
  92. if (xs_curl_force_ipv6) {
  93. /* force IPv6 */
  94. curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
  95. }
  96. /* obey redirections */
  97. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  98. /* store response headers here */
  99. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);
  100. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (curl_read_callback) _header_callback);
  101. struct _payload_data ipd = { NULL, 0, 0 };
  102. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ipd);
  103. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_read_callback) _data_callback);
  104. if (strcmp(method, "POST") == 0 || strcmp(method, "PUT") == 0) {
  105. CURLoption curl_method = method[1] == 'O' ? CURLOPT_POST : CURLOPT_UPLOAD;
  106. curl_easy_setopt(curl, curl_method, 1L);
  107. if (body != NULL) {
  108. if (b_size <= 0)
  109. b_size = xs_size(body);
  110. /* add the content-length header */
  111. curl_easy_setopt(curl, curl_method == CURLOPT_POST ? CURLOPT_POSTFIELDSIZE : CURLOPT_INFILESIZE, b_size);
  112. pd.data = (char *)body;
  113. pd.size = b_size;
  114. pd.offset = 0;
  115. curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
  116. curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback) _post_callback);
  117. }
  118. }
  119. /* fill the request headers */
  120. xs_dict_foreach(headers, k, v) {
  121. xs *h = xs_fmt("%s: %s", k, v);
  122. list = curl_slist_append(list, h);
  123. }
  124. /* disable server support for 100-continue */
  125. list = curl_slist_append(list, "Expect:");
  126. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
  127. /* do it */
  128. CURLcode cc = curl_easy_perform(curl);
  129. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &lstatus);
  130. curl_easy_cleanup(curl);
  131. curl_slist_free_all(list);
  132. if (status != NULL) {
  133. if (lstatus == 0) {
  134. /* set the timeout error to a fake HTTP status, or propagate as is */
  135. if (cc == CURLE_OPERATION_TIMEDOUT)
  136. lstatus = 599;
  137. else
  138. lstatus = -cc;
  139. }
  140. *status = (int) lstatus;
  141. }
  142. if (p_size != NULL)
  143. *p_size = ipd.size;
  144. if (payload != NULL) {
  145. *payload = ipd.data;
  146. /* add an asciiz just in case (but not touching p_size) */
  147. if (ipd.data != NULL)
  148. ipd.data[ipd.size] = '\0';
  149. }
  150. else
  151. xs_free(ipd.data);
  152. return response;
  153. }
  154. int xs_smtp_request(const char *url, const char *user, const char *pass,
  155. const char *from, const char *to, const xs_str *body,
  156. int use_ssl)
  157. {
  158. CURL *curl;
  159. CURLcode res = CURLE_OK;
  160. struct curl_slist *rcpt = NULL;
  161. struct _payload_data pd = {
  162. .data = (char *)body,
  163. .size = strlen(body),
  164. .offset = 0
  165. };
  166. curl = curl_easy_init();
  167. curl_easy_setopt(curl, CURLOPT_URL, url);
  168. if (user && pass) {
  169. /* allow authless connections, to, e.g. localhost */
  170. curl_easy_setopt(curl, CURLOPT_USERNAME, user);
  171. curl_easy_setopt(curl, CURLOPT_PASSWORD, pass);
  172. }
  173. if (use_ssl)
  174. curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
  175. curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
  176. rcpt = curl_slist_append(rcpt, to);
  177. curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, rcpt);
  178. curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
  179. curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback) _post_callback);
  180. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  181. res = curl_easy_perform(curl);
  182. curl_easy_cleanup(curl);
  183. curl_slist_free_all(rcpt);
  184. return (int)res;
  185. }
  186. const char *xs_curl_strerr(int errnum)
  187. {
  188. CURLcode cc = errnum < 0 ? -errnum : errnum;
  189. return curl_easy_strerror(cc);
  190. }
  191. #endif /* XS_IMPLEMENTATION */
  192. #endif /* _XS_CURL_H */