Http.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_HTTP_HPP
  28. #define _ZT_HTTP_HPP
  29. #include <map>
  30. #include <string>
  31. #include <stdexcept>
  32. #include "Thread.hpp"
  33. #include "../ext/http-parser/http_parser.h"
  34. namespace ZeroTier {
  35. class Http
  36. {
  37. public:
  38. /**
  39. * HTTP request methods
  40. */
  41. enum Method
  42. {
  43. HTTP_METHOD_GET,
  44. HTTP_METHOD_HEAD
  45. };
  46. /**
  47. * An empty headers map for convenience
  48. */
  49. static const std::map<std::string,std::string> EMPTY_HEADERS;
  50. /**
  51. * HTTP request
  52. */
  53. class Request : protected Thread
  54. {
  55. public:
  56. /**
  57. * Create and issue an HTTP request
  58. *
  59. * The supplied handler is called when the request is
  60. * complete or if an error occurs. A code of zero indicates
  61. * that the server could not be reached, and a description
  62. * of the error will be in 'body'. If the handler returns
  63. * false the Request object deletes itself. Otherwise the
  64. * object must be deleted by other code.
  65. *
  66. * @param m Request method
  67. * @param url Destination URL
  68. * @param rh Request headers
  69. * @param rb Request body or empty string for none (currently unused)
  70. * @param handler Request handler function
  71. * @param arg First argument to request handler
  72. */
  73. Request(
  74. Http::Method m,
  75. const std::string &url,
  76. const std::map<std::string,std::string> &rh,
  77. const std::string &rb,
  78. bool (*handler)(Request *,void *,const std::string &,int,const std::map<std::string,std::string> &,const std::string &),
  79. void *arg);
  80. /**
  81. * Destruction cancels any in-progress request
  82. */
  83. virtual ~Request();
  84. protected:
  85. virtual void main()
  86. throw();
  87. private:
  88. // HTTP parser handlers
  89. static int _http_on_message_begin(http_parser *parser);
  90. static int _http_on_url(http_parser *parser,const char *data,size_t length);
  91. static int _http_on_status_complete(http_parser *parser);
  92. static int _http_on_header_field(http_parser *parser,const char *data,size_t length);
  93. static int _http_on_header_value(http_parser *parser,const char *data,size_t length);
  94. static int _http_on_headers_complete(http_parser *parser);
  95. static int _http_on_body(http_parser *parser,const char *data,size_t length);
  96. static int _http_on_message_complete(http_parser *parser);
  97. http_parser _parser;
  98. std::string _url;
  99. std::map<std::string,std::string> _requestHeaders;
  100. std::map<std::string,std::string> _responseHeaders;
  101. std::string _currentHeaderField;
  102. std::string _responseBody;
  103. bool (*_handler)(Request *,void *,const std::string &,int,const std::map<std::string,std::string> &,const std::string &);
  104. void *_arg;
  105. Http::Method _method;
  106. int _responseStatusCode;
  107. bool _messageComplete;
  108. volatile int _fd;
  109. };
  110. };
  111. } // namespace ZeroTier
  112. #endif