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 | #ifndef HTTP_REQUEST_H |
32 | #define HTTP_REQUEST_H |
33 | |
34 | #include "core/io/http_client.h" |
35 | #include "core/io/stream_peer_gzip.h" |
36 | #include "core/os/thread.h" |
37 | #include "core/templates/safe_refcount.h" |
38 | #include "scene/main/node.h" |
39 | |
40 | class Timer; |
41 | |
42 | class HTTPRequest : public Node { |
43 | GDCLASS(HTTPRequest, Node); |
44 | |
45 | public: |
46 | enum Result { |
47 | RESULT_SUCCESS, |
48 | RESULT_CHUNKED_BODY_SIZE_MISMATCH, |
49 | RESULT_CANT_CONNECT, |
50 | RESULT_CANT_RESOLVE, |
51 | RESULT_CONNECTION_ERROR, |
52 | RESULT_TLS_HANDSHAKE_ERROR, |
53 | RESULT_NO_RESPONSE, |
54 | RESULT_BODY_SIZE_LIMIT_EXCEEDED, |
55 | RESULT_BODY_DECOMPRESS_FAILED, |
56 | RESULT_REQUEST_FAILED, |
57 | RESULT_DOWNLOAD_FILE_CANT_OPEN, |
58 | RESULT_DOWNLOAD_FILE_WRITE_ERROR, |
59 | RESULT_REDIRECT_LIMIT_REACHED, |
60 | RESULT_TIMEOUT |
61 | |
62 | }; |
63 | |
64 | private: |
65 | bool requesting = false; |
66 | |
67 | String request_string; |
68 | String url; |
69 | int port = 80; |
70 | Vector<String> ; |
71 | bool use_tls = false; |
72 | Ref<TLSOptions> tls_options; |
73 | HTTPClient::Method method; |
74 | Vector<uint8_t> request_data; |
75 | |
76 | bool request_sent = false; |
77 | Ref<HTTPClient> client; |
78 | PackedByteArray body; |
79 | SafeFlag use_threads; |
80 | bool accept_gzip = true; |
81 | |
82 | bool got_response = false; |
83 | int response_code = 0; |
84 | Vector<String> ; |
85 | |
86 | String download_to_file; |
87 | |
88 | Ref<StreamPeerGZIP> decompressor; |
89 | Ref<FileAccess> file; |
90 | |
91 | int body_len = -1; |
92 | SafeNumeric<int> downloaded; |
93 | SafeNumeric<int> final_body_size; |
94 | int body_size_limit = -1; |
95 | |
96 | int redirections = 0; |
97 | |
98 | bool _update_connection(); |
99 | |
100 | int max_redirects = 8; |
101 | |
102 | double timeout = 0; |
103 | |
104 | void _redirect_request(const String &p_new_url); |
105 | |
106 | bool _handle_response(bool *ret_value); |
107 | |
108 | Error _parse_url(const String &p_url); |
109 | Error _request(); |
110 | |
111 | bool (const PackedStringArray &, const String &); |
112 | String (const PackedStringArray &, const String &); |
113 | |
114 | SafeFlag thread_done; |
115 | SafeFlag thread_request_quit; |
116 | |
117 | Thread thread; |
118 | |
119 | void _defer_done(int p_status, int p_code, const PackedStringArray &, const PackedByteArray &p_data); |
120 | void _request_done(int p_status, int p_code, const PackedStringArray &, const PackedByteArray &p_data); |
121 | static void _thread_func(void *p_userdata); |
122 | |
123 | protected: |
124 | void _notification(int p_what); |
125 | static void _bind_methods(); |
126 | |
127 | public: |
128 | Error request(const String &p_url, const Vector<String> & = Vector<String>(), HTTPClient::Method p_method = HTTPClient::METHOD_GET, const String &p_request_data = "" ); //connects to a full url and perform request |
129 | Error request_raw(const String &p_url, const Vector<String> & = 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 |
130 | void cancel_request(); |
131 | HTTPClient::Status get_http_client_status() const; |
132 | |
133 | void set_use_threads(bool p_use); |
134 | bool is_using_threads() const; |
135 | |
136 | void set_accept_gzip(bool p_gzip); |
137 | bool is_accepting_gzip() const; |
138 | |
139 | void set_download_file(const String &p_file); |
140 | String get_download_file() const; |
141 | |
142 | void set_download_chunk_size(int p_chunk_size); |
143 | int get_download_chunk_size() const; |
144 | |
145 | void set_body_size_limit(int p_bytes); |
146 | int get_body_size_limit() const; |
147 | |
148 | void set_max_redirects(int p_max); |
149 | int get_max_redirects() const; |
150 | |
151 | Timer *timer = nullptr; |
152 | |
153 | void set_timeout(double p_timeout); |
154 | double get_timeout(); |
155 | |
156 | void _timeout(); |
157 | |
158 | int get_downloaded_bytes() const; |
159 | int get_body_size() const; |
160 | |
161 | void set_http_proxy(const String &p_host, int p_port); |
162 | void set_https_proxy(const String &p_host, int p_port); |
163 | |
164 | void set_tls_options(const Ref<TLSOptions> &p_options); |
165 | |
166 | HTTPRequest(); |
167 | }; |
168 | |
169 | VARIANT_ENUM_CAST(HTTPRequest::Result); |
170 | |
171 | #endif // HTTP_REQUEST_H |
172 | |