Http.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include "Http.hpp"
  31. #include "Phy.hpp"
  32. #include "OSUtils.hpp"
  33. #include "../node/Constants.hpp"
  34. #include "../node/Utils.hpp"
  35. #include "../ext/http-parser/http_parser.h"
  36. namespace ZeroTier {
  37. namespace {
  38. static int ShttpOnMessageBegin(http_parser *parser);
  39. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length);
  40. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length);
  41. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length);
  42. static int ShttpOnValue(http_parser *parser,const char *ptr,size_t length);
  43. static int ShttpOnHeadersComplete(http_parser *parser);
  44. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length);
  45. static int ShttpOnMessageComplete(http_parser *parser);
  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. struct HttpPhyHandler
  57. {
  58. // not used
  59. inline void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len) {}
  60. inline void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) {}
  61. inline void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
  62. {
  63. if (success) {
  64. phy->tcpSetNotifyWritable(sock,true);
  65. } else {
  66. *responseBody = "connection failed";
  67. error = true;
  68. done = true;
  69. }
  70. }
  71. inline void phyOnTcpClose(PhySocket *sock,void **uptr)
  72. {
  73. done = true;
  74. }
  75. inline void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  76. {
  77. lastActivity = OSUtils::now();
  78. http_parser_execute(&parser,&HTTP_PARSER_SETTINGS,(const char *)data,len);
  79. if ((parser.upgrade)||(parser.http_errno != HPE_OK))
  80. phy->close(sock);
  81. }
  82. inline void phyOnTcpWritable(PhySocket *sock,void **uptr)
  83. {
  84. if (writePtr < writeSize) {
  85. long n = phy->tcpSend(sock,writeBuf + writePtr,writeSize - writePtr,true);
  86. if (n > 0)
  87. writePtr += n;
  88. }
  89. if (writePtr >= writeSize)
  90. phy->tcpSetNotifyWritable(sock,false);
  91. }
  92. http_parser parser;
  93. std::string currentHeaderField;
  94. std::string currentHeaderValue;
  95. unsigned long messageSize;
  96. unsigned long writePtr;
  97. uint64_t lastActivity;
  98. unsigned long writeSize;
  99. char writeBuf[32768];
  100. unsigned long maxResponseSize;
  101. std::map<std::string,std::string> *responseHeaders;
  102. std::string *responseBody;
  103. bool error;
  104. bool done;
  105. Phy<HttpPhyHandler *> *phy;
  106. PhySocket *sock;
  107. };
  108. static int ShttpOnMessageBegin(http_parser *parser)
  109. {
  110. return 0;
  111. }
  112. static int ShttpOnUrl(http_parser *parser,const char *ptr,size_t length)
  113. {
  114. return 0;
  115. }
  116. static int ShttpOnStatus(http_parser *parser,const char *ptr,size_t length)
  117. {
  118. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  119. hh->messageSize += (unsigned long)length;
  120. if (hh->messageSize > hh->maxResponseSize)
  121. return -1;
  122. return 0;
  123. }
  124. static int ShttpOnHeaderField(http_parser *parser,const char *ptr,size_t length)
  125. {
  126. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  127. hh->messageSize += (unsigned long)length;
  128. if (hh->messageSize > hh->maxResponseSize)
  129. return -1;
  130. if ((hh->currentHeaderField.length())&&(hh->currentHeaderValue.length())) {
  131. (*hh->responseHeaders)[hh->currentHeaderField] = hh->currentHeaderValue;
  132. hh->currentHeaderField = "";
  133. hh->currentHeaderValue = "";
  134. }
  135. for(size_t i=0;i<length;++i)
  136. hh->currentHeaderField.push_back(OSUtils::toLower(ptr[i]));
  137. return 0;
  138. }
  139. static int ShttpOnValue(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. hh->currentHeaderValue.append(ptr,length);
  146. return 0;
  147. }
  148. static int ShttpOnHeadersComplete(http_parser *parser)
  149. {
  150. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  151. if ((hh->currentHeaderField.length())&&(hh->currentHeaderValue.length()))
  152. (*hh->responseHeaders)[hh->currentHeaderField] = hh->currentHeaderValue;
  153. return 0;
  154. }
  155. static int ShttpOnBody(http_parser *parser,const char *ptr,size_t length)
  156. {
  157. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  158. hh->messageSize += (unsigned long)length;
  159. if (hh->messageSize > hh->maxResponseSize)
  160. return -1;
  161. hh->responseBody->append(ptr,length);
  162. return 0;
  163. }
  164. static int ShttpOnMessageComplete(http_parser *parser)
  165. {
  166. HttpPhyHandler *hh = reinterpret_cast<HttpPhyHandler *>(parser->data);
  167. hh->phy->close(hh->sock);
  168. return 0;
  169. }
  170. } // anonymous namespace
  171. unsigned int Http::_do(
  172. const char *method,
  173. unsigned long maxResponseSize,
  174. unsigned long timeout,
  175. const struct sockaddr *remoteAddress,
  176. const char *path,
  177. const std::map<std::string,std::string> &requestHeaders,
  178. const void *requestBody,
  179. unsigned long requestBodyLength,
  180. std::map<std::string,std::string> &responseHeaders,
  181. std::string &responseBody)
  182. {
  183. try {
  184. responseHeaders.clear();
  185. responseBody = "";
  186. HttpPhyHandler handler;
  187. http_parser_init(&(handler.parser),HTTP_RESPONSE);
  188. handler.parser.data = (void *)&handler;
  189. handler.messageSize = 0;
  190. handler.writePtr = 0;
  191. handler.lastActivity = OSUtils::now();
  192. try {
  193. handler.writeSize = Utils::snprintf(handler.writeBuf,sizeof(handler.writeBuf),"%s %s HTTP/1.1\r\n",method,path);
  194. for(std::map<std::string,std::string>::const_iterator h(requestHeaders.begin());h!=requestHeaders.end();++h)
  195. 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());
  196. handler.writeSize += Utils::snprintf(handler.writeBuf + handler.writeSize,sizeof(handler.writeBuf) - handler.writeSize,"\r\n");
  197. if ((requestBody)&&(requestBodyLength)) {
  198. if ((handler.writeSize + requestBodyLength) > sizeof(handler.writeBuf)) {
  199. responseBody = "request too large";
  200. return 0;
  201. }
  202. memcpy(handler.writeBuf + handler.writeSize,requestBody,requestBodyLength);
  203. handler.writeSize += requestBodyLength;
  204. }
  205. } catch ( ... ) {
  206. responseBody = "request too large";
  207. return 0;
  208. }
  209. handler.maxResponseSize = maxResponseSize;
  210. handler.responseHeaders = &responseHeaders;
  211. handler.responseBody = &responseBody;
  212. handler.error = false;
  213. handler.done = false;
  214. Phy<HttpPhyHandler *> phy(&handler,true);
  215. bool instantConnect = false;
  216. handler.phy = &phy;
  217. handler.sock = phy.tcpConnect((const struct sockaddr *)remoteAddress,instantConnect,(void *)0,true);
  218. if (!handler.sock) {
  219. responseBody = "connection failed (2)";
  220. return 0;
  221. }
  222. while (!handler.done) {
  223. phy.poll(timeout / 2);
  224. if ((timeout)&&((unsigned long)(OSUtils::now() - handler.lastActivity) > timeout)) {
  225. phy.close(handler.sock);
  226. responseBody = "timed out";
  227. return 0;
  228. }
  229. }
  230. return ((handler.error) ? 0 : ((handler.parser.http_errno != HPE_OK) ? 0 : handler.parser.status_code));
  231. } catch (std::exception &exc) {
  232. responseBody = exc.what();
  233. return 0;
  234. } catch ( ... ) {
  235. responseBody = "unknown exception";
  236. return 0;
  237. }
  238. }
  239. } // namespace ZeroTier