diff options
Diffstat (limited to 'core/io/http_client.cpp')
-rw-r--r-- | core/io/http_client.cpp | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index d7098b4c43..680e0ef7f9 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -72,7 +72,6 @@ Error HTTPClient::connect(const String &p_host, int p_port, bool p_ssl,bool p_ve return OK; } - void HTTPClient::set_connection(const Ref<StreamPeer>& p_connection){ close(); @@ -80,12 +79,65 @@ void HTTPClient::set_connection(const Ref<StreamPeer>& p_connection){ } - Ref<StreamPeer> HTTPClient::get_connection() const { return connection; } +Error HTTPClient::request_raw( Method p_method, const String& p_url, const Vector<String>& p_headers,const DVector<uint8_t>& p_body) { + + ERR_FAIL_INDEX_V(p_method,METHOD_MAX,ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(status!=STATUS_CONNECTED,ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(connection.is_null(),ERR_INVALID_DATA); + + + static const char* _methods[METHOD_MAX]={ + "GET", + "HEAD", + "POST", + "PUT", + "DELETE", + "OPTIONS", + "TRACE", + "CONNECT"}; + + String request=String(_methods[p_method])+" "+p_url+" HTTP/1.1\r\n"; + request+="Host: "+conn_host+":"+itos(conn_port)+"\r\n"; + bool add_clen=p_body.size()>0; + for(int i=0;i<p_headers.size();i++) { + request+=p_headers[i]+"\r\n"; + if (add_clen && p_headers[i].find("Content-Length:")==0) { + add_clen=false; + } + } + if (add_clen) { + request+="Content-Length: "+itos(p_body.size())+"\r\n"; + //should it add utf8 encoding? not sure + } + request+="\r\n"; + CharString cs=request.utf8(); + + DVector<uint8_t> data; + + //Maybe this goes faster somehow? + for(int i=0;i<cs.length();i++) { + data.append( cs[i] ); + } + data.append_array( p_body ); + + DVector<uint8_t>::Read r = data.read(); + Error err = connection->put_data(&r[0], data.size()); + + if (err) { + close(); + status=STATUS_CONNECTION_ERROR; + return err; + } + + status=STATUS_REQUESTING; + + return OK; +} Error HTTPClient::request( Method p_method, const String& p_url, const Vector<String>& p_headers,const String& p_body) { @@ -157,6 +209,7 @@ int HTTPClient::get_response_code() const { return response_num; } + Error HTTPClient::get_response_headers(List<String> *r_response) { if (!response_headers.size()) @@ -586,6 +639,7 @@ void HTTPClient::_bind_methods() { ObjectTypeDB::bind_method(_MD("connect:Error","host","port","use_ssl","verify_host"),&HTTPClient::connect,DEFVAL(false),DEFVAL(true)); ObjectTypeDB::bind_method(_MD("set_connection","connection:StreamPeer"),&HTTPClient::set_connection); ObjectTypeDB::bind_method(_MD("get_connection:StreamPeer"),&HTTPClient::get_connection); + ObjectTypeDB::bind_method(_MD("request_raw","method","url","headers","body"),&HTTPClient::request_raw,DEFVAL(String())); ObjectTypeDB::bind_method(_MD("request","method","url","headers","body"),&HTTPClient::request,DEFVAL(String())); ObjectTypeDB::bind_method(_MD("send_body_text","body"),&HTTPClient::send_body_text); ObjectTypeDB::bind_method(_MD("send_body_data","body"),&HTTPClient::send_body_data); |