2017-10-26 01:39:41 +00:00
/**************************************************************************/
2022-08-28 18:27:45 +00:00
/* http_client_web.cpp */
2017-10-26 01:39:41 +00:00
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2018-01-04 23:50:27 +00:00
2022-08-28 18:27:45 +00:00
# include "http_client_web.h"
2020-05-12 15:01:17 +00:00
2022-08-28 18:27:45 +00:00
void HTTPClientWeb : : _parse_headers ( int p_len , const char * * p_headers , void * p_ref ) {
HTTPClientWeb * client = static_cast < HTTPClientWeb * > ( p_ref ) ;
2021-03-02 15:00:24 +00:00
for ( int i = 0 ; i < p_len ; i + + ) {
client - > response_headers . push_back ( String : : utf8 ( p_headers [ i ] ) ) ;
}
}
2017-10-26 01:39:41 +00:00
2023-01-20 00:51:35 +00:00
Error HTTPClientWeb : : connect_to_host ( const String & p_host , int p_port , Ref < TLSOptions > p_tls_options ) {
ERR_FAIL_COND_V ( p_tls_options . is_valid ( ) & & p_tls_options - > is_server ( ) , ERR_INVALID_PARAMETER ) ;
2017-10-26 01:39:41 +00:00
close ( ) ;
2017-12-12 19:02:25 +00:00
port = p_port ;
2023-01-20 00:51:35 +00:00
use_tls = p_tls_options . is_valid ( ) ;
2017-12-12 19:02:25 +00:00
2017-10-26 01:39:41 +00:00
host = p_host ;
2017-12-12 19:02:25 +00:00
2017-12-15 14:32:44 +00:00
String host_lower = host . to_lower ( ) ;
2017-12-12 19:02:25 +00:00
if ( host_lower . begins_with ( " http:// " ) ) {
2017-12-15 14:32:44 +00:00
host = host . substr ( 7 , host . length ( ) - 7 ) ;
2023-01-20 00:51:35 +00:00
use_tls = false ;
2017-12-12 19:02:25 +00:00
} else if ( host_lower . begins_with ( " https:// " ) ) {
use_tls = true ;
2017-12-15 14:32:44 +00:00
host = host . substr ( 8 , host . length ( ) - 8 ) ;
2017-10-26 01:39:41 +00:00
}
2017-12-12 19:02:25 +00:00
ERR_FAIL_COND_V ( host . length ( ) < HOST_MIN_LEN , ERR_INVALID_PARAMETER ) ;
if ( port < 0 ) {
if ( use_tls ) {
port = PORT_HTTPS ;
} else {
port = PORT_HTTP ;
}
}
2017-10-26 01:39:41 +00:00
status = host . is_valid_ip_address ( ) ? STATUS_CONNECTING : STATUS_RESOLVING ;
2017-12-12 19:02:25 +00:00
2017-10-26 01:39:41 +00:00
return OK ;
}
2022-08-28 18:27:45 +00:00
void HTTPClientWeb : : set_connection ( const Ref < StreamPeer > & p_connection ) {
ERR_FAIL_MSG ( " Accessing an HTTPClientWeb's StreamPeer is not supported for the Web platform. " ) ;
2017-10-26 01:39:41 +00:00
}
2022-08-28 18:27:45 +00:00
Ref < StreamPeer > HTTPClientWeb : : get_connection ( ) const {
ERR_FAIL_V_MSG ( Ref < RefCounted > ( ) , " Accessing an HTTPClientWeb's StreamPeer is not supported for the Web platform. " ) ;
2017-10-26 01:39:41 +00:00
}
2022-08-28 18:27:45 +00:00
Error HTTPClientWeb : : request ( Method p_method , const String & p_url , const Vector < String > & p_headers , const uint8_t * p_body , int p_body_len ) {
2017-10-26 01:39:41 +00:00
ERR_FAIL_INDEX_V ( p_method , METHOD_MAX , ERR_INVALID_PARAMETER ) ;
2022-08-28 18:27:45 +00:00
ERR_FAIL_COND_V_MSG ( p_method = = METHOD_TRACE | | p_method = = METHOD_CONNECT , ERR_UNAVAILABLE , " HTTP methods TRACE and CONNECT are not supported for the Web platform. " ) ;
2017-10-26 01:39:41 +00:00
ERR_FAIL_COND_V ( status ! = STATUS_CONNECTED , ERR_INVALID_PARAMETER ) ;
2020-12-29 08:54:59 +00:00
ERR_FAIL_COND_V ( host . is_empty ( ) , ERR_UNCONFIGURED ) ;
2017-10-26 01:39:41 +00:00
ERR_FAIL_COND_V ( port < 0 , ERR_UNCONFIGURED ) ;
2017-12-12 19:02:25 +00:00
ERR_FAIL_COND_V ( ! p_url . begins_with ( " / " ) , ERR_INVALID_PARAMETER ) ;
2017-10-26 01:39:41 +00:00
2022-01-14 02:22:23 +00:00
Error err = verify_headers ( p_headers ) ;
if ( err ) {
return err ;
}
2018-03-30 12:31:18 +00:00
String url = ( use_tls ? " https:// " : " http:// " ) + host + " : " + itos ( port ) + p_url ;
2021-03-02 15:00:24 +00:00
Vector < CharString > keeper ;
Vector < const char * > c_strings ;
2017-10-26 01:39:41 +00:00
for ( int i = 0 ; i < p_headers . size ( ) ; i + + ) {
2021-03-02 15:00:24 +00:00
keeper . push_back ( p_headers [ i ] . utf8 ( ) ) ;
c_strings . push_back ( keeper [ i ] . get_data ( ) ) ;
}
if ( js_id ) {
godot_js_fetch_free ( js_id ) ;
2017-10-26 01:39:41 +00:00
}
2021-03-02 15:00:24 +00:00
js_id = godot_js_fetch_create ( _methods [ p_method ] , url . utf8 ( ) . get_data ( ) , c_strings . ptrw ( ) , c_strings . size ( ) , p_body , p_body_len ) ;
2017-10-26 01:39:41 +00:00
status = STATUS_REQUESTING ;
return OK ;
}
2022-08-28 18:27:45 +00:00
void HTTPClientWeb : : close ( ) {
2017-10-26 01:39:41 +00:00
host = " " ;
port = - 1 ;
use_tls = false ;
status = STATUS_DISCONNECTED ;
polled_response_code = 0 ;
2021-03-02 15:00:24 +00:00
response_headers . resize ( 0 ) ;
response_buffer . resize ( 0 ) ;
if ( js_id ) {
godot_js_fetch_free ( js_id ) ;
js_id = 0 ;
}
2017-10-26 01:39:41 +00:00
}
2022-08-28 18:27:45 +00:00
HTTPClientWeb : : Status HTTPClientWeb : : get_status ( ) const {
2017-10-26 01:39:41 +00:00
return status ;
}
2022-08-28 18:27:45 +00:00
bool HTTPClientWeb : : has_response ( ) const {
2021-03-02 15:00:24 +00:00
return response_headers . size ( ) > 0 ;
2017-10-26 01:39:41 +00:00
}
2022-08-28 18:27:45 +00:00
bool HTTPClientWeb : : is_response_chunked ( ) const {
2021-03-02 15:00:24 +00:00
return godot_js_fetch_is_chunked ( js_id ) ;
2017-10-26 01:39:41 +00:00
}
2022-08-28 18:27:45 +00:00
int HTTPClientWeb : : get_response_code ( ) const {
2017-10-26 01:39:41 +00:00
return polled_response_code ;
}
2022-08-28 18:27:45 +00:00
Error HTTPClientWeb : : get_response_headers ( List < String > * r_response ) {
2021-03-02 15:00:24 +00:00
if ( ! response_headers . size ( ) ) {
2017-10-26 01:39:41 +00:00
return ERR_INVALID_PARAMETER ;
}
2021-03-02 15:00:24 +00:00
for ( int i = 0 ; i < response_headers . size ( ) ; i + + ) {
r_response - > push_back ( response_headers [ i ] ) ;
}
response_headers . clear ( ) ;
2017-10-26 01:39:41 +00:00
return OK ;
}
2022-08-28 18:27:45 +00:00
int64_t HTTPClientWeb : : get_response_body_length ( ) const {
2023-07-24 06:32:44 +00:00
// Body length cannot be consistently retrieved from the web.
// Reading the "content-length" value will return a meaningless value when the response is compressed,
// as reading will return uncompressed chunks in any case, resulting in a mismatch between the detected
// body size and the actual size returned by repeatedly calling read_response_body_chunk.
// Additionally, while "content-length" is considered a safe CORS header, "content-encoding" is not,
// so using the "content-encoding" to decide if "content-length" is meaningful is not an option either.
// We simply must accept the fact that browsers are awful when it comes to networking APIs.
// See GH-47597, and GH-79327.
return - 1 ;
2017-10-26 01:39:41 +00:00
}
2022-08-28 18:27:45 +00:00
PackedByteArray HTTPClientWeb : : read_response_body_chunk ( ) {
2020-02-17 21:06:54 +00:00
ERR_FAIL_COND_V ( status ! = STATUS_BODY , PackedByteArray ( ) ) ;
2017-10-26 01:39:41 +00:00
2021-03-02 15:00:24 +00:00
if ( response_buffer . size ( ) ! = read_limit ) {
response_buffer . resize ( read_limit ) ;
}
int read = godot_js_fetch_read_chunk ( js_id , response_buffer . ptrw ( ) , read_limit ) ;
// Check if the stream is over.
godot_js_fetch_state_t state = godot_js_fetch_state_get ( js_id ) ;
if ( state = = GODOT_JS_FETCH_STATE_DONE ) {
status = STATUS_DISCONNECTED ;
} else if ( state ! = GODOT_JS_FETCH_STATE_BODY ) {
status = STATUS_CONNECTION_ERROR ;
2017-10-26 01:39:41 +00:00
}
2021-03-02 15:00:24 +00:00
PackedByteArray chunk ;
if ( ! read ) {
return chunk ;
}
chunk . resize ( read ) ;
2021-04-27 14:19:21 +00:00
memcpy ( chunk . ptrw ( ) , response_buffer . ptr ( ) , read ) ;
2017-10-26 01:39:41 +00:00
return chunk ;
}
2022-08-28 18:27:45 +00:00
void HTTPClientWeb : : set_blocking_mode ( bool p_enable ) {
ERR_FAIL_COND_MSG ( p_enable , " HTTPClientWeb blocking mode is not supported for the Web platform. " ) ;
2017-10-26 01:39:41 +00:00
}
2022-08-28 18:27:45 +00:00
bool HTTPClientWeb : : is_blocking_mode_enabled ( ) const {
2017-10-26 01:39:41 +00:00
return false ;
}
2022-08-28 18:27:45 +00:00
void HTTPClientWeb : : set_read_chunk_size ( int p_size ) {
2017-10-26 01:39:41 +00:00
read_limit = p_size ;
}
2022-08-28 18:27:45 +00:00
int HTTPClientWeb : : get_read_chunk_size ( ) const {
2019-11-24 12:20:24 +00:00
return read_limit ;
}
2022-08-28 18:27:45 +00:00
Error HTTPClientWeb : : poll ( ) {
2017-10-26 01:39:41 +00:00
switch ( status ) {
case STATUS_DISCONNECTED :
return ERR_UNCONFIGURED ;
case STATUS_RESOLVING :
status = STATUS_CONNECTING ;
return OK ;
case STATUS_CONNECTING :
status = STATUS_CONNECTED ;
return OK ;
case STATUS_CONNECTED :
return OK ;
2021-03-02 15:00:24 +00:00
case STATUS_BODY : {
godot_js_fetch_state_t state = godot_js_fetch_state_get ( js_id ) ;
if ( state = = GODOT_JS_FETCH_STATE_DONE ) {
status = STATUS_DISCONNECTED ;
} else if ( state ! = GODOT_JS_FETCH_STATE_BODY ) {
status = STATUS_CONNECTION_ERROR ;
return ERR_CONNECTION_ERROR ;
}
return OK ;
}
2017-10-26 01:39:41 +00:00
case STATUS_CONNECTION_ERROR :
return ERR_CONNECTION_ERROR ;
2018-10-02 00:30:27 +00:00
case STATUS_REQUESTING : {
2018-02-17 16:42:58 +00:00
# ifdef DEBUG_ENABLED
2021-03-02 15:00:24 +00:00
// forcing synchronous requests is not possible on the web
if ( last_polling_frame = = Engine : : get_singleton ( ) - > get_process_frames ( ) ) {
2022-08-28 18:27:45 +00:00
WARN_PRINT ( " HTTPClientWeb polled multiple times in one frame, "
2021-03-02 15:00:24 +00:00
" but request cannot progress more than once per "
2022-08-28 18:27:45 +00:00
" frame on the Web platform. " ) ;
2018-02-17 16:42:58 +00:00
}
2021-01-10 10:05:38 +00:00
last_polling_frame = Engine : : get_singleton ( ) - > get_process_frames ( ) ;
2018-02-17 16:42:58 +00:00
# endif
2021-03-02 15:00:24 +00:00
polled_response_code = godot_js_fetch_http_status_get ( js_id ) ;
godot_js_fetch_state_t js_state = godot_js_fetch_state_get ( js_id ) ;
if ( js_state = = GODOT_JS_FETCH_STATE_REQUESTING ) {
2018-02-16 04:11:25 +00:00
return OK ;
2021-03-02 15:00:24 +00:00
} else if ( js_state = = GODOT_JS_FETCH_STATE_ERROR ) {
// Fetch is in error state.
status = STATUS_CONNECTION_ERROR ;
return ERR_CONNECTION_ERROR ;
}
if ( godot_js_fetch_read_headers ( js_id , & _parse_headers , this ) ) {
// Failed to parse headers.
2018-02-16 04:11:25 +00:00
status = STATUS_CONNECTION_ERROR ;
return ERR_CONNECTION_ERROR ;
2017-10-26 01:39:41 +00:00
}
status = STATUS_BODY ;
break ;
2018-10-02 00:30:27 +00:00
}
default :
ERR_FAIL_V ( ERR_BUG ) ;
2017-10-26 01:39:41 +00:00
}
return OK ;
}
2024-04-22 14:34:44 +00:00
HTTPClient * HTTPClientWeb : : _create_func ( bool p_notify_postinitialize ) {
return static_cast < HTTPClient * > ( ClassDB : : creator < HTTPClientWeb > ( p_notify_postinitialize ) ) ;
2021-03-23 16:03:29 +00:00
}
2024-04-22 14:34:44 +00:00
HTTPClient * ( * HTTPClient : : _create ) ( bool p_notify_postinitialize ) = HTTPClientWeb : : _create_func ;
2021-03-23 16:03:29 +00:00
2022-08-28 18:27:45 +00:00
HTTPClientWeb : : HTTPClientWeb ( ) {
2017-10-26 01:39:41 +00:00
}
2022-08-28 18:27:45 +00:00
HTTPClientWeb : : ~ HTTPClientWeb ( ) {
2021-03-02 15:00:24 +00:00
close ( ) ;
2017-10-26 01:39:41 +00:00
}