xs_curl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  2. #ifndef _XS_CURL_H
  3. #define _XS_CURL_H
  4. xs_dict *xs_http_request(char *method, char *url, xs_dict *headers,
  5. xs_str *body, int b_size, int *status,
  6. xs_str **payload, int *p_size, int timeout);
  7. #ifdef XS_IMPLEMENTATION
  8. #include <curl/curl.h>
  9. static size_t _header_callback(char *buffer, size_t size,
  10. size_t nitems, xs_dict **userdata)
  11. {
  12. xs_dict *headers = *userdata;
  13. xs *l;
  14. /* get the line */
  15. l = xs_str_new(NULL);
  16. l = xs_append_m(l, buffer, size * nitems);
  17. l = xs_strip_i(l);
  18. /* only the HTTP/x 200 line and the last one doesn't have ': ' */
  19. if (xs_str_in(l, ": ") != -1) {
  20. xs *knv = xs_split_n(l, ": ", 1);
  21. xs_tolower_i(xs_list_get(knv, 0));
  22. headers = xs_dict_set(headers, xs_list_get(knv, 0), xs_list_get(knv, 1));
  23. }
  24. else
  25. if (xs_startswith(l, "HTTP/"))
  26. headers = xs_dict_set(headers, "_proto", l);
  27. *userdata = headers;
  28. return nitems * size;
  29. }
  30. struct _payload_data {
  31. char *data;
  32. int size;
  33. int offset;
  34. };
  35. static int _data_callback(void *buffer, size_t size,
  36. size_t nitems, struct _payload_data *pd)
  37. {
  38. int sz = size * nitems;
  39. /* open space */
  40. pd->size += sz;
  41. pd->data = xs_realloc(pd->data, pd->size + 1);
  42. /* copy data */
  43. memcpy(pd->data + pd->offset, buffer, sz);
  44. pd->offset += sz;
  45. return sz;
  46. }
  47. static int _post_callback(char *buffer, size_t size,
  48. size_t nitems, struct _payload_data *pd)
  49. {
  50. /* size of data left */
  51. int sz = pd->size - pd->offset;
  52. /* if it's still bigger than the provided space, trim */
  53. if (sz > (int) (size * nitems))
  54. sz = size * nitems;
  55. memcpy(buffer, pd->data + pd->offset, sz);
  56. /* skip sent data */
  57. pd->offset += sz;
  58. return sz;
  59. }
  60. xs_dict *xs_http_request(char *method, char *url, xs_dict *headers,
  61. xs_str *body, int b_size, int *status,
  62. xs_str **payload, int *p_size, int timeout)
  63. /* does an HTTP request */
  64. {
  65. xs_dict *response;
  66. CURL *curl;
  67. struct curl_slist *list = NULL;
  68. xs_dict *p;
  69. xs_str *k;
  70. xs_val *v;
  71. long lstatus;
  72. struct _payload_data pd;
  73. response = xs_dict_new();
  74. curl = curl_easy_init();
  75. curl_easy_setopt(curl, CURLOPT_URL, url);
  76. if (timeout <= 0)
  77. timeout = 8;
  78. curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long) timeout);
  79. #ifdef FORCE_HTTP_1_1
  80. /* force HTTP/1.1 */
  81. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  82. #endif
  83. /* obey redirections */
  84. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  85. /* store response headers here */
  86. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);
  87. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _header_callback);
  88. struct _payload_data ipd = { NULL, 0, 0 };
  89. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ipd);
  90. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _data_callback);
  91. if (strcmp(method, "POST") == 0) {
  92. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  93. if (body != NULL) {
  94. if (b_size <= 0)
  95. b_size = xs_size(body);
  96. /* add the content-length header */
  97. char tmp[32];
  98. sprintf(tmp, "content-length: %d", b_size);
  99. list = curl_slist_append(list, tmp);
  100. pd.data = 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. p = headers;
  109. while (xs_dict_iter(&p, &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. 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. *status = (int) lstatus;
  123. if (p_size != NULL)
  124. *p_size = ipd.size;
  125. if (payload != NULL) {
  126. *payload = ipd.data;
  127. /* add an asciiz just in case (but not touching p_size) */
  128. if (ipd.data != NULL)
  129. ipd.data[ipd.size] = '\0';
  130. }
  131. else
  132. xs_free(ipd.data);
  133. return response;
  134. }
  135. #endif /* XS_IMPLEMENTATION */
  136. #endif /* _XS_CURL_H */