http_client.cpp 21 KB

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