xs_curl.h 6.6 KB

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