http_client.cpp 24 KB

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