Http.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_HTTP_HPP
  19. #define ZT_HTTP_HPP
  20. #include <string>
  21. #include <map>
  22. #include <stdexcept>
  23. #if defined(_WIN32) || defined(_WIN64)
  24. #include <WinSock2.h>
  25. #include <WS2tcpip.h>
  26. #include <Windows.h>
  27. #else
  28. #include <unistd.h>
  29. #include <sys/time.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <arpa/inet.h>
  33. #include <netinet/in.h>
  34. #endif
  35. namespace ZeroTier {
  36. /**
  37. * Simple synchronous HTTP client used for updater and cli
  38. */
  39. class Http
  40. {
  41. public:
  42. /**
  43. * Make HTTP GET request
  44. *
  45. * The caller must set all headers, including Host.
  46. *
  47. * @return HTTP status code or 0 on error (responseBody will contain error message)
  48. */
  49. static inline unsigned int GET(
  50. unsigned long maxResponseSize,
  51. unsigned long timeout,
  52. const struct sockaddr *remoteAddress,
  53. const char *path,
  54. const std::map<std::string,std::string> &requestHeaders,
  55. std::map<std::string,std::string> &responseHeaders,
  56. std::string &responseBody)
  57. {
  58. return _do(
  59. "GET",
  60. maxResponseSize,
  61. timeout,
  62. remoteAddress,
  63. path,
  64. requestHeaders,
  65. (const void *)0,
  66. 0,
  67. responseHeaders,
  68. responseBody);
  69. }
  70. /**
  71. * Make HTTP DELETE request
  72. *
  73. * The caller must set all headers, including Host.
  74. *
  75. * @return HTTP status code or 0 on error (responseBody will contain error message)
  76. */
  77. static inline unsigned int DEL(
  78. unsigned long maxResponseSize,
  79. unsigned long timeout,
  80. const struct sockaddr *remoteAddress,
  81. const char *path,
  82. const std::map<std::string,std::string> &requestHeaders,
  83. std::map<std::string,std::string> &responseHeaders,
  84. std::string &responseBody)
  85. {
  86. return _do(
  87. "DELETE",
  88. maxResponseSize,
  89. timeout,
  90. remoteAddress,
  91. path,
  92. requestHeaders,
  93. (const void *)0,
  94. 0,
  95. responseHeaders,
  96. responseBody);
  97. }
  98. /**
  99. * Make HTTP POST request
  100. *
  101. * It is the responsibility of the caller to set all headers. With POST, the
  102. * Content-Length and Content-Type headers must be set or the POST will not
  103. * work.
  104. *
  105. * @return HTTP status code or 0 on error (responseBody will contain error message)
  106. */
  107. static inline unsigned int POST(
  108. unsigned long maxResponseSize,
  109. unsigned long timeout,
  110. const struct sockaddr *remoteAddress,
  111. const char *path,
  112. const std::map<std::string,std::string> &requestHeaders,
  113. const void *postData,
  114. unsigned long postDataLength,
  115. std::map<std::string,std::string> &responseHeaders,
  116. std::string &responseBody)
  117. {
  118. return _do(
  119. "POST",
  120. maxResponseSize,
  121. timeout,
  122. remoteAddress,
  123. path,
  124. requestHeaders,
  125. postData,
  126. postDataLength,
  127. responseHeaders,
  128. responseBody);
  129. }
  130. private:
  131. static unsigned int _do(
  132. const char *method,
  133. unsigned long maxResponseSize,
  134. unsigned long timeout,
  135. const struct sockaddr *remoteAddress,
  136. const char *path,
  137. const std::map<std::string,std::string> &requestHeaders,
  138. const void *requestBody,
  139. unsigned long requestBodyLength,
  140. std::map<std::string,std::string> &responseHeaders,
  141. std::string &responseBody);
  142. };
  143. } // namespace ZeroTier
  144. #endif