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