xs_curl.h 4.2 KB

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