2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* http_request.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2016-03-04 15:09:45 +01:00
|
|
|
#include "http_request.h"
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void HTTPRequest::_redirect_request(const String &p_new_url) {
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Error HTTPRequest::_request() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return client->connect_to_host(url, port, use_ssl, validate_ssl);
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error HTTPRequest::_parse_url(const String &p_url) {
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
url = p_url;
|
|
|
|
use_ssl = false;
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
request_string = "";
|
|
|
|
port = 80;
|
|
|
|
request_sent = false;
|
|
|
|
got_response = false;
|
|
|
|
body_len = -1;
|
2016-03-04 15:09:45 +01:00
|
|
|
body.resize(0);
|
2017-03-05 16:44:50 +01:00
|
|
|
downloaded = 0;
|
|
|
|
redirections = 0;
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-12-12 20:02:25 +01:00
|
|
|
String url_lower = url.to_lower();
|
|
|
|
if (url_lower.begins_with("http://")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
url = url.substr(7, url.length() - 7);
|
2017-12-12 20:02:25 +01:00
|
|
|
} else if (url_lower.begins_with("https://")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
url = url.substr(8, url.length() - 8);
|
|
|
|
use_ssl = true;
|
|
|
|
port = 443;
|
2016-03-04 15:09:45 +01:00
|
|
|
} else {
|
2019-08-31 14:07:03 +02:00
|
|
|
ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, "Malformed URL: " + url + ".");
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2019-08-31 14:07:03 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(url.length() < 1, ERR_INVALID_PARAMETER, "URL too short: " + url + ".");
|
2016-03-04 15:09:45 +01:00
|
|
|
|
|
|
|
int slash_pos = url.find("/");
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (slash_pos != -1) {
|
|
|
|
request_string = url.substr(slash_pos, url.length());
|
|
|
|
url = url.substr(0, slash_pos);
|
2016-03-04 15:09:45 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
request_string = "/";
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int colon_pos = url.find(":");
|
2017-03-05 16:44:50 +01:00
|
|
|
if (colon_pos != -1) {
|
|
|
|
port = url.substr(colon_pos + 1, url.length()).to_int();
|
|
|
|
url = url.substr(0, colon_pos);
|
|
|
|
ERR_FAIL_COND_V(port < 1 || port > 65535, ERR_INVALID_PARAMETER);
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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) {
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
|
2019-08-08 22:11:48 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(requesting, ERR_BUSY, "HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2019-07-04 15:17:52 +02:00
|
|
|
if (timeout > 0) {
|
|
|
|
timer->stop();
|
|
|
|
timer->start(timeout);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
method = p_method;
|
2016-08-31 22:59:09 +02:00
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
Error err = _parse_url(p_url);
|
2016-07-24 21:09:43 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
validate_ssl = p_ssl_validate_domain;
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
headers = p_custom_headers;
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2016-08-31 22:59:09 +02:00
|
|
|
request_data = p_request_data;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
requesting = true;
|
2016-07-24 21:09:43 +02:00
|
|
|
|
|
|
|
if (use_threads) {
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
thread_done = false;
|
|
|
|
thread_request_quit = false;
|
2016-07-24 21:09:43 +02:00
|
|
|
client->set_blocking_mode(true);
|
2017-03-05 16:44:50 +01:00
|
|
|
thread = Thread::create(_thread_func, this);
|
2016-07-24 21:09:43 +02:00
|
|
|
} else {
|
|
|
|
client->set_blocking_mode(false);
|
|
|
|
err = _request();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (err != OK) {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
|
2016-07-24 21:09:43 +02:00
|
|
|
return ERR_CANT_CONNECT;
|
|
|
|
}
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-01-10 22:02:19 +01:00
|
|
|
set_process_internal(true);
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2016-07-24 21:09:43 +02:00
|
|
|
return OK;
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2016-07-24 21:09:43 +02:00
|
|
|
void HTTPRequest::_thread_func(void *p_userdata) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HTTPRequest *hr = (HTTPRequest *)p_userdata;
|
2016-07-24 21:09:43 +02:00
|
|
|
|
|
|
|
Error err = hr->_request();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (err != OK) {
|
2020-02-17 22:06:54 +01:00
|
|
|
hr->call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
|
2016-07-24 21:09:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (!hr->thread_request_quit) {
|
2016-07-24 21:09:43 +02:00
|
|
|
|
|
|
|
bool exit = hr->_update_connection();
|
|
|
|
if (exit)
|
|
|
|
break;
|
|
|
|
OS::get_singleton()->delay_usec(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
hr->thread_done = true;
|
2016-07-24 21:09:43 +02:00
|
|
|
}
|
|
|
|
|
2016-03-04 15:09:45 +01:00
|
|
|
void HTTPRequest::cancel_request() {
|
|
|
|
|
2019-07-04 15:17:52 +02:00
|
|
|
timer->stop();
|
|
|
|
|
2016-03-04 15:09:45 +01:00
|
|
|
if (!requesting)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!use_threads) {
|
2017-01-10 22:02:19 +01:00
|
|
|
set_process_internal(false);
|
2016-07-24 21:09:43 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
thread_request_quit = true;
|
2016-07-24 21:09:43 +02:00
|
|
|
Thread::wait_to_finish(thread);
|
|
|
|
memdelete(thread);
|
2017-03-05 16:44:50 +01:00
|
|
|
thread = NULL;
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
if (file) {
|
|
|
|
memdelete(file);
|
2017-03-05 16:44:50 +01:00
|
|
|
file = NULL;
|
2016-03-12 14:44:12 +01:00
|
|
|
}
|
2016-03-04 15:09:45 +01:00
|
|
|
client->close();
|
|
|
|
body.resize(0);
|
2017-03-05 16:44:50 +01:00
|
|
|
got_response = false;
|
|
|
|
response_code = -1;
|
|
|
|
request_sent = false;
|
|
|
|
requesting = false;
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
bool HTTPRequest::_handle_response(bool *ret_value) {
|
|
|
|
|
|
|
|
if (!client->has_response()) {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_NO_RESPONSE, 0, PackedStringArray(), PackedByteArray());
|
2017-03-05 16:44:50 +01:00
|
|
|
*ret_value = true;
|
2016-03-12 14:44:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
got_response = true;
|
|
|
|
response_code = client->get_response_code();
|
2016-03-12 14:44:12 +01:00
|
|
|
List<String> rheaders;
|
|
|
|
client->get_response_headers(&rheaders);
|
|
|
|
response_headers.resize(0);
|
2017-03-05 16:44:50 +01:00
|
|
|
downloaded = 0;
|
|
|
|
for (List<String>::Element *E = rheaders.front(); E; E = E->next()) {
|
2016-03-12 14:44:12 +01:00
|
|
|
response_headers.push_back(E->get());
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (response_code == 301 || response_code == 302) {
|
2017-12-12 20:02:25 +01:00
|
|
|
// Handle redirect
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (max_redirects >= 0 && redirections >= max_redirects) {
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_REDIRECT_LIMIT_REACHED, response_code, response_headers, PackedByteArray());
|
2017-03-05 16:44:50 +01:00
|
|
|
*ret_value = true;
|
2016-03-12 14:44:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
String new_request;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (List<String>::Element *E = rheaders.front(); E; E = E->next()) {
|
|
|
|
if (E->get().findn("Location: ") != -1) {
|
|
|
|
new_request = E->get().substr(9, E->get().length()).strip_edges();
|
2016-03-12 14:44:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (new_request != "") {
|
2017-12-12 20:02:25 +01:00
|
|
|
// Process redirect
|
2016-03-12 14:44:12 +01:00
|
|
|
client->close();
|
2017-12-12 20:02:25 +01:00
|
|
|
int new_redirs = redirections + 1; // Because _request() will clear it
|
2016-03-12 14:44:12 +01:00
|
|
|
Error err;
|
|
|
|
if (new_request.begins_with("http")) {
|
2017-12-12 20:02:25 +01:00
|
|
|
// New url, request all again
|
2019-01-14 23:04:47 +01:00
|
|
|
_parse_url(new_request);
|
2016-03-12 14:44:12 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
request_string = new_request;
|
2016-03-12 14:44:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = _request();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (err == OK) {
|
|
|
|
request_sent = false;
|
|
|
|
got_response = false;
|
|
|
|
body_len = -1;
|
2016-03-12 14:44:12 +01:00
|
|
|
body.resize(0);
|
2017-03-05 16:44:50 +01:00
|
|
|
downloaded = 0;
|
|
|
|
redirections = new_redirs;
|
|
|
|
*ret_value = false;
|
2016-03-12 14:44:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:09:45 +01:00
|
|
|
bool HTTPRequest::_update_connection() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (client->get_status()) {
|
2016-03-04 15:09:45 +01:00
|
|
|
case HTTPClient::STATUS_DISCONNECTED: {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
|
2017-12-12 20:02:25 +01:00
|
|
|
return true; // End it, since it's doing something
|
2016-03-04 15:09:45 +01:00
|
|
|
} break;
|
|
|
|
case HTTPClient::STATUS_RESOLVING: {
|
|
|
|
client->poll();
|
2017-12-12 20:02:25 +01:00
|
|
|
// Must wait
|
2016-03-04 15:09:45 +01:00
|
|
|
return false;
|
|
|
|
} break;
|
|
|
|
case HTTPClient::STATUS_CANT_RESOLVE: {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_CANT_RESOLVE, 0, PackedStringArray(), PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case HTTPClient::STATUS_CONNECTING: {
|
|
|
|
client->poll();
|
2017-12-12 20:02:25 +01:00
|
|
|
// Must wait
|
2016-03-04 15:09:45 +01:00
|
|
|
return false;
|
2017-12-12 20:02:25 +01:00
|
|
|
} break; // Connecting to IP
|
2016-03-04 15:09:45 +01:00
|
|
|
case HTTPClient::STATUS_CANT_CONNECT: {
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_CANT_CONNECT, 0, PackedStringArray(), PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case HTTPClient::STATUS_CONNECTED: {
|
|
|
|
|
|
|
|
if (request_sent) {
|
|
|
|
|
|
|
|
if (!got_response) {
|
|
|
|
|
2017-12-12 20:02:25 +01:00
|
|
|
// No body
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
bool ret_value;
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
if (_handle_response(&ret_value))
|
|
|
|
return ret_value;
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-04-08 11:03:37 +02:00
|
|
|
if (body_len < 0) {
|
2017-12-12 20:02:25 +01:00
|
|
|
// Chunked transfer is done
|
2017-03-05 16:44:50 +01:00
|
|
|
call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_CHUNKED_BODY_SIZE_MISMATCH, response_code, response_headers, PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
2017-12-12 20:02:25 +01:00
|
|
|
// Request migh have been done
|
2016-03-04 15:09:45 +01:00
|
|
|
} else {
|
2017-12-12 20:02:25 +01:00
|
|
|
// Did not request yet, do request
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error err = client->request(method, request_string, headers, request_data);
|
|
|
|
if (err != OK) {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_CONNECTION_ERROR, 0, PackedStringArray(), PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
request_sent = true;
|
2016-03-04 15:09:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-12-12 20:02:25 +01:00
|
|
|
} break; // Connected: break requests only accepted here
|
2016-03-04 15:09:45 +01:00
|
|
|
case HTTPClient::STATUS_REQUESTING: {
|
2017-12-12 20:02:25 +01:00
|
|
|
// Must wait, still requesting
|
2016-03-04 15:09:45 +01:00
|
|
|
client->poll();
|
|
|
|
return false;
|
|
|
|
|
2017-12-12 20:02:25 +01:00
|
|
|
} break; // Request in progress
|
2016-03-04 15:09:45 +01:00
|
|
|
case HTTPClient::STATUS_BODY: {
|
|
|
|
|
|
|
|
if (!got_response) {
|
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
bool ret_value;
|
|
|
|
|
|
|
|
if (_handle_response(&ret_value))
|
|
|
|
return ret_value;
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (!client->is_response_chunked() && client->get_response_body_length() == 0) {
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-11 17:46:14 +02:00
|
|
|
// No body len (-1) if chunked or no content-length header was provided.
|
|
|
|
// Change your webserver configuration if you want body len.
|
|
|
|
body_len = client->get_response_body_length();
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2018-09-11 17:46:14 +02:00
|
|
|
if (body_size_limit >= 0 && body_len > body_size_limit) {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PackedByteArray());
|
2018-09-11 17:46:14 +02:00
|
|
|
return true;
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (download_to_file != String()) {
|
|
|
|
file = FileAccess::open(download_to_file, FileAccess::WRITE);
|
2016-03-12 14:44:12 +01:00
|
|
|
if (!file) {
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_DOWNLOAD_FILE_CANT_OPEN, response_code, response_headers, PackedByteArray());
|
2016-07-24 21:09:43 +02:00
|
|
|
return true;
|
2016-03-12 14:44:12 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
client->poll();
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedByteArray chunk = client->read_response_body_chunk();
|
2017-03-05 16:44:50 +01:00
|
|
|
downloaded += chunk.size();
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
if (file) {
|
2020-02-17 22:06:54 +01:00
|
|
|
const uint8_t *r = chunk.ptr();
|
|
|
|
file->store_buffer(r, chunk.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
if (file->get_error() != OK) {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_DOWNLOAD_FILE_WRITE_ERROR, response_code, response_headers, PackedByteArray());
|
2016-03-12 14:44:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
body.append_array(chunk);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (body_size_limit >= 0 && downloaded > body_size_limit) {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_BODY_SIZE_LIMIT_EXCEEDED, response_code, response_headers, PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (body_len >= 0) {
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (downloaded == body_len) {
|
|
|
|
call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-11 17:46:14 +02:00
|
|
|
} else if (client->get_status() == HTTPClient::STATUS_DISCONNECTED) {
|
|
|
|
// We read till EOF, with no errors. Request is done.
|
|
|
|
call_deferred("_request_done", RESULT_SUCCESS, response_code, response_headers, body);
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
2017-12-12 20:02:25 +01:00
|
|
|
} break; // Request resulted in body: break which must be read
|
2016-03-04 15:09:45 +01:00
|
|
|
case HTTPClient::STATUS_CONNECTION_ERROR: {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_CONNECTION_ERROR, 0, PackedStringArray(), PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
} break;
|
|
|
|
case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR: {
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_SSL_HANDSHAKE_ERROR, 0, PackedStringArray(), PackedByteArray());
|
2016-03-04 15:09:45 +01:00
|
|
|
return true;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_V(false);
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
void HTTPRequest::_request_done(int p_status, int p_code, const PackedStringArray &headers, const PackedByteArray &p_data) {
|
2016-07-24 21:09:43 +02:00
|
|
|
|
|
|
|
cancel_request();
|
2017-03-05 16:44:50 +01:00
|
|
|
emit_signal("request_completed", p_status, p_code, headers, p_data);
|
2016-07-24 21:09:43 +02:00
|
|
|
}
|
|
|
|
|
2016-03-04 15:09:45 +01:00
|
|
|
void HTTPRequest::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2016-07-24 21:09:43 +02:00
|
|
|
if (use_threads)
|
|
|
|
return;
|
2016-03-04 15:09:45 +01:00
|
|
|
bool done = _update_connection();
|
|
|
|
if (done) {
|
|
|
|
|
2017-01-10 22:02:19 +01:00
|
|
|
set_process_internal(false);
|
2017-12-12 20:02:25 +01:00
|
|
|
// cancel_request(); called from _request done now
|
2016-07-24 21:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
2016-07-24 21:09:43 +02:00
|
|
|
if (requesting) {
|
2016-03-04 15:09:45 +01:00
|
|
|
cancel_request();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPRequest::set_use_threads(bool p_use) {
|
|
|
|
|
2019-07-01 09:15:35 +02:00
|
|
|
ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
|
2017-03-05 16:44:50 +01:00
|
|
|
use_threads = p_use;
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPRequest::is_using_threads() const {
|
|
|
|
|
|
|
|
return use_threads;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPRequest::set_body_size_limit(int p_bytes) {
|
|
|
|
|
2019-07-01 09:15:35 +02:00
|
|
|
ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
body_size_limit = p_bytes;
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int HTTPRequest::get_body_size_limit() const {
|
|
|
|
|
|
|
|
return body_size_limit;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void HTTPRequest::set_download_file(const String &p_file) {
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2019-07-01 09:15:35 +02:00
|
|
|
ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
download_to_file = p_file;
|
2016-03-12 14:44:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String HTTPRequest::get_download_file() const {
|
|
|
|
|
|
|
|
return download_to_file;
|
|
|
|
}
|
2019-11-24 13:20:24 +01:00
|
|
|
|
|
|
|
void HTTPRequest::set_download_chunk_size(int p_chunk_size) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(get_http_client_status() != HTTPClient::STATUS_DISCONNECTED);
|
|
|
|
|
|
|
|
client->set_read_chunk_size(p_chunk_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int HTTPRequest::get_download_chunk_size() const {
|
|
|
|
return client->get_read_chunk_size();
|
|
|
|
}
|
|
|
|
|
2016-03-04 15:09:45 +01:00
|
|
|
HTTPClient::Status HTTPRequest::get_http_client_status() const {
|
|
|
|
return client->get_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPRequest::set_max_redirects(int p_max) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
max_redirects = p_max;
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int HTTPRequest::get_max_redirects() const {
|
2016-03-04 15:09:45 +01:00
|
|
|
|
|
|
|
return max_redirects;
|
|
|
|
}
|
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
int HTTPRequest::get_downloaded_bytes() const {
|
|
|
|
|
|
|
|
return downloaded;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
int HTTPRequest::get_body_size() const {
|
2016-03-12 14:44:12 +01:00
|
|
|
return body_len;
|
|
|
|
}
|
|
|
|
|
2019-07-04 15:17:52 +02:00
|
|
|
void HTTPRequest::set_timeout(int p_timeout) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(p_timeout < 0);
|
|
|
|
timeout = p_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HTTPRequest::get_timeout() {
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HTTPRequest::_timeout() {
|
|
|
|
|
|
|
|
cancel_request();
|
2020-02-17 22:06:54 +01:00
|
|
|
call_deferred("_request_done", RESULT_TIMEOUT, 0, PackedStringArray(), PackedByteArray());
|
2019-07-04 15:17:52 +02:00
|
|
|
}
|
|
|
|
|
2016-03-04 15:09:45 +01:00
|
|
|
void HTTPRequest::_bind_methods() {
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("request", "url", "custom_headers", "ssl_validate_domain", "method", "request_data"), &HTTPRequest::request, DEFVAL(PackedStringArray()), DEFVAL(true), DEFVAL(HTTPClient::METHOD_GET), DEFVAL(String()));
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("cancel_request"), &HTTPRequest::cancel_request);
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_http_client_status"), &HTTPRequest::get_http_client_status);
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_threads", "enable"), &HTTPRequest::set_use_threads);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_using_threads"), &HTTPRequest::is_using_threads);
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_body_size_limit", "bytes"), &HTTPRequest::set_body_size_limit);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_body_size_limit"), &HTTPRequest::get_body_size_limit);
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_max_redirects", "amount"), &HTTPRequest::set_max_redirects);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_redirects"), &HTTPRequest::get_max_redirects);
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_download_file", "path"), &HTTPRequest::set_download_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_download_file"), &HTTPRequest::get_download_file);
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_downloaded_bytes"), &HTTPRequest::get_downloaded_bytes);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_body_size"), &HTTPRequest::get_body_size);
|
2016-03-12 14:44:12 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_redirect_request"), &HTTPRequest::_redirect_request);
|
|
|
|
ClassDB::bind_method(D_METHOD("_request_done"), &HTTPRequest::_request_done);
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2019-07-04 15:17:52 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_timeout", "timeout"), &HTTPRequest::set_timeout);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_timeout"), &HTTPRequest::get_timeout);
|
|
|
|
|
2019-11-24 13:20:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_download_chunk_size"), &HTTPRequest::set_download_chunk_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_download_chunk_size"), &HTTPRequest::get_download_chunk_size);
|
|
|
|
|
2018-01-11 23:35:12 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "download_file", PROPERTY_HINT_FILE), "set_download_file", "get_download_file");
|
2019-11-24 13:20:24 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "download_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_download_chunk_size", "get_download_chunk_size");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_threads"), "set_use_threads", "is_using_threads");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "body_size_limit", PROPERTY_HINT_RANGE, "-1,2000000000"), "set_body_size_limit", "get_body_size_limit");
|
2017-12-12 20:02:25 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_redirects", PROPERTY_HINT_RANGE, "-1,64"), "set_max_redirects", "get_max_redirects");
|
2019-07-04 15:17:52 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "timeout", PROPERTY_HINT_RANGE, "0,86400"), "set_timeout", "get_timeout");
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("request_completed", PropertyInfo(Variant::INT, "result"), PropertyInfo(Variant::INT, "response_code"), PropertyInfo(Variant::PACKED_STRING_ARRAY, "headers"), PropertyInfo(Variant::PACKED_BYTE_ARRAY, "body")));
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESULT_SUCCESS);
|
|
|
|
//BIND_ENUM_CONSTANT( RESULT_NO_BODY );
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_CHUNKED_BODY_SIZE_MISMATCH);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_CANT_CONNECT);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_CANT_RESOLVE);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_CONNECTION_ERROR);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_SSL_HANDSHAKE_ERROR);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_NO_RESPONSE);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_BODY_SIZE_LIMIT_EXCEEDED);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_REQUEST_FAILED);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_CANT_OPEN);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_DOWNLOAD_FILE_WRITE_ERROR);
|
|
|
|
BIND_ENUM_CONSTANT(RESULT_REDIRECT_LIMIT_REACHED);
|
2019-07-04 15:17:52 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESULT_TIMEOUT);
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HTTPRequest::HTTPRequest() {
|
|
|
|
|
|
|
|
thread = NULL;
|
2016-03-04 15:09:45 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
port = 80;
|
|
|
|
redirections = 0;
|
|
|
|
max_redirects = 8;
|
|
|
|
body_len = -1;
|
|
|
|
got_response = false;
|
|
|
|
validate_ssl = false;
|
|
|
|
use_ssl = false;
|
|
|
|
response_code = 0;
|
|
|
|
request_sent = false;
|
|
|
|
requesting = false;
|
|
|
|
client.instance();
|
|
|
|
use_threads = false;
|
|
|
|
thread_done = false;
|
2017-11-02 03:12:28 +01:00
|
|
|
downloaded = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
body_size_limit = -1;
|
|
|
|
file = NULL;
|
2019-07-04 15:17:52 +02:00
|
|
|
|
|
|
|
timer = memnew(Timer);
|
|
|
|
timer->set_one_shot(true);
|
2020-02-21 18:28:45 +01:00
|
|
|
timer->connect("timeout", callable_mp(this, &HTTPRequest::_timeout));
|
2019-07-04 15:17:52 +02:00
|
|
|
add_child(timer);
|
|
|
|
timeout = 0;
|
2016-03-04 15:09:45 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 14:44:12 +01:00
|
|
|
HTTPRequest::~HTTPRequest() {
|
|
|
|
if (file)
|
|
|
|
memdelete(file);
|
|
|
|
}
|