http_client_javascript.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*************************************************************************/
  2. /* http_client_javascript.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 HTTP_CLIENT_JAVASCRIPT_H
  31. #define HTTP_CLIENT_JAVASCRIPT_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_body_length_get(int p_id);
  49. extern int godot_js_fetch_http_status_get(int p_id);
  50. extern int godot_js_fetch_is_chunked(int p_id);
  51. #ifdef __cplusplus
  52. }
  53. #endif
  54. class HTTPClientJavaScript : public HTTPClient {
  55. private:
  56. int js_id = 0;
  57. Status status = STATUS_DISCONNECTED;
  58. // 64 KiB by default (favors fast download speeds at the cost of memory usage).
  59. int read_limit = 65536;
  60. String host;
  61. int port = -1;
  62. bool use_tls = false;
  63. int polled_response_code = 0;
  64. Vector<String> response_headers;
  65. Vector<uint8_t> response_buffer;
  66. #ifdef DEBUG_ENABLED
  67. uint64_t last_polling_frame = 0;
  68. #endif
  69. static void _parse_headers(int p_len, const char **p_headers, void *p_ref);
  70. public:
  71. static HTTPClient *_create_func();
  72. Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) override;
  73. Error connect_to_host(const String &p_host, int p_port = -1, bool p_ssl = false, bool p_verify_host = true) override;
  74. void set_connection(const Ref<StreamPeer> &p_connection) override;
  75. Ref<StreamPeer> get_connection() const override;
  76. void close() override;
  77. Status get_status() const override;
  78. bool has_response() const override;
  79. bool is_response_chunked() const override;
  80. int get_response_code() const override;
  81. Error get_response_headers(List<String> *r_response) override;
  82. int64_t get_response_body_length() const override;
  83. PackedByteArray read_response_body_chunk() override;
  84. void set_blocking_mode(bool p_enable) override;
  85. bool is_blocking_mode_enabled() const override;
  86. void set_read_chunk_size(int p_size) override;
  87. int get_read_chunk_size() const override;
  88. Error poll() override;
  89. HTTPClientJavaScript();
  90. ~HTTPClientJavaScript();
  91. };
  92. #endif // HTTP_CLIENT_JAVASCRIPT_H