Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/web/http_client_web.h
10277 views
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
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
private:
60
int js_id = 0;
61
Status status = STATUS_DISCONNECTED;
62
63
// 64 KiB by default (favors fast download speeds at the cost of memory usage).
64
int read_limit = 65536;
65
66
String host;
67
int port = -1;
68
bool use_tls = false;
69
70
int polled_response_code = 0;
71
Vector<String> response_headers;
72
Vector<uint8_t> response_buffer;
73
74
#ifdef DEBUG_ENABLED
75
uint64_t last_polling_frame = 0;
76
#endif
77
78
static void _parse_headers(int p_len, const char **p_headers, void *p_ref);
79
80
public:
81
static HTTPClient *_create_func(bool p_notify_postinitialize);
82
83
Error request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) override;
84
85
Error connect_to_host(const String &p_host, int p_port = -1, Ref<TLSOptions> p_tls_options = Ref<TLSOptions>()) override;
86
void set_connection(const Ref<StreamPeer> &p_connection) override;
87
Ref<StreamPeer> get_connection() const override;
88
void close() override;
89
Status get_status() const override;
90
bool has_response() const override;
91
bool is_response_chunked() const override;
92
int get_response_code() const override;
93
Error get_response_headers(List<String> *r_response) override;
94
int64_t get_response_body_length() const override;
95
PackedByteArray read_response_body_chunk() override;
96
void set_blocking_mode(bool p_enable) override;
97
bool is_blocking_mode_enabled() const override;
98
void set_read_chunk_size(int p_size) override;
99
int get_read_chunk_size() const override;
100
Error poll() override;
101
HTTPClientWeb();
102
~HTTPClientWeb();
103
};
104
105