xs_curl.h 6.3 KB

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