Http.cpp 8.3 KB

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