Debian Codegraph @

scene/main/http_request.h

raw symbols SCIP
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 _is_content_header(const String &p_header) const;
106 bool _is_method_safe() const;
107 Error _get_redirect_headers(Vector<String> *r_headers);
108
109 bool _handle_response(bool *ret_value);
110
111 Error _parse_url(const String &p_url);
112 Error _request();
113
114 bool has_header(const PackedStringArray &p_headers, const String &p_header_name);
115 String get_header_value(const PackedStringArray &p_headers, const String &header_name);
116
117 SafeFlag thread_done;
118 SafeFlag thread_request_quit;
119
120 Thread thread;
121
122 void _defer_done(int p_status, int p_code, const PackedStringArray &p_headers, const PackedByteArray &p_data);
123 void _request_done(int p_status, int p_code, const PackedStringArray &p_headers, const PackedByteArray &p_data);
124 static void _thread_func(void *p_userdata);
125
126 protected:
127 void _notification(int p_what);
128 static void _bind_methods();
129
130 public:
131 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
132 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
133 void cancel_request();
134 HTTPClient::Status get_http_client_status() const;
135
136 void set_use_threads(bool p_use);
137 bool is_using_threads() const;
138
139 void set_accept_gzip(bool p_gzip);
140 bool is_accepting_gzip() const;
141
142 void set_download_file(const String &p_file);
143 String get_download_file() const;
144
145 void set_download_chunk_size(int p_chunk_size);
146 int get_download_chunk_size() const;
147
148 void set_body_size_limit(int p_bytes);
149 int get_body_size_limit() const;
150
151 void set_max_redirects(int p_max);
152 int get_max_redirects() const;
153
154 Timer *timer = nullptr;
155
156 void set_timeout(double p_timeout);
157 double get_timeout();
158
159 void _timeout();
160
161 int get_downloaded_bytes() const;
162 int get_body_size() const;
163
164 void set_http_proxy(const String &p_host, int p_port);
165 void set_https_proxy(const String &p_host, int p_port);
166
167 void set_tls_options(const Ref<TLSOptions> &p_options);
168
169 HTTPRequest();
170 };
171
172 VARIANT_ENUM_CAST(HTTPRequest::Result);