HttpClient.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef ZT_HTTPCLIENT_HPP
  28. #define ZT_HTTPCLIENT_HPP
  29. #include <string>
  30. #include <map>
  31. #include <set>
  32. #include "../node/Mutex.hpp"
  33. namespace ZeroTier {
  34. class HttpClient_Private_Request;
  35. /**
  36. * HTTP client that does queries in the background
  37. *
  38. * The handler method takes the following arguments: an arbitrary pointer, an
  39. * HTTP response code, the URL queried, whether or not the message body was
  40. * stored on disk, and the message body.
  41. *
  42. * If stored on disk, the body string contains the path and the file must be
  43. * moved or deleted by the receiver when it's done. If an error occurs, the
  44. * response code will be negative and the body will be the error message.
  45. *
  46. * All headers in the returned headers map will have their header names
  47. * converted to lower case, e.g. "content-type".
  48. *
  49. * Currently only the "http" transport is guaranteed to be supported on all
  50. * platforms.
  51. */
  52. class HttpClient
  53. {
  54. public:
  55. friend class HttpClient_Private_Request;
  56. typedef void * Request;
  57. HttpClient();
  58. ~HttpClient();
  59. /**
  60. * Empty map for convenience use
  61. */
  62. static const std::map<std::string,std::string> NO_HEADERS;
  63. /**
  64. * Request a URL using the GET method
  65. */
  66. inline Request GET(
  67. const std::string &url,
  68. const std::map<std::string,std::string> &headers,
  69. unsigned int timeout,
  70. void (*handler)(void *,int,const std::string &,const std::string &),
  71. void *arg)
  72. {
  73. return _do("GET",url,headers,timeout,handler,arg);
  74. }
  75. /**
  76. * Cancel a request
  77. *
  78. * If the request is not active, this does nothing. This may take some time
  79. * depending on HTTP implementation. It may also not kill instantly, but
  80. * it will prevent the handler function from ever being called and cause the
  81. * request to die silently when complete.
  82. */
  83. void cancel(Request req);
  84. private:
  85. Request _do(
  86. const char *method,
  87. const std::string &url,
  88. const std::map<std::string,std::string> &headers,
  89. unsigned int timeout,
  90. void (*handler)(void *,int,const std::string &,const std::string &),
  91. void *arg);
  92. std::set<Request> _requests;
  93. Mutex _requests_m;
  94. };
  95. } // namespace ZeroTier
  96. #endif