http_client.cpp 21 KB

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