xs_curl.h 5.2 KB

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