Http.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include "Http.hpp"
  22. #include "Phy.hpp"
  23. #include "OSUtils.hpp"
  24. #include "../node/Constants.hpp"
  25. #include "../node/Utils.hpp"
  26. #ifdef ZT_USE_SYSTEM_HTTP_PARSER
  27. #include <http_parser.h>
  28. #else
  29. #include "../ext/http-parser/http_parser.h"
  30. #endif
  31. namespace ZeroTier {
  32. namespace {
  33. static int ShttpOnMessageBegin(http_parser *parser);
  34. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length);
  35. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  36. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length);
  37. #else
  38. static int ShttpOnStatus(http_parser *parser);
  39. #endif
  40. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length);
  41. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length);
  42. static int ShttpOnHeadersComplete(http_parser *parser);
  43. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length);
  44. static int ShttpOnMessageComplete(http_parser *parser);
  45. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 1)
  46. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  47. ShttpOnMessageBegin,
  48. ShttpOnUrl,
  49. ShttpOnStatus,
  50. ShttpOnHeaderField,
  51. ShttpOnValue,
  52. ShttpOnHeadersComplete,
  53. ShttpOnBody,
  54. ShttpOnMessageComplete
  55. };
  56. #else
  57. static const struct http_parser_settings HTTP_PARSER_SETTINGS = {
  58. ShttpOnMessageBegin,
  59. ShttpOnUrl,
  60. ShttpOnHeaderField,
  61. ShttpOnValue,
  62. ShttpOnHeadersComplete,
  63. ShttpOnBody,
  64. ShttpOnMessageComplete
  65. };
  66. #endif
  67. struct HttpPhyHandler
  68. {
  69. // not used
  70. inline void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len) {}
  71. inline void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) {}
  72. inline void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
  73. {
  74. if (success) {
  75. phy->setNotifyWritable(sock,true);
  76. } else {
  77. *responseBody = "connection failed";
  78. error = true;
  79. done = true;
  80. }
  81. }
  82. inline void phyOnTcpClose(PhySocket *sock,void **uptr)
  83. {
  84. done = true;
  85. }
  86. inline void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  87. {
  88. lastActivity = OSUtils::now();
  89. http_parser_execute(&parser,&HTTP_PARSER_SETTINGS,(const char *)data,len);
  90. if ((parser.upgrade)||(parser.http_errno != HPE_OK))
  91. phy->close(sock);
  92. }
  93. inline void phyOnTcpWritable(PhySocket *sock,void **uptr)
  94. {
  95. if (writePtr < writeSize) {
  96. long n = phy->streamSend(sock,writeBuf + writePtr,writeSize - writePtr,true);
  97. if (n > 0)
  98. writePtr += n;
  99. }
  100. if (writePtr >= writeSize)
  101. phy->setNotifyWritable(sock,false);
  102. }
  103. inline void phyOnFileDescriptorActivity(PhySocket *sock,void **uptr,bool readable,bool writable) {}
  104. #ifdef __UNIX_LIKE__
  105. inline void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN) {}
  106. inline void phyOnUnixClose(PhySocket *sock,void **uptr) {}
  107. inline void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  108. inline void phyOnUnixWritable(PhySocket *sock,void **uptr) {}
  109. #endif // __UNIX_LIKE__
  110. http_parser parser;
  111. std::string currentHeaderField;
  112. std::string currentHeaderValue;
  113. unsigned long messageSize;
  114. unsigned long writePtr;
  115. uint64_t lastActivity;
  116. unsigned long writeSize;
  117. char writeBuf[32768];
  118. unsigned long maxResponseSize;
  119. std::map<std::string,std::string> *responseHeaders;
  120. std::string *responseBody;
  121. bool error;
  122. bool done;
  123. Phy<HttpPhyHandler *> *phy;
  124. PhySocket *sock;
  125. };
  126. static int ShttpOnMessageBegin(http_parser *parser)
  127. {
  128. return 0;
  129. }
  130. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length)
  131. {
  132. return 0;
  133. }
  134. #if (HTTP_PARSER_VERSION_MAJOR >= 2) && (HTTP_PARSER_VERSION_MINOR >= 2)
  135. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length)
  136. #else
  137. static int ShttpOnStatus(http_parser *parser)
  138. #endif
  139. {
  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. */
  146. return 0;
  147. }
  148. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length)
  149. {
  150. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  151. hh->messageSize += (unsigned long)length;
  152. if (hh->messageSize > hh->maxResponseSize)
  153. return -1;
  154. if ((hh->currentHeaderField.length())&&(hh->currentHeaderValue.length())) {
  155. (*hh->responseHeaders)[hh->currentHeaderField] = hh->currentHeaderValue;
  156. hh->currentHeaderField = "";
  157. hh->currentHeaderValue = "";
  158. }
  159. for(size_t i=0;i<length;++i)
  160. hh->currentHeaderField.push_back(OSUtils::toLower(ptr[i]));
  161. return 0;
  162. }
  163. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length)
  164. {
  165. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  166. hh->messageSize += (unsigned long)length;
  167. if (hh->messageSize > hh->maxResponseSize)
  168. return -1;
  169. hh->currentHeaderValue.append(ptr,length);
  170. return 0;
  171. }
  172. static int ShttpOnHeadersComplete(http_parser *parser)
  173. {
  174. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  175. if ((hh->currentHeaderField.length())&&(hh->currentHeaderValue.length()))
  176. (*hh->responseHeaders)[hh->currentHeaderField] = hh->currentHeaderValue;
  177. return 0;
  178. }
  179. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length)
  180. {
  181. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  182. hh->messageSize += (unsigned long)length;
  183. if (hh->messageSize > hh->maxResponseSize)
  184. return -1;
  185. hh->responseBody->append(ptr,length);
  186. return 0;
  187. }
  188. static int ShttpOnMessageComplete(http_parser *parser)
  189. {
  190. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  191. hh->phy->close(hh->sock);
  192. return 0;
  193. }
  194. } // anonymous namespace
  195. unsigned int Http::_do(
  196. const char *method,
  197. unsigned long maxResponseSize,
  198. unsigned long timeout,
  199. const struct sockaddr *remoteAddress,
  200. const char *path,
  201. const std::map<std::string,std::string> &requestHeaders,
  202. const void *requestBody,
  203. unsigned long requestBodyLength,
  204. std::map<std::string,std::string> &responseHeaders,
  205. std::string &responseBody)
  206. {
  207. try {
  208. responseHeaders.clear();
  209. responseBody = "";
  210. HttpPhyHandler handler;
  211. http_parser_init(&(handler.parser),HTTP_RESPONSE);
  212. handler.parser.data = (void *)&handler;
  213. handler.messageSize = 0;
  214. handler.writePtr = 0;
  215. handler.lastActivity = OSUtils::now();
  216. try {
  217. handler.writeSize = Utils::snprintf(handler.writeBuf,sizeof(handler.writeBuf),"%s %s HTTP/1.1\r\n",method,path);
  218. for(std::map<std::string,std::string>::const_iterator h(requestHeaders.begin());h!=requestHeaders.end();++h)
  219. handler.writeSize += Utils::snprintf(handler.writeBuf + handler.writeSize,sizeof(handler.writeBuf) - handler.writeSize,"%s: %s\r\n",h->first.c_str(),h->second.c_str());
  220. handler.writeSize += Utils::snprintf(handler.writeBuf + handler.writeSize,sizeof(handler.writeBuf) - handler.writeSize,"\r\n");
  221. if ((requestBody)&&(requestBodyLength)) {
  222. if ((handler.writeSize + requestBodyLength) > sizeof(handler.writeBuf)) {
  223. responseBody = "request too large";
  224. return 0;
  225. }
  226. memcpy(handler.writeBuf + handler.writeSize,requestBody,requestBodyLength);
  227. handler.writeSize += requestBodyLength;
  228. }
  229. } catch ( ... ) {
  230. responseBody = "request too large";
  231. return 0;
  232. }
  233. handler.maxResponseSize = maxResponseSize;
  234. handler.responseHeaders = &responseHeaders;
  235. handler.responseBody = &responseBody;
  236. handler.error = false;
  237. handler.done = false;
  238. Phy<HttpPhyHandler *> phy(&handler,true,true);
  239. bool instantConnect = false;
  240. handler.phy = &phy;
  241. handler.sock = phy.tcpConnect((const struct sockaddr *)remoteAddress,instantConnect,(void *)0,true);
  242. if (!handler.sock) {
  243. responseBody = "connection failed (2)";
  244. return 0;
  245. }
  246. while (!handler.done) {
  247. phy.poll(timeout / 2);
  248. if ((timeout)&&((unsigned long)(OSUtils::now() - handler.lastActivity) > timeout)) {
  249. phy.close(handler.sock);
  250. responseBody = "timed out";
  251. return 0;
  252. }
  253. }
  254. return ((handler.error) ? 0 : ((handler.parser.http_errno != HPE_OK) ? 0 : handler.parser.status_code));
  255. } catch (std::exception &exc) {
  256. responseBody = exc.what();
  257. return 0;
  258. } catch ( ... ) {
  259. responseBody = "unknown exception";
  260. return 0;
  261. }
  262. }
  263. } // namespace ZeroTier