http_client.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************/
  2. /* http_client.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef HTTP_CLIENT_H
  31. #define HTTP_CLIENT_H
  32. #include "core/io/ip.h"
  33. #include "core/io/stream_peer.h"
  34. #include "core/io/stream_peer_tcp.h"
  35. #include "core/reference.h"
  36. class HTTPClient : public Reference {
  37. GDCLASS(HTTPClient, Reference);
  38. public:
  39. enum ResponseCode {
  40. // 1xx informational
  41. RESPONSE_CONTINUE = 100,
  42. RESPONSE_SWITCHING_PROTOCOLS = 101,
  43. RESPONSE_PROCESSING = 102,
  44. // 2xx successful
  45. RESPONSE_OK = 200,
  46. RESPONSE_CREATED = 201,
  47. RESPONSE_ACCEPTED = 202,
  48. RESPONSE_NON_AUTHORITATIVE_INFORMATION = 203,
  49. RESPONSE_NO_CONTENT = 204,
  50. RESPONSE_RESET_CONTENT = 205,
  51. RESPONSE_PARTIAL_CONTENT = 206,
  52. RESPONSE_MULTI_STATUS = 207,
  53. RESPONSE_ALREADY_REPORTED = 208,
  54. RESPONSE_IM_USED = 226,
  55. // 3xx redirection
  56. RESPONSE_MULTIPLE_CHOICES = 300,
  57. RESPONSE_MOVED_PERMANENTLY = 301,
  58. RESPONSE_FOUND = 302,
  59. RESPONSE_SEE_OTHER = 303,
  60. RESPONSE_NOT_MODIFIED = 304,
  61. RESPONSE_USE_PROXY = 305,
  62. RESPONSE_SWITCH_PROXY = 306,
  63. RESPONSE_TEMPORARY_REDIRECT = 307,
  64. RESPONSE_PERMANENT_REDIRECT = 308,
  65. // 4xx client error
  66. RESPONSE_BAD_REQUEST = 400,
  67. RESPONSE_UNAUTHORIZED = 401,
  68. RESPONSE_PAYMENT_REQUIRED = 402,
  69. RESPONSE_FORBIDDEN = 403,
  70. RESPONSE_NOT_FOUND = 404,
  71. RESPONSE_METHOD_NOT_ALLOWED = 405,
  72. RESPONSE_NOT_ACCEPTABLE = 406,
  73. RESPONSE_PROXY_AUTHENTICATION_REQUIRED = 407,
  74. RESPONSE_REQUEST_TIMEOUT = 408,
  75. RESPONSE_CONFLICT = 409,
  76. RESPONSE_GONE = 410,
  77. RESPONSE_LENGTH_REQUIRED = 411,
  78. RESPONSE_PRECONDITION_FAILED = 412,
  79. RESPONSE_REQUEST_ENTITY_TOO_LARGE = 413,
  80. RESPONSE_REQUEST_URI_TOO_LONG = 414,
  81. RESPONSE_UNSUPPORTED_MEDIA_TYPE = 415,
  82. RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
  83. RESPONSE_EXPECTATION_FAILED = 417,
  84. RESPONSE_IM_A_TEAPOT = 418,
  85. RESPONSE_MISDIRECTED_REQUEST = 421,
  86. RESPONSE_UNPROCESSABLE_ENTITY = 422,
  87. RESPONSE_LOCKED = 423,
  88. RESPONSE_FAILED_DEPENDENCY = 424,
  89. RESPONSE_UPGRADE_REQUIRED = 426,
  90. RESPONSE_PRECONDITION_REQUIRED = 428,
  91. RESPONSE_TOO_MANY_REQUESTS = 429,
  92. RESPONSE_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
  93. RESPONSE_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
  94. // 5xx server error
  95. RESPONSE_INTERNAL_SERVER_ERROR = 500,
  96. RESPONSE_NOT_IMPLEMENTED = 501,
  97. RESPONSE_BAD_GATEWAY = 502,
  98. RESPONSE_SERVICE_UNAVAILABLE = 503,
  99. RESPONSE_GATEWAY_TIMEOUT = 504,
  100. RESPONSE_HTTP_VERSION_NOT_SUPPORTED = 505,
  101. RESPONSE_VARIANT_ALSO_NEGOTIATES = 506,
  102. RESPONSE_INSUFFICIENT_STORAGE = 507,
  103. RESPONSE_LOOP_DETECTED = 508,
  104. RESPONSE_NOT_EXTENDED = 510,
  105. RESPONSE_NETWORK_AUTH_REQUIRED = 511,
  106. };
  107. enum Method {
  108. METHOD_GET,
  109. METHOD_HEAD,
  110. METHOD_POST,
  111. METHOD_PUT,
  112. METHOD_DELETE,
  113. METHOD_OPTIONS,
  114. METHOD_TRACE,
  115. METHOD_CONNECT,
  116. METHOD_PATCH,
  117. METHOD_MAX
  118. };
  119. enum Status {
  120. STATUS_DISCONNECTED,
  121. STATUS_RESOLVING, // Resolving hostname (if passed a hostname)
  122. STATUS_CANT_RESOLVE,
  123. STATUS_CONNECTING, // Connecting to IP
  124. STATUS_CANT_CONNECT,
  125. STATUS_CONNECTED, // Connected, requests can be made
  126. STATUS_REQUESTING, // Request in progress
  127. STATUS_BODY, // Request resulted in body, which must be read
  128. STATUS_CONNECTION_ERROR,
  129. STATUS_SSL_HANDSHAKE_ERROR,
  130. };
  131. private:
  132. static const char *_methods[METHOD_MAX];
  133. static const int HOST_MIN_LEN = 4;
  134. enum Port {
  135. PORT_HTTP = 80,
  136. PORT_HTTPS = 443,
  137. };
  138. #ifndef JAVASCRIPT_ENABLED
  139. Status status;
  140. IP::ResolverID resolving;
  141. int conn_port;
  142. String conn_host;
  143. bool ssl;
  144. bool ssl_verify_host;
  145. bool blocking;
  146. bool handshaking;
  147. Vector<uint8_t> response_str;
  148. bool chunked;
  149. Vector<uint8_t> chunk;
  150. int chunk_left;
  151. int body_size;
  152. int body_left;
  153. bool read_until_eof;
  154. Ref<StreamPeerTCP> tcp_connection;
  155. Ref<StreamPeer> connection;
  156. int response_num;
  157. Vector<String> response_headers;
  158. int read_chunk_size;
  159. Error _get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received);
  160. #else
  161. #include "platform/javascript/http_client.h.inc"
  162. #endif
  163. PoolStringArray _get_response_headers();
  164. Dictionary _get_response_headers_as_dictionary();
  165. static void _bind_methods();
  166. public:
  167. Error connect_to_host(const String &p_host, int p_port = -1, bool p_ssl = false, bool p_verify_host = true);
  168. void set_connection(const Ref<StreamPeer> &p_connection);
  169. Ref<StreamPeer> get_connection() const;
  170. Error request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const PoolVector<uint8_t> &p_body);
  171. Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body = String());
  172. void close();
  173. Status get_status() const;
  174. bool has_response() const;
  175. bool is_response_chunked() const;
  176. int get_response_code() const;
  177. Error get_response_headers(List<String> *r_response);
  178. int get_response_body_length() const;
  179. PoolByteArray read_response_body_chunk(); // Can't get body as partial text because of most encodings UTF8, gzip, etc.
  180. void set_blocking_mode(bool p_enable); // Useful mostly if running in a thread
  181. bool is_blocking_mode_enabled() const;
  182. void set_read_chunk_size(int p_size);
  183. Error poll();
  184. String query_string_from_dict(const Dictionary &p_dict);
  185. HTTPClient();
  186. ~HTTPClient();
  187. };
  188. VARIANT_ENUM_CAST(HTTPClient::ResponseCode)
  189. VARIANT_ENUM_CAST(HTTPClient::Method);
  190. VARIANT_ENUM_CAST(HTTPClient::Status);
  191. #endif // HTTP_CLIENT_H