http_client.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef HTTP_CLIENT_H
  31. #define HTTP_CLIENT_H
  32. #include "core/crypto/crypto.h"
  33. #include "core/io/ip.h"
  34. #include "core/io/stream_peer.h"
  35. #include "core/io/stream_peer_tcp.h"
  36. #include "core/object/ref_counted.h"
  37. class HTTPClient : public RefCounted {
  38. GDCLASS(HTTPClient, RefCounted);
  39. public:
  40. enum ResponseCode {
  41. // 1xx informational
  42. RESPONSE_CONTINUE = 100,
  43. RESPONSE_SWITCHING_PROTOCOLS = 101,
  44. RESPONSE_PROCESSING = 102,
  45. // 2xx successful
  46. RESPONSE_OK = 200,
  47. RESPONSE_CREATED = 201,
  48. RESPONSE_ACCEPTED = 202,
  49. RESPONSE_NON_AUTHORITATIVE_INFORMATION = 203,
  50. RESPONSE_NO_CONTENT = 204,
  51. RESPONSE_RESET_CONTENT = 205,
  52. RESPONSE_PARTIAL_CONTENT = 206,
  53. RESPONSE_MULTI_STATUS = 207,
  54. RESPONSE_ALREADY_REPORTED = 208,
  55. RESPONSE_IM_USED = 226,
  56. // 3xx redirection
  57. RESPONSE_MULTIPLE_CHOICES = 300,
  58. RESPONSE_MOVED_PERMANENTLY = 301,
  59. RESPONSE_FOUND = 302,
  60. RESPONSE_SEE_OTHER = 303,
  61. RESPONSE_NOT_MODIFIED = 304,
  62. RESPONSE_USE_PROXY = 305,
  63. RESPONSE_SWITCH_PROXY = 306,
  64. RESPONSE_TEMPORARY_REDIRECT = 307,
  65. RESPONSE_PERMANENT_REDIRECT = 308,
  66. // 4xx client error
  67. RESPONSE_BAD_REQUEST = 400,
  68. RESPONSE_UNAUTHORIZED = 401,
  69. RESPONSE_PAYMENT_REQUIRED = 402,
  70. RESPONSE_FORBIDDEN = 403,
  71. RESPONSE_NOT_FOUND = 404,
  72. RESPONSE_METHOD_NOT_ALLOWED = 405,
  73. RESPONSE_NOT_ACCEPTABLE = 406,
  74. RESPONSE_PROXY_AUTHENTICATION_REQUIRED = 407,
  75. RESPONSE_REQUEST_TIMEOUT = 408,
  76. RESPONSE_CONFLICT = 409,
  77. RESPONSE_GONE = 410,
  78. RESPONSE_LENGTH_REQUIRED = 411,
  79. RESPONSE_PRECONDITION_FAILED = 412,
  80. RESPONSE_REQUEST_ENTITY_TOO_LARGE = 413,
  81. RESPONSE_REQUEST_URI_TOO_LONG = 414,
  82. RESPONSE_UNSUPPORTED_MEDIA_TYPE = 415,
  83. RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
  84. RESPONSE_EXPECTATION_FAILED = 417,
  85. RESPONSE_IM_A_TEAPOT = 418,
  86. RESPONSE_MISDIRECTED_REQUEST = 421,
  87. RESPONSE_UNPROCESSABLE_ENTITY = 422,
  88. RESPONSE_LOCKED = 423,
  89. RESPONSE_FAILED_DEPENDENCY = 424,
  90. RESPONSE_UPGRADE_REQUIRED = 426,
  91. RESPONSE_PRECONDITION_REQUIRED = 428,
  92. RESPONSE_TOO_MANY_REQUESTS = 429,
  93. RESPONSE_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
  94. RESPONSE_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
  95. // 5xx server error
  96. RESPONSE_INTERNAL_SERVER_ERROR = 500,
  97. RESPONSE_NOT_IMPLEMENTED = 501,
  98. RESPONSE_BAD_GATEWAY = 502,
  99. RESPONSE_SERVICE_UNAVAILABLE = 503,
  100. RESPONSE_GATEWAY_TIMEOUT = 504,
  101. RESPONSE_HTTP_VERSION_NOT_SUPPORTED = 505,
  102. RESPONSE_VARIANT_ALSO_NEGOTIATES = 506,
  103. RESPONSE_INSUFFICIENT_STORAGE = 507,
  104. RESPONSE_LOOP_DETECTED = 508,
  105. RESPONSE_NOT_EXTENDED = 510,
  106. RESPONSE_NETWORK_AUTH_REQUIRED = 511,
  107. };
  108. enum Method {
  109. METHOD_GET,
  110. METHOD_HEAD,
  111. METHOD_POST,
  112. METHOD_PUT,
  113. METHOD_DELETE,
  114. METHOD_OPTIONS,
  115. METHOD_TRACE,
  116. METHOD_CONNECT,
  117. METHOD_PATCH,
  118. METHOD_MAX
  119. };
  120. enum Status {
  121. STATUS_DISCONNECTED,
  122. STATUS_RESOLVING, // Resolving hostname (if passed a hostname)
  123. STATUS_CANT_RESOLVE,
  124. STATUS_CONNECTING, // Connecting to IP
  125. STATUS_CANT_CONNECT,
  126. STATUS_CONNECTED, // Connected, requests can be made
  127. STATUS_REQUESTING, // Request in progress
  128. STATUS_BODY, // Request resulted in body, which must be read
  129. STATUS_CONNECTION_ERROR,
  130. STATUS_TLS_HANDSHAKE_ERROR,
  131. };
  132. protected:
  133. static const char *_methods[METHOD_MAX];
  134. static const int HOST_MIN_LEN = 4;
  135. enum Port {
  136. PORT_HTTP = 80,
  137. PORT_HTTPS = 443,
  138. };
  139. PackedStringArray _get_response_headers();
  140. Dictionary _get_response_headers_as_dictionary();
  141. Error _request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const Vector<uint8_t> &p_body);
  142. Error _request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body = String());
  143. static HTTPClient *(*_create)();
  144. static void _bind_methods();
  145. public:
  146. static HTTPClient *create();
  147. String query_string_from_dict(const Dictionary &p_dict);
  148. Error verify_headers(const Vector<String> &p_headers);
  149. 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;
  150. virtual Error connect_to_host(const String &p_host, int p_port = -1, Ref<TLSOptions> p_tls_options = Ref<TLSOptions>()) = 0;
  151. virtual void set_connection(const Ref<StreamPeer> &p_connection) = 0;
  152. virtual Ref<StreamPeer> get_connection() const = 0;
  153. virtual void close() = 0;
  154. virtual Status get_status() const = 0;
  155. virtual bool has_response() const = 0;
  156. virtual bool is_response_chunked() const = 0;
  157. virtual int get_response_code() const = 0;
  158. virtual Error get_response_headers(List<String> *r_response) = 0;
  159. virtual int64_t get_response_body_length() const = 0;
  160. virtual PackedByteArray read_response_body_chunk() = 0; // Can't get body as partial text because of most encodings UTF8, gzip, etc.
  161. virtual void set_blocking_mode(bool p_enable) = 0; // Useful mostly if running in a thread
  162. virtual bool is_blocking_mode_enabled() const = 0;
  163. virtual void set_read_chunk_size(int p_size) = 0;
  164. virtual int get_read_chunk_size() const = 0;
  165. virtual Error poll() = 0;
  166. // Use empty string or -1 to unset
  167. virtual void set_http_proxy(const String &p_host, int p_port);
  168. virtual void set_https_proxy(const String &p_host, int p_port);
  169. HTTPClient() {}
  170. virtual ~HTTPClient() {}
  171. };
  172. VARIANT_ENUM_CAST(HTTPClient::ResponseCode)
  173. VARIANT_ENUM_CAST(HTTPClient::Method);
  174. VARIANT_ENUM_CAST(HTTPClient::Status);
  175. #endif // HTTP_CLIENT_H