Debian Codegraph @

core/io/http_client.h

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