| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  http_client_tcp.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_CLIENT_TCP_H | 
|---|
| 32 | #define HTTP_CLIENT_TCP_H | 
|---|
| 33 |  | 
|---|
| 34 | #include "http_client.h" | 
|---|
| 35 |  | 
|---|
| 36 | #include "core/crypto/crypto.h" | 
|---|
| 37 |  | 
|---|
| 38 | class HTTPClientTCP : public HTTPClient { | 
|---|
| 39 | private: | 
|---|
| 40 | Status status = STATUS_DISCONNECTED; | 
|---|
| 41 | IP::ResolverID resolving = IP::RESOLVER_INVALID_ID; | 
|---|
| 42 | Array ip_candidates; | 
|---|
| 43 | int conn_port = -1; // Server to make requests to. | 
|---|
| 44 | String conn_host; | 
|---|
| 45 | int server_port = -1; // Server to connect to (might be a proxy server). | 
|---|
| 46 | String server_host; | 
|---|
| 47 | int http_proxy_port = -1; // Proxy server for http requests. | 
|---|
| 48 | String http_proxy_host; | 
|---|
| 49 | int https_proxy_port = -1; // Proxy server for https requests. | 
|---|
| 50 | String https_proxy_host; | 
|---|
| 51 | bool blocking = false; | 
|---|
| 52 | bool handshaking = false; | 
|---|
| 53 | bool head_request = false; | 
|---|
| 54 | Ref<TLSOptions> tls_options; | 
|---|
| 55 |  | 
|---|
| 56 | Vector<uint8_t> response_str; | 
|---|
| 57 |  | 
|---|
| 58 | bool chunked = false; | 
|---|
| 59 | Vector<uint8_t> chunk; | 
|---|
| 60 | int chunk_left = 0; | 
|---|
| 61 | bool chunk_trailer_part = false; | 
|---|
| 62 | int64_t body_size = -1; | 
|---|
| 63 | int64_t body_left = 0; | 
|---|
| 64 | bool read_until_eof = false; | 
|---|
| 65 |  | 
|---|
| 66 | Ref<StreamPeerBuffer> request_buffer; | 
|---|
| 67 | Ref<StreamPeerTCP> tcp_connection; | 
|---|
| 68 | Ref<StreamPeer> connection; | 
|---|
| 69 | Ref<HTTPClientTCP> proxy_client; // Negotiate with proxy server. | 
|---|
| 70 |  | 
|---|
| 71 | int response_num = 0; | 
|---|
| 72 | Vector<String> ; | 
|---|
| 73 | // 64 KiB by default (favors fast download speeds at the cost of memory usage). | 
|---|
| 74 | int read_chunk_size = 65536; | 
|---|
| 75 |  | 
|---|
| 76 | Error _get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received); | 
|---|
| 77 |  | 
|---|
| 78 | public: | 
|---|
| 79 | static HTTPClient *_create_func(); | 
|---|
| 80 |  | 
|---|
| 81 | Error request(Method p_method, const String &p_url, const Vector<String> &, const uint8_t *p_body, int p_body_size) override; | 
|---|
| 82 |  | 
|---|
| 83 | Error connect_to_host(const String &p_host, int p_port = -1, Ref<TLSOptions> p_tls_options = Ref<TLSOptions>()) override; | 
|---|
| 84 | void set_connection(const Ref<StreamPeer> &p_connection) override; | 
|---|
| 85 | Ref<StreamPeer> get_connection() const override; | 
|---|
| 86 | void close() override; | 
|---|
| 87 | Status get_status() const override; | 
|---|
| 88 | bool has_response() const override; | 
|---|
| 89 | bool is_response_chunked() const override; | 
|---|
| 90 | int get_response_code() const override; | 
|---|
| 91 | Error (List<String> *r_response) override; | 
|---|
| 92 | int64_t get_response_body_length() const override; | 
|---|
| 93 | PackedByteArray read_response_body_chunk() override; | 
|---|
| 94 | void set_blocking_mode(bool p_enable) override; | 
|---|
| 95 | bool is_blocking_mode_enabled() const override; | 
|---|
| 96 | void set_read_chunk_size(int p_size) override; | 
|---|
| 97 | int get_read_chunk_size() const override; | 
|---|
| 98 | Error poll() override; | 
|---|
| 99 | void set_http_proxy(const String &p_host, int p_port) override; | 
|---|
| 100 | void set_https_proxy(const String &p_host, int p_port) override; | 
|---|
| 101 | HTTPClientTCP(); | 
|---|
| 102 | }; | 
|---|
| 103 |  | 
|---|
| 104 | #endif // HTTP_CLIENT_TCP_H | 
|---|
| 105 |  | 
|---|