fixes to HTTPClient

-small unchunked files should work properly
-blocking mode should work properly
This commit is contained in:
Juan Linietsky
2015-02-11 07:57:39 -03:00
parent b4e1c1d717
commit 78a268c2eb
2 changed files with 39 additions and 10 deletions

View File

@ -273,7 +273,7 @@ Error HTTPClient::poll(){
while(true) { while(true) {
uint8_t byte; uint8_t byte;
int rec=0; int rec=0;
Error err = connection->get_partial_data(&byte,1,rec); Error err = _get_http_data(&byte,1,rec);
if (err!=OK) { if (err!=OK) {
close(); close();
status=STATUS_CONNECTION_ERROR; status=STATUS_CONNECTION_ERROR;
@ -417,7 +417,7 @@ ByteArray HTTPClient::read_response_body_chunk() {
//reading len //reading len
uint8_t b; uint8_t b;
int rec=0; int rec=0;
err = connection->get_partial_data(&b,1,rec); err = _get_http_data(&b,1,rec);
if (rec==0) if (rec==0)
break; break;
@ -471,7 +471,7 @@ ByteArray HTTPClient::read_response_body_chunk() {
} else { } else {
int rec=0; int rec=0;
err = connection->get_partial_data(&chunk[chunk.size()-chunk_left],chunk_left,rec); err = _get_http_data(&chunk[chunk.size()-chunk_left],chunk_left,rec);
if (rec==0) { if (rec==0) {
break; break;
} }
@ -502,18 +502,23 @@ ByteArray HTTPClient::read_response_body_chunk() {
} }
} else { } else {
int to_read = MIN(body_left,read_chunk_size);
ByteArray ret; ByteArray ret;
ret.resize(MAX(body_left,tmp_read.size())); ret.resize(to_read);
ByteArray::Write w = ret.write(); ByteArray::Write w = ret.write();
int _offset = 0; int _offset = 0;
while (body_left > 0) { while (to_read > 0) {
ByteArray::Write r = tmp_read.write();
int rec=0; int rec=0;
err = connection->get_partial_data(r.ptr(),MIN(body_left,tmp_read.size()),rec); err = _get_http_data(w.ptr()+_offset,to_read,rec);
if (rec>0) { if (rec>0) {
copymem(w.ptr()+_offset,r.ptr(),rec);
body_left-=rec; body_left-=rec;
to_read-=rec;
_offset += rec; _offset += rec;
} else {
if (to_read>0) //ended up reading less
ret.resize(_offset);
break;
} }
} }
if (body_left==0) { if (body_left==0) {
@ -557,6 +562,20 @@ bool HTTPClient::is_blocking_mode_enabled() const{
return blocking; return blocking;
} }
Error HTTPClient::_get_http_data(uint8_t* p_buffer, int p_bytes,int &r_received) {
if (blocking) {
Error err = connection->get_data(p_buffer,p_bytes);
if (err==OK)
r_received=p_bytes;
else
r_received=0;
return err;
} else {
return connection->get_partial_data(p_buffer,p_bytes,r_received);
}
}
void HTTPClient::_bind_methods() { void HTTPClient::_bind_methods() {
@ -574,6 +593,7 @@ void HTTPClient::_bind_methods() {
ObjectTypeDB::bind_method(_MD("get_response_headers_as_dictionary"),&HTTPClient::_get_response_headers_as_dictionary); ObjectTypeDB::bind_method(_MD("get_response_headers_as_dictionary"),&HTTPClient::_get_response_headers_as_dictionary);
ObjectTypeDB::bind_method(_MD("get_response_body_length"),&HTTPClient::get_response_body_length); ObjectTypeDB::bind_method(_MD("get_response_body_length"),&HTTPClient::get_response_body_length);
ObjectTypeDB::bind_method(_MD("read_response_body_chunk"),&HTTPClient::read_response_body_chunk); ObjectTypeDB::bind_method(_MD("read_response_body_chunk"),&HTTPClient::read_response_body_chunk);
ObjectTypeDB::bind_method(_MD("set_read_chunk_size","bytes"),&HTTPClient::set_read_chunk_size);
ObjectTypeDB::bind_method(_MD("set_blocking_mode","enabled"),&HTTPClient::set_blocking_mode); ObjectTypeDB::bind_method(_MD("set_blocking_mode","enabled"),&HTTPClient::set_blocking_mode);
ObjectTypeDB::bind_method(_MD("is_blocking_mode_enabled"),&HTTPClient::is_blocking_mode_enabled); ObjectTypeDB::bind_method(_MD("is_blocking_mode_enabled"),&HTTPClient::is_blocking_mode_enabled);
@ -664,6 +684,11 @@ void HTTPClient::_bind_methods() {
} }
void HTTPClient::set_read_chunk_size(int p_size) {
ERR_FAIL_COND(p_size<256 || p_size>(1<<24));
read_chunk_size=p_size;
}
HTTPClient::HTTPClient(){ HTTPClient::HTTPClient(){
tcp_connection = StreamPeerTCP::create_ref(); tcp_connection = StreamPeerTCP::create_ref();
@ -677,7 +702,7 @@ HTTPClient::HTTPClient(){
response_num=0; response_num=0;
ssl=false; ssl=false;
blocking=false; blocking=false;
tmp_read.resize(4096); read_chunk_size=4096;
} }
HTTPClient::~HTTPClient(){ HTTPClient::~HTTPClient(){

View File

@ -157,7 +157,10 @@ private:
static void _bind_methods(); static void _bind_methods();
StringArray _get_response_headers(); StringArray _get_response_headers();
Dictionary _get_response_headers_as_dictionary(); Dictionary _get_response_headers_as_dictionary();
ByteArray tmp_read; int read_chunk_size;
Error _get_http_data(uint8_t* p_buffer, int p_bytes,int &r_received);
public: public:
@ -185,6 +188,7 @@ public:
void set_blocking_mode(bool p_enable); //useful mostly if running in a thread void set_blocking_mode(bool p_enable); //useful mostly if running in a thread
bool is_blocking_mode_enabled() const; bool is_blocking_mode_enabled() const;
void set_read_chunk_size(int p_size);
Error poll(); Error poll();