Http.cpp 8.3 KB

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