Http.hpp 4.0 KB

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