http_request.h 5.8 KB

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