Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/main/http_request.h
9902 views
1
/**************************************************************************/
2
/* http_request.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/io/http_client.h"
34
#include "core/io/stream_peer_gzip.h"
35
#include "core/os/thread.h"
36
#include "core/templates/safe_refcount.h"
37
#include "scene/main/node.h"
38
39
class Timer;
40
41
class HTTPRequest : public Node {
42
GDCLASS(HTTPRequest, Node);
43
44
public:
45
enum Result {
46
RESULT_SUCCESS,
47
RESULT_CHUNKED_BODY_SIZE_MISMATCH,
48
RESULT_CANT_CONNECT,
49
RESULT_CANT_RESOLVE,
50
RESULT_CONNECTION_ERROR,
51
RESULT_TLS_HANDSHAKE_ERROR,
52
RESULT_NO_RESPONSE,
53
RESULT_BODY_SIZE_LIMIT_EXCEEDED,
54
RESULT_BODY_DECOMPRESS_FAILED,
55
RESULT_REQUEST_FAILED,
56
RESULT_DOWNLOAD_FILE_CANT_OPEN,
57
RESULT_DOWNLOAD_FILE_WRITE_ERROR,
58
RESULT_REDIRECT_LIMIT_REACHED,
59
RESULT_TIMEOUT
60
61
};
62
63
private:
64
bool requesting = false;
65
66
String request_string;
67
String url;
68
int port = 80;
69
Vector<String> headers;
70
bool use_tls = false;
71
Ref<TLSOptions> tls_options;
72
HTTPClient::Method method;
73
Vector<uint8_t> request_data;
74
75
bool request_sent = false;
76
Ref<HTTPClient> client;
77
PackedByteArray body;
78
SafeFlag use_threads;
79
bool accept_gzip = true;
80
81
bool got_response = false;
82
int response_code = 0;
83
Vector<String> response_headers;
84
85
String download_to_file;
86
87
Ref<StreamPeerGZIP> decompressor;
88
Ref<FileAccess> file;
89
90
int body_len = -1;
91
SafeNumeric<int> downloaded;
92
SafeNumeric<int> final_body_size;
93
int body_size_limit = -1;
94
95
int redirections = 0;
96
97
bool _update_connection();
98
99
int max_redirects = 8;
100
101
double timeout = 0;
102
103
void _redirect_request(const String &p_new_url);
104
105
bool _handle_response(bool *ret_value);
106
107
Error _parse_url(const String &p_url);
108
Error _request();
109
110
bool has_header(const PackedStringArray &p_headers, const String &p_header_name);
111
String get_header_value(const PackedStringArray &p_headers, const String &header_name);
112
113
SafeFlag thread_done;
114
SafeFlag thread_request_quit;
115
116
Thread thread;
117
118
void _defer_done(int p_status, int p_code, const PackedStringArray &p_headers, const PackedByteArray &p_data);
119
void _request_done(int p_status, int p_code, const PackedStringArray &p_headers, const PackedByteArray &p_data);
120
static void _thread_func(void *p_userdata);
121
122
protected:
123
void _notification(int p_what);
124
static void _bind_methods();
125
126
public:
127
Error request(const String &p_url, const Vector<String> &p_custom_headers = Vector<String>(), HTTPClient::Method p_method = HTTPClient::METHOD_GET, const String &p_request_data = ""); //connects to a full url and perform request
128
Error request_raw(const String &p_url, const Vector<String> &p_custom_headers = Vector<String>(), HTTPClient::Method p_method = HTTPClient::METHOD_GET, const Vector<uint8_t> &p_request_data_raw = Vector<uint8_t>()); //connects to a full url and perform request
129
void cancel_request();
130
HTTPClient::Status get_http_client_status() const;
131
132
void set_use_threads(bool p_use);
133
bool is_using_threads() const;
134
135
void set_accept_gzip(bool p_gzip);
136
bool is_accepting_gzip() const;
137
138
void set_download_file(const String &p_file);
139
String get_download_file() const;
140
141
void set_download_chunk_size(int p_chunk_size);
142
int get_download_chunk_size() const;
143
144
void set_body_size_limit(int p_bytes);
145
int get_body_size_limit() const;
146
147
void set_max_redirects(int p_max);
148
int get_max_redirects() const;
149
150
Timer *timer = nullptr;
151
152
void set_timeout(double p_timeout);
153
double get_timeout();
154
155
void _timeout();
156
157
int get_downloaded_bytes() const;
158
int get_body_size() const;
159
160
void set_http_proxy(const String &p_host, int p_port);
161
void set_https_proxy(const String &p_host, int p_port);
162
163
void set_tls_options(const Ref<TLSOptions> &p_options);
164
165
HTTPRequest();
166
};
167
168
VARIANT_ENUM_CAST(HTTPRequest::Result);
169
170