http_request.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "core/io/http_client.h"
  33. #include "core/os/file_access.h"
  34. #include "core/os/thread.h"
  35. #include "node.h"
  36. class HTTPRequest : public Node {
  37. GDCLASS(HTTPRequest, Node);
  38. public:
  39. enum Result {
  40. RESULT_SUCCESS,
  41. RESULT_CHUNKED_BODY_SIZE_MISMATCH,
  42. RESULT_CANT_CONNECT,
  43. RESULT_CANT_RESOLVE,
  44. RESULT_CONNECTION_ERROR,
  45. RESULT_SSL_HANDSHAKE_ERROR,
  46. RESULT_NO_RESPONSE,
  47. RESULT_BODY_SIZE_LIMIT_EXCEEDED,
  48. RESULT_REQUEST_FAILED,
  49. RESULT_DOWNLOAD_FILE_CANT_OPEN,
  50. RESULT_DOWNLOAD_FILE_WRITE_ERROR,
  51. RESULT_REDIRECT_LIMIT_REACHED
  52. };
  53. private:
  54. bool requesting;
  55. String request_string;
  56. String url;
  57. int port;
  58. Vector<String> headers;
  59. bool validate_ssl;
  60. bool use_ssl;
  61. HTTPClient::Method method;
  62. String request_data;
  63. bool request_sent;
  64. Ref<HTTPClient> client;
  65. PoolByteArray body;
  66. volatile bool use_threads;
  67. bool got_response;
  68. int response_code;
  69. PoolVector<String> response_headers;
  70. String download_to_file;
  71. FileAccess *file;
  72. int body_len;
  73. volatile int downloaded;
  74. int body_size_limit;
  75. int redirections;
  76. HTTPClient::Status status;
  77. bool _update_connection();
  78. int max_redirects;
  79. void _redirect_request(const String &p_new_url);
  80. bool _handle_response(bool *ret_value);
  81. Error _parse_url(const String &p_url);
  82. Error _request();
  83. volatile bool thread_done;
  84. volatile bool thread_request_quit;
  85. Thread *thread;
  86. void _request_done(int p_status, int p_code, const PoolStringArray &headers, const PoolByteArray &p_data);
  87. static void _thread_func(void *p_userdata);
  88. protected:
  89. void _notification(int p_what);
  90. static void _bind_methods();
  91. public:
  92. 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
  93. void cancel_request();
  94. HTTPClient::Status get_http_client_status() const;
  95. void set_use_threads(bool p_use);
  96. bool is_using_threads() const;
  97. void set_download_file(const String &p_file);
  98. String get_download_file() const;
  99. void set_body_size_limit(int p_bytes);
  100. int get_body_size_limit() const;
  101. void set_max_redirects(int p_max);
  102. int get_max_redirects() const;
  103. int get_downloaded_bytes() const;
  104. int get_body_size() const;
  105. HTTPRequest();
  106. ~HTTPRequest();
  107. };
  108. VARIANT_ENUM_CAST(HTTPRequest::Result);
  109. #endif // HTTPREQUEST_H