Http.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_HTTP_HPP
  27. #define ZT_HTTP_HPP
  28. #include <string>
  29. #include <map>
  30. #include <stdexcept>
  31. #if defined(_WIN32) || defined(_WIN64)
  32. #include <WinSock2.h>
  33. #include <WS2tcpip.h>
  34. #include <Windows.h>
  35. #else
  36. #include <unistd.h>
  37. #include <sys/time.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <arpa/inet.h>
  41. #include <netinet/in.h>
  42. #endif
  43. namespace ZeroTier {
  44. /**
  45. * Simple synchronous HTTP client used for updater and cli
  46. */
  47. class Http
  48. {
  49. public:
  50. /**
  51. * Make HTTP GET request
  52. *
  53. * The caller must set all headers, including Host.
  54. *
  55. * @return HTTP status code or 0 on error (responseBody will contain error message)
  56. */
  57. static inline unsigned int GET(
  58. unsigned long maxResponseSize,
  59. unsigned long timeout,
  60. const struct sockaddr *remoteAddress,
  61. const char *path,
  62. const std::map<std::string,std::string> &requestHeaders,
  63. std::map<std::string,std::string> &responseHeaders,
  64. std::string &responseBody)
  65. {
  66. return _do(
  67. "GET",
  68. maxResponseSize,
  69. timeout,
  70. remoteAddress,
  71. path,
  72. requestHeaders,
  73. (const void *)0,
  74. 0,
  75. responseHeaders,
  76. responseBody);
  77. }
  78. /**
  79. * Make HTTP DELETE request
  80. *
  81. * The caller must set all headers, including Host.
  82. *
  83. * @return HTTP status code or 0 on error (responseBody will contain error message)
  84. */
  85. static inline unsigned int DEL(
  86. unsigned long maxResponseSize,
  87. unsigned long timeout,
  88. const struct sockaddr *remoteAddress,
  89. const char *path,
  90. const std::map<std::string,std::string> &requestHeaders,
  91. std::map<std::string,std::string> &responseHeaders,
  92. std::string &responseBody)
  93. {
  94. return _do(
  95. "DELETE",
  96. maxResponseSize,
  97. timeout,
  98. remoteAddress,
  99. path,
  100. requestHeaders,
  101. (const void *)0,
  102. 0,
  103. responseHeaders,
  104. responseBody);
  105. }
  106. /**
  107. * Make HTTP POST request
  108. *
  109. * It is the responsibility of the caller to set all headers. With POST, the
  110. * Content-Length and Content-Type headers must be set or the POST will not
  111. * work.
  112. *
  113. * @return HTTP status code or 0 on error (responseBody will contain error message)
  114. */
  115. static inline unsigned int POST(
  116. unsigned long maxResponseSize,
  117. unsigned long timeout,
  118. const struct sockaddr *remoteAddress,
  119. const char *path,
  120. const std::map<std::string,std::string> &requestHeaders,
  121. const void *postData,
  122. unsigned long postDataLength,
  123. std::map<std::string,std::string> &responseHeaders,
  124. std::string &responseBody)
  125. {
  126. return _do(
  127. "POST",
  128. maxResponseSize,
  129. timeout,
  130. remoteAddress,
  131. path,
  132. requestHeaders,
  133. postData,
  134. postDataLength,
  135. responseHeaders,
  136. responseBody);
  137. }
  138. /**
  139. * Make HTTP PUT request
  140. *
  141. * It is the responsibility of the caller to set all headers. With PUT, the
  142. * Content-Length and Content-Type headers must be set or the PUT will not
  143. * work.
  144. *
  145. * @return HTTP status code or 0 on error (responseBody will contain error message)
  146. */
  147. static inline unsigned int PUT(
  148. unsigned long maxResponseSize,
  149. unsigned long timeout,
  150. const struct sockaddr *remoteAddress,
  151. const char *path,
  152. const std::map<std::string,std::string> &requestHeaders,
  153. const void *postData,
  154. unsigned long postDataLength,
  155. std::map<std::string,std::string> &responseHeaders,
  156. std::string &responseBody)
  157. {
  158. return _do(
  159. "PUT",
  160. maxResponseSize,
  161. timeout,
  162. remoteAddress,
  163. path,
  164. requestHeaders,
  165. postData,
  166. postDataLength,
  167. responseHeaders,
  168. responseBody);
  169. }
  170. private:
  171. static unsigned int _do(
  172. const char *method,
  173. unsigned long maxResponseSize,
  174. unsigned long timeout,
  175. const struct sockaddr *remoteAddress,
  176. const char *path,
  177. const std::map<std::string,std::string> &requestHeaders,
  178. const void *requestBody,
  179. unsigned long requestBodyLength,
  180. std::map<std::string,std::string> &responseHeaders,
  181. std::string &responseBody);
  182. };
  183. } // namespace ZeroTier
  184. #endif