Http.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_HTTP_HPP
  14. #define ZT_HTTP_HPP
  15. #include <string>
  16. #include <map>
  17. #include <stdexcept>
  18. #if defined(_WIN32) || defined(_WIN64)
  19. #include <winsock2.h>
  20. #include <ws2tcpip.h>
  21. #include <windows.h>
  22. #else
  23. #include <unistd.h>
  24. #include <sys/time.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <arpa/inet.h>
  28. #include <netinet/in.h>
  29. #endif
  30. namespace ZeroTier {
  31. /**
  32. * Simple synchronous HTTP client used for updater and cli
  33. */
  34. class Http
  35. {
  36. public:
  37. /**
  38. * Make HTTP GET request
  39. *
  40. * The caller must set all headers, including Host.
  41. *
  42. * @return HTTP status code or 0 on error (responseBody will contain error message)
  43. */
  44. static inline unsigned int GET(
  45. unsigned long maxResponseSize,
  46. unsigned long timeout,
  47. const struct sockaddr *remoteAddress,
  48. const char *path,
  49. const std::map<std::string,std::string> &requestHeaders,
  50. std::map<std::string,std::string> &responseHeaders,
  51. std::string &responseBody)
  52. {
  53. return _do(
  54. "GET",
  55. maxResponseSize,
  56. timeout,
  57. remoteAddress,
  58. path,
  59. requestHeaders,
  60. (const void *)0,
  61. 0,
  62. responseHeaders,
  63. responseBody);
  64. }
  65. /**
  66. * Make HTTP DELETE request
  67. *
  68. * The caller must set all headers, including Host.
  69. *
  70. * @return HTTP status code or 0 on error (responseBody will contain error message)
  71. */
  72. static inline unsigned int DEL(
  73. unsigned long maxResponseSize,
  74. unsigned long timeout,
  75. const struct sockaddr *remoteAddress,
  76. const char *path,
  77. const std::map<std::string,std::string> &requestHeaders,
  78. std::map<std::string,std::string> &responseHeaders,
  79. std::string &responseBody)
  80. {
  81. return _do(
  82. "DELETE",
  83. maxResponseSize,
  84. timeout,
  85. remoteAddress,
  86. path,
  87. requestHeaders,
  88. (const void *)0,
  89. 0,
  90. responseHeaders,
  91. responseBody);
  92. }
  93. /**
  94. * Make HTTP POST request
  95. *
  96. * It is the responsibility of the caller to set all headers. With POST, the
  97. * Content-Length and Content-Type headers must be set or the POST will not
  98. * work.
  99. *
  100. * @return HTTP status code or 0 on error (responseBody will contain error message)
  101. */
  102. static inline unsigned int POST(
  103. unsigned long maxResponseSize,
  104. unsigned long timeout,
  105. const struct sockaddr *remoteAddress,
  106. const char *path,
  107. const std::map<std::string,std::string> &requestHeaders,
  108. const void *postData,
  109. unsigned long postDataLength,
  110. std::map<std::string,std::string> &responseHeaders,
  111. std::string &responseBody)
  112. {
  113. return _do(
  114. "POST",
  115. maxResponseSize,
  116. timeout,
  117. remoteAddress,
  118. path,
  119. requestHeaders,
  120. postData,
  121. postDataLength,
  122. responseHeaders,
  123. responseBody);
  124. }
  125. /**
  126. * Make HTTP PUT request
  127. *
  128. * It is the responsibility of the caller to set all headers. With PUT, the
  129. * Content-Length and Content-Type headers must be set or the PUT will not
  130. * work.
  131. *
  132. * @return HTTP status code or 0 on error (responseBody will contain error message)
  133. */
  134. static inline unsigned int PUT(
  135. unsigned long maxResponseSize,
  136. unsigned long timeout,
  137. const struct sockaddr *remoteAddress,
  138. const char *path,
  139. const std::map<std::string,std::string> &requestHeaders,
  140. const void *postData,
  141. unsigned long postDataLength,
  142. std::map<std::string,std::string> &responseHeaders,
  143. std::string &responseBody)
  144. {
  145. return _do(
  146. "PUT",
  147. maxResponseSize,
  148. timeout,
  149. remoteAddress,
  150. path,
  151. requestHeaders,
  152. postData,
  153. postDataLength,
  154. responseHeaders,
  155. responseBody);
  156. }
  157. private:
  158. static unsigned int _do(
  159. const char *method,
  160. unsigned long maxResponseSize,
  161. unsigned long timeout,
  162. const struct sockaddr *remoteAddress,
  163. const char *path,
  164. const std::map<std::string,std::string> &requestHeaders,
  165. const void *requestBody,
  166. unsigned long requestBodyLength,
  167. std::map<std::string,std::string> &responseHeaders,
  168. std::string &responseBody);
  169. };
  170. } // namespace ZeroTier
  171. #endif