2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* http_client.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01: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). */
|
2014-02-10 02:10:30 +01: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
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "http_client.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
|
|
|
|
#include "core/io/stream_peer_ssl.h"
|
|
|
|
#include "core/version.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-12 20:02:25 +01:00
|
|
|
const char *HTTPClient::_methods[METHOD_MAX] = {
|
|
|
|
"GET",
|
|
|
|
"HEAD",
|
|
|
|
"POST",
|
|
|
|
"PUT",
|
|
|
|
"DELETE",
|
|
|
|
"OPTIONS",
|
|
|
|
"TRACE",
|
|
|
|
"CONNECT",
|
|
|
|
"PATCH"
|
|
|
|
};
|
|
|
|
|
2017-10-26 03:39:41 +02:00
|
|
|
#ifndef JAVASCRIPT_ENABLED
|
2017-03-05 16:44:50 +01:00
|
|
|
Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) {
|
2014-02-10 02:10:30 +01:00
|
|
|
close();
|
2017-12-12 20:02:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
conn_port = p_port;
|
|
|
|
conn_host = p_host;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-12 20:02:25 +01:00
|
|
|
ssl = p_ssl;
|
|
|
|
ssl_verify_host = p_verify_host;
|
|
|
|
|
|
|
|
String host_lower = conn_host.to_lower();
|
|
|
|
if (host_lower.begins_with("http://")) {
|
2017-12-15 15:32:44 +01:00
|
|
|
conn_host = conn_host.substr(7, conn_host.length() - 7);
|
2017-12-12 20:02:25 +01:00
|
|
|
} else if (host_lower.begins_with("https://")) {
|
|
|
|
ssl = true;
|
2017-12-15 15:32:44 +01:00
|
|
|
conn_host = conn_host.substr(8, conn_host.length() - 8);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 20:02:25 +01:00
|
|
|
ERR_FAIL_COND_V(conn_host.length() < HOST_MIN_LEN, ERR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
if (conn_port < 0) {
|
|
|
|
if (ssl) {
|
|
|
|
conn_port = PORT_HTTPS;
|
|
|
|
} else {
|
|
|
|
conn_port = PORT_HTTP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
connection = tcp_connection;
|
2014-04-29 02:56:43 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (conn_host.is_valid_ip_address()) {
|
2017-12-12 20:02:25 +01:00
|
|
|
// Host contains valid IP
|
2017-03-05 16:44:50 +01:00
|
|
|
Error err = tcp_connection->connect_to_host(IP_Address(conn_host), p_port);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (err) {
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CANT_CONNECT;
|
2014-02-10 02:10:30 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTING;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-12-12 20:02:25 +01:00
|
|
|
// Host contains hostname and needs to be resolved to IP
|
2017-03-05 16:44:50 +01:00
|
|
|
resolving = IP::get_singleton()->resolve_hostname_queue_item(conn_host);
|
|
|
|
status = STATUS_RESOLVING;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void HTTPClient::set_connection(const Ref<StreamPeer> &p_connection) {
|
2019-11-16 08:16:26 +01:00
|
|
|
ERR_FAIL_COND_MSG(p_connection.is_null(), "Connection is not a reference to a valid StreamPeer object.");
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
close();
|
2017-03-05 16:44:50 +01:00
|
|
|
connection = p_connection;
|
|
|
|
status = STATUS_CONNECTED;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-04-17 06:23:34 +02:00
|
|
|
Ref<StreamPeer> HTTPClient::get_connection() const {
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const Vector<uint8_t> &p_body) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
|
2017-12-12 20:02:25 +01:00
|
|
|
ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
|
|
|
|
ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA);
|
2016-05-04 19:49:01 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n";
|
2017-12-12 20:02:25 +01:00
|
|
|
if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) {
|
|
|
|
// Don't append the standard ports
|
2017-06-13 22:58:23 +02:00
|
|
|
request += "Host: " + conn_host + "\r\n";
|
|
|
|
} else {
|
|
|
|
request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n";
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
bool add_clen = p_body.size() > 0;
|
2018-07-04 01:18:26 +02:00
|
|
|
bool add_uagent = true;
|
|
|
|
bool add_accept = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_headers.size(); i++) {
|
|
|
|
request += p_headers[i] + "\r\n";
|
2018-07-04 01:18:26 +02:00
|
|
|
if (add_clen && p_headers[i].findn("Content-Length:") == 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
add_clen = false;
|
2016-05-04 19:49:01 +02:00
|
|
|
}
|
2018-07-04 01:18:26 +02:00
|
|
|
if (add_uagent && p_headers[i].findn("User-Agent:") == 0) {
|
|
|
|
add_uagent = false;
|
|
|
|
}
|
|
|
|
if (add_accept && p_headers[i].findn("Accept:") == 0) {
|
|
|
|
add_accept = false;
|
|
|
|
}
|
2016-05-04 19:49:01 +02:00
|
|
|
}
|
|
|
|
if (add_clen) {
|
2017-03-05 16:44:50 +01:00
|
|
|
request += "Content-Length: " + itos(p_body.size()) + "\r\n";
|
2017-12-12 20:02:25 +01:00
|
|
|
// Should it add utf8 encoding?
|
2016-05-04 19:49:01 +02:00
|
|
|
}
|
2018-07-04 01:18:26 +02:00
|
|
|
if (add_uagent) {
|
|
|
|
request += "User-Agent: GodotEngine/" + String(VERSION_FULL_BUILD) + " (" + OS::get_singleton()->get_name() + ")\r\n";
|
|
|
|
}
|
|
|
|
if (add_accept) {
|
|
|
|
request += "Accept: */*\r\n";
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
request += "\r\n";
|
|
|
|
CharString cs = request.utf8();
|
2016-05-21 15:29:25 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
Vector<uint8_t> data;
|
2017-12-12 20:02:25 +01:00
|
|
|
data.resize(cs.length());
|
|
|
|
{
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *data_write = data.ptrw();
|
2017-12-12 20:02:25 +01:00
|
|
|
for (int i = 0; i < cs.length(); i++) {
|
|
|
|
data_write[i] = cs[i];
|
|
|
|
}
|
2016-05-04 19:49:01 +02:00
|
|
|
}
|
2017-12-12 20:02:25 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
data.append_array(p_body);
|
2016-05-04 19:49:01 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
const uint8_t *r = data.ptr();
|
2016-05-04 19:49:01 +02:00
|
|
|
Error err = connection->put_data(&r[0], data.size());
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
close();
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTION_ERROR;
|
2016-05-04 19:49:01 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_REQUESTING;
|
2019-11-15 14:59:49 +01:00
|
|
|
head_request = p_method == METHOD_HEAD;
|
2016-05-04 19:49:01 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2016-04-17 06:23:34 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error HTTPClient::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body) {
|
|
|
|
ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
|
2017-12-12 20:02:25 +01:00
|
|
|
ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
|
|
|
|
ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n";
|
2017-12-12 20:02:25 +01:00
|
|
|
if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) {
|
|
|
|
// Don't append the standard ports
|
2017-06-13 22:58:23 +02:00
|
|
|
request += "Host: " + conn_host + "\r\n";
|
|
|
|
} else {
|
|
|
|
request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n";
|
|
|
|
}
|
2018-07-04 01:18:26 +02:00
|
|
|
bool add_uagent = true;
|
|
|
|
bool add_accept = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool add_clen = p_body.length() > 0;
|
|
|
|
for (int i = 0; i < p_headers.size(); i++) {
|
|
|
|
request += p_headers[i] + "\r\n";
|
2018-07-04 01:18:26 +02:00
|
|
|
if (add_clen && p_headers[i].findn("Content-Length:") == 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
add_clen = false;
|
2014-04-05 23:50:09 +02:00
|
|
|
}
|
2018-07-04 01:18:26 +02:00
|
|
|
if (add_uagent && p_headers[i].findn("User-Agent:") == 0) {
|
|
|
|
add_uagent = false;
|
|
|
|
}
|
|
|
|
if (add_accept && p_headers[i].findn("Accept:") == 0) {
|
|
|
|
add_accept = false;
|
|
|
|
}
|
2014-04-05 23:50:09 +02:00
|
|
|
}
|
|
|
|
if (add_clen) {
|
2017-03-05 16:44:50 +01:00
|
|
|
request += "Content-Length: " + itos(p_body.utf8().length()) + "\r\n";
|
2017-12-12 20:02:25 +01:00
|
|
|
// Should it add utf8 encoding?
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2018-07-04 01:18:26 +02:00
|
|
|
if (add_uagent) {
|
|
|
|
request += "User-Agent: GodotEngine/" + String(VERSION_FULL_BUILD) + " (" + OS::get_singleton()->get_name() + ")\r\n";
|
|
|
|
}
|
|
|
|
if (add_accept) {
|
|
|
|
request += "Accept: */*\r\n";
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
request += "\r\n";
|
|
|
|
request += p_body;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
CharString cs = request.utf8();
|
|
|
|
Error err = connection->put_data((const uint8_t *)cs.ptr(), cs.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
if (err) {
|
|
|
|
close();
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTION_ERROR;
|
2014-02-10 02:10:30 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_REQUESTING;
|
2019-11-15 14:59:49 +01:00
|
|
|
head_request = p_method == METHOD_HEAD;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPClient::has_response() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
return response_headers.size() != 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPClient::is_response_chunked() const {
|
|
|
|
return chunked;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HTTPClient::get_response_code() const {
|
|
|
|
return response_num;
|
|
|
|
}
|
2016-05-04 19:49:01 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Error HTTPClient::get_response_headers(List<String> *r_response) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!response_headers.size()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_INVALID_PARAMETER;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < response_headers.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
r_response->push_back(response_headers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
response_headers.clear();
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void HTTPClient::close() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (tcp_connection->get_status() != StreamPeerTCP::STATUS_NONE) {
|
2017-01-14 15:07:57 +01:00
|
|
|
tcp_connection->disconnect_from_host();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
connection.unref();
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_DISCONNECTED;
|
2019-11-15 14:59:49 +01:00
|
|
|
head_request = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (resolving != IP::RESOLVER_INVALID_ID) {
|
2014-02-10 02:10:30 +01:00
|
|
|
IP::get_singleton()->erase_resolve_item(resolving);
|
2017-03-05 16:44:50 +01:00
|
|
|
resolving = IP::RESOLVER_INVALID_ID;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response_headers.clear();
|
|
|
|
response_str.clear();
|
2018-09-11 17:46:14 +02:00
|
|
|
body_size = -1;
|
2017-03-05 16:44:50 +01:00
|
|
|
body_left = 0;
|
|
|
|
chunk_left = 0;
|
2020-05-14 11:00:19 +02:00
|
|
|
chunk_trailer_part = false;
|
2018-04-18 20:50:31 +02:00
|
|
|
read_until_eof = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
response_num = 0;
|
2018-07-15 01:38:52 +02:00
|
|
|
handshaking = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error HTTPClient::poll() {
|
|
|
|
switch (status) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case STATUS_RESOLVING: {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(resolving == IP::RESOLVER_INVALID_ID, ERR_BUG);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
IP::ResolverStatus rstatus = IP::get_singleton()->get_resolve_item_status(resolving);
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (rstatus) {
|
|
|
|
case IP::RESOLVER_STATUS_WAITING:
|
2017-12-12 20:02:25 +01:00
|
|
|
return OK; // Still resolving
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case IP::RESOLVER_STATUS_DONE: {
|
|
|
|
IP_Address host = IP::get_singleton()->get_resolve_item_address(resolving);
|
2017-03-05 16:44:50 +01:00
|
|
|
Error err = tcp_connection->connect_to_host(host, conn_port);
|
2014-02-10 02:10:30 +01:00
|
|
|
IP::get_singleton()->erase_resolve_item(resolving);
|
2017-03-05 16:44:50 +01:00
|
|
|
resolving = IP::RESOLVER_INVALID_ID;
|
2014-02-10 02:10:30 +01:00
|
|
|
if (err) {
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CANT_CONNECT;
|
2014-02-10 02:10:30 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTING;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case IP::RESOLVER_STATUS_NONE:
|
|
|
|
case IP::RESOLVER_STATUS_ERROR: {
|
|
|
|
IP::get_singleton()->erase_resolve_item(resolving);
|
2017-03-05 16:44:50 +01:00
|
|
|
resolving = IP::RESOLVER_INVALID_ID;
|
2014-02-10 02:10:30 +01:00
|
|
|
close();
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CANT_RESOLVE;
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_CANT_RESOLVE;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case STATUS_CONNECTING: {
|
|
|
|
StreamPeerTCP::Status s = tcp_connection->get_status();
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (s) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case StreamPeerTCP::STATUS_CONNECTING: {
|
2017-12-12 20:02:25 +01:00
|
|
|
return OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case StreamPeerTCP::STATUS_CONNECTED: {
|
2014-04-29 02:56:43 +02:00
|
|
|
if (ssl) {
|
2018-07-15 01:38:52 +02:00
|
|
|
Ref<StreamPeerSSL> ssl;
|
|
|
|
if (!handshaking) {
|
|
|
|
// Connect the StreamPeerSSL and start handshaking
|
|
|
|
ssl = Ref<StreamPeerSSL>(StreamPeerSSL::create());
|
|
|
|
ssl->set_blocking_handshake_enabled(false);
|
|
|
|
Error err = ssl->connect_to_stream(tcp_connection, ssl_verify_host, conn_host);
|
|
|
|
if (err != OK) {
|
|
|
|
close();
|
|
|
|
status = STATUS_SSL_HANDSHAKE_ERROR;
|
|
|
|
return ERR_CANT_CONNECT;
|
|
|
|
}
|
|
|
|
connection = ssl;
|
|
|
|
handshaking = true;
|
|
|
|
} else {
|
|
|
|
// We are already handshaking, which means we can use your already active SSL connection
|
2020-03-17 07:33:00 +01:00
|
|
|
ssl = static_cast<Ref<StreamPeerSSL>>(connection);
|
2019-06-06 15:46:54 +02:00
|
|
|
if (ssl.is_null()) {
|
|
|
|
close();
|
|
|
|
status = STATUS_SSL_HANDSHAKE_ERROR;
|
|
|
|
return ERR_CANT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2018-07-15 01:38:52 +02:00
|
|
|
ssl->poll(); // Try to finish the handshake
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl->get_status() == StreamPeerSSL::STATUS_CONNECTED) {
|
2018-09-13 03:38:39 +02:00
|
|
|
// Handshake has been successful
|
2018-07-15 01:38:52 +02:00
|
|
|
handshaking = false;
|
|
|
|
status = STATUS_CONNECTED;
|
|
|
|
return OK;
|
|
|
|
} else if (ssl->get_status() != StreamPeerSSL::STATUS_HANDSHAKING) {
|
|
|
|
// Handshake has failed
|
2014-04-29 02:56:43 +02:00
|
|
|
close();
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_SSL_HANDSHAKE_ERROR;
|
2014-04-29 02:56:43 +02:00
|
|
|
return ERR_CANT_CONNECT;
|
|
|
|
}
|
2018-07-15 01:38:52 +02:00
|
|
|
// ... we will need to poll more for handshake to finish
|
|
|
|
} else {
|
|
|
|
status = STATUS_CONNECTED;
|
2014-04-29 02:56:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
|
|
|
} break;
|
|
|
|
case StreamPeerTCP::STATUS_ERROR:
|
|
|
|
case StreamPeerTCP::STATUS_NONE: {
|
|
|
|
close();
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CANT_CONNECT;
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_CANT_CONNECT;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} break;
|
2018-09-26 13:13:56 +02:00
|
|
|
case STATUS_BODY:
|
2014-02-10 02:10:30 +01:00
|
|
|
case STATUS_CONNECTED: {
|
2018-09-14 14:13:11 +02:00
|
|
|
// Check if we are still connected
|
|
|
|
if (ssl) {
|
|
|
|
Ref<StreamPeerSSL> tmp = connection;
|
|
|
|
tmp->poll();
|
|
|
|
if (tmp->get_status() != StreamPeerSSL::STATUS_CONNECTED) {
|
|
|
|
status = STATUS_CONNECTION_ERROR;
|
|
|
|
return ERR_CONNECTION_ERROR;
|
|
|
|
}
|
|
|
|
} else if (tcp_connection->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
|
|
|
|
status = STATUS_CONNECTION_ERROR;
|
|
|
|
return ERR_CONNECTION_ERROR;
|
|
|
|
}
|
2017-12-12 20:02:25 +01:00
|
|
|
// Connection established, requests can now be made
|
2014-02-10 02:10:30 +01:00
|
|
|
return OK;
|
|
|
|
} break;
|
|
|
|
case STATUS_REQUESTING: {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
uint8_t byte;
|
2017-03-05 16:44:50 +01:00
|
|
|
int rec = 0;
|
|
|
|
Error err = _get_http_data(&byte, 1, rec);
|
|
|
|
if (err != OK) {
|
2014-02-10 02:10:30 +01:00
|
|
|
close();
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTION_ERROR;
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_CONNECTION_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (rec == 0) {
|
2017-12-12 20:02:25 +01:00
|
|
|
return OK; // Still requesting, keep trying!
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
response_str.push_back(byte);
|
|
|
|
int rs = response_str.size();
|
|
|
|
if (
|
2017-03-05 16:44:50 +01:00
|
|
|
(rs >= 2 && response_str[rs - 2] == '\n' && response_str[rs - 1] == '\n') ||
|
|
|
|
(rs >= 4 && response_str[rs - 4] == '\r' && response_str[rs - 3] == '\n' && response_str[rs - 2] == '\r' && response_str[rs - 1] == '\n')) {
|
2017-12-12 20:02:25 +01:00
|
|
|
// End of response, parse.
|
2014-02-10 02:10:30 +01:00
|
|
|
response_str.push_back(0);
|
|
|
|
String response;
|
2017-03-05 16:44:50 +01:00
|
|
|
response.parse_utf8((const char *)response_str.ptr());
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<String> responses = response.split("\n");
|
2018-09-11 17:46:14 +02:00
|
|
|
body_size = -1;
|
2017-03-05 16:44:50 +01:00
|
|
|
chunked = false;
|
|
|
|
body_left = 0;
|
|
|
|
chunk_left = 0;
|
2019-02-20 04:33:31 +01:00
|
|
|
chunk_trailer_part = false;
|
2018-04-18 20:50:31 +02:00
|
|
|
read_until_eof = false;
|
2016-04-27 21:07:49 +02:00
|
|
|
response_str.clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
response_headers.clear();
|
|
|
|
response_num = RESPONSE_OK;
|
|
|
|
|
2019-02-16 17:05:13 +01:00
|
|
|
// Per the HTTP 1.1 spec, keep-alive is the default.
|
2019-05-19 12:34:40 +02:00
|
|
|
// Not following that specification breaks standard implementations.
|
2019-02-16 17:05:13 +01:00
|
|
|
// Broken web servers should be fixed.
|
|
|
|
bool keep_alive = true;
|
2018-04-18 20:50:31 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < responses.size(); i++) {
|
2016-02-19 21:12:16 +01:00
|
|
|
String header = responses[i].strip_edges();
|
|
|
|
String s = header.to_lower();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (s.length() == 0) {
|
2016-03-09 00:00:52 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-01-11 11:47:42 +01:00
|
|
|
if (s.begins_with("content-length:")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
body_size = s.substr(s.find(":") + 1, s.length()).strip_edges().to_int();
|
|
|
|
body_left = body_size;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-04-18 20:50:31 +02:00
|
|
|
} else if (s.begins_with("transfer-encoding:")) {
|
2017-03-05 16:44:50 +01:00
|
|
|
String encoding = header.substr(header.find(":") + 1, header.length()).strip_edges();
|
|
|
|
if (encoding == "chunked") {
|
|
|
|
chunked = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2019-02-16 17:05:13 +01:00
|
|
|
} else if (s.begins_with("connection: close")) {
|
|
|
|
keep_alive = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (i == 0 && responses[i].begins_with("HTTP")) {
|
|
|
|
String num = responses[i].get_slicec(' ', 1);
|
|
|
|
response_num = num.to_int();
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2016-02-19 21:12:16 +01:00
|
|
|
response_headers.push_back(header);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:59:49 +01:00
|
|
|
// This is a HEAD request, we wont receive anything.
|
|
|
|
if (head_request) {
|
|
|
|
body_size = 0;
|
|
|
|
body_left = 0;
|
|
|
|
}
|
|
|
|
|
2018-09-11 17:46:14 +02:00
|
|
|
if (body_size != -1 || chunked) {
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_BODY;
|
2018-04-18 20:50:31 +02:00
|
|
|
} else if (!keep_alive) {
|
|
|
|
read_until_eof = true;
|
|
|
|
status = STATUS_BODY;
|
|
|
|
} else {
|
|
|
|
status = STATUS_CONNECTED;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case STATUS_DISCONNECTED: {
|
|
|
|
return ERR_UNCONFIGURED;
|
|
|
|
} break;
|
2018-09-26 13:13:56 +02:00
|
|
|
case STATUS_CONNECTION_ERROR:
|
|
|
|
case STATUS_SSL_HANDSHAKE_ERROR: {
|
2014-02-10 02:10:30 +01:00
|
|
|
return ERR_CONNECTION_ERROR;
|
|
|
|
} break;
|
|
|
|
case STATUS_CANT_CONNECT: {
|
|
|
|
return ERR_CANT_CONNECT;
|
|
|
|
} break;
|
|
|
|
case STATUS_CANT_RESOLVE: {
|
|
|
|
return ERR_CANT_RESOLVE;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HTTPClient::get_response_body_length() const {
|
|
|
|
return body_size;
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedByteArray HTTPClient::read_response_body_chunk() {
|
|
|
|
ERR_FAIL_COND_V(status != STATUS_BODY, PackedByteArray());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedByteArray ret;
|
2017-03-05 16:44:50 +01:00
|
|
|
Error err = OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (chunked) {
|
2017-03-05 16:44:50 +01:00
|
|
|
while (true) {
|
2019-02-20 04:33:31 +01:00
|
|
|
if (chunk_trailer_part) {
|
|
|
|
// We need to consume the trailer part too or keep-alive will break
|
|
|
|
uint8_t b;
|
|
|
|
int rec = 0;
|
|
|
|
err = _get_http_data(&b, 1, rec);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (rec == 0) {
|
2019-02-20 04:33:31 +01:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-02-20 04:33:31 +01:00
|
|
|
|
|
|
|
chunk.push_back(b);
|
|
|
|
int cs = chunk.size();
|
|
|
|
if ((cs >= 2 && chunk[cs - 2] == '\r' && chunk[cs - 1] == '\n')) {
|
|
|
|
if (cs == 2) {
|
|
|
|
// Finally over
|
|
|
|
chunk_trailer_part = false;
|
|
|
|
status = STATUS_CONNECTED;
|
|
|
|
chunk.clear();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// We do not process nor return the trailer data
|
|
|
|
chunk.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (chunk_left == 0) {
|
2017-12-12 20:02:25 +01:00
|
|
|
// Reading length
|
2014-02-10 02:10:30 +01:00
|
|
|
uint8_t b;
|
2017-03-05 16:44:50 +01:00
|
|
|
int rec = 0;
|
|
|
|
err = _get_http_data(&b, 1, rec);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (rec == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
chunk.push_back(b);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (chunk.size() > 32) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_PRINT("HTTP Invalid chunk hex len");
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTION_ERROR;
|
2019-02-20 03:06:02 +01:00
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (chunk.size() > 2 && chunk[chunk.size() - 2] == '\r' && chunk[chunk.size() - 1] == '\n') {
|
|
|
|
int len = 0;
|
|
|
|
for (int i = 0; i < chunk.size() - 2; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
char c = chunk[i];
|
2017-03-05 16:44:50 +01:00
|
|
|
int v = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (c >= '0' && c <= '9') {
|
2017-03-05 16:44:50 +01:00
|
|
|
v = c - '0';
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
2017-03-05 16:44:50 +01:00
|
|
|
v = c - 'a' + 10;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
2017-03-05 16:44:50 +01:00
|
|
|
v = c - 'A' + 10;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_PRINT("HTTP Chunk len not in hex!!");
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTION_ERROR;
|
2019-02-20 03:06:02 +01:00
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
len <<= 4;
|
|
|
|
len |= v;
|
|
|
|
if (len > (1 << 24)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_PRINT("HTTP Chunk too big!! >16mb");
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTION_ERROR;
|
2019-02-20 03:06:02 +01:00
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (len == 0) {
|
2017-12-12 20:02:25 +01:00
|
|
|
// End reached!
|
2019-02-20 04:33:31 +01:00
|
|
|
chunk_trailer_part = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
chunk.clear();
|
2019-02-20 03:06:02 +01:00
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
chunk_left = len + 2;
|
2014-02-10 02:10:30 +01:00
|
|
|
chunk.resize(chunk_left);
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
int rec = 0;
|
2018-07-25 03:11:03 +02:00
|
|
|
err = _get_http_data(&chunk.write[chunk.size() - chunk_left], chunk_left, rec);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (rec == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
chunk_left -= rec;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (chunk_left == 0) {
|
|
|
|
if (chunk[chunk.size() - 2] != '\r' || chunk[chunk.size() - 1] != '\n') {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_PRINT("HTTP Invalid chunk terminator (not \\r\\n)");
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTION_ERROR;
|
2019-02-20 03:06:02 +01:00
|
|
|
break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ret.resize(chunk.size() - 2);
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *w = ret.ptrw();
|
|
|
|
copymem(w, chunk.ptr(), chunk.size() - 2);
|
2014-02-10 02:10:30 +01:00
|
|
|
chunk.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2018-04-18 20:50:31 +02:00
|
|
|
int to_read = !read_until_eof ? MIN(body_left, read_chunk_size) : read_chunk_size;
|
2015-02-11 11:57:39 +01:00
|
|
|
ret.resize(to_read);
|
2014-08-21 17:08:53 +02:00
|
|
|
int _offset = 0;
|
2019-02-20 03:06:02 +01:00
|
|
|
while (to_read > 0) {
|
2017-03-05 16:44:50 +01:00
|
|
|
int rec = 0;
|
2017-02-07 10:21:17 +01:00
|
|
|
{
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *w = ret.ptrw();
|
|
|
|
err = _get_http_data(w + _offset, to_read, rec);
|
2017-02-07 10:21:17 +01:00
|
|
|
}
|
2019-02-20 03:06:02 +01:00
|
|
|
if (rec <= 0) { // Ended up reading less
|
|
|
|
ret.resize(_offset);
|
2015-02-11 11:57:39 +01:00
|
|
|
break;
|
2018-04-18 20:50:31 +02:00
|
|
|
} else {
|
|
|
|
_offset += rec;
|
2019-02-20 03:06:02 +01:00
|
|
|
to_read -= rec;
|
2018-04-18 20:50:31 +02:00
|
|
|
if (!read_until_eof) {
|
|
|
|
body_left -= rec;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
if (err != OK) {
|
2019-02-20 03:06:02 +01:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-21 17:08:53 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (err != OK) {
|
2014-02-10 02:10:30 +01:00
|
|
|
close();
|
2018-04-18 20:50:31 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (err == ERR_FILE_EOF) {
|
2017-12-12 20:02:25 +01:00
|
|
|
status = STATUS_DISCONNECTED; // Server disconnected
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTION_ERROR;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2019-02-20 03:06:02 +01:00
|
|
|
} else if (body_left == 0 && !chunked && !read_until_eof) {
|
2017-03-05 16:44:50 +01:00
|
|
|
status = STATUS_CONNECTED;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 03:06:02 +01:00
|
|
|
return ret;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HTTPClient::Status HTTPClient::get_status() const {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-04-29 02:56:43 +02:00
|
|
|
void HTTPClient::set_blocking_mode(bool p_enable) {
|
2017-03-05 16:44:50 +01:00
|
|
|
blocking = p_enable;
|
2014-04-29 02:56:43 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool HTTPClient::is_blocking_mode_enabled() const {
|
2014-04-29 02:56:43 +02:00
|
|
|
return blocking;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error HTTPClient::_get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
|
2015-02-11 11:57:39 +01:00
|
|
|
if (blocking) {
|
2018-09-11 17:46:14 +02:00
|
|
|
// We can't use StreamPeer.get_data, since when reaching EOF we will get an
|
|
|
|
// error without knowing how many bytes we received.
|
|
|
|
Error err = ERR_FILE_EOF;
|
2018-09-15 21:10:45 +02:00
|
|
|
int read = 0;
|
2018-09-11 17:46:14 +02:00
|
|
|
int left = p_bytes;
|
|
|
|
r_received = 0;
|
|
|
|
while (left > 0) {
|
2018-09-15 21:10:45 +02:00
|
|
|
err = connection->get_partial_data(p_buffer + r_received, left, read);
|
2018-09-11 17:46:14 +02:00
|
|
|
if (err == OK) {
|
|
|
|
r_received += read;
|
|
|
|
} else if (err == ERR_FILE_EOF) {
|
|
|
|
r_received += read;
|
|
|
|
return err;
|
|
|
|
} else {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
left -= read;
|
|
|
|
}
|
2015-02-11 11:57:39 +01:00
|
|
|
return err;
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
return connection->get_partial_data(p_buffer, p_bytes, r_received);
|
2015-02-11 11:57:39 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-29 02:56:43 +02:00
|
|
|
|
2017-10-26 03:39:41 +02:00
|
|
|
void HTTPClient::set_read_chunk_size(int p_size) {
|
|
|
|
ERR_FAIL_COND(p_size < 256 || p_size > (1 << 24));
|
|
|
|
read_chunk_size = p_size;
|
|
|
|
}
|
|
|
|
|
2019-11-24 13:20:24 +01:00
|
|
|
int HTTPClient::get_read_chunk_size() const {
|
|
|
|
return read_chunk_size;
|
|
|
|
}
|
|
|
|
|
2017-10-26 03:39:41 +02:00
|
|
|
HTTPClient::HTTPClient() {
|
2018-09-02 06:36:45 +02:00
|
|
|
tcp_connection.instance();
|
2017-10-26 03:39:41 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
HTTPClient::~HTTPClient() {}
|
2017-10-26 03:39:41 +02:00
|
|
|
|
|
|
|
#endif // #ifndef JAVASCRIPT_ENABLED
|
|
|
|
|
|
|
|
String HTTPClient::query_string_from_dict(const Dictionary &p_dict) {
|
|
|
|
String query = "";
|
|
|
|
Array keys = p_dict.keys();
|
|
|
|
for (int i = 0; i < keys.size(); ++i) {
|
2018-04-12 21:12:34 +02:00
|
|
|
String encoded_key = String(keys[i]).http_escape();
|
2018-03-17 13:09:26 +01:00
|
|
|
Variant value = p_dict[keys[i]];
|
|
|
|
switch (value.get_type()) {
|
|
|
|
case Variant::ARRAY: {
|
|
|
|
// Repeat the key with every values
|
|
|
|
Array values = value;
|
|
|
|
for (int j = 0; j < values.size(); ++j) {
|
2018-04-12 21:12:34 +02:00
|
|
|
query += "&" + encoded_key + "=" + String(values[j]).http_escape();
|
2018-03-17 13:09:26 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Variant::NIL: {
|
|
|
|
// Add the key with no value
|
|
|
|
query += "&" + encoded_key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// Add the key-value pair
|
2018-04-12 21:12:34 +02:00
|
|
|
query += "&" + encoded_key + "=" + String(value).http_escape();
|
2018-03-17 13:09:26 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-26 03:39:41 +02:00
|
|
|
}
|
|
|
|
query.erase(0, 1);
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary HTTPClient::_get_response_headers_as_dictionary() {
|
|
|
|
List<String> rh;
|
|
|
|
get_response_headers(&rh);
|
|
|
|
Dictionary ret;
|
|
|
|
for (const List<String>::Element *E = rh.front(); E; E = E->next()) {
|
2019-06-26 15:08:25 +02:00
|
|
|
const String &s = E->get();
|
2017-10-26 03:39:41 +02:00
|
|
|
int sp = s.find(":");
|
2020-05-14 16:41:43 +02:00
|
|
|
if (sp == -1) {
|
2017-10-26 03:39:41 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-26 03:39:41 +02:00
|
|
|
String key = s.substr(0, sp).strip_edges();
|
|
|
|
String value = s.substr(sp + 1, s.length()).strip_edges();
|
|
|
|
ret[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedStringArray HTTPClient::_get_response_headers() {
|
2017-10-26 03:39:41 +02:00
|
|
|
List<String> rh;
|
|
|
|
get_response_headers(&rh);
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedStringArray ret;
|
2017-10-26 03:39:41 +02:00
|
|
|
ret.resize(rh.size());
|
|
|
|
int idx = 0;
|
|
|
|
for (const List<String>::Element *E = rh.front(); E; E = E->next()) {
|
|
|
|
ret.set(idx++, E->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void HTTPClient::_bind_methods() {
|
2017-12-12 20:02:25 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port", "use_ssl", "verify_host"), &HTTPClient::connect_to_host, DEFVAL(-1), DEFVAL(false), DEFVAL(true));
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_connection", "connection"), &HTTPClient::set_connection);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_connection"), &HTTPClient::get_connection);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("request_raw", "method", "url", "headers", "body"), &HTTPClient::request_raw);
|
|
|
|
ClassDB::bind_method(D_METHOD("request", "method", "url", "headers", "body"), &HTTPClient::request, DEFVAL(String()));
|
|
|
|
ClassDB::bind_method(D_METHOD("close"), &HTTPClient::close);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("has_response"), &HTTPClient::has_response);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_response_chunked"), &HTTPClient::is_response_chunked);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_response_code"), &HTTPClient::get_response_code);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_response_headers"), &HTTPClient::_get_response_headers);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_response_headers_as_dictionary"), &HTTPClient::_get_response_headers_as_dictionary);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_response_body_length"), &HTTPClient::get_response_body_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("read_response_body_chunk"), &HTTPClient::read_response_body_chunk);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_read_chunk_size", "bytes"), &HTTPClient::set_read_chunk_size);
|
2019-11-24 13:20:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_read_chunk_size"), &HTTPClient::get_read_chunk_size);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_blocking_mode", "enabled"), &HTTPClient::set_blocking_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_blocking_mode_enabled"), &HTTPClient::is_blocking_mode_enabled);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_status"), &HTTPClient::get_status);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("poll"), &HTTPClient::poll);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("query_string_from_dict", "fields"), &HTTPClient::query_string_from_dict);
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2018-01-11 23:35:12 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_mode_enabled"), "set_blocking_mode", "is_blocking_mode_enabled");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "connection", PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer", 0), "set_connection", "get_connection");
|
2019-11-24 13:20:24 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "read_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_read_chunk_size", "get_read_chunk_size");
|
2018-01-11 23:35:12 +01:00
|
|
|
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(METHOD_GET);
|
|
|
|
BIND_ENUM_CONSTANT(METHOD_HEAD);
|
|
|
|
BIND_ENUM_CONSTANT(METHOD_POST);
|
|
|
|
BIND_ENUM_CONSTANT(METHOD_PUT);
|
|
|
|
BIND_ENUM_CONSTANT(METHOD_DELETE);
|
|
|
|
BIND_ENUM_CONSTANT(METHOD_OPTIONS);
|
|
|
|
BIND_ENUM_CONSTANT(METHOD_TRACE);
|
|
|
|
BIND_ENUM_CONSTANT(METHOD_CONNECT);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(METHOD_PATCH);
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(METHOD_MAX);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(STATUS_DISCONNECTED);
|
2017-12-12 20:02:25 +01:00
|
|
|
BIND_ENUM_CONSTANT(STATUS_RESOLVING); // Resolving hostname (if hostname was passed in)
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(STATUS_CANT_RESOLVE);
|
2017-12-12 20:02:25 +01:00
|
|
|
BIND_ENUM_CONSTANT(STATUS_CONNECTING); // Connecting to IP
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(STATUS_CANT_CONNECT);
|
2017-12-12 20:02:25 +01:00
|
|
|
BIND_ENUM_CONSTANT(STATUS_CONNECTED); // Connected, now accepting requests
|
|
|
|
BIND_ENUM_CONSTANT(STATUS_REQUESTING); // Request in progress
|
|
|
|
BIND_ENUM_CONSTANT(STATUS_BODY); // Request resulted in body which must be read
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(STATUS_CONNECTION_ERROR);
|
|
|
|
BIND_ENUM_CONSTANT(STATUS_SSL_HANDSHAKE_ERROR);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_CONTINUE);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_SWITCHING_PROTOCOLS);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_PROCESSING);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// 2xx successful
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_OK);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_CREATED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_ACCEPTED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_NON_AUTHORITATIVE_INFORMATION);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_NO_CONTENT);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_RESET_CONTENT);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_PARTIAL_CONTENT);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_MULTI_STATUS);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_ALREADY_REPORTED);
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_IM_USED);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// 3xx redirection
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_MULTIPLE_CHOICES);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_MOVED_PERMANENTLY);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_FOUND);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_SEE_OTHER);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_NOT_MODIFIED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_USE_PROXY);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_SWITCH_PROXY);
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_TEMPORARY_REDIRECT);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_PERMANENT_REDIRECT);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// 4xx client error
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_BAD_REQUEST);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_UNAUTHORIZED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_PAYMENT_REQUIRED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_FORBIDDEN);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_NOT_FOUND);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_METHOD_NOT_ALLOWED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_NOT_ACCEPTABLE);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_PROXY_AUTHENTICATION_REQUIRED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_REQUEST_TIMEOUT);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_CONFLICT);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_GONE);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_LENGTH_REQUIRED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_PRECONDITION_FAILED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_REQUEST_ENTITY_TOO_LARGE);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_REQUEST_URI_TOO_LONG);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_UNSUPPORTED_MEDIA_TYPE);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_EXPECTATION_FAILED);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_IM_A_TEAPOT);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_MISDIRECTED_REQUEST);
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_UNPROCESSABLE_ENTITY);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_LOCKED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_FAILED_DEPENDENCY);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_UPGRADE_REQUIRED);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_PRECONDITION_REQUIRED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_TOO_MANY_REQUESTS);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_REQUEST_HEADER_FIELDS_TOO_LARGE);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_UNAVAILABLE_FOR_LEGAL_REASONS);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
// 5xx server error
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_INTERNAL_SERVER_ERROR);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_NOT_IMPLEMENTED);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_BAD_GATEWAY);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_SERVICE_UNAVAILABLE);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_GATEWAY_TIMEOUT);
|
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_HTTP_VERSION_NOT_SUPPORTED);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_VARIANT_ALSO_NEGOTIATES);
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_INSUFFICIENT_STORAGE);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_LOOP_DETECTED);
|
2017-08-20 17:45:01 +02:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_NOT_EXTENDED);
|
2017-12-10 23:18:44 +01:00
|
|
|
BIND_ENUM_CONSTANT(RESPONSE_NETWORK_AUTH_REQUIRED);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|