xs_curl.h 6.1 KB

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