xs_curl.h 5.0 KB

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