xs_curl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* copyright (c) 2022 grunfink - MIT license */
  2. #ifndef _XS_CURL_H
  3. #define _XS_CURL_H
  4. d_char *xs_http_request(char *method, char *url, d_char *headers,
  5. d_char *body, int b_size,
  6. int *status, d_char **payload, int *p_size);
  7. #ifdef XS_IMPLEMENTATION
  8. #include <curl/curl.h>
  9. static size_t _header_callback(char *buffer, size_t size,
  10. size_t nitems, d_char **userdata)
  11. {
  12. d_char *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. d_char *xs_http_request(char *method, char *url, d_char *headers,
  61. d_char *body, int b_size,
  62. int *status, d_char **payload, int *p_size)
  63. /* does an HTTP request */
  64. {
  65. d_char *response;
  66. CURL *curl;
  67. struct curl_slist *list = NULL;
  68. char *k, *v, *p;
  69. long lstatus;
  70. struct _payload_data pd;
  71. response = xs_dict_new();
  72. curl = curl_easy_init();
  73. curl_easy_setopt(curl, CURLOPT_URL, url);
  74. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 8L);
  75. #ifdef FORCE_HTTP_1_1
  76. /* force HTTP/1.1 */
  77. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  78. #endif
  79. /* obey redirections */
  80. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  81. /* store response headers here */
  82. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);
  83. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _header_callback);
  84. struct _payload_data ipd = { NULL, 0, 0 };
  85. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ipd);
  86. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _data_callback);
  87. if (strcmp(method, "POST") == 0) {
  88. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  89. if (body != NULL) {
  90. if (b_size <= 0)
  91. b_size = xs_size(body);
  92. /* add the content-length header */
  93. char tmp[32];
  94. sprintf(tmp, "content-length: %d", b_size);
  95. list = curl_slist_append(list, tmp);
  96. pd.data = body;
  97. pd.size = b_size;
  98. pd.offset = 0;
  99. curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
  100. curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback);
  101. }
  102. }
  103. /* fill the request headers */
  104. p = headers;
  105. while (xs_dict_iter(&p, &k, &v)) {
  106. xs *h = xs_fmt("%s: %s", k, v);
  107. list = curl_slist_append(list, h);
  108. }
  109. /* disable server support for 100-continue */
  110. list = curl_slist_append(list, "Expect:");
  111. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
  112. /* do it */
  113. curl_easy_perform(curl);
  114. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &lstatus);
  115. curl_easy_cleanup(curl);
  116. curl_slist_free_all(list);
  117. if (status != NULL)
  118. *status = (int) lstatus;
  119. if (p_size != NULL)
  120. *p_size = ipd.size;
  121. if (payload != NULL) {
  122. *payload = ipd.data;
  123. /* add an asciiz just in case (but not touching p_size) */
  124. if (ipd.data != NULL)
  125. ipd.data[ipd.size] = '\0';
  126. }
  127. else
  128. xs_free(ipd.data);
  129. return response;
  130. }
  131. #endif /* XS_IMPLEMENTATION */
  132. #endif /* _XS_CURL_H */