Http.cpp 8.6 KB

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