http_client.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*************************************************************************/
  2. /* http_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "http_client.h"
  30. #include "io/stream_peer_ssl.h"
  31. void HTTPClient::set_ip_type(IP::Type p_type) {
  32. ip_type = p_type;
  33. }
  34. Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl,bool p_verify_host){
  35. close();
  36. tcp_connection->set_ip_type(ip_type);
  37. conn_port=p_port;
  38. conn_host=p_host;
  39. if (conn_host.begins_with("http://")) {
  40. conn_host=conn_host.replace_first("http://","");
  41. } else if (conn_host.begins_with("https://")) {
  42. //use https
  43. conn_host=conn_host.replace_first("https://","");
  44. }
  45. ssl=p_ssl;
  46. ssl_verify_host=p_verify_host;
  47. connection=tcp_connection;
  48. if (conn_host.is_valid_ip_address()) {
  49. //is ip
  50. Error err = tcp_connection->connect_to_host(IP_Address(conn_host),p_port);
  51. if (err) {
  52. status=STATUS_CANT_CONNECT;
  53. return err;
  54. }
  55. status=STATUS_CONNECTING;
  56. } else {
  57. //is hostname
  58. resolving=IP::get_singleton()->resolve_hostname_queue_item(conn_host, ip_type);
  59. status=STATUS_RESOLVING;
  60. }
  61. return OK;
  62. }
  63. void HTTPClient::set_connection(const Ref<StreamPeer>& p_connection){
  64. close();
  65. connection=p_connection;
  66. status=STATUS_CONNECTED;
  67. }
  68. Ref<StreamPeer> HTTPClient::get_connection() const {
  69. return connection;
  70. }
  71. Error HTTPClient::request_raw( Method p_method, const String& p_url, const Vector<String>& p_headers,const PoolVector<uint8_t>& p_body) {
  72. ERR_FAIL_INDEX_V(p_method,METHOD_MAX,ERR_INVALID_PARAMETER);
  73. ERR_FAIL_COND_V(status!=STATUS_CONNECTED,ERR_INVALID_PARAMETER);
  74. ERR_FAIL_COND_V(connection.is_null(),ERR_INVALID_DATA);
  75. static const char* _methods[METHOD_MAX]={
  76. "GET",
  77. "HEAD",
  78. "POST",
  79. "PUT",
  80. "DELETE",
  81. "OPTIONS",
  82. "TRACE",
  83. "CONNECT"};
  84. String request=String(_methods[p_method])+" "+p_url+" HTTP/1.1\r\n";
  85. request+="Host: "+conn_host+":"+itos(conn_port)+"\r\n";
  86. bool add_clen=p_body.size()>0;
  87. for(int i=0;i<p_headers.size();i++) {
  88. request+=p_headers[i]+"\r\n";
  89. if (add_clen && p_headers[i].find("Content-Length:")==0) {
  90. add_clen=false;
  91. }
  92. }
  93. if (add_clen) {
  94. request+="Content-Length: "+itos(p_body.size())+"\r\n";
  95. //should it add utf8 encoding? not sure
  96. }
  97. request+="\r\n";
  98. CharString cs=request.utf8();
  99. PoolVector<uint8_t> data;
  100. //Maybe this goes faster somehow?
  101. for(int i=0;i<cs.length();i++) {
  102. data.append( cs[i] );
  103. }
  104. data.append_array( p_body );
  105. PoolVector<uint8_t>::Read r = data.read();
  106. Error err = connection->put_data(&r[0], data.size());
  107. if (err) {
  108. close();
  109. status=STATUS_CONNECTION_ERROR;
  110. return err;
  111. }
  112. status=STATUS_REQUESTING;
  113. return OK;
  114. }
  115. Error HTTPClient::request( Method p_method, const String& p_url, const Vector<String>& p_headers,const String& p_body) {
  116. ERR_FAIL_INDEX_V(p_method,METHOD_MAX,ERR_INVALID_PARAMETER);
  117. ERR_FAIL_COND_V(status!=STATUS_CONNECTED,ERR_INVALID_PARAMETER);
  118. ERR_FAIL_COND_V(connection.is_null(),ERR_INVALID_DATA);
  119. static const char* _methods[METHOD_MAX]={
  120. "GET",
  121. "HEAD",
  122. "POST",
  123. "PUT",
  124. "DELETE",
  125. "OPTIONS",
  126. "TRACE",
  127. "CONNECT"};
  128. String request=String(_methods[p_method])+" "+p_url+" HTTP/1.1\r\n";
  129. request+="Host: "+conn_host+":"+itos(conn_port)+"\r\n";
  130. bool add_clen=p_body.length()>0;
  131. for(int i=0;i<p_headers.size();i++) {
  132. request+=p_headers[i]+"\r\n";
  133. if (add_clen && p_headers[i].find("Content-Length:")==0) {
  134. add_clen=false;
  135. }
  136. }
  137. if (add_clen) {
  138. request+="Content-Length: "+itos(p_body.utf8().length())+"\r\n";
  139. //should it add utf8 encoding? not sure
  140. }
  141. request+="\r\n";
  142. request+=p_body;
  143. CharString cs=request.utf8();
  144. Error err = connection->put_data((const uint8_t*)cs.ptr(),cs.length());
  145. if (err) {
  146. close();
  147. status=STATUS_CONNECTION_ERROR;
  148. return err;
  149. }
  150. status=STATUS_REQUESTING;
  151. return OK;
  152. }
  153. Error HTTPClient::send_body_text(const String& p_body){
  154. return OK;
  155. }
  156. Error HTTPClient::send_body_data(const PoolByteArray& p_body){
  157. return OK;
  158. }
  159. bool HTTPClient::has_response() const {
  160. return response_headers.size()!=0;
  161. }
  162. bool HTTPClient::is_response_chunked() const {
  163. return chunked;
  164. }
  165. int HTTPClient::get_response_code() const {
  166. return response_num;
  167. }
  168. Error HTTPClient::get_response_headers(List<String> *r_response) {
  169. if (!response_headers.size())
  170. return ERR_INVALID_PARAMETER;
  171. for(int i=0;i<response_headers.size();i++) {
  172. r_response->push_back(response_headers[i]);
  173. }
  174. response_headers.clear();
  175. return OK;
  176. }
  177. void HTTPClient::close(){
  178. if (tcp_connection->get_status()!=StreamPeerTCP::STATUS_NONE)
  179. tcp_connection->disconnect_from_host();
  180. connection.unref();
  181. status=STATUS_DISCONNECTED;
  182. if (resolving!=IP::RESOLVER_INVALID_ID) {
  183. IP::get_singleton()->erase_resolve_item(resolving);
  184. resolving=IP::RESOLVER_INVALID_ID;
  185. }
  186. response_headers.clear();
  187. response_str.clear();
  188. body_size=0;
  189. body_left=0;
  190. chunk_left=0;
  191. response_num=0;
  192. }
  193. Error HTTPClient::poll(){
  194. switch(status) {
  195. case STATUS_RESOLVING: {
  196. ERR_FAIL_COND_V(resolving==IP::RESOLVER_INVALID_ID,ERR_BUG);
  197. IP::ResolverStatus rstatus = IP::get_singleton()->get_resolve_item_status(resolving);
  198. switch(rstatus) {
  199. case IP::RESOLVER_STATUS_WAITING: return OK; //still resolving
  200. case IP::RESOLVER_STATUS_DONE: {
  201. IP_Address host = IP::get_singleton()->get_resolve_item_address(resolving);
  202. Error err = tcp_connection->connect_to_host(host,conn_port);
  203. IP::get_singleton()->erase_resolve_item(resolving);
  204. resolving=IP::RESOLVER_INVALID_ID;
  205. if (err) {
  206. status=STATUS_CANT_CONNECT;
  207. return err;
  208. }
  209. status=STATUS_CONNECTING;
  210. } break;
  211. case IP::RESOLVER_STATUS_NONE:
  212. case IP::RESOLVER_STATUS_ERROR: {
  213. IP::get_singleton()->erase_resolve_item(resolving);
  214. resolving=IP::RESOLVER_INVALID_ID;
  215. close();
  216. status=STATUS_CANT_RESOLVE;
  217. return ERR_CANT_RESOLVE;
  218. } break;
  219. }
  220. } break;
  221. case STATUS_CONNECTING: {
  222. StreamPeerTCP::Status s = tcp_connection->get_status();
  223. switch(s) {
  224. case StreamPeerTCP::STATUS_CONNECTING: {
  225. return OK; //do none
  226. } break;
  227. case StreamPeerTCP::STATUS_CONNECTED: {
  228. if (ssl) {
  229. Ref<StreamPeerSSL> ssl = StreamPeerSSL::create();
  230. Error err = ssl->connect_to_stream(tcp_connection,true,ssl_verify_host?conn_host:String());
  231. if (err!=OK) {
  232. close();
  233. status=STATUS_SSL_HANDSHAKE_ERROR;
  234. return ERR_CANT_CONNECT;
  235. }
  236. //print_line("SSL! TURNED ON!");
  237. connection=ssl;
  238. }
  239. status=STATUS_CONNECTED;
  240. return OK;
  241. } break;
  242. case StreamPeerTCP::STATUS_ERROR:
  243. case StreamPeerTCP::STATUS_NONE: {
  244. close();
  245. status=STATUS_CANT_CONNECT;
  246. return ERR_CANT_CONNECT;
  247. } break;
  248. }
  249. } break;
  250. case STATUS_CONNECTED: {
  251. //request something please
  252. return OK;
  253. } break;
  254. case STATUS_REQUESTING: {
  255. while(true) {
  256. uint8_t byte;
  257. int rec=0;
  258. Error err = _get_http_data(&byte,1,rec);
  259. if (err!=OK) {
  260. close();
  261. status=STATUS_CONNECTION_ERROR;
  262. return ERR_CONNECTION_ERROR;
  263. }
  264. if (rec==0)
  265. return OK; //keep trying!
  266. response_str.push_back(byte);
  267. int rs = response_str.size();
  268. if (
  269. (rs>=2 && response_str[rs-2]=='\n' && response_str[rs-1]=='\n') ||
  270. (rs>=4 && response_str[rs-4]=='\r' && response_str[rs-3]=='\n' && rs>=4 && response_str[rs-2]=='\r' && response_str[rs-1]=='\n')
  271. ) {
  272. //end of response, parse.
  273. response_str.push_back(0);
  274. String response;
  275. response.parse_utf8((const char*)response_str.ptr());
  276. //print_line("END OF RESPONSE? :\n"+response+"\n------");
  277. Vector<String> responses = response.split("\n");
  278. body_size=0;
  279. chunked=false;
  280. body_left=0;
  281. chunk_left=0;
  282. response_str.clear();
  283. response_headers.clear();
  284. response_num = RESPONSE_OK;
  285. for(int i=0;i<responses.size();i++) {
  286. String header = responses[i].strip_edges();
  287. String s = header.to_lower();
  288. if (s.length()==0)
  289. continue;
  290. if (s.begins_with("content-length:")) {
  291. body_size = s.substr(s.find(":")+1,s.length()).strip_edges().to_int();
  292. body_left=body_size;
  293. }
  294. if (s.begins_with("transfer-encoding:")) {
  295. String encoding = header.substr(header.find(":")+1,header.length()).strip_edges();
  296. //print_line("TRANSFER ENCODING: "+encoding);
  297. if (encoding=="chunked") {
  298. chunked=true;
  299. }
  300. }
  301. if (i==0 && responses[i].begins_with("HTTP")) {
  302. String num = responses[i].get_slicec(' ',1);
  303. response_num=num.to_int();
  304. } else {
  305. response_headers.push_back(header);
  306. }
  307. }
  308. if (body_size==0 && !chunked) {
  309. status=STATUS_CONNECTED; //ask for something again?
  310. } else {
  311. status=STATUS_BODY;
  312. }
  313. return OK;
  314. }
  315. }
  316. //wait for response
  317. return OK;
  318. } break;
  319. case STATUS_DISCONNECTED: {
  320. return ERR_UNCONFIGURED;
  321. } break;
  322. case STATUS_CONNECTION_ERROR: {
  323. return ERR_CONNECTION_ERROR;
  324. } break;
  325. case STATUS_CANT_CONNECT: {
  326. return ERR_CANT_CONNECT;
  327. } break;
  328. case STATUS_CANT_RESOLVE: {
  329. return ERR_CANT_RESOLVE;
  330. } break;
  331. }
  332. return OK;
  333. }
  334. Dictionary HTTPClient::_get_response_headers_as_dictionary() {
  335. List<String> rh;
  336. get_response_headers(&rh);
  337. Dictionary ret;
  338. for(const List<String>::Element *E=rh.front();E;E=E->next()) {
  339. String s = E->get();
  340. int sp = s.find(":");
  341. if (sp==-1)
  342. continue;
  343. String key = s.substr(0,sp).strip_edges();
  344. String value = s.substr(sp+1,s.length()).strip_edges();
  345. ret[key]=value;
  346. }
  347. return ret;
  348. }
  349. PoolStringArray HTTPClient::_get_response_headers() {
  350. List<String> rh;
  351. get_response_headers(&rh);
  352. PoolStringArray ret;
  353. ret.resize(rh.size());
  354. int idx=0;
  355. for(const List<String>::Element *E=rh.front();E;E=E->next()) {
  356. ret.set(idx++,E->get());
  357. }
  358. return ret;
  359. }
  360. int HTTPClient::get_response_body_length() const {
  361. return body_size;
  362. }
  363. PoolByteArray HTTPClient::read_response_body_chunk() {
  364. ERR_FAIL_COND_V( status !=STATUS_BODY, PoolByteArray() );
  365. Error err=OK;
  366. if (chunked) {
  367. while(true) {
  368. if (chunk_left==0) {
  369. //reading len
  370. uint8_t b;
  371. int rec=0;
  372. err = _get_http_data(&b,1,rec);
  373. if (rec==0)
  374. break;
  375. chunk.push_back(b);
  376. if (chunk.size()>32) {
  377. ERR_PRINT("HTTP Invalid chunk hex len");
  378. status=STATUS_CONNECTION_ERROR;
  379. return PoolByteArray();
  380. }
  381. if (chunk.size()>2 && chunk[chunk.size()-2]=='\r' && chunk[chunk.size()-1]=='\n') {
  382. int len=0;
  383. for(int i=0;i<chunk.size()-2;i++) {
  384. char c = chunk[i];
  385. int v=0;
  386. if (c>='0' && c<='9')
  387. v=c-'0';
  388. else if (c>='a' && c<='f')
  389. v=c-'a'+10;
  390. else if (c>='A' && c<='F')
  391. v=c-'A'+10;
  392. else {
  393. ERR_PRINT("HTTP Chunk len not in hex!!");
  394. status=STATUS_CONNECTION_ERROR;
  395. return PoolByteArray();
  396. }
  397. len<<=4;
  398. len|=v;
  399. if (len>(1<<24)) {
  400. ERR_PRINT("HTTP Chunk too big!! >16mb");
  401. status=STATUS_CONNECTION_ERROR;
  402. return PoolByteArray();
  403. }
  404. }
  405. if (len==0) {
  406. //end!
  407. status=STATUS_CONNECTED;
  408. chunk.clear();
  409. return PoolByteArray();
  410. }
  411. chunk_left=len+2;
  412. chunk.resize(chunk_left);
  413. }
  414. } else {
  415. int rec=0;
  416. err = _get_http_data(&chunk[chunk.size()-chunk_left],chunk_left,rec);
  417. if (rec==0) {
  418. break;
  419. }
  420. chunk_left-=rec;
  421. if (chunk_left==0) {
  422. if (chunk[chunk.size()-2]!='\r' || chunk[chunk.size()-1]!='\n') {
  423. ERR_PRINT("HTTP Invalid chunk terminator (not \\r\\n)");
  424. status=STATUS_CONNECTION_ERROR;
  425. return PoolByteArray();
  426. }
  427. PoolByteArray ret;
  428. ret.resize(chunk.size()-2);
  429. {
  430. PoolByteArray::Write w = ret.write();
  431. copymem(w.ptr(),chunk.ptr(),chunk.size()-2);
  432. }
  433. chunk.clear();
  434. return ret;
  435. }
  436. break;
  437. }
  438. }
  439. } else {
  440. int to_read = MIN(body_left,read_chunk_size);
  441. PoolByteArray ret;
  442. ret.resize(to_read);
  443. PoolByteArray::Write w = ret.write();
  444. int _offset = 0;
  445. while (to_read > 0) {
  446. int rec=0;
  447. err = _get_http_data(w.ptr()+_offset,to_read,rec);
  448. if (rec>0) {
  449. body_left-=rec;
  450. to_read-=rec;
  451. _offset += rec;
  452. } else {
  453. if (to_read>0) //ended up reading less
  454. ret.resize(_offset);
  455. break;
  456. }
  457. }
  458. if (body_left==0) {
  459. status=STATUS_CONNECTED;
  460. }
  461. return ret;
  462. }
  463. if (err!=OK) {
  464. close();
  465. if (err==ERR_FILE_EOF) {
  466. status=STATUS_DISCONNECTED; //server disconnected
  467. } else {
  468. status=STATUS_CONNECTION_ERROR;
  469. }
  470. } else if (body_left==0 && !chunked) {
  471. status=STATUS_CONNECTED;
  472. }
  473. return PoolByteArray();
  474. }
  475. HTTPClient::Status HTTPClient::get_status() const {
  476. return status;
  477. }
  478. void HTTPClient::set_blocking_mode(bool p_enable) {
  479. blocking=p_enable;
  480. }
  481. bool HTTPClient::is_blocking_mode_enabled() const{
  482. return blocking;
  483. }
  484. Error HTTPClient::_get_http_data(uint8_t* p_buffer, int p_bytes,int &r_received) {
  485. if (blocking) {
  486. Error err = connection->get_data(p_buffer,p_bytes);
  487. if (err==OK)
  488. r_received=p_bytes;
  489. else
  490. r_received=0;
  491. return err;
  492. } else {
  493. return connection->get_partial_data(p_buffer,p_bytes,r_received);
  494. }
  495. }
  496. void HTTPClient::_bind_methods() {
  497. ClassDB::bind_method(_MD("set_ip_type","ip_type"),&HTTPClient::set_ip_type);
  498. ClassDB::bind_method(_MD("connect_to_host:Error","host","port","use_ssl","verify_host"),&HTTPClient::connect_to_host,DEFVAL(false),DEFVAL(true));
  499. ClassDB::bind_method(_MD("set_connection","connection:StreamPeer"),&HTTPClient::set_connection);
  500. ClassDB::bind_method(_MD("get_connection:StreamPeer"),&HTTPClient::get_connection);
  501. ClassDB::bind_method(_MD("request_raw","method","url","headers","body"),&HTTPClient::request_raw);
  502. ClassDB::bind_method(_MD("request","method","url","headers","body"),&HTTPClient::request,DEFVAL(String()));
  503. ClassDB::bind_method(_MD("send_body_text","body"),&HTTPClient::send_body_text);
  504. ClassDB::bind_method(_MD("send_body_data","body"),&HTTPClient::send_body_data);
  505. ClassDB::bind_method(_MD("close"),&HTTPClient::close);
  506. ClassDB::bind_method(_MD("has_response"),&HTTPClient::has_response);
  507. ClassDB::bind_method(_MD("is_response_chunked"),&HTTPClient::is_response_chunked);
  508. ClassDB::bind_method(_MD("get_response_code"),&HTTPClient::get_response_code);
  509. ClassDB::bind_method(_MD("get_response_headers"),&HTTPClient::_get_response_headers);
  510. ClassDB::bind_method(_MD("get_response_headers_as_dictionary"),&HTTPClient::_get_response_headers_as_dictionary);
  511. ClassDB::bind_method(_MD("get_response_body_length"),&HTTPClient::get_response_body_length);
  512. ClassDB::bind_method(_MD("read_response_body_chunk"),&HTTPClient::read_response_body_chunk);
  513. ClassDB::bind_method(_MD("set_read_chunk_size","bytes"),&HTTPClient::set_read_chunk_size);
  514. ClassDB::bind_method(_MD("set_blocking_mode","enabled"),&HTTPClient::set_blocking_mode);
  515. ClassDB::bind_method(_MD("is_blocking_mode_enabled"),&HTTPClient::is_blocking_mode_enabled);
  516. ClassDB::bind_method(_MD("get_status"),&HTTPClient::get_status);
  517. ClassDB::bind_method(_MD("poll:Error"),&HTTPClient::poll);
  518. ClassDB::bind_method(_MD("query_string_from_dict:String","fields"),&HTTPClient::query_string_from_dict);
  519. BIND_CONSTANT( METHOD_GET );
  520. BIND_CONSTANT( METHOD_HEAD );
  521. BIND_CONSTANT( METHOD_POST );
  522. BIND_CONSTANT( METHOD_PUT );
  523. BIND_CONSTANT( METHOD_DELETE );
  524. BIND_CONSTANT( METHOD_OPTIONS );
  525. BIND_CONSTANT( METHOD_TRACE );
  526. BIND_CONSTANT( METHOD_CONNECT );
  527. BIND_CONSTANT( METHOD_MAX );
  528. BIND_CONSTANT( STATUS_DISCONNECTED );
  529. BIND_CONSTANT( STATUS_RESOLVING ); //resolving hostname (if passed a hostname)
  530. BIND_CONSTANT( STATUS_CANT_RESOLVE );
  531. BIND_CONSTANT( STATUS_CONNECTING ); //connecting to ip
  532. BIND_CONSTANT( STATUS_CANT_CONNECT );
  533. BIND_CONSTANT( STATUS_CONNECTED ); //connected ); requests only accepted here
  534. BIND_CONSTANT( STATUS_REQUESTING ); // request in progress
  535. BIND_CONSTANT( STATUS_BODY ); // request resulted in body ); which must be read
  536. BIND_CONSTANT( STATUS_CONNECTION_ERROR );
  537. BIND_CONSTANT( STATUS_SSL_HANDSHAKE_ERROR );
  538. BIND_CONSTANT( RESPONSE_CONTINUE );
  539. BIND_CONSTANT( RESPONSE_SWITCHING_PROTOCOLS );
  540. BIND_CONSTANT( RESPONSE_PROCESSING );
  541. // 2xx successful
  542. BIND_CONSTANT( RESPONSE_OK );
  543. BIND_CONSTANT( RESPONSE_CREATED );
  544. BIND_CONSTANT( RESPONSE_ACCEPTED );
  545. BIND_CONSTANT( RESPONSE_NON_AUTHORITATIVE_INFORMATION );
  546. BIND_CONSTANT( RESPONSE_NO_CONTENT );
  547. BIND_CONSTANT( RESPONSE_RESET_CONTENT );
  548. BIND_CONSTANT( RESPONSE_PARTIAL_CONTENT );
  549. BIND_CONSTANT( RESPONSE_MULTI_STATUS );
  550. BIND_CONSTANT( RESPONSE_IM_USED );
  551. // 3xx redirection
  552. BIND_CONSTANT( RESPONSE_MULTIPLE_CHOICES );
  553. BIND_CONSTANT( RESPONSE_MOVED_PERMANENTLY );
  554. BIND_CONSTANT( RESPONSE_FOUND );
  555. BIND_CONSTANT( RESPONSE_SEE_OTHER );
  556. BIND_CONSTANT( RESPONSE_NOT_MODIFIED );
  557. BIND_CONSTANT( RESPONSE_USE_PROXY );
  558. BIND_CONSTANT( RESPONSE_TEMPORARY_REDIRECT );
  559. // 4xx client error
  560. BIND_CONSTANT( RESPONSE_BAD_REQUEST );
  561. BIND_CONSTANT( RESPONSE_UNAUTHORIZED );
  562. BIND_CONSTANT( RESPONSE_PAYMENT_REQUIRED );
  563. BIND_CONSTANT( RESPONSE_FORBIDDEN );
  564. BIND_CONSTANT( RESPONSE_NOT_FOUND );
  565. BIND_CONSTANT( RESPONSE_METHOD_NOT_ALLOWED );
  566. BIND_CONSTANT( RESPONSE_NOT_ACCEPTABLE );
  567. BIND_CONSTANT( RESPONSE_PROXY_AUTHENTICATION_REQUIRED );
  568. BIND_CONSTANT( RESPONSE_REQUEST_TIMEOUT );
  569. BIND_CONSTANT( RESPONSE_CONFLICT );
  570. BIND_CONSTANT( RESPONSE_GONE );
  571. BIND_CONSTANT( RESPONSE_LENGTH_REQUIRED );
  572. BIND_CONSTANT( RESPONSE_PRECONDITION_FAILED );
  573. BIND_CONSTANT( RESPONSE_REQUEST_ENTITY_TOO_LARGE );
  574. BIND_CONSTANT( RESPONSE_REQUEST_URI_TOO_LONG );
  575. BIND_CONSTANT( RESPONSE_UNSUPPORTED_MEDIA_TYPE );
  576. BIND_CONSTANT( RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE );
  577. BIND_CONSTANT( RESPONSE_EXPECTATION_FAILED );
  578. BIND_CONSTANT( RESPONSE_UNPROCESSABLE_ENTITY );
  579. BIND_CONSTANT( RESPONSE_LOCKED );
  580. BIND_CONSTANT( RESPONSE_FAILED_DEPENDENCY );
  581. BIND_CONSTANT( RESPONSE_UPGRADE_REQUIRED );
  582. // 5xx server error
  583. BIND_CONSTANT( RESPONSE_INTERNAL_SERVER_ERROR );
  584. BIND_CONSTANT( RESPONSE_NOT_IMPLEMENTED );
  585. BIND_CONSTANT( RESPONSE_BAD_GATEWAY );
  586. BIND_CONSTANT( RESPONSE_SERVICE_UNAVAILABLE );
  587. BIND_CONSTANT( RESPONSE_GATEWAY_TIMEOUT );
  588. BIND_CONSTANT( RESPONSE_HTTP_VERSION_NOT_SUPPORTED );
  589. BIND_CONSTANT( RESPONSE_INSUFFICIENT_STORAGE );
  590. BIND_CONSTANT( RESPONSE_NOT_EXTENDED );
  591. }
  592. void HTTPClient::set_read_chunk_size(int p_size) {
  593. ERR_FAIL_COND(p_size<256 || p_size>(1<<24));
  594. read_chunk_size=p_size;
  595. }
  596. String HTTPClient::query_string_from_dict(const Dictionary& p_dict) {
  597. String query = "";
  598. Array keys = p_dict.keys();
  599. for (int i = 0; i < keys.size(); ++i) {
  600. query += "&" + String(keys[i]).http_escape() + "=" + String(p_dict[keys[i]]).http_escape();
  601. }
  602. query.erase(0, 1);
  603. return query;
  604. }
  605. HTTPClient::HTTPClient(){
  606. ip_type = IP::TYPE_ANY;
  607. tcp_connection = StreamPeerTCP::create_ref();
  608. resolving = IP::RESOLVER_INVALID_ID;
  609. status=STATUS_DISCONNECTED;
  610. conn_port=80;
  611. body_size=0;
  612. chunked=false;
  613. body_left=0;
  614. chunk_left=0;
  615. response_num=0;
  616. ssl=false;
  617. blocking=false;
  618. read_chunk_size=4096;
  619. }
  620. HTTPClient::~HTTPClient(){
  621. }