Http.hpp 4.0 KB

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