younow.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <util/curl/curl-helper.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <util/dstr.h>
  5. #include "util/base.h"
  6. #include "younow.h"
  7. struct younow_mem_struct {
  8. char *memory;
  9. size_t size;
  10. };
  11. static char *current_ingest = NULL;
  12. static size_t younow_write_cb(void *contents, size_t size, size_t nmemb,
  13. void *userp)
  14. {
  15. size_t realsize = size * nmemb;
  16. struct younow_mem_struct *mem = (struct younow_mem_struct *)userp;
  17. mem->memory = realloc(mem->memory, mem->size + realsize + 1);
  18. if (mem->memory == NULL) {
  19. blog(LOG_WARNING, "yyounow_write_cb: realloc returned NULL");
  20. return 0;
  21. }
  22. memcpy(&(mem->memory[mem->size]), contents, realsize);
  23. mem->size += realsize;
  24. mem->memory[mem->size] = 0;
  25. return realsize;
  26. }
  27. const char *younow_get_ingest(const char *server, const char *key)
  28. {
  29. CURL *curl_handle;
  30. CURLcode res;
  31. struct younow_mem_struct chunk;
  32. struct dstr uri;
  33. long response_code;
  34. // find the delimiter in stream key
  35. const char *delim = strchr(key, '_');
  36. if (delim == NULL) {
  37. blog(LOG_WARNING,
  38. "younow_get_ingest: delimiter not found in stream key");
  39. return server;
  40. }
  41. curl_handle = curl_easy_init();
  42. chunk.memory = malloc(1); /* will be grown as needed by realloc */
  43. chunk.size = 0; /* no data at this point */
  44. dstr_init(&uri);
  45. dstr_copy(&uri, server);
  46. dstr_ncat(&uri, key, delim - key);
  47. curl_easy_setopt(curl_handle, CURLOPT_URL, uri.array);
  48. curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, true);
  49. curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 2L);
  50. curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 3L);
  51. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, younow_write_cb);
  52. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
  53. curl_obs_set_revoke_setting(curl_handle);
  54. res = curl_easy_perform(curl_handle);
  55. dstr_free(&uri);
  56. if (res != CURLE_OK) {
  57. blog(LOG_WARNING,
  58. "younow_get_ingest: curl_easy_perform() failed: %s",
  59. curl_easy_strerror(res));
  60. curl_easy_cleanup(curl_handle);
  61. free(chunk.memory);
  62. return server;
  63. }
  64. curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &response_code);
  65. if (response_code != 200) {
  66. blog(LOG_WARNING,
  67. "younow_get_ingest: curl_easy_perform() returned code: %ld",
  68. response_code);
  69. curl_easy_cleanup(curl_handle);
  70. free(chunk.memory);
  71. return server;
  72. }
  73. curl_easy_cleanup(curl_handle);
  74. if (chunk.size == 0) {
  75. blog(LOG_WARNING,
  76. "younow_get_ingest: curl_easy_perform() returned empty response");
  77. free(chunk.memory);
  78. return server;
  79. }
  80. if (current_ingest) {
  81. free(current_ingest);
  82. current_ingest = NULL;
  83. }
  84. current_ingest = strdup(chunk.memory);
  85. free(chunk.memory);
  86. blog(LOG_INFO, "younow_get_ingest: returning ingest: %s",
  87. current_ingest);
  88. return current_ingest;
  89. }