Http.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include "Http.hpp"
  14. #include "../node/Constants.hpp"
  15. #include "../node/Utils.hpp"
  16. #include "OSUtils.hpp"
  17. #include "Phy.hpp"
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #ifdef ZT_USE_SYSTEM_HTTP_PARSER
  22. #include <http_parser.h>
  23. #else
  24. #include "../ext/http-parser/http_parser.h"
  25. #endif
  26. namespace ZeroTier {
  27. namespace {
  28. static int ShttpOnMessageBegin(http_parser* parser);
  29. static int ShttpOnUrl(http_parser* parser, const char* ptr, size_t length);
  30. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  31. static int ShttpOnStatus(http_parser* parser, const char* ptr, size_t length);
  32. #else
  33. static int ShttpOnStatus(http_parser* parser);
  34. #endif
  35. static int ShttpOnHeaderField(http_parser* parser, const char* ptr, size_t length);
  36. static int ShttpOnValue(http_parser* parser, const char* ptr, size_t length);
  37. static int ShttpOnHeadersComplete(http_parser* parser);
  38. static int ShttpOnBody(http_parser* parser, const char* ptr, size_t length);
  39. static int ShttpOnMessageComplete(http_parser* parser);
  40. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 1)
  41. static const struct http_parser_settings HTTP_PARSER_SETTINGS = { ShttpOnMessageBegin, ShttpOnUrl, ShttpOnStatus, ShttpOnHeaderField, ShttpOnValue, ShttpOnHeadersComplete, ShttpOnBody, ShttpOnMessageComplete };
  42. #else
  43. static const struct http_parser_settings HTTP_PARSER_SETTINGS = { ShttpOnMessageBegin, ShttpOnUrl, ShttpOnHeaderField, ShttpOnValue, ShttpOnHeadersComplete, ShttpOnBody, ShttpOnMessageComplete };
  44. #endif
  45. struct HttpPhyHandler {
  46. // not used
  47. inline void phyOnDatagram(PhySocket* sock, void** uptr, const struct sockaddr* localAddr, const struct sockaddr* from, void* data, unsigned long len)
  48. {
  49. }
  50. inline void phyOnTcpAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN, const struct sockaddr* from)
  51. {
  52. }
  53. inline void phyOnTcpConnect(PhySocket* sock, void** uptr, bool success)
  54. {
  55. if (success) {
  56. phy->setNotifyWritable(sock, true);
  57. }
  58. else {
  59. *responseBody = "connection failed";
  60. error = true;
  61. done = true;
  62. }
  63. }
  64. inline void phyOnTcpClose(PhySocket* sock, void** uptr)
  65. {
  66. done = true;
  67. }
  68. inline void phyOnTcpData(PhySocket* sock, void** uptr, void* data, unsigned long len)
  69. {
  70. lastActivity = OSUtils::now();
  71. http_parser_execute(&parser, &HTTP_PARSER_SETTINGS, (const char*)data, len);
  72. if ((parser.upgrade) || (parser.http_errno != HPE_OK))
  73. phy->close(sock);
  74. }
  75. inline void phyOnTcpWritable(PhySocket* sock, void** uptr)
  76. {
  77. if (writePtr < (unsigned long)writeBuf.length()) {
  78. long n = phy->streamSend(sock, writeBuf.data() + writePtr, (unsigned long)writeBuf.length() - writePtr, true);
  79. if (n > 0)
  80. writePtr += n;
  81. }
  82. if (writePtr >= (unsigned long)writeBuf.length())
  83. phy->setNotifyWritable(sock, false);
  84. }
  85. inline void phyOnFileDescriptorActivity(PhySocket* sock, void** uptr, bool readable, bool writable)
  86. {
  87. }
  88. #ifdef __UNIX_LIKE__
  89. inline void phyOnUnixAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN)
  90. {
  91. }
  92. inline void phyOnUnixClose(PhySocket* sock, void** uptr)
  93. {
  94. }
  95. inline void phyOnUnixData(PhySocket* sock, void** uptr, void* data, unsigned long len)
  96. {
  97. }
  98. inline void phyOnUnixWritable(PhySocket* sock, void** uptr)
  99. {
  100. }
  101. #endif // __UNIX_LIKE__
  102. http_parser parser;
  103. std::string currentHeaderField;
  104. std::string currentHeaderValue;
  105. unsigned long messageSize;
  106. unsigned long writePtr;
  107. uint64_t lastActivity;
  108. std::string writeBuf;
  109. unsigned long maxResponseSize;
  110. std::map<std::string, std::string>* responseHeaders;
  111. std::string* responseBody;
  112. bool error;
  113. bool done;
  114. Phy<HttpPhyHandler*>* phy;
  115. PhySocket* sock;
  116. };
  117. static int ShttpOnMessageBegin(http_parser* parser)
  118. {
  119. return 0;
  120. }
  121. static int ShttpOnUrl(http_parser* parser, const char* ptr, size_t length)
  122. {
  123. return 0;
  124. }
  125. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  126. static int ShttpOnStatus(http_parser* parser, const char* ptr, size_t length)
  127. #else
  128. static int ShttpOnStatus(http_parser* parser)
  129. #endif
  130. {
  131. /*
  132. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  133. hh->messageSize += (unsigned long)length;
  134. if (hh->messageSize > hh->maxResponseSize)
  135. return -1;
  136. */
  137. return 0;
  138. }
  139. static int ShttpOnHeaderField(http_parser* parser, const char* ptr, size_t length)
  140. {
  141. HttpPhyHandler* hh = reinterpret_cast<HttpPhyHandler*>(parser->data);
  142. hh->messageSize += (unsigned long)length;
  143. if (hh->messageSize > hh->maxResponseSize)
  144. return -1;
  145. if ((hh->currentHeaderField.length()) && (hh->currentHeaderValue.length())) {
  146. (*hh->responseHeaders)[hh->currentHeaderField] = hh->currentHeaderValue;
  147. hh->currentHeaderField = "";
  148. hh->currentHeaderValue = "";
  149. }
  150. for (size_t i = 0; i < length; ++i)
  151. hh->currentHeaderField.push_back(OSUtils::toLower(ptr[i]));
  152. return 0;
  153. }
  154. static int ShttpOnValue(http_parser* parser, const char* ptr, size_t length)
  155. {
  156. HttpPhyHandler* hh = reinterpret_cast<HttpPhyHandler*>(parser->data);
  157. hh->messageSize += (unsigned long)length;
  158. if (hh->messageSize > hh->maxResponseSize)
  159. return -1;
  160. hh->currentHeaderValue.append(ptr, length);
  161. return 0;
  162. }
  163. static int ShttpOnHeadersComplete(http_parser* parser)
  164. {
  165. HttpPhyHandler* hh = reinterpret_cast<HttpPhyHandler*>(parser->data);
  166. if ((hh->currentHeaderField.length()) && (hh->currentHeaderValue.length()))
  167. (*hh->responseHeaders)[hh->currentHeaderField] = hh->currentHeaderValue;
  168. return 0;
  169. }
  170. static int ShttpOnBody(http_parser* parser, const char* ptr, size_t length)
  171. {
  172. HttpPhyHandler* hh = reinterpret_cast<HttpPhyHandler*>(parser->data);
  173. hh->messageSize += (unsigned long)length;
  174. if (hh->messageSize > hh->maxResponseSize)
  175. return -1;
  176. hh->responseBody->append(ptr, length);
  177. return 0;
  178. }
  179. static int ShttpOnMessageComplete(http_parser* parser)
  180. {
  181. HttpPhyHandler* hh = reinterpret_cast<HttpPhyHandler*>(parser->data);
  182. hh->phy->close(hh->sock);
  183. return 0;
  184. }
  185. } // anonymous namespace
  186. unsigned int Http::_do(
  187. const char* method,
  188. unsigned long maxResponseSize,
  189. unsigned long timeout,
  190. const struct sockaddr* remoteAddress,
  191. const char* path,
  192. const std::map<std::string, std::string>& requestHeaders,
  193. const void* requestBody,
  194. unsigned long requestBodyLength,
  195. std::map<std::string, std::string>& responseHeaders,
  196. std::string& responseBody)
  197. {
  198. try {
  199. responseHeaders.clear();
  200. responseBody = "";
  201. HttpPhyHandler handler;
  202. http_parser_init(&(handler.parser), HTTP_RESPONSE);
  203. handler.parser.data = (void*)&handler;
  204. handler.messageSize = 0;
  205. handler.writePtr = 0;
  206. handler.lastActivity = OSUtils::now();
  207. try {
  208. char tmp[1024];
  209. OSUtils::ztsnprintf(tmp, sizeof(tmp), "%s %s HTTP/1.1\r\n", method, path);
  210. handler.writeBuf.append(tmp);
  211. for (std::map<std::string, std::string>::const_iterator h(requestHeaders.begin()); h != requestHeaders.end(); ++h) {
  212. OSUtils::ztsnprintf(tmp, sizeof(tmp), "%s: %s\r\n", h->first.c_str(), h->second.c_str());
  213. handler.writeBuf.append(tmp);
  214. }
  215. handler.writeBuf.append("\r\n");
  216. if ((requestBody) && (requestBodyLength))
  217. handler.writeBuf.append((const char*)requestBody, requestBodyLength);
  218. }
  219. catch (...) {
  220. responseBody = "request too large";
  221. return 0;
  222. }
  223. if (maxResponseSize) {
  224. handler.maxResponseSize = maxResponseSize;
  225. }
  226. else {
  227. handler.maxResponseSize = 2147483647;
  228. }
  229. handler.responseHeaders = &responseHeaders;
  230. handler.responseBody = &responseBody;
  231. handler.error = false;
  232. handler.done = false;
  233. Phy<HttpPhyHandler*> phy(&handler, true, true);
  234. bool instantConnect = false;
  235. handler.phy = &phy;
  236. handler.sock = phy.tcpConnect((const struct sockaddr*)remoteAddress, instantConnect, (void*)0, true);
  237. if (! handler.sock) {
  238. responseBody = "connection failed (2)";
  239. return 0;
  240. }
  241. while (! handler.done) {
  242. phy.poll(timeout / 2);
  243. if ((timeout) && ((unsigned long)(OSUtils::now() - handler.lastActivity) > timeout)) {
  244. phy.close(handler.sock);
  245. responseBody = "timed out";
  246. return 0;
  247. }
  248. }
  249. return ((handler.error) ? 0 : ((handler.parser.http_errno != HPE_OK) ? 0 : handler.parser.status_code));
  250. }
  251. catch (std::exception& exc) {
  252. responseBody = exc.what();
  253. return 0;
  254. }
  255. catch (...) {
  256. responseBody = "unknown exception";
  257. return 0;
  258. }
  259. }
  260. } // namespace ZeroTier