2017-10-26 03:39:41 +02:00
/*************************************************************************/
/* http_client_javascript.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2018-01-05 00:50:27 +01:00
/* https://godotengine.org */
2017-10-26 03:39:41 +02:00
/*************************************************************************/
2020-01-01 11:16:22 +01:00
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
2017-10-26 03:39:41 +02:00
/* */
/* 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-05 00:50:27 +01:00
2018-09-11 18:13:45 +02:00
# include "core/io/http_client.h"
2017-10-26 03:39:41 +02:00
# include "http_request.h"
Error HTTPClient : : connect_to_host ( const String & p_host , int p_port , bool p_ssl , bool p_verify_host ) {
close ( ) ;
if ( p_ssl & & ! p_verify_host ) {
WARN_PRINT ( " Disabling HTTPClient's host verification is not supported for the HTML5 platform, host will be verified " ) ;
}
2017-12-12 20:02:25 +01:00
port = p_port ;
use_tls = p_ssl ;
2017-10-26 03:39:41 +02:00
host = p_host ;
2017-12-12 20:02:25 +01:00
2017-12-15 15:32:44 +01:00
String host_lower = host . to_lower ( ) ;
2017-12-12 20:02:25 +01:00
if ( host_lower . begins_with ( " http:// " ) ) {
2017-12-15 15:32:44 +01:00
host = host . substr ( 7 , host . length ( ) - 7 ) ;
2017-12-12 20:02:25 +01:00
} else if ( host_lower . begins_with ( " https:// " ) ) {
use_tls = true ;
2017-12-15 15:32:44 +01:00
host = host . substr ( 8 , host . length ( ) - 8 ) ;
2017-10-26 03:39:41 +02:00
}
2017-12-12 20:02:25 +01: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 03:39:41 +02:00
status = host . is_valid_ip_address ( ) ? STATUS_CONNECTING : STATUS_RESOLVING ;
2017-12-12 20:02:25 +01:00
2017-10-26 03:39:41 +02:00
return OK ;
}
void HTTPClient : : set_connection ( const Ref < StreamPeer > & p_connection ) {
2019-08-09 06:49:33 +02:00
ERR_FAIL_MSG ( " Accessing an HTTPClient's StreamPeer is not supported for the HTML5 platform. " ) ;
2017-10-26 03:39:41 +02:00
}
Ref < StreamPeer > HTTPClient : : get_connection ( ) const {
2019-08-09 06:49:33 +02:00
ERR_FAIL_V_MSG ( REF ( ) , " Accessing an HTTPClient's StreamPeer is not supported for the HTML5 platform. " ) ;
2017-10-26 03:39:41 +02:00
}
Error HTTPClient : : prepare_request ( Method p_method , const String & p_url , const Vector < String > & p_headers ) {
ERR_FAIL_INDEX_V ( p_method , METHOD_MAX , ERR_INVALID_PARAMETER ) ;
2019-08-09 06:49:33 +02: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 HTML5 platform. " ) ;
2017-10-26 03:39:41 +02:00
ERR_FAIL_COND_V ( status ! = STATUS_CONNECTED , ERR_INVALID_PARAMETER ) ;
ERR_FAIL_COND_V ( host . empty ( ) , ERR_UNCONFIGURED ) ;
ERR_FAIL_COND_V ( port < 0 , ERR_UNCONFIGURED ) ;
2017-12-12 20:02:25 +01:00
ERR_FAIL_COND_V ( ! p_url . begins_with ( " / " ) , ERR_INVALID_PARAMETER ) ;
2017-10-26 03:39:41 +02:00
2018-03-30 14:31:18 +02:00
String url = ( use_tls ? " https:// " : " http:// " ) + host + " : " + itos ( port ) + p_url ;
2017-10-26 03:39:41 +02:00
godot_xhr_reset ( xhr_id ) ;
godot_xhr_open ( xhr_id , _methods [ p_method ] , url . utf8 ( ) . get_data ( ) ,
username . empty ( ) ? NULL : username . utf8 ( ) . get_data ( ) ,
password . empty ( ) ? NULL : password . utf8 ( ) . get_data ( ) ) ;
for ( int i = 0 ; i < p_headers . size ( ) ; i + + ) {
int header_separator = p_headers [ i ] . find ( " : " ) ;
ERR_FAIL_COND_V ( header_separator < 0 , ERR_INVALID_PARAMETER ) ;
godot_xhr_set_request_header ( xhr_id ,
p_headers [ i ] . left ( header_separator ) . utf8 ( ) . get_data ( ) ,
p_headers [ i ] . right ( header_separator + 2 ) . utf8 ( ) . get_data ( ) ) ;
}
response_read_offset = 0 ;
status = STATUS_REQUESTING ;
return OK ;
}
2020-02-17 22:06:54 +01:00
Error HTTPClient : : request_raw ( Method p_method , const String & p_url , const Vector < String > & p_headers , const Vector < uint8_t > & p_body ) {
2017-10-26 03:39:41 +02:00
Error err = prepare_request ( p_method , p_url , p_headers ) ;
if ( err ! = OK )
return err ;
2020-02-17 22:06:54 +01:00
const uint8_t * read = p_body . ptr ( ) ;
2017-10-26 03:39:41 +02:00
godot_xhr_send_data ( xhr_id , read . ptr ( ) , p_body . size ( ) ) ;
return OK ;
}
Error HTTPClient : : request ( Method p_method , const String & p_url , const Vector < String > & p_headers , const String & p_body ) {
Error err = prepare_request ( p_method , p_url , p_headers ) ;
if ( err ! = OK )
return err ;
godot_xhr_send_string ( xhr_id , p_body . utf8 ( ) . get_data ( ) ) ;
return OK ;
}
void HTTPClient : : close ( ) {
host = " " ;
port = - 1 ;
use_tls = false ;
status = STATUS_DISCONNECTED ;
polled_response . resize ( 0 ) ;
polled_response_code = 0 ;
polled_response_header = String ( ) ;
godot_xhr_reset ( xhr_id ) ;
}
HTTPClient : : Status HTTPClient : : get_status ( ) const {
return status ;
}
bool HTTPClient : : has_response ( ) const {
return ! polled_response_header . empty ( ) ;
}
bool HTTPClient : : is_response_chunked ( ) const {
// TODO evaluate using moz-chunked-arraybuffer, fetch & ReadableStream
return false ;
}
int HTTPClient : : get_response_code ( ) const {
return polled_response_code ;
}
Error HTTPClient : : get_response_headers ( List < String > * r_response ) {
2018-02-16 05:38:36 +01:00
if ( polled_response_header . empty ( ) )
2017-10-26 03:39:41 +02:00
return ERR_INVALID_PARAMETER ;
Vector < String > header_lines = polled_response_header . split ( " \r \n " , false ) ;
for ( int i = 0 ; i < header_lines . size ( ) ; + + i ) {
r_response - > push_back ( header_lines [ i ] ) ;
}
polled_response_header = String ( ) ;
return OK ;
}
int HTTPClient : : get_response_body_length ( ) const {
return polled_response . size ( ) ;
}
2020-02-17 22:06:54 +01:00
PackedByteArray HTTPClient : : read_response_body_chunk ( ) {
2017-10-26 03:39:41 +02:00
2020-02-17 22:06:54 +01:00
ERR_FAIL_COND_V ( status ! = STATUS_BODY , PackedByteArray ( ) ) ;
2017-10-26 03:39:41 +02:00
int to_read = MIN ( read_limit , polled_response . size ( ) - response_read_offset ) ;
2020-02-17 22:06:54 +01:00
PackedByteArray chunk ;
2017-10-26 03:39:41 +02:00
chunk . resize ( to_read ) ;
2020-02-17 22:06:54 +01:00
uint8_t * write = chunk . ptrw ( ) ;
const uint8_t * read = polled_response . ptr ( ) ;
2017-10-26 03:39:41 +02:00
memcpy ( write . ptr ( ) , read . ptr ( ) + response_read_offset , to_read ) ;
2020-02-17 22:06:54 +01:00
write = uint8_t * ( ) ;
read = const uint8_t * ( ) ;
2017-10-26 03:39:41 +02:00
response_read_offset + = to_read ;
if ( response_read_offset = = polled_response . size ( ) ) {
status = STATUS_CONNECTED ;
polled_response . resize ( 0 ) ;
godot_xhr_reset ( xhr_id ) ;
}
return chunk ;
}
void HTTPClient : : set_blocking_mode ( bool p_enable ) {
2019-08-09 06:49:33 +02:00
ERR_FAIL_COND_MSG ( p_enable , " HTTPClient blocking mode is not supported for the HTML5 platform. " ) ;
2017-10-26 03:39:41 +02:00
}
bool HTTPClient : : is_blocking_mode_enabled ( ) const {
return false ;
}
void HTTPClient : : set_read_chunk_size ( int p_size ) {
read_limit = p_size ;
}
2019-11-24 13:20:24 +01:00
int HTTPClient : : get_read_chunk_size ( ) const {
return read_limit ;
}
2017-10-26 03:39:41 +02:00
Error HTTPClient : : poll ( ) {
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 :
case STATUS_BODY :
return OK ;
case STATUS_CONNECTION_ERROR :
return ERR_CONNECTION_ERROR ;
2018-10-02 02:30:27 +02:00
case STATUS_REQUESTING : {
2018-02-17 17:42:58 +01:00
# ifdef DEBUG_ENABLED
if ( ! has_polled ) {
has_polled = true ;
} else {
// forcing synchronous requests is not possible on the web
if ( last_polling_frame = = Engine : : get_singleton ( ) - > get_idle_frames ( ) ) {
WARN_PRINT ( " HTTPClient polled multiple times in one frame, "
" but request cannot progress more than once per "
" frame on the HTML5 platform. " ) ;
}
}
last_polling_frame = Engine : : get_singleton ( ) - > get_idle_frames ( ) ;
# endif
2017-10-26 03:39:41 +02:00
polled_response_code = godot_xhr_get_status ( xhr_id ) ;
2018-02-16 05:11:25 +01:00
if ( godot_xhr_get_ready_state ( xhr_id ) ! = XHR_READY_STATE_DONE ) {
return OK ;
} else if ( ! polled_response_code ) {
status = STATUS_CONNECTION_ERROR ;
return ERR_CONNECTION_ERROR ;
2017-10-26 03:39:41 +02:00
}
status = STATUS_BODY ;
2020-02-17 22:06:54 +01:00
PackedByteArray bytes ;
2017-10-26 03:39:41 +02:00
int len = godot_xhr_get_response_headers_length ( xhr_id ) ;
2018-02-16 05:38:36 +01:00
bytes . resize ( len + 1 ) ;
2020-02-17 22:06:54 +01:00
uint8_t * write = bytes . ptrw ( ) ;
2017-10-26 03:39:41 +02:00
godot_xhr_get_response_headers ( xhr_id , reinterpret_cast < char * > ( write . ptr ( ) ) , len ) ;
2018-02-16 05:38:36 +01:00
write [ len ] = 0 ;
2020-02-17 22:06:54 +01:00
write = uint8_t * ( ) ;
2017-10-26 03:39:41 +02:00
2020-02-17 22:06:54 +01:00
const uint8_t * read = bytes . ptr ( ) ;
2017-10-26 03:39:41 +02:00
polled_response_header = String : : utf8 ( reinterpret_cast < const char * > ( read . ptr ( ) ) ) ;
2020-02-17 22:06:54 +01:00
read = const uint8_t * ( ) ;
2017-10-26 03:39:41 +02:00
2018-02-16 05:11:25 +01:00
polled_response . resize ( godot_xhr_get_response_length ( xhr_id ) ) ;
2020-02-17 22:06:54 +01:00
write = polled_response . ptrw ( ) ;
2018-02-16 05:11:25 +01:00
godot_xhr_get_response ( xhr_id , write . ptr ( ) , polled_response . size ( ) ) ;
2020-02-17 22:06:54 +01:00
write = uint8_t * ( ) ;
2017-10-26 03:39:41 +02:00
break ;
2018-10-02 02:30:27 +02:00
}
default :
ERR_FAIL_V ( ERR_BUG ) ;
2017-10-26 03:39:41 +02:00
}
return OK ;
}
HTTPClient : : HTTPClient ( ) {
xhr_id = godot_xhr_new ( ) ;
read_limit = 4096 ;
status = STATUS_DISCONNECTED ;
port = - 1 ;
use_tls = false ;
polled_response_code = 0 ;
2018-02-17 17:42:58 +01:00
# ifdef DEBUG_ENABLED
has_polled = false ;
last_polling_frame = 0 ;
# endif
2017-10-26 03:39:41 +02:00
}
HTTPClient : : ~ HTTPClient ( ) {
godot_xhr_free ( xhr_id ) ;
}