http_request.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/os/file_access.h"
  34. #include "core/os/thread.h"
  35. #include "core/safe_refcount.h"
  36. #include "node.h"
  37. #include "scene/main/timer.h"
  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_SSL_HANDSHAKE_ERROR,
  48. RESULT_NO_RESPONSE,
  49. RESULT_BODY_SIZE_LIMIT_EXCEEDED,
  50. RESULT_REQUEST_FAILED,
  51. RESULT_DOWNLOAD_FILE_CANT_OPEN,
  52. RESULT_DOWNLOAD_FILE_WRITE_ERROR,
  53. RESULT_REDIRECT_LIMIT_REACHED,
  54. RESULT_TIMEOUT
  55. };
  56. private:
  57. bool requesting;
  58. String request_string;
  59. String url;
  60. int port;
  61. Vector<String> headers;
  62. bool validate_ssl;
  63. bool use_ssl;
  64. HTTPClient::Method method;
  65. PoolVector<uint8_t> request_data;
  66. bool request_sent;
  67. Ref<HTTPClient> client;
  68. PoolByteArray body;
  69. SafeFlag use_threads;
  70. bool got_response;
  71. int response_code;
  72. PoolVector<String> response_headers;
  73. String download_to_file;
  74. FileAccess *file;
  75. int body_len;
  76. SafeNumeric<int> downloaded;
  77. int body_size_limit;
  78. int redirections;
  79. bool _update_connection();
  80. int max_redirects;
  81. double timeout;
  82. void _redirect_request(const String &p_new_url);
  83. bool _handle_response(bool *ret_value);
  84. Error _parse_url(const String &p_url);
  85. Error _request();
  86. SafeFlag thread_done;
  87. SafeFlag thread_request_quit;
  88. Thread thread;
  89. void _request_done(int p_status, int p_code, const PoolStringArray &p_headers, const PoolByteArray &p_data);
  90. static void _thread_func(void *p_userdata);
  91. protected:
  92. void _notification(int p_what);
  93. static void _bind_methods();
  94. public:
  95. 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
  96. Error request_raw(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 PoolVector<uint8_t> &p_request_data_raw = PoolVector<uint8_t>()); //connects to a full url and perform request
  97. void cancel_request();
  98. HTTPClient::Status get_http_client_status() const;
  99. void set_use_threads(bool p_use);
  100. bool is_using_threads() const;
  101. void set_download_file(const String &p_file);
  102. String get_download_file() const;
  103. void set_download_chunk_size(int p_chunk_size);
  104. int get_download_chunk_size() const;
  105. void set_body_size_limit(int p_bytes);
  106. int get_body_size_limit() const;
  107. void set_max_redirects(int p_max);
  108. int get_max_redirects() const;
  109. Timer *timer;
  110. void set_timeout(double p_timeout);
  111. double get_timeout();
  112. void _timeout();
  113. int get_downloaded_bytes() const;
  114. int get_body_size() const;
  115. // Use empty string or -1 to unset.
  116. void set_http_proxy(const String &p_host, int p_port);
  117. void set_https_proxy(const String &p_host, int p_port);
  118. HTTPRequest();
  119. ~HTTPRequest();
  120. };
  121. VARIANT_ENUM_CAST(HTTPRequest::Result);
  122. #endif // HTTP_REQUEST_H