http_client.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************/
  2. /* http_client.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #pragma once
  31. #include "core/crypto/crypto.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/object/ref_counted.h"
  36. class HTTPClient : public RefCounted {
  37. GDCLASS(HTTPClient, RefCounted);
  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_TLS_HANDSHAKE_ERROR,
  130. };
  131. protected:
  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. PackedStringArray _get_response_headers();
  139. Dictionary _get_response_headers_as_dictionary();
  140. Error _request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const Vector<uint8_t> &p_body);
  141. Error _request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body = String());
  142. static HTTPClient *(*_create)(bool p_notify_postinitialize);
  143. static void _bind_methods();
  144. public:
  145. static HTTPClient *create(bool p_notify_postinitialize = true);
  146. String query_string_from_dict(const Dictionary &p_dict);
  147. Error verify_headers(const Vector<String> &p_headers);
  148. virtual Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) = 0;
  149. virtual Error connect_to_host(const String &p_host, int p_port = -1, Ref<TLSOptions> p_tls_options = Ref<TLSOptions>()) = 0;
  150. virtual void set_connection(const Ref<StreamPeer> &p_connection) = 0;
  151. virtual Ref<StreamPeer> get_connection() const = 0;
  152. virtual void close() = 0;
  153. virtual Status get_status() const = 0;
  154. virtual bool has_response() const = 0;
  155. virtual bool is_response_chunked() const = 0;
  156. virtual int get_response_code() const = 0;
  157. virtual Error get_response_headers(List<String> *r_response) = 0;
  158. virtual int64_t get_response_body_length() const = 0;
  159. virtual PackedByteArray read_response_body_chunk() = 0; // Can't get body as partial text because of most encodings UTF8, gzip, etc.
  160. virtual void set_blocking_mode(bool p_enable) = 0; // Useful mostly if running in a thread
  161. virtual bool is_blocking_mode_enabled() const = 0;
  162. virtual void set_read_chunk_size(int p_size) = 0;
  163. virtual int get_read_chunk_size() const = 0;
  164. virtual Error poll() = 0;
  165. // Use empty string or -1 to unset
  166. virtual void set_http_proxy(const String &p_host, int p_port);
  167. virtual void set_https_proxy(const String &p_host, int p_port);
  168. HTTPClient() {}
  169. virtual ~HTTPClient() {}
  170. };
  171. VARIANT_ENUM_CAST(HTTPClient::ResponseCode)
  172. VARIANT_ENUM_CAST(HTTPClient::Method);
  173. VARIANT_ENUM_CAST(HTTPClient::Status);