http_client.cpp 20 KB

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