Http.cpp 8.3 KB

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