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