glue.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright (c) 2007-2022 Bruce A Henderson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <brl.mod/blitz.mod/blitz.h>
  20. #include <curl/curl.h>
  21. struct curlHttpPost {
  22. struct curl_httppost * post;
  23. struct curl_httppost * last;
  24. };
  25. int bmx_curl_easy_setopt_int(CURL *curl, int option, int param) {
  26. return curl_easy_setopt(curl, option, param);
  27. }
  28. int bmx_curl_easy_setopt_str(CURL *curl, int option, char * param) {
  29. return curl_easy_setopt(curl, option, param);
  30. }
  31. int bmx_curl_easy_setopt_obj(CURL *curl, int option, BBObject * param) {
  32. return curl_easy_setopt(curl, option, param);
  33. }
  34. int bmx_curl_easy_setopt_ptr(CURL *curl, int option, void * param) {
  35. return curl_easy_setopt(curl, option, param);
  36. }
  37. int bmx_curl_easy_setopt_bbint64(CURL *curl, int option, BBInt64 param) {
  38. return curl_easy_setopt(curl, option, param);
  39. }
  40. void bmx_curl_formadd_name_content(struct curlHttpPost * httpPost, const char * name, const char * content) {
  41. curl_formadd(&httpPost->post, &httpPost->last, CURLFORM_PTRNAME, name, CURLFORM_PTRCONTENTS, content, CURLFORM_END);
  42. }
  43. void bmx_curl_formadd_name_content_type(struct curlHttpPost * httpPost, const char * name, const char * content, const char * type) {
  44. curl_formadd(&httpPost->post, &httpPost->last, CURLFORM_PTRNAME, name, CURLFORM_PTRCONTENTS, content, CURLFORM_CONTENTTYPE, type, CURLFORM_END);
  45. }
  46. void bmx_curl_formadd_name_file(struct curlHttpPost * httpPost, const char * name, const char * file, int kind) {
  47. if (kind == 1) {
  48. curl_formadd(&httpPost->post, &httpPost->last, CURLFORM_PTRNAME, name, CURLFORM_FILE, file, CURLFORM_END);
  49. } else {
  50. curl_formadd(&httpPost->post, &httpPost->last, CURLFORM_PTRNAME, name, CURLFORM_FILECONTENT, file, CURLFORM_END);
  51. }
  52. }
  53. void bmx_curl_formadd_name_file_type(struct curlHttpPost * httpPost, const char * name, const char * file, const char * type, int kind) {
  54. if (kind == 1) {
  55. curl_formadd(&httpPost->post, &httpPost->last, CURLFORM_PTRNAME, name, CURLFORM_FILE, file, CURLFORM_CONTENTTYPE, type, CURLFORM_END);
  56. } else {
  57. curl_formadd(&httpPost->post, &httpPost->last, CURLFORM_PTRNAME, name, CURLFORM_FILECONTENT, file, CURLFORM_CONTENTTYPE, type, CURLFORM_END);
  58. }
  59. }
  60. void bmx_curl_formadd_name_buffer(struct curlHttpPost * httpPost, const char * name, const char * bname, void * buffer, int length) {
  61. curl_formadd(&httpPost->post, &httpPost->last, CURLFORM_PTRNAME, name, CURLFORM_BUFFER, bname, CURLFORM_BUFFERPTR, buffer, CURLFORM_BUFFERLENGTH, length, CURLFORM_END);
  62. }
  63. void bmx_curl_setopt(struct curlHttpPost * httpPost, CURL * curl) {
  64. curl_easy_setopt(curl, CURLOPT_HTTPPOST, httpPost->post);
  65. }
  66. CURLcode bmx_curl_easy_getinfo_string(CURL *curl, CURLINFO info, char * s) {
  67. return curl_easy_getinfo(curl, info, s);
  68. }
  69. CURLcode bmx_curl_easy_getinfo_int(CURL *curl, CURLINFO info, int * value) {
  70. return curl_easy_getinfo(curl, info, value);
  71. }
  72. CURLcode bmx_curl_easy_getinfo_double(CURL *curl, CURLINFO info, double * value) {
  73. return curl_easy_getinfo(curl, info, value);
  74. }
  75. CURLcode bmx_curl_easy_getinfo_long(CURL *curl, CURLINFO info, BBInt64 * value) {
  76. return curl_easy_getinfo(curl, info, value);
  77. }
  78. char * bmx_curl_easy_getinfo_obj(CURL * curl, CURLINFO info, CURLcode * error) {
  79. char * priv = NULL;
  80. *error = curl_easy_getinfo(curl, info, priv);
  81. return priv;
  82. }
  83. int bmx_curl_multiselect(CURLM * multi, double timeout) {
  84. fd_set readfds;
  85. fd_set writefds;
  86. fd_set exceptfds;
  87. int maxfd;
  88. struct timeval to;
  89. long curl_timeout = -1;
  90. curl_multi_timeout(multi, &curl_timeout);
  91. if (curl_timeout >=0) {
  92. to.tv_sec = curl_timeout / 1000;
  93. if (to.tv_sec > 1) {
  94. to.tv_sec = 1;
  95. } else {
  96. to.tv_usec = (curl_timeout % 1000) * 1000;
  97. }
  98. } else {
  99. unsigned long conv = (unsigned long) (timeout * 1000000.0);
  100. to.tv_sec = conv / 1000000;
  101. to.tv_usec = conv % 1000000;
  102. }
  103. FD_ZERO(&readfds);
  104. FD_ZERO(&writefds);
  105. FD_ZERO(&exceptfds);
  106. curl_multi_fdset(multi, &readfds, &writefds, &exceptfds, &maxfd);
  107. if (maxfd == -1) {
  108. #ifdef _WIN32
  109. Sleep(100);
  110. return 0;
  111. #else
  112. struct timeval wait = { 0, 100 * 1000 };
  113. return select(0, NULL, NULL, NULL, &wait);
  114. #endif
  115. } else {
  116. return select(maxfd + 1, &readfds, &writefds, &exceptfds, &to);
  117. }
  118. }
  119. CURLMSG bmx_curl_CURLMsg_msg(CURLMsg * message) {
  120. return message->msg;
  121. }
  122. CURLcode bmx_curl_CURLMsg_result(CURLMsg * message) {
  123. return message->data.result;
  124. }
  125. CURL * bmx_curl_CURLMsg_easy_handle(CURLMsg * message) {
  126. return message->easy_handle;
  127. }
  128. CURLcode bmx_curl_easy_getinfo_slist(CURL * curl, CURLINFO info, struct curl_slist * list) {
  129. return curl_easy_getinfo(curl, info, list);
  130. }
  131. void bmx_curl_easy_setopt_slist(CURL *curl, CURLoption option, struct curl_slist * slist) {
  132. curl_easy_setopt(curl, option, slist);
  133. }
  134. void bmx_curl_multi_setopt_int(CURLM * multi, CURLMoption option, int value) {
  135. curl_multi_setopt(multi, option, value);
  136. }