| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/http_client.h" |
| 34 |
|
| 35 |
#ifdef __cplusplus |
| 36 |
extern "C" { |
| 37 |
#endif |
| 38 |
|
| 39 |
typedef enum { |
| 40 |
GODOT_JS_FETCH_STATE_REQUESTING = 0, |
| 41 |
GODOT_JS_FETCH_STATE_BODY = 1, |
| 42 |
GODOT_JS_FETCH_STATE_DONE = 2, |
| 43 |
GODOT_JS_FETCH_STATE_ERROR = -1, |
| 44 |
} godot_js_fetch_state_t; |
| 45 |
|
| 46 |
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); |
| 47 |
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); |
| 48 |
extern int godot_js_fetch_read_chunk(int p_id, uint8_t *p_buf, int p_buf_size); |
| 49 |
extern void godot_js_fetch_free(int p_id); |
| 50 |
extern godot_js_fetch_state_t godot_js_fetch_state_get(int p_id); |
| 51 |
extern int godot_js_fetch_http_status_get(int p_id); |
| 52 |
extern int godot_js_fetch_is_chunked(int p_id); |
| 53 |
|
| 54 |
#ifdef __cplusplus |
| 55 |
} |
| 56 |
#endif |
| 57 |
|
| 58 |
class HTTPClientWeb : public HTTPClient { |
| 59 |
GDSOFTCLASS(HTTPClientWeb, HTTPClient); |
| 60 |
|
| 61 |
private: |
| 62 |
int js_id = 0; |
| 63 |
Status status = STATUS_DISCONNECTED; |
| 64 |
|
| 65 |
|
| 66 |
int read_limit = 65536; |
| 67 |
|
| 68 |
String host; |
| 69 |
int port = -1; |
| 70 |
bool use_tls = false; |
| 71 |
|
| 72 |
int polled_response_code = 0; |
| 73 |
Vector<String> response_headers; |
| 74 |
Vector<uint8_t> response_buffer; |
| 75 |
|
| 76 |
#ifdef DEBUG_ENABLED |
| 77 |
uint64_t last_polling_frame = 0; |
| 78 |
#endif |
| 79 |
|
| 80 |
static void _parse_headers(int p_len, const char **p_headers, void *p_ref); |
| 81 |
|
| 82 |
public: |
| 83 |
static HTTPClient *_create_func(bool p_notify_postinitialize); |
| 84 |
|
| 85 |
Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) override; |
| 86 |
|
| 87 |
Error connect_to_host(const String &p_host, int p_port = -1, Ref<TLSOptions> p_tls_options = Ref<TLSOptions>()) override; |
| 88 |
void set_connection(const Ref<StreamPeer> &p_connection) override; |
| 89 |
Ref<StreamPeer> get_connection() const override; |
| 90 |
void close() override; |
| 91 |
Status get_status() const override; |
| 92 |
bool has_response() const override; |
| 93 |
bool is_response_chunked() const override; |
| 94 |
int get_response_code() const override; |
| 95 |
Error get_response_headers(List<String> *r_response) override; |
| 96 |
int64_t get_response_body_length() const override; |
| 97 |
PackedByteArray read_response_body_chunk() override; |
| 98 |
void set_blocking_mode(bool p_enable) override; |
| 99 |
bool is_blocking_mode_enabled() const override; |
| 100 |
void set_read_chunk_size(int p_size) override; |
| 101 |
int get_read_chunk_size() const override; |
| 102 |
Error poll() override; |
| 103 |
HTTPClientWeb(); |
| 104 |
~HTTPClientWeb(); |
| 105 |
}; |