http_client.cpp 22 KB

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