Http.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_HTTP_HPP
  9. #define ZT_HTTP_HPP
  10. #include <map>
  11. #include <stdexcept>
  12. #include <string>
  13. #if defined(_WIN32) || defined(_WIN64)
  14. // clang-format off
  15. #include <winsock2.h>
  16. #include <ws2tcpip.h>
  17. #include <windows.h>
  18. // clang-format on
  19. #else
  20. #include <arpa/inet.h>
  21. #include <netinet/in.h>
  22. #include <sys/socket.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #endif
  27. namespace ZeroTier {
  28. /**
  29. * Simple synchronous HTTP client used for updater and cli
  30. */
  31. class Http {
  32. public:
  33. /**
  34. * Make HTTP GET request
  35. *
  36. * The caller must set all headers, including Host.
  37. *
  38. * @return HTTP status code or 0 on error (responseBody will contain error message)
  39. */
  40. static inline unsigned int
  41. GET(unsigned long maxResponseSize,
  42. unsigned long timeout,
  43. const struct sockaddr* remoteAddress,
  44. const char* path,
  45. const std::map<std::string, std::string>& requestHeaders,
  46. std::map<std::string, std::string>& responseHeaders,
  47. std::string& responseBody)
  48. {
  49. return _do("GET", maxResponseSize, timeout, remoteAddress, path, requestHeaders, (const void*)0, 0, responseHeaders, responseBody);
  50. }
  51. /**
  52. * Make HTTP DELETE 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
  59. DEL(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("DELETE", maxResponseSize, timeout, remoteAddress, path, requestHeaders, (const void*)0, 0, responseHeaders, responseBody);
  68. }
  69. /**
  70. * Make HTTP POST request
  71. *
  72. * It is the responsibility of the caller to set all headers. With POST, the
  73. * Content-Length and Content-Type headers must be set or the POST will not
  74. * work.
  75. *
  76. * @return HTTP status code or 0 on error (responseBody will contain error message)
  77. */
  78. static inline unsigned int POST(
  79. unsigned long maxResponseSize,
  80. unsigned long timeout,
  81. const struct sockaddr* remoteAddress,
  82. const char* path,
  83. const std::map<std::string, std::string>& requestHeaders,
  84. const void* postData,
  85. unsigned long postDataLength,
  86. std::map<std::string, std::string>& responseHeaders,
  87. std::string& responseBody)
  88. {
  89. return _do("POST", maxResponseSize, timeout, remoteAddress, path, requestHeaders, postData, postDataLength, responseHeaders, responseBody);
  90. }
  91. /**
  92. * Make HTTP PUT request
  93. *
  94. * It is the responsibility of the caller to set all headers. With PUT, the
  95. * Content-Length and Content-Type headers must be set or the PUT will not
  96. * work.
  97. *
  98. * @return HTTP status code or 0 on error (responseBody will contain error message)
  99. */
  100. static inline unsigned int
  101. PUT(unsigned long maxResponseSize,
  102. unsigned long timeout,
  103. const struct sockaddr* remoteAddress,
  104. const char* path,
  105. const std::map<std::string, std::string>& requestHeaders,
  106. const void* postData,
  107. unsigned long postDataLength,
  108. std::map<std::string, std::string>& responseHeaders,
  109. std::string& responseBody)
  110. {
  111. return _do("PUT", maxResponseSize, timeout, remoteAddress, path, requestHeaders, postData, postDataLength, responseHeaders, responseBody);
  112. }
  113. private:
  114. static unsigned int
  115. _do(const char* method,
  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* requestBody,
  122. unsigned long requestBodyLength,
  123. std::map<std::string, std::string>& responseHeaders,
  124. std::string& responseBody);
  125. };
  126. } // namespace ZeroTier
  127. #endif