HttpClient.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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 "Constants.hpp"
  32. namespace ZeroTier {
  33. /**
  34. * HTTP client that does queries in the background
  35. *
  36. * The handler method takes the following arguments: an arbitrary pointer, an
  37. * HTTP response code, the URL queried, whether or not the message body was
  38. * stored on disk, and the message body.
  39. *
  40. * If stored on disk, the body string contains the path and the file must be
  41. * moved or deleted by the receiver when it's done. If an error occurs, the
  42. * response code will be negative and the body will be the error message.
  43. *
  44. * All headers in the returned headers map will have their header names
  45. * converted to lower case, e.g. "content-type".
  46. *
  47. * Currently only the "http" transport is guaranteed to be supported on all
  48. * platforms.
  49. */
  50. class HttpClient
  51. {
  52. public:
  53. typedef void * Request;
  54. /**
  55. * Empty map for convenience use
  56. */
  57. static const std::map<std::string,std::string> NO_HEADERS;
  58. /**
  59. * Request a URL using the GET method
  60. */
  61. static inline Request GET(
  62. const std::string &url,
  63. const std::map<std::string,std::string> &headers,
  64. unsigned int timeout,
  65. void (*handler)(void *,int,const std::string &,bool,const std::string &),
  66. void *arg)
  67. {
  68. return _do("GET",url,headers,timeout,handler,arg);
  69. }
  70. private:
  71. static Request _do(
  72. const char *method,
  73. const std::string &url,
  74. const std::map<std::string,std::string> &headers,
  75. unsigned int timeout,
  76. void (*handler)(void *,int,const std::string &,bool,const std::string &),
  77. void *arg);
  78. };
  79. } // namespace ZeroTier
  80. #endif