http_client_web.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**************************************************************************/
  2. /* http_client_web.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_CLIENT_WEB_H
  31. #define HTTP_CLIENT_WEB_H
  32. #include "core/io/http_client.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #include <stddef.h>
  37. typedef enum {
  38. GODOT_JS_FETCH_STATE_REQUESTING = 0,
  39. GODOT_JS_FETCH_STATE_BODY = 1,
  40. GODOT_JS_FETCH_STATE_DONE = 2,
  41. GODOT_JS_FETCH_STATE_ERROR = -1,
  42. } godot_js_fetch_state_t;
  43. extern int godot_js_fetch_create(const char *p_method, const char *p_url, const char **p_headers, int p_headers_len, const uint8_t *p_body, int p_body_len);
  44. extern int godot_js_fetch_read_headers(int p_id, void (*parse_callback)(int p_size, const char **p_headers, void *p_ref), void *p_ref);
  45. extern int godot_js_fetch_read_chunk(int p_id, uint8_t *p_buf, int p_buf_size);
  46. extern void godot_js_fetch_free(int p_id);
  47. extern godot_js_fetch_state_t godot_js_fetch_state_get(int p_id);
  48. extern int godot_js_fetch_http_status_get(int p_id);
  49. extern int godot_js_fetch_is_chunked(int p_id);
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. class HTTPClientWeb : public HTTPClient {
  54. private:
  55. int js_id = 0;
  56. Status status = STATUS_DISCONNECTED;
  57. // 64 KiB by default (favors fast download speeds at the cost of memory usage).
  58. int read_limit = 65536;
  59. String host;
  60. int port = -1;
  61. bool use_tls = false;
  62. int polled_response_code = 0;
  63. Vector<String> response_headers;
  64. Vector<uint8_t> response_buffer;
  65. #ifdef DEBUG_ENABLED
  66. uint64_t last_polling_frame = 0;
  67. #endif
  68. static void _parse_headers(int p_len, const char **p_headers, void *p_ref);
  69. public:
  70. static HTTPClient *_create_func(bool p_notify_postinitialize);
  71. Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) override;
  72. Error connect_to_host(const String &p_host, int p_port = -1, Ref<TLSOptions> p_tls_options = Ref<TLSOptions>()) override;
  73. void set_connection(const Ref<StreamPeer> &p_connection) override;
  74. Ref<StreamPeer> get_connection() const override;
  75. void close() override;
  76. Status get_status() const override;
  77. bool has_response() const override;
  78. bool is_response_chunked() const override;
  79. int get_response_code() const override;
  80. Error get_response_headers(List<String> *r_response) override;
  81. int64_t get_response_body_length() const override;
  82. PackedByteArray read_response_body_chunk() override;
  83. void set_blocking_mode(bool p_enable) override;
  84. bool is_blocking_mode_enabled() const override;
  85. void set_read_chunk_size(int p_size) override;
  86. int get_read_chunk_size() const override;
  87. Error poll() override;
  88. HTTPClientWeb();
  89. ~HTTPClientWeb();
  90. };
  91. #endif // HTTP_CLIENT_WEB_H