xs_curl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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(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(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 > 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. #ifdef FORCE_TIMEOUT
  75. curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
  76. #endif
  77. #ifdef FORCE_HTTP_1_1
  78. /* force HTTP/1.1 */
  79. curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  80. #endif
  81. /* obey redirections */
  82. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  83. /* store response headers here */
  84. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response);
  85. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, _header_callback);
  86. struct _payload_data ipd = { NULL, 0, 0 };
  87. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ipd);
  88. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _data_callback);
  89. if (strcmp(method, "POST") == 0) {
  90. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  91. if (body != NULL) {
  92. if (b_size <= 0)
  93. b_size = xs_size(body);
  94. /* add the content-length header */
  95. char tmp[32];
  96. sprintf(tmp, "content-length: %d", b_size);
  97. list = curl_slist_append(list, tmp);
  98. pd.data = body;
  99. pd.size = b_size;
  100. pd.offset = 0;
  101. curl_easy_setopt(curl, CURLOPT_READDATA, &pd);
  102. curl_easy_setopt(curl, CURLOPT_READFUNCTION, _post_callback);
  103. }
  104. }
  105. /* fill the request headers */
  106. p = headers;
  107. while (xs_dict_iter(&p, &k, &v)) {
  108. xs *h = xs_fmt("%s: %s", k, v);
  109. list = curl_slist_append(list, h);
  110. }
  111. /* disable server support for 100-continue */
  112. list = curl_slist_append(list, "Expect:");
  113. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
  114. /* do it */
  115. curl_easy_perform(curl);
  116. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &lstatus);
  117. curl_easy_cleanup(curl);
  118. curl_slist_free_all(list);
  119. if (status != NULL)
  120. *status = (int) lstatus;
  121. if (p_size != NULL)
  122. *p_size = ipd.size;
  123. if (payload != NULL) {
  124. *payload = ipd.data;
  125. /* add an asciiz just in case (but not touching p_size) */
  126. if (ipd.data != NULL)
  127. ipd.data[ipd.size] = '\0';
  128. }
  129. else
  130. xs_free(ipd.data);
  131. return response;
  132. }
  133. #endif /* XS_IMPLEMENTATION */
  134. #endif /* _XS_CURL_H */