http_client_web.h 4.6 KB

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