HttpClient.hpp 3.1 KB

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