Debian Codegraph @

core/io/http_client.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* http_client.cpp */
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 #include "http_client.h"
32
33 const char *HTTPClient::_methods[METHOD_MAX] = {
34 "GET",
35 "HEAD",
36 "POST",
37 "PUT",
38 "DELETE",
39 "OPTIONS",
40 "TRACE",
41 "CONNECT",
42 "PATCH"
43 };
44
45 HTTPClient *HTTPClient::create(bool p_notify_postinitialize) {
46 if (_create) {
47 return _create(p_notify_postinitialize);
48 }
49 return nullptr;
50 }
51
52 void HTTPClient::set_http_proxy(const String &p_host, int p_port) {
53 WARN_PRINT("HTTP proxy feature is not available");
54 }
55
56 void HTTPClient::set_https_proxy(const String &p_host, int p_port) {
57 WARN_PRINT("HTTPS proxy feature is not available");
58 }
59
60 Error HTTPClient::_request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const Vector<uint8_t> &p_body) {
61 int size = p_body.size();
62 return request(p_method, p_url, p_headers, size > 0 ? p_body.ptr() : nullptr, size);
63 }
64
65 Error HTTPClient::_request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body) {
66 CharString body_utf8 = p_body.utf8();
67 int size = body_utf8.length();
68 return request(p_method, p_url, p_headers, size > 0 ? (const uint8_t *)body_utf8.get_data() : nullptr, size);
69 }
70
71 String HTTPClient::query_string_from_dict(const Dictionary &p_dict) {
72 String query = "";
73 for (const KeyValue<Variant, Variant> &kv : p_dict) {
74 String encoded_key = String(kv.key).uri_encode();
75 const Variant &value = kv.value;
76 switch (value.get_type()) {
77 case Variant::ARRAY: {
78 // Repeat the key with every values
79 Array values = value;
80 for (int j = 0; j < values.size(); ++j) {
81 query += "&" + encoded_key + "=" + String(values[j]).uri_encode();
82 }
83 break;
84 }
85 case Variant::NIL: {
86 // Add the key with no value
87 query += "&" + encoded_key;
88 break;
89 }
90 default: {
91 // Add the key-value pair
92 query += "&" + encoded_key + "=" + String(value).uri_encode();
93 }
94 }
95 }
96 return query.substr(1);
97 }
98
99 Error HTTPClient::verify_headers(const Vector<String> &p_headers) {
100 for (int i = 0; i < p_headers.size(); i++) {
101 String sanitized = p_headers[i].strip_edges();
102 ERR_FAIL_COND_V_MSG(sanitized.is_empty(), ERR_INVALID_PARAMETER, vformat("Invalid HTTP header at index %d: empty.", i));
103 ERR_FAIL_COND_V_MSG(sanitized.find_char(':') < 1, ERR_INVALID_PARAMETER,
104 vformat("Invalid HTTP header at index %d: String must contain header-value pair, delimited by ':', but was: '%s'.", i, p_headers[i]));
105 }
106
107 return OK;
108 }
109
110 Dictionary HTTPClient::_get_response_headers_as_dictionary() {
111 List<String> rh;
112 get_response_headers(&rh);
113 Dictionary ret;
114 for (const String &s : rh) {
115 int sp = s.find_char(':');
116 if (sp == -1) {
117 continue;
118 }
119 String key = s.substr(0, sp).strip_edges();
120 String value = s.substr(sp + 1).strip_edges();
121 ret[key] = value;
122 }
123
124 return ret;
125 }
126
127 PackedStringArray HTTPClient::_get_response_headers() {
128 List<String> rh;
129 get_response_headers(&rh);
130 PackedStringArray ret;
131 ret.resize(rh.size());
132 int idx = 0;
133 for (const String &E : rh) {
134 ret.set(idx++, E);
135 }
136
137 return ret;
138 }
139
140 void HTTPClient::_bind_methods() {
141 ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port", "tls_options"), &HTTPClient::connect_to_host, DEFVAL(-1), DEFVAL(Ref<TLSOptions>()));
142 ClassDB::bind_method(D_METHOD("set_connection", "connection"), &HTTPClient::set_connection);
143 ClassDB::bind_method(D_METHOD("get_connection"), &HTTPClient::get_connection);
144 ClassDB::bind_method(D_METHOD("request_raw", "method", "url", "headers", "body"), &HTTPClient::_request_raw);
145 ClassDB::bind_method(D_METHOD("request", "method", "url", "headers", "body"), &HTTPClient::_request, DEFVAL(String()));
146 ClassDB::bind_method(D_METHOD("close"), &HTTPClient::close);
147
148 ClassDB::bind_method(D_METHOD("has_response"), &HTTPClient::has_response);
149 ClassDB::bind_method(D_METHOD("is_response_chunked"), &HTTPClient::is_response_chunked);
150 ClassDB::bind_method(D_METHOD("get_response_code"), &HTTPClient::get_response_code);
151 ClassDB::bind_method(D_METHOD("get_response_headers"), &HTTPClient::_get_response_headers);
152 ClassDB::bind_method(D_METHOD("get_response_headers_as_dictionary"), &HTTPClient::_get_response_headers_as_dictionary);
153 ClassDB::bind_method(D_METHOD("get_response_body_length"), &HTTPClient::get_response_body_length);
154 ClassDB::bind_method(D_METHOD("read_response_body_chunk"), &HTTPClient::read_response_body_chunk);
155 ClassDB::bind_method(D_METHOD("set_read_chunk_size", "bytes"), &HTTPClient::set_read_chunk_size);
156 ClassDB::bind_method(D_METHOD("get_read_chunk_size"), &HTTPClient::get_read_chunk_size);
157
158 ClassDB::bind_method(D_METHOD("set_blocking_mode", "enabled"), &HTTPClient::set_blocking_mode);
159 ClassDB::bind_method(D_METHOD("is_blocking_mode_enabled"), &HTTPClient::is_blocking_mode_enabled);
160
161 ClassDB::bind_method(D_METHOD("get_status"), &HTTPClient::get_status);
162 ClassDB::bind_method(D_METHOD("poll"), &HTTPClient::poll);
163
164 ClassDB::bind_method(D_METHOD("set_http_proxy", "host", "port"), &HTTPClient::set_http_proxy);
165 ClassDB::bind_method(D_METHOD("set_https_proxy", "host", "port"), &HTTPClient::set_https_proxy);
166
167 ClassDB::bind_method(D_METHOD("query_string_from_dict", "fields"), &HTTPClient::query_string_from_dict);
168
169 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_mode_enabled"), "set_blocking_mode", "is_blocking_mode_enabled");
170 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "connection", PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer", PROPERTY_USAGE_NONE), "set_connection", "get_connection");
171 ADD_PROPERTY(PropertyInfo(Variant::INT, "read_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_read_chunk_size", "get_read_chunk_size");
172
173 BIND_ENUM_CONSTANT(METHOD_GET);
174 BIND_ENUM_CONSTANT(METHOD_HEAD);
175 BIND_ENUM_CONSTANT(METHOD_POST);
176 BIND_ENUM_CONSTANT(METHOD_PUT);
177 BIND_ENUM_CONSTANT(METHOD_DELETE);
178 BIND_ENUM_CONSTANT(METHOD_OPTIONS);
179 BIND_ENUM_CONSTANT(METHOD_TRACE);
180 BIND_ENUM_CONSTANT(METHOD_CONNECT);
181 BIND_ENUM_CONSTANT(METHOD_PATCH);
182 BIND_ENUM_CONSTANT(METHOD_MAX);
183
184 BIND_ENUM_CONSTANT(STATUS_DISCONNECTED);
185 BIND_ENUM_CONSTANT(STATUS_RESOLVING); // Resolving hostname (if hostname was passed in)
186 BIND_ENUM_CONSTANT(STATUS_CANT_RESOLVE);
187 BIND_ENUM_CONSTANT(STATUS_CONNECTING); // Connecting to IP
188 BIND_ENUM_CONSTANT(STATUS_CANT_CONNECT);
189 BIND_ENUM_CONSTANT(STATUS_CONNECTED); // Connected, now accepting requests
190 BIND_ENUM_CONSTANT(STATUS_REQUESTING); // Request in progress
191 BIND_ENUM_CONSTANT(STATUS_BODY); // Request resulted in body which must be read
192 BIND_ENUM_CONSTANT(STATUS_CONNECTION_ERROR);
193 BIND_ENUM_CONSTANT(STATUS_TLS_HANDSHAKE_ERROR);
194
195 BIND_ENUM_CONSTANT(RESPONSE_CONTINUE);
196 BIND_ENUM_CONSTANT(RESPONSE_SWITCHING_PROTOCOLS);
197 BIND_ENUM_CONSTANT(RESPONSE_PROCESSING);
198
199 // 2xx successful
200 BIND_ENUM_CONSTANT(RESPONSE_OK);
201 BIND_ENUM_CONSTANT(RESPONSE_CREATED);
202 BIND_ENUM_CONSTANT(RESPONSE_ACCEPTED);
203 BIND_ENUM_CONSTANT(RESPONSE_NON_AUTHORITATIVE_INFORMATION);
204 BIND_ENUM_CONSTANT(RESPONSE_NO_CONTENT);
205 BIND_ENUM_CONSTANT(RESPONSE_RESET_CONTENT);
206 BIND_ENUM_CONSTANT(RESPONSE_PARTIAL_CONTENT);
207 BIND_ENUM_CONSTANT(RESPONSE_MULTI_STATUS);
208 BIND_ENUM_CONSTANT(RESPONSE_ALREADY_REPORTED);
209 BIND_ENUM_CONSTANT(RESPONSE_IM_USED);
210
211 // 3xx redirection
212 BIND_ENUM_CONSTANT(RESPONSE_MULTIPLE_CHOICES);
213 BIND_ENUM_CONSTANT(RESPONSE_MOVED_PERMANENTLY);
214 BIND_ENUM_CONSTANT(RESPONSE_FOUND);
215 BIND_ENUM_CONSTANT(RESPONSE_SEE_OTHER);
216 BIND_ENUM_CONSTANT(RESPONSE_NOT_MODIFIED);
217 BIND_ENUM_CONSTANT(RESPONSE_USE_PROXY);
218 BIND_ENUM_CONSTANT(RESPONSE_SWITCH_PROXY);
219 BIND_ENUM_CONSTANT(RESPONSE_TEMPORARY_REDIRECT);
220 BIND_ENUM_CONSTANT(RESPONSE_PERMANENT_REDIRECT);
221
222 // 4xx client error
223 BIND_ENUM_CONSTANT(RESPONSE_BAD_REQUEST);
224 BIND_ENUM_CONSTANT(RESPONSE_UNAUTHORIZED);
225 BIND_ENUM_CONSTANT(RESPONSE_PAYMENT_REQUIRED);
226 BIND_ENUM_CONSTANT(RESPONSE_FORBIDDEN);
227 BIND_ENUM_CONSTANT(RESPONSE_NOT_FOUND);
228 BIND_ENUM_CONSTANT(RESPONSE_METHOD_NOT_ALLOWED);
229 BIND_ENUM_CONSTANT(RESPONSE_NOT_ACCEPTABLE);
230 BIND_ENUM_CONSTANT(RESPONSE_PROXY_AUTHENTICATION_REQUIRED);
231 BIND_ENUM_CONSTANT(RESPONSE_REQUEST_TIMEOUT);
232 BIND_ENUM_CONSTANT(RESPONSE_CONFLICT);
233 BIND_ENUM_CONSTANT(RESPONSE_GONE);
234 BIND_ENUM_CONSTANT(RESPONSE_LENGTH_REQUIRED);
235 BIND_ENUM_CONSTANT(RESPONSE_PRECONDITION_FAILED);
236 BIND_ENUM_CONSTANT(RESPONSE_REQUEST_ENTITY_TOO_LARGE);
237 BIND_ENUM_CONSTANT(RESPONSE_REQUEST_URI_TOO_LONG);
238 BIND_ENUM_CONSTANT(RESPONSE_UNSUPPORTED_MEDIA_TYPE);
239 BIND_ENUM_CONSTANT(RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE);
240 BIND_ENUM_CONSTANT(RESPONSE_EXPECTATION_FAILED);
241 BIND_ENUM_CONSTANT(RESPONSE_IM_A_TEAPOT);
242 BIND_ENUM_CONSTANT(RESPONSE_MISDIRECTED_REQUEST);
243 BIND_ENUM_CONSTANT(RESPONSE_UNPROCESSABLE_ENTITY);
244 BIND_ENUM_CONSTANT(RESPONSE_LOCKED);
245 BIND_ENUM_CONSTANT(RESPONSE_FAILED_DEPENDENCY);
246 BIND_ENUM_CONSTANT(RESPONSE_UPGRADE_REQUIRED);
247 BIND_ENUM_CONSTANT(RESPONSE_PRECONDITION_REQUIRED);
248 BIND_ENUM_CONSTANT(RESPONSE_TOO_MANY_REQUESTS);
249 BIND_ENUM_CONSTANT(RESPONSE_REQUEST_HEADER_FIELDS_TOO_LARGE);
250 BIND_ENUM_CONSTANT(RESPONSE_UNAVAILABLE_FOR_LEGAL_REASONS);
251
252 // 5xx server error
253 BIND_ENUM_CONSTANT(RESPONSE_INTERNAL_SERVER_ERROR);
254 BIND_ENUM_CONSTANT(RESPONSE_NOT_IMPLEMENTED);
255 BIND_ENUM_CONSTANT(RESPONSE_BAD_GATEWAY);
256 BIND_ENUM_CONSTANT(RESPONSE_SERVICE_UNAVAILABLE);
257 BIND_ENUM_CONSTANT(RESPONSE_GATEWAY_TIMEOUT);
258 BIND_ENUM_CONSTANT(RESPONSE_HTTP_VERSION_NOT_SUPPORTED);
259 BIND_ENUM_CONSTANT(RESPONSE_VARIANT_ALSO_NEGOTIATES);
260 BIND_ENUM_CONSTANT(RESPONSE_INSUFFICIENT_STORAGE);
261 BIND_ENUM_CONSTANT(RESPONSE_LOOP_DETECTED);
262 BIND_ENUM_CONSTANT(RESPONSE_NOT_EXTENDED);
263 BIND_ENUM_CONSTANT(RESPONSE_NETWORK_AUTH_REQUIRED);
264 }