1 /*************************************************************************/
2 /* http_request.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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_request.h"
32
_redirect_request(const String & p_new_url)33 void HTTPRequest::_redirect_request(const String &p_new_url) {
34 }
35
_request()36 Error HTTPRequest::_request() {
37
38 return client->connect_to_host(url, port, use_ssl, validate_ssl);
39 }
40
_parse_url(const String & p_url)41 Error HTTPRequest::_parse_url(const String &p_url) {
42
43 url = p_url;
44 use_ssl = false;
45
46 request_string = "";
47 port = 80;
48 request_sent = false;
49 got_response = false;
50 body_len = -1;
51 body.resize(0);
52 downloaded = 0;
53 redirections = 0;
54
55 String url_lower = url.to_lower();
56 if (url_lower.begins_with("http://")) {
57 url = url.substr(7, url.length() - 7);
58 } else if (url_lower.begins_with("https://")) {
59 url = url.substr(8, url.length() - 8);
60 use_ssl = true;
61 port = 443;
62 } else {
63 ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Malformed URL: " + url + ".");
64 }
65
66 ERR_FAIL_COND_V_MSG(url.length() < 1, ERR_INVALID_PARAMETER, "URL too short: " + url + ".");
67
68 int slash_pos = url.find("/");
69
70 if (slash_pos != -1) {
71 request_string = url.substr(slash_pos, url.length());
72 url = url.substr(0, slash_pos);
73 } else {
74 request_string = "/";
75 }
76
77 int colon_pos = url.find(":");
78 if (colon_pos != -1) {
79 port = url.substr(colon_pos + 1, url.length()).to_int();
80 url = url.substr(0, colon_pos);
81 ERR_FAIL_COND_V(port < 1 || port > 65535, ERR_INVALID_PARAMETER);
82 }
83
84 return OK;
85 }
86
request(const String & p_url,const Vector<String> & p_custom_headers,bool p_ssl_validate_domain,HTTPClient::Method p_method,const String & p_request_data)87 Error HTTPRequest::request(const String &p_url, const Vector<String> &p_custom_headers, bool p_ssl_validate_domain, HTTPClient::Method p_method, const String &p_request_data) {
88
89 ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
90 ERR_FAIL_COND_V_MSG(requesting, ERR_BUSY, "HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
91
92 if (timeout > 0) {
93 timer->stop();
94 timer->start(timeout);
95 }
96
97 method = p_method;
98
99 Error err = _parse_url(p_url);
100 if (err)
101 return err;
102
103 validate_ssl = p_ssl_validate_domain;
104
105 headers = p_custom_headers;
106
107 request_data = p_request_data;
108
109 requesting = true;
110
111 if (use_threads) {
112
113 thread_done = false;
114 thread_request_quit = false;
115 client->set_blocking_mode(true);
116 thread = Thread::create(_thread_func, this);
117 } else {
118 client->set_blocking_mode(false);
119 err = _request();
120 if (err != OK) {
121 call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
122 return ERR_CANT_CONNECT;
123 }
124
125 set_process_internal(true);
126 }
127
128 return OK;
129 }
130
_thread_func(void * p_userdata)131 void HTTPRequest::_thread_func(void *p_userdata) {
132
133 HTTPRequest *hr = (HTTPRequest *)p_userdata;
134
135 Error err = hr->_request();
136
137 if (err != OK) {
138 hr->call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
139 } else {
140 while (!hr->thread_request_quit) {
141
142 bool exit = hr->_update_connection();
143 if (exit)
144 break;
145 OS::get_singleton()->delay_usec(1);
146 }
147 }
148
149 hr->thread_done = true;
150 }
151
cancel_request()152 void HTTPRequest::cancel_request() {
153
154 timer->stop();
155
156 if (!requesting)
157 return;
158
159 if (!use_threads) {
160 set_process_internal(false);
161 } else {
162 thread_request_quit = true;
163 Thread::wait_to_finish(thread);
164 memdelete(thread);
165 thread = NULL;
166 }
167
168 if (file) {
169 memdelete(file);
170 file = NULL;
171 }
172 client->close();
173 body.resize(0);
174 got_response = false;
175 response_code = -1;
176 request_sent = false;
177 requesting = false;
178 }
179
_handle_response(bool * ret_value)180 bool HTTPRequest::_handle_response(bool *ret_value) {
181
182 if (!client->has_response()) {
183 call_deferred("_request_done", RESULT_NO_RESPONSE, 0, PoolStringArray(), PoolByteArray());
184 *ret_value = true;
185 return true;
186 }
187
188 got_response = true;
189 response_code = client->get_response_code();
190 List<String> rheaders;
191 client->get_response_headers(&rheaders);
192 response_headers.resize(0);
193 downloaded = 0;
194 for (List<String>::Element *E = rheaders.front(); E; E = E->next()) {
195 response_headers.push_back(E->get());
196 }
197
198 if (response_code == 301 || response_code == 302) {
199 // Handle redirect
200
201 if (max_redirects >= 0 && redirections >= max_redirects) {
202
203 call_deferred("_request_done", RESULT_REDIRECT_LIMIT_REACHED, response_code, response_headers, PoolByteArray());
204 *ret_value = true;
205 return true;
206 }
207
208 String new_request;
209
210 for (List<String>::Element *E = rheaders.front(); E; E = E->next()) {
211 if (E->get().findn("Location: ") != -1) {
212 new_request = E->get().substr(9, E->get().length()).strip_edges();
213 }
214 }
215
216 if (new_request != "") {
217 // Process redirect
218 client->close();
219 int new_redirs = redirections + 1; // Because _request() will clear it
220 Error err;
221 if (new_request.begins_with("http")) {
222 // New url, request all again
223 _parse_url(new_request);
224 } else {
225 request_string = new_request;
226 }
227
228 err = _request();
229 if (err == OK) {
230 request_sent = false;
231 got_response = false;
232 body_len = -1;
233 body.resize(0);
234 downloaded = 0;
235 redirections = new_redirs;
236 *ret_value = false;
237 return true;
238 }
239 }
240 }
241
242 return false;
243 }
244
_update_connection()245 bool HTTPRequest::_update_connection() {
246
247 switch (client->get_status()) {
248 case HTTPClient::STATUS_DISCONNECTED: {
249 call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
250 return true; // End it, since it's doing something
251 } break;
252 case HTTPClient::STATUS_RESOLVING: {
253 client->poll();
254 // Must wait
255 return false;
256 } break;
257 case HTTPClient::STATUS_CANT_RESOLVE: {
258 call_deferred("_request_done", RESULT_CANT_RESOLVE, 0, PoolStringArray(), PoolByteArray());
259 return true;
260
261 } break;
262 case HTTPClient::STATUS_CONNECTING: {
263 client->poll();
264 // Must wait
265 return false;
266 } break; // Connecting to IP
267 case HTTPClient::STATUS_CANT_CONNECT: {
268
269 call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PoolStringArray(), PoolByteArray());
270 return true;
271
272 } break;
273 case HTTPClient::STATUS_CONNECTED: {
274
275 if (request_sent) {
276
277 if (!got_response) {
278
279 // No body
280
281 bool ret_value;
282
283 if (_handle_response(&ret_value))
284 return ret_value;
285
286 call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PoolByteArray());
287 return true;
288 }
289 if (body_len < 0) {
290 // Chunked transfer is done
291 call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
292 return true;
293 }
294
295 call_deferred("_request_done", RESULT_CHUNKED_BODY_SIZE_MISMATCH, response_code, response_headers, PoolByteArray());
296 return true;
297 // Request migh have been done
298 } else {
299 // Did not request yet, do request
300
301 Error err = client->request(method, request_string, headers, request_data);
302 if (err != OK) {
303 call_deferred("_request_done", RESULT_CONNECTION_ERROR, 0, PoolStringArray(), PoolByteArray());
304 return true;
305 }
306
307 request_sent = true;
308 return false;
309 }
310 } break; // Connected: break requests only accepted here
311 case HTTPClient::STATUS_REQUESTING: {
312 // Must wait, still requesting
313 client->poll();
314 return false;
315
316 } break; // Request in progress
317 case HTTPClient::STATUS_BODY: {
318
319 if (!got_response) {
320
321 bool ret_value;
322
323 if (_handle_response(&ret_value))
324 return ret_value;
325
326 if (!client->is_response_chunked() && client->get_response_body_length() == 0) {
327
328 call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PoolByteArray());
329 return true;
330 }
331
332 // No body len (-1) if chunked or no content-length header was provided.
333 // Change your webserver configuration if you want body len.
334 body_len = client->get_response_body_length();
335
336 if (body_size_limit >= 0 && body_len > body_size_limit) {
337 call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PoolByteArray());
338 return true;
339 }
340
341 if (download_to_file != String()) {
342 file = FileAccess::open(download_to_file, FileAccess::WRITE);
343 if (!file) {
344
345 call_deferred("_request_done", RESULT_DOWNLOAD_FILE_CANT_OPEN, response_code, response_headers, PoolByteArray());
346 return true;
347 }
348 }
349 }
350
351 client->poll();
352
353 PoolByteArray chunk = client->read_response_body_chunk();
354 downloaded += chunk.size();
355
356 if (file) {
357 PoolByteArray::Read r = chunk.read();
358 file->store_buffer(r.ptr(), chunk.size());
359 if (file->get_error() != OK) {
360 call_deferred("_request_done", RESULT_DOWNLOAD_FILE_WRITE_ERROR, response_code, response_headers, PoolByteArray());
361 return true;
362 }
363 } else {
364 body.append_array(chunk);
365 }
366
367 if (body_size_limit >= 0 && downloaded > body_size_limit) {
368 call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PoolByteArray());
369 return true;
370 }
371
372 if (body_len >= 0) {
373
374 if (downloaded == body_len) {
375 call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
376 return true;
377 }
378 } else if (client->get_status() == HTTPClient::STATUS_DISCONNECTED) {
379 // We read till EOF, with no errors. Request is done.
380 call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
381 }
382
383 return false;
384
385 } break; // Request resulted in body: break which must be read
386 case HTTPClient::STATUS_CONNECTION_ERROR: {
387 call_deferred("_request_done", RESULT_CONNECTION_ERROR, 0, PoolStringArray(), PoolByteArray());
388 return true;
389 } break;
390 case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR: {
391 call_deferred("_request_done", RESULT_SSL_HANDSHAKE_ERROR, 0, PoolStringArray(), PoolByteArray());
392 return true;
393 } break;
394 }
395
396 ERR_FAIL_V(false);
397 }
398
_request_done(int p_status,int p_code,const PoolStringArray & headers,const PoolByteArray & p_data)399 void HTTPRequest::_request_done(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data) {
400
401 cancel_request();
402 emit_signal("request_completed", p_status, p_code, headers, p_data);
403 }
404
_notification(int p_what)405 void HTTPRequest::_notification(int p_what) {
406
407 if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
408
409 if (use_threads)
410 return;
411 bool done = _update_connection();
412 if (done) {
413
414 set_process_internal(false);
415 // cancel_request(); called from _request done now
416 }
417 }
418
419 if (p_what == NOTIFICATION_EXIT_TREE) {
420 if (requesting) {
421 cancel_request();
422 }
423 }
424 }
425
set_use_threads(bool p_use)426 void HTTPRequest::set_use_threads(bool p_use) {
427
428 ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
429 use_threads = p_use;
430 }
431
is_using_threads() const432 bool HTTPRequest::is_using_threads() const {
433
434 return use_threads;
435 }
436
set_body_size_limit(int p_bytes)437 void HTTPRequest::set_body_size_limit(int p_bytes) {
438
439 ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
440
441 body_size_limit = p_bytes;
442 }
443
get_body_size_limit() const444 int HTTPRequest::get_body_size_limit() const {
445
446 return body_size_limit;
447 }
448
set_download_file(const String & p_file)449 void HTTPRequest::set_download_file(const String &p_file) {
450
451 ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
452
453 download_to_file = p_file;
454 }
455
get_download_file() const456 String HTTPRequest::get_download_file() const {
457
458 return download_to_file;
459 }
460
set_download_chunk_size(int p_chunk_size)461 void HTTPRequest::set_download_chunk_size(int p_chunk_size) {
462
463 ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
464
465 client->set_read_chunk_size(p_chunk_size);
466 }
467
get_download_chunk_size() const468 int HTTPRequest::get_download_chunk_size() const {
469 return client->get_read_chunk_size();
470 }
471
get_http_client_status() const472 HTTPClient::Status HTTPRequest::get_http_client_status() const {
473 return client->get_status();
474 }
475
set_max_redirects(int p_max)476 void HTTPRequest::set_max_redirects(int p_max) {
477
478 max_redirects = p_max;
479 }
480
get_max_redirects() const481 int HTTPRequest::get_max_redirects() const {
482
483 return max_redirects;
484 }
485
get_downloaded_bytes() const486 int HTTPRequest::get_downloaded_bytes() const {
487
488 return downloaded;
489 }
get_body_size() const490 int HTTPRequest::get_body_size() const {
491 return body_len;
492 }
493
set_timeout(int p_timeout)494 void HTTPRequest::set_timeout(int p_timeout) {
495
496 ERR_FAIL_COND(p_timeout < 0);
497 timeout = p_timeout;
498 }
499
get_timeout()500 int HTTPRequest::get_timeout() {
501
502 return timeout;
503 }
504
_timeout()505 void HTTPRequest::_timeout() {
506
507 cancel_request();
508 call_deferred("_request_done", RESULT_TIMEOUT, 0, PoolStringArray(), PoolByteArray());
509 }
510
_bind_methods()511 void HTTPRequest::_bind_methods() {
512
513 ClassDB::bind_method(D_METHOD("request", "url", "custom_headers", "ssl_validate_domain", "method", "request_data"), &HTTPRequest::request, DEFVAL(PoolStringArray()), DEFVAL(true), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(String()));
514 ClassDB::bind_method(D_METHOD("cancel_request"), &HTTPRequest::cancel_request);
515
516 ClassDB::bind_method(D_METHOD("get_http_client_status"), &HTTPRequest::get_http_client_status);
517
518 ClassDB::bind_method(D_METHOD("set_use_threads", "enable"), &HTTPRequest::set_use_threads);
519 ClassDB::bind_method(D_METHOD("is_using_threads"), &HTTPRequest::is_using_threads);
520
521 ClassDB::bind_method(D_METHOD("set_body_size_limit", "bytes"), &HTTPRequest::set_body_size_limit);
522 ClassDB::bind_method(D_METHOD("get_body_size_limit"), &HTTPRequest::get_body_size_limit);
523
524 ClassDB::bind_method(D_METHOD("set_max_redirects", "amount"), &HTTPRequest::set_max_redirects);
525 ClassDB::bind_method(D_METHOD("get_max_redirects"), &HTTPRequest::get_max_redirects);
526
527 ClassDB::bind_method(D_METHOD("set_download_file", "path"), &HTTPRequest::set_download_file);
528 ClassDB::bind_method(D_METHOD("get_download_file"), &HTTPRequest::get_download_file);
529
530 ClassDB::bind_method(D_METHOD("get_downloaded_bytes"), &HTTPRequest::get_downloaded_bytes);
531 ClassDB::bind_method(D_METHOD("get_body_size"), &HTTPRequest::get_body_size);
532
533 ClassDB::bind_method(D_METHOD("_redirect_request"), &HTTPRequest::_redirect_request);
534 ClassDB::bind_method(D_METHOD("_request_done"), &HTTPRequest::_request_done);
535
536 ClassDB::bind_method(D_METHOD("set_timeout", "timeout"), &HTTPRequest::set_timeout);
537 ClassDB::bind_method(D_METHOD("get_timeout"), &HTTPRequest::get_timeout);
538
539 ClassDB::bind_method(D_METHOD("set_download_chunk_size"), &HTTPRequest::set_download_chunk_size);
540 ClassDB::bind_method(D_METHOD("get_download_chunk_size"), &HTTPRequest::get_download_chunk_size);
541
542 ClassDB::bind_method(D_METHOD("_timeout"), &HTTPRequest::_timeout);
543
544 ADD_PROPERTY(PropertyInfo(Variant::STRING, "download_file", PROPERTY_HINT_FILE), "set_download_file", "get_download_file");
545 ADD_PROPERTY(PropertyInfo(Variant::INT, "download_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_download_chunk_size", "get_download_chunk_size");
546 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "is_using_threads");
547 ADD_PROPERTY(PropertyInfo(Variant::INT, "body_size_limit", PROPERTY_HINT_RANGE, "-1,2000000000"), "set_body_size_limit", "get_body_size_limit");
548 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_redirects", PROPERTY_HINT_RANGE, "-1,64"), "set_max_redirects", "get_max_redirects");
549 ADD_PROPERTY(PropertyInfo(Variant::INT, "timeout", PROPERTY_HINT_RANGE, "0,86400"), "set_timeout", "get_timeout");
550
551 ADD_SIGNAL(MethodInfo("request_completed", PropertyInfo(Variant::INT, "result"), PropertyInfo(Variant::INT, "response_code"), PropertyInfo(Variant::POOL_STRING_ARRAY, "headers"), PropertyInfo(Variant::POOL_BYTE_ARRAY, "body")));
552
553 BIND_ENUM_CONSTANT(RESULT_SUCCESS);
554 //BIND_ENUM_CONSTANT( RESULT_NO_BODY );
555 BIND_ENUM_CONSTANT(RESULT_CHUNKED_BODY_SIZE_MISMATCH);
556 BIND_ENUM_CONSTANT(RESULT_CANT_CONNECT);
557 BIND_ENUM_CONSTANT(RESULT_CANT_RESOLVE);
558 BIND_ENUM_CONSTANT(RESULT_CONNECTION_ERROR);
559 BIND_ENUM_CONSTANT(RESULT_SSL_HANDSHAKE_ERROR);
560 BIND_ENUM_CONSTANT(RESULT_NO_RESPONSE);
561 BIND_ENUM_CONSTANT(RESULT_BODY_SIZE_LIMIT_EXCEEDED);
562 BIND_ENUM_CONSTANT(RESULT_REQUEST_FAILED);
563 BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_CANT_OPEN);
564 BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_WRITE_ERROR);
565 BIND_ENUM_CONSTANT(RESULT_REDIRECT_LIMIT_REACHED);
566 BIND_ENUM_CONSTANT(RESULT_TIMEOUT);
567 }
568
HTTPRequest()569 HTTPRequest::HTTPRequest() {
570
571 thread = NULL;
572
573 port = 80;
574 redirections = 0;
575 max_redirects = 8;
576 body_len = -1;
577 got_response = false;
578 validate_ssl = false;
579 use_ssl = false;
580 response_code = 0;
581 request_sent = false;
582 requesting = false;
583 client.instance();
584 use_threads = false;
585 thread_done = false;
586 downloaded = 0;
587 body_size_limit = -1;
588 file = NULL;
589
590 timer = memnew(Timer);
591 timer->set_one_shot(true);
592 timer->connect("timeout", this, "_timeout");
593 add_child(timer);
594 timeout = 0;
595 }
596
~HTTPRequest()597 HTTPRequest::~HTTPRequest() {
598 if (file)
599 memdelete(file);
600 }
601