1 /*************************************************************************/ 2 /* http_request.h */ 3 /*************************************************************************/ 4 /* This file is part of: */ 5 /* GODOT ENGINE */ 6 /* https://godotengine.org */ 7 /*************************************************************************/ 8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ 9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ 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 #ifndef HTTPREQUEST_H 31 #define HTTPREQUEST_H 32 33 #include "io/http_client.h" 34 #include "node.h" 35 #include "os/file_access.h" 36 #include "os/thread.h" 37 38 class HTTPRequest : public Node { 39 40 OBJ_TYPE(HTTPRequest, Node); 41 42 public: 43 enum Result { 44 RESULT_SUCCESS, 45 //RESULT_NO_BODY, 46 RESULT_CHUNKED_BODY_SIZE_MISMATCH, 47 RESULT_CANT_CONNECT, 48 RESULT_CANT_RESOLVE, 49 RESULT_CONNECTION_ERROR, 50 RESULT_SSL_HANDSHAKE_ERROR, 51 RESULT_NO_RESPONSE, 52 RESULT_BODY_SIZE_LIMIT_EXCEEDED, 53 RESULT_REQUEST_FAILED, 54 RESULT_DOWNLOAD_FILE_CANT_OPEN, 55 RESULT_DOWNLOAD_FILE_WRITE_ERROR, 56 RESULT_REDIRECT_LIMIT_REACHED 57 58 }; 59 60 private: 61 bool requesting; 62 63 String request_string; 64 String url; 65 int port; 66 Vector<String> headers; 67 bool validate_ssl; 68 bool use_ssl; 69 HTTPClient::Method method; 70 String request_data; 71 72 bool request_sent; 73 Ref<HTTPClient> client; 74 ByteArray body; 75 volatile bool use_threads; 76 77 bool got_response; 78 int response_code; 79 DVector<String> response_headers; 80 81 String download_to_file; 82 83 FileAccess *file; 84 85 int body_len; 86 volatile int downloaded; 87 int body_size_limit; 88 89 int redirections; 90 91 HTTPClient::Status status; 92 93 bool _update_connection(); 94 95 int max_redirects; 96 97 void _redirect_request(const String &p_new_url); 98 99 bool _handle_response(bool *ret_value); 100 101 Error _parse_url(const String &p_url); 102 Error _request(); 103 104 volatile bool thread_done; 105 volatile bool thread_request_quit; 106 107 Thread *thread; 108 109 void _request_done(int p_status, int p_code, const StringArray &headers, const ByteArray &p_data); 110 static void _thread_func(void *p_userdata); 111 112 protected: 113 void _notification(int p_what); 114 static void _bind_methods(); 115 116 public: 117 Error request(const String &p_url, const Vector<String> &p_custom_headers = Vector<String>(), bool p_ssl_validate_domain = true, HTTPClient::Method p_method = HTTPClient::METHOD_GET, const String &p_request_data = ""); //connects to a full url and perform request 118 void cancel_request(); 119 HTTPClient::Status get_http_client_status() const; 120 121 void set_use_threads(bool p_use); 122 bool is_using_threads() const; 123 124 void set_download_file(const String &p_file); 125 String get_download_file() const; 126 127 void set_body_size_limit(int p_bytes); 128 int get_body_size_limit() const; 129 130 void set_max_redirects(int p_max); 131 int get_max_redirects() const; 132 133 int get_downloaded_bytes() const; 134 int get_body_size() const; 135 136 HTTPRequest(); 137 ~HTTPRequest(); 138 }; 139 140 #endif // HTTPREQUEST_H 141