Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a floating-point number for the timeout property in HTTPRequest #60402

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions doc/classes/HTTPRequest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,21 @@
</methods>
<members>
<member name="body_size_limit" type="int" setter="set_body_size_limit" getter="get_body_size_limit" default="-1">
Maximum allowed size for response bodies.
Maximum allowed size for response bodies ([code]-1[/code] means no limit). When only small files are expected, this can be used to prevent disallow receiving files that are too large, preventing potential denial of service attacks.
</member>
<member name="download_chunk_size" type="int" setter="set_download_chunk_size" getter="get_download_chunk_size" default="65536">
The size of the buffer used and maximum bytes to read per iteration. See [member HTTPClient.read_chunk_size].
Set this to a lower value (e.g. 4096 for 4 KiB) when downloading small files to decrease memory usage at the cost of download speeds.
</member>
<member name="download_file" type="String" setter="set_download_file" getter="get_download_file" default="&quot;&quot;">
The file to download into. Will output any received file into it.
The file to download into. If set to a non-empty string, the request output will be written to the file located at the path. If a file already exists at the specified location, it will be overwritten as soon as body data begins to be received.
[b]Note:[/b] Folders are not automatically created when the file is created. If [member download_file] points to a subfolder, it's recommended to create the necessary folders beforehand using [method Directory.make_dir_recursive] to ensure the file can be written.
</member>
<member name="max_redirects" type="int" setter="set_max_redirects" getter="get_max_redirects" default="8">
Maximum number of allowed redirects.
Maximum number of allowed redirects. This is used to prevent endless redirect loops.
</member>
<member name="timeout" type="int" setter="set_timeout" getter="get_timeout" default="0">
<member name="timeout" type="float" setter="set_timeout" getter="get_timeout" default="0.0">
If set to a value greater than [code]0.0[/code], the HTTP request will time out after [code]timeout[/code] seconds have passed and the request is not [i]completed[/i] yet. For small HTTP requests such as REST API usage, set [member timeout] to a value greater than [code]0.0[/code] to prevent the application from getting stuck if the request fails to get a response in a timely manner. For file downloads, leave this to [code]0.0[/code] to prevent the download from failing if it takes too much time.
</member>
<member name="use_threads" type="bool" setter="set_use_threads" getter="is_using_threads" default="false">
If [code]true[/code], multithreading is used to improve performance.
Expand Down
18 changes: 11 additions & 7 deletions scene/main/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Error HTTPRequest::request_raw(const String &p_url, const Vector<String> &p_cust
ERR_FAIL_COND_V(!is_inside_tree(), ERR_UNCONFIGURED);
ERR_FAIL_COND_V_MSG(requesting, ERR_BUSY, "HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");

if (timeout > 0) {
if (timeout > 0.0) {
timer->stop();
timer->start(timeout);
}
Expand Down Expand Up @@ -471,12 +471,16 @@ void HTTPRequest::set_https_proxy(const String &p_host, int p_port) {
client->set_https_proxy(p_host, p_port);
}

void HTTPRequest::set_timeout(int p_timeout) {
ERR_FAIL_COND(p_timeout < 0);
timeout = p_timeout;
void HTTPRequest::set_timeout(double p_timeout) {
if (Math::is_zero_approx(p_timeout)) {
timeout = 0.0;
} else {
ERR_FAIL_COND(p_timeout < 0.0);
timeout = p_timeout;
}
}

int HTTPRequest::get_timeout() {
double HTTPRequest::get_timeout() {
return timeout;
}

Expand Down Expand Up @@ -526,7 +530,7 @@ void HTTPRequest::_bind_methods() {
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");
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_redirects", PROPERTY_HINT_RANGE, "-1,64"), "set_max_redirects", "get_max_redirects");
ADD_PROPERTY(PropertyInfo(Variant::INT, "timeout", PROPERTY_HINT_RANGE, "0,86400"), "set_timeout", "get_timeout");
ADD_PROPERTY(PropertyInfo(Variant::REAL, "timeout", PROPERTY_HINT_RANGE, "0,3600,0.1,or_greater"), "set_timeout", "get_timeout");

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")));

Expand Down Expand Up @@ -565,7 +569,7 @@ HTTPRequest::HTTPRequest() {
timer->set_one_shot(true);
timer->connect("timeout", this, "_timeout");
add_child(timer);
timeout = 0;
timeout = 0.0;
}

HTTPRequest::~HTTPRequest() {
Expand Down
6 changes: 3 additions & 3 deletions scene/main/http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class HTTPRequest : public Node {

int max_redirects;

int timeout;
double timeout;

void _redirect_request(const String &p_new_url);

Expand Down Expand Up @@ -138,8 +138,8 @@ class HTTPRequest : public Node {

Timer *timer;

void set_timeout(int p_timeout);
int get_timeout();
void set_timeout(double p_timeout);
double get_timeout();

void _timeout();

Expand Down