curl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <curl/curl.h>
  4. #include "utils.h"
  5. #include "curl.h"
  6. #include "keystone.h"
  7. void init_string(struct string *s) {
  8. s->len = 0;
  9. s->ptr = malloc(s->len+1);
  10. if (s->ptr == NULL) {
  11. fprintf(stderr, "malloc() failed\n");
  12. exit(EXIT_FAILURE);
  13. }
  14. memset(s->ptr, 0, s->len+1);
  15. }
  16. size_t gobble(void *ptr, size_t size, size_t nmemb, void *s_) {
  17. return size*nmemb;
  18. }
  19. size_t string_stuffer(void *ptr, size_t size, size_t nmemb, void *s_) {
  20. struct string *s = s_;
  21. size_t new_len = s->len + size*nmemb;
  22. s->ptr = realloc(s->ptr, new_len+1);
  23. if (s->ptr == NULL) {
  24. fprintf(stderr, "realloc() failed\n");
  25. exit(EXIT_FAILURE);
  26. }
  27. memcpy(s->ptr+s->len, ptr, size*nmemb);
  28. s->ptr[new_len] = '\0';
  29. s->len = new_len;
  30. return size*nmemb;
  31. }
  32. int submit_api_request(struct string *s, const char *endpoint, const char *path) {
  33. char url[MAX_BUF_SZ] = { 0 };
  34. api_url_build(url, endpoint, path);
  35. CURLcode err = curl_global_init(CURL_GLOBAL_NOTHING);
  36. if(err) {
  37. WHEREAMI();
  38. fprintf(stderr, "Something went wrong initialising libcurl (error %u)\n", err);
  39. exit(err);
  40. }
  41. CURL* handle = curl_easy_init();
  42. if(handle == NULL) {
  43. WHEREAMI();
  44. fprintf(stderr, "Something went wrong initialising curl session.\n");
  45. err = 1;
  46. goto easy_cleanup;
  47. }
  48. char errbuf[CURL_ERROR_SIZE] = { 0 };
  49. err = curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf);
  50. if(err) {
  51. WHEREAMI();
  52. fprintf(stderr, "Something went wrong preparing error buffer (error %u)\n", err);
  53. fprintf(stderr, "Current contents of buffer:\n%s\n", errbuf);
  54. goto easy_cleanup;
  55. }
  56. err = curl_easy_setopt(handle, CURLOPT_URL, url);
  57. if(err) {
  58. WHEREAMI();
  59. fprintf(stderr, "Error %u: %s\n", err, errbuf);
  60. goto easy_cleanup;
  61. }
  62. if(token_cache.data[0] == '\0') {
  63. get_token();
  64. if(token_cache.data[0] == '\0') {
  65. WHEREAMI();
  66. fprintf(stderr, "Error retrieving token\n");
  67. }
  68. }
  69. char token_header[512] = { 0 };
  70. strncpy(token_header, "X-Auth-Token: ", 64);
  71. strncat(token_header, token_cache.data, 256);
  72. struct curl_slist *H = NULL;
  73. H = curl_slist_append(H, token_header);
  74. if(H == NULL) {
  75. WHEREAMI();
  76. fprintf(stderr, "Something went wrong setting token request header\n");
  77. goto easy_cleanup;
  78. }
  79. err = curl_easy_setopt(handle, CURLOPT_HTTPHEADER, H);
  80. if(err) {
  81. WHEREAMI();
  82. fprintf(stderr, "Error %u: %s\n", err, errbuf);
  83. goto easy_cleanup;
  84. }
  85. err = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, string_stuffer);
  86. if(err) {
  87. WHEREAMI();
  88. fprintf(stderr, "Error %u: %s\n", err, errbuf);
  89. goto easy_cleanup;
  90. }
  91. init_string(s);
  92. err = curl_easy_setopt(handle, CURLOPT_WRITEDATA, s);
  93. if(err) {
  94. WHEREAMI();
  95. fprintf(stderr, "Error %u: %s\n", err, errbuf);
  96. goto easy_cleanup;
  97. }
  98. err = curl_easy_perform(handle);
  99. if(err) {
  100. WHEREAMI();
  101. fprintf(stderr, "Error %u: %s\n", err, errbuf);
  102. goto easy_cleanup;
  103. }
  104. s->ptr[s->len] = '\0';
  105. easy_cleanup:
  106. if(H != NULL) {
  107. curl_slist_free_all(H);
  108. }
  109. if(handle != NULL) {
  110. curl_easy_cleanup(handle);
  111. }
  112. curl_global_cleanup();
  113. return err;
  114. }