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