http_client.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*************************************************************************/
  2. /* http_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/io/stream_peer_ssl.h"
  32. #include "core/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. ERR_FAIL_COND_MSG(p_connection.is_null(), "Connection is not a reference to a valid StreamPeer object.");
  84. close();
  85. connection = p_connection;
  86. status = STATUS_CONNECTED;
  87. }
  88. Ref<StreamPeer> HTTPClient::get_connection() const {
  89. return connection;
  90. }
  91. Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const Vector<uint8_t> &p_body) {
  92. ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
  93. ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER);
  94. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
  95. ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA);
  96. String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n";
  97. if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) {
  98. // Don't append the standard ports
  99. request += "Host: " + conn_host + "\r\n";
  100. } else {
  101. request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n";
  102. }
  103. bool add_clen = p_body.size() > 0;
  104. bool add_uagent = true;
  105. bool add_accept = true;
  106. for (int i = 0; i < p_headers.size(); i++) {
  107. request += p_headers[i] + "\r\n";
  108. if (add_clen && p_headers[i].findn("Content-Length:") == 0) {
  109. add_clen = false;
  110. }
  111. if (add_uagent && p_headers[i].findn("User-Agent:") == 0) {
  112. add_uagent = false;
  113. }
  114. if (add_accept && p_headers[i].findn("Accept:") == 0) {
  115. add_accept = false;
  116. }
  117. }
  118. if (add_clen) {
  119. request += "Content-Length: " + itos(p_body.size()) + "\r\n";
  120. // Should it add utf8 encoding?
  121. }
  122. if (add_uagent) {
  123. request += "User-Agent: GodotEngine/" + String(VERSION_FULL_BUILD) + " (" + OS::get_singleton()->get_name() + ")\r\n";
  124. }
  125. if (add_accept) {
  126. request += "Accept: */*\r\n";
  127. }
  128. request += "\r\n";
  129. CharString cs = request.utf8();
  130. Vector<uint8_t> data;
  131. data.resize(cs.length());
  132. {
  133. uint8_t *data_write = data.ptrw();
  134. for (int i = 0; i < cs.length(); i++) {
  135. data_write[i] = cs[i];
  136. }
  137. }
  138. data.append_array(p_body);
  139. const uint8_t *r = data.ptr();
  140. Error err = connection->put_data(&r[0], data.size());
  141. if (err) {
  142. close();
  143. status = STATUS_CONNECTION_ERROR;
  144. return err;
  145. }
  146. status = STATUS_REQUESTING;
  147. head_request = p_method == METHOD_HEAD;
  148. return OK;
  149. }
  150. Error HTTPClient::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body) {
  151. ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
  152. ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER);
  153. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
  154. ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA);
  155. String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n";
  156. if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) {
  157. // Don't append the standard ports
  158. request += "Host: " + conn_host + "\r\n";
  159. } else {
  160. request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n";
  161. }
  162. bool add_uagent = true;
  163. bool add_accept = true;
  164. bool add_clen = p_body.length() > 0;
  165. for (int i = 0; i < p_headers.size(); i++) {
  166. request += p_headers[i] + "\r\n";
  167. if (add_clen && p_headers[i].findn("Content-Length:") == 0) {
  168. add_clen = false;
  169. }
  170. if (add_uagent && p_headers[i].findn("User-Agent:") == 0) {
  171. add_uagent = false;
  172. }
  173. if (add_accept && p_headers[i].findn("Accept:") == 0) {
  174. add_accept = false;
  175. }
  176. }
  177. if (add_clen) {
  178. request += "Content-Length: " + itos(p_body.utf8().length()) + "\r\n";
  179. // Should it add utf8 encoding?
  180. }
  181. if (add_uagent) {
  182. request += "User-Agent: GodotEngine/" + String(VERSION_FULL_BUILD) + " (" + OS::get_singleton()->get_name() + ")\r\n";
  183. }
  184. if (add_accept) {
  185. request += "Accept: */*\r\n";
  186. }
  187. request += "\r\n";
  188. request += p_body;
  189. CharString cs = request.utf8();
  190. Error err = connection->put_data((const uint8_t *)cs.ptr(), cs.length());
  191. if (err) {
  192. close();
  193. status = STATUS_CONNECTION_ERROR;
  194. return err;
  195. }
  196. status = STATUS_REQUESTING;
  197. head_request = p_method == METHOD_HEAD;
  198. return OK;
  199. }
  200. bool HTTPClient::has_response() const {
  201. return response_headers.size() != 0;
  202. }
  203. bool HTTPClient::is_response_chunked() const {
  204. return chunked;
  205. }
  206. int HTTPClient::get_response_code() const {
  207. return response_num;
  208. }
  209. Error HTTPClient::get_response_headers(List<String> *r_response) {
  210. if (!response_headers.size())
  211. return ERR_INVALID_PARAMETER;
  212. for (int i = 0; i < response_headers.size(); i++) {
  213. r_response->push_back(response_headers[i]);
  214. }
  215. response_headers.clear();
  216. return OK;
  217. }
  218. void HTTPClient::close() {
  219. if (tcp_connection->get_status() != StreamPeerTCP::STATUS_NONE)
  220. tcp_connection->disconnect_from_host();
  221. connection.unref();
  222. status = STATUS_DISCONNECTED;
  223. head_request = false;
  224. if (resolving != IP::RESOLVER_INVALID_ID) {
  225. IP::get_singleton()->erase_resolve_item(resolving);
  226. resolving = IP::RESOLVER_INVALID_ID;
  227. }
  228. response_headers.clear();
  229. response_str.clear();
  230. body_size = -1;
  231. body_left = 0;
  232. chunk_left = 0;
  233. chunk_trailer_part = 0;
  234. read_until_eof = false;
  235. response_num = 0;
  236. handshaking = false;
  237. }
  238. Error HTTPClient::poll() {
  239. switch (status) {
  240. case STATUS_RESOLVING: {
  241. ERR_FAIL_COND_V(resolving == IP::RESOLVER_INVALID_ID, ERR_BUG);
  242. IP::ResolverStatus rstatus = IP::get_singleton()->get_resolve_item_status(resolving);
  243. switch (rstatus) {
  244. case IP::RESOLVER_STATUS_WAITING:
  245. return OK; // Still resolving
  246. case IP::RESOLVER_STATUS_DONE: {
  247. IP_Address host = IP::get_singleton()->get_resolve_item_address(resolving);
  248. Error err = tcp_connection->connect_to_host(host, conn_port);
  249. IP::get_singleton()->erase_resolve_item(resolving);
  250. resolving = IP::RESOLVER_INVALID_ID;
  251. if (err) {
  252. status = STATUS_CANT_CONNECT;
  253. return err;
  254. }
  255. status = STATUS_CONNECTING;
  256. } break;
  257. case IP::RESOLVER_STATUS_NONE:
  258. case IP::RESOLVER_STATUS_ERROR: {
  259. IP::get_singleton()->erase_resolve_item(resolving);
  260. resolving = IP::RESOLVER_INVALID_ID;
  261. close();
  262. status = STATUS_CANT_RESOLVE;
  263. return ERR_CANT_RESOLVE;
  264. } break;
  265. }
  266. } break;
  267. case STATUS_CONNECTING: {
  268. StreamPeerTCP::Status s = tcp_connection->get_status();
  269. switch (s) {
  270. case StreamPeerTCP::STATUS_CONNECTING: {
  271. return OK;
  272. } break;
  273. case StreamPeerTCP::STATUS_CONNECTED: {
  274. if (ssl) {
  275. Ref<StreamPeerSSL> ssl;
  276. if (!handshaking) {
  277. // Connect the StreamPeerSSL and start handshaking
  278. ssl = Ref<StreamPeerSSL>(StreamPeerSSL::create());
  279. ssl->set_blocking_handshake_enabled(false);
  280. Error err = ssl->connect_to_stream(tcp_connection, ssl_verify_host, conn_host);
  281. if (err != OK) {
  282. close();
  283. status = STATUS_SSL_HANDSHAKE_ERROR;
  284. return ERR_CANT_CONNECT;
  285. }
  286. connection = ssl;
  287. handshaking = true;
  288. } else {
  289. // We are already handshaking, which means we can use your already active SSL connection
  290. ssl = static_cast<Ref<StreamPeerSSL> >(connection);
  291. if (ssl.is_null()) {
  292. close();
  293. status = STATUS_SSL_HANDSHAKE_ERROR;
  294. return ERR_CANT_CONNECT;
  295. }
  296. ssl->poll(); // Try to finish the handshake
  297. }
  298. if (ssl->get_status() == StreamPeerSSL::STATUS_CONNECTED) {
  299. // Handshake has been successful
  300. handshaking = false;
  301. status = STATUS_CONNECTED;
  302. return OK;
  303. } else if (ssl->get_status() != StreamPeerSSL::STATUS_HANDSHAKING) {
  304. // Handshake has failed
  305. close();
  306. status = STATUS_SSL_HANDSHAKE_ERROR;
  307. return ERR_CANT_CONNECT;
  308. }
  309. // ... we will need to poll more for handshake to finish
  310. } else {
  311. status = STATUS_CONNECTED;
  312. }
  313. return OK;
  314. } break;
  315. case StreamPeerTCP::STATUS_ERROR:
  316. case StreamPeerTCP::STATUS_NONE: {
  317. close();
  318. status = STATUS_CANT_CONNECT;
  319. return ERR_CANT_CONNECT;
  320. } break;
  321. }
  322. } break;
  323. case STATUS_BODY:
  324. case STATUS_CONNECTED: {
  325. // Check if we are still connected
  326. if (ssl) {
  327. Ref<StreamPeerSSL> tmp = connection;
  328. tmp->poll();
  329. if (tmp->get_status() != StreamPeerSSL::STATUS_CONNECTED) {
  330. status = STATUS_CONNECTION_ERROR;
  331. return ERR_CONNECTION_ERROR;
  332. }
  333. } else if (tcp_connection->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  334. status = STATUS_CONNECTION_ERROR;
  335. return ERR_CONNECTION_ERROR;
  336. }
  337. // Connection established, requests can now be made
  338. return OK;
  339. } break;
  340. case STATUS_REQUESTING: {
  341. while (true) {
  342. uint8_t byte;
  343. int rec = 0;
  344. Error err = _get_http_data(&byte, 1, rec);
  345. if (err != OK) {
  346. close();
  347. status = STATUS_CONNECTION_ERROR;
  348. return ERR_CONNECTION_ERROR;
  349. }
  350. if (rec == 0)
  351. return OK; // Still requesting, keep trying!
  352. response_str.push_back(byte);
  353. int rs = response_str.size();
  354. if (
  355. (rs >= 2 && response_str[rs - 2] == '\n' && response_str[rs - 1] == '\n') ||
  356. (rs >= 4 && response_str[rs - 4] == '\r' && response_str[rs - 3] == '\n' && response_str[rs - 2] == '\r' && response_str[rs - 1] == '\n')) {
  357. // End of response, parse.
  358. response_str.push_back(0);
  359. String response;
  360. response.parse_utf8((const char *)response_str.ptr());
  361. Vector<String> responses = response.split("\n");
  362. body_size = -1;
  363. chunked = false;
  364. body_left = 0;
  365. chunk_left = 0;
  366. chunk_trailer_part = false;
  367. read_until_eof = false;
  368. response_str.clear();
  369. response_headers.clear();
  370. response_num = RESPONSE_OK;
  371. // Per the HTTP 1.1 spec, keep-alive is the default.
  372. // Not following that specification breaks standard implementations.
  373. // Broken web servers should be fixed.
  374. bool keep_alive = true;
  375. for (int i = 0; i < responses.size(); i++) {
  376. String header = responses[i].strip_edges();
  377. String s = header.to_lower();
  378. if (s.length() == 0)
  379. continue;
  380. if (s.begins_with("content-length:")) {
  381. body_size = s.substr(s.find(":") + 1, s.length()).strip_edges().to_int();
  382. body_left = body_size;
  383. } else if (s.begins_with("transfer-encoding:")) {
  384. String encoding = header.substr(header.find(":") + 1, header.length()).strip_edges();
  385. if (encoding == "chunked") {
  386. chunked = true;
  387. }
  388. } else if (s.begins_with("connection: close")) {
  389. keep_alive = false;
  390. }
  391. if (i == 0 && responses[i].begins_with("HTTP")) {
  392. String num = responses[i].get_slicec(' ', 1);
  393. response_num = num.to_int();
  394. } else {
  395. response_headers.push_back(header);
  396. }
  397. }
  398. // This is a HEAD request, we wont receive anything.
  399. if (head_request) {
  400. body_size = 0;
  401. body_left = 0;
  402. }
  403. if (body_size != -1 || chunked) {
  404. status = STATUS_BODY;
  405. } else if (!keep_alive) {
  406. read_until_eof = true;
  407. status = STATUS_BODY;
  408. } else {
  409. status = STATUS_CONNECTED;
  410. }
  411. return OK;
  412. }
  413. }
  414. } break;
  415. case STATUS_DISCONNECTED: {
  416. return ERR_UNCONFIGURED;
  417. } break;
  418. case STATUS_CONNECTION_ERROR:
  419. case STATUS_SSL_HANDSHAKE_ERROR: {
  420. return ERR_CONNECTION_ERROR;
  421. } break;
  422. case STATUS_CANT_CONNECT: {
  423. return ERR_CANT_CONNECT;
  424. } break;
  425. case STATUS_CANT_RESOLVE: {
  426. return ERR_CANT_RESOLVE;
  427. } break;
  428. }
  429. return OK;
  430. }
  431. int HTTPClient::get_response_body_length() const {
  432. return body_size;
  433. }
  434. PackedByteArray HTTPClient::read_response_body_chunk() {
  435. ERR_FAIL_COND_V(status != STATUS_BODY, PackedByteArray());
  436. PackedByteArray ret;
  437. Error err = OK;
  438. if (chunked) {
  439. while (true) {
  440. if (chunk_trailer_part) {
  441. // We need to consume the trailer part too or keep-alive will break
  442. uint8_t b;
  443. int rec = 0;
  444. err = _get_http_data(&b, 1, rec);
  445. if (rec == 0)
  446. break;
  447. chunk.push_back(b);
  448. int cs = chunk.size();
  449. if ((cs >= 2 && chunk[cs - 2] == '\r' && chunk[cs - 1] == '\n')) {
  450. if (cs == 2) {
  451. // Finally over
  452. chunk_trailer_part = false;
  453. status = STATUS_CONNECTED;
  454. chunk.clear();
  455. break;
  456. } else {
  457. // We do not process nor return the trailer data
  458. chunk.clear();
  459. }
  460. }
  461. } else if (chunk_left == 0) {
  462. // Reading length
  463. uint8_t b;
  464. int rec = 0;
  465. err = _get_http_data(&b, 1, rec);
  466. if (rec == 0)
  467. break;
  468. chunk.push_back(b);
  469. if (chunk.size() > 32) {
  470. ERR_PRINT("HTTP Invalid chunk hex len");
  471. status = STATUS_CONNECTION_ERROR;
  472. break;
  473. }
  474. if (chunk.size() > 2 && chunk[chunk.size() - 2] == '\r' && chunk[chunk.size() - 1] == '\n') {
  475. int len = 0;
  476. for (int i = 0; i < chunk.size() - 2; i++) {
  477. char c = chunk[i];
  478. int v = 0;
  479. if (c >= '0' && c <= '9')
  480. v = c - '0';
  481. else if (c >= 'a' && c <= 'f')
  482. v = c - 'a' + 10;
  483. else if (c >= 'A' && c <= 'F')
  484. v = c - 'A' + 10;
  485. else {
  486. ERR_PRINT("HTTP Chunk len not in hex!!");
  487. status = STATUS_CONNECTION_ERROR;
  488. break;
  489. }
  490. len <<= 4;
  491. len |= v;
  492. if (len > (1 << 24)) {
  493. ERR_PRINT("HTTP Chunk too big!! >16mb");
  494. status = STATUS_CONNECTION_ERROR;
  495. break;
  496. }
  497. }
  498. if (len == 0) {
  499. // End reached!
  500. chunk_trailer_part = true;
  501. chunk.clear();
  502. break;
  503. }
  504. chunk_left = len + 2;
  505. chunk.resize(chunk_left);
  506. }
  507. } else {
  508. int rec = 0;
  509. err = _get_http_data(&chunk.write[chunk.size() - chunk_left], chunk_left, rec);
  510. if (rec == 0) {
  511. break;
  512. }
  513. chunk_left -= rec;
  514. if (chunk_left == 0) {
  515. if (chunk[chunk.size() - 2] != '\r' || chunk[chunk.size() - 1] != '\n') {
  516. ERR_PRINT("HTTP Invalid chunk terminator (not \\r\\n)");
  517. status = STATUS_CONNECTION_ERROR;
  518. break;
  519. }
  520. ret.resize(chunk.size() - 2);
  521. uint8_t *w = ret.ptrw();
  522. copymem(w, chunk.ptr(), chunk.size() - 2);
  523. chunk.clear();
  524. }
  525. break;
  526. }
  527. }
  528. } else {
  529. int to_read = !read_until_eof ? MIN(body_left, read_chunk_size) : read_chunk_size;
  530. ret.resize(to_read);
  531. int _offset = 0;
  532. while (to_read > 0) {
  533. int rec = 0;
  534. {
  535. uint8_t *w = ret.ptrw();
  536. err = _get_http_data(w + _offset, to_read, rec);
  537. }
  538. if (rec <= 0) { // Ended up reading less
  539. ret.resize(_offset);
  540. break;
  541. } else {
  542. _offset += rec;
  543. to_read -= rec;
  544. if (!read_until_eof) {
  545. body_left -= rec;
  546. }
  547. }
  548. if (err != OK)
  549. break;
  550. }
  551. }
  552. if (err != OK) {
  553. close();
  554. if (err == ERR_FILE_EOF) {
  555. status = STATUS_DISCONNECTED; // Server disconnected
  556. } else {
  557. status = STATUS_CONNECTION_ERROR;
  558. }
  559. } else if (body_left == 0 && !chunked && !read_until_eof) {
  560. status = STATUS_CONNECTED;
  561. }
  562. return ret;
  563. }
  564. HTTPClient::Status HTTPClient::get_status() const {
  565. return status;
  566. }
  567. void HTTPClient::set_blocking_mode(bool p_enable) {
  568. blocking = p_enable;
  569. }
  570. bool HTTPClient::is_blocking_mode_enabled() const {
  571. return blocking;
  572. }
  573. Error HTTPClient::_get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  574. if (blocking) {
  575. // We can't use StreamPeer.get_data, since when reaching EOF we will get an
  576. // error without knowing how many bytes we received.
  577. Error err = ERR_FILE_EOF;
  578. int read = 0;
  579. int left = p_bytes;
  580. r_received = 0;
  581. while (left > 0) {
  582. err = connection->get_partial_data(p_buffer + r_received, left, read);
  583. if (err == OK) {
  584. r_received += read;
  585. } else if (err == ERR_FILE_EOF) {
  586. r_received += read;
  587. return err;
  588. } else {
  589. return err;
  590. }
  591. left -= read;
  592. }
  593. return err;
  594. } else {
  595. return connection->get_partial_data(p_buffer, p_bytes, r_received);
  596. }
  597. }
  598. void HTTPClient::set_read_chunk_size(int p_size) {
  599. ERR_FAIL_COND(p_size < 256 || p_size > (1 << 24));
  600. read_chunk_size = p_size;
  601. }
  602. int HTTPClient::get_read_chunk_size() const {
  603. return read_chunk_size;
  604. }
  605. HTTPClient::HTTPClient() {
  606. tcp_connection.instance();
  607. resolving = IP::RESOLVER_INVALID_ID;
  608. status = STATUS_DISCONNECTED;
  609. head_request = false;
  610. conn_port = -1;
  611. body_size = -1;
  612. chunked = false;
  613. body_left = 0;
  614. read_until_eof = false;
  615. chunk_left = 0;
  616. chunk_trailer_part = false;
  617. response_num = 0;
  618. ssl = false;
  619. blocking = false;
  620. handshaking = false;
  621. read_chunk_size = 4096;
  622. }
  623. HTTPClient::~HTTPClient() {
  624. }
  625. #endif // #ifndef JAVASCRIPT_ENABLED
  626. String HTTPClient::query_string_from_dict(const Dictionary &p_dict) {
  627. String query = "";
  628. Array keys = p_dict.keys();
  629. for (int i = 0; i < keys.size(); ++i) {
  630. String encoded_key = String(keys[i]).http_escape();
  631. Variant value = p_dict[keys[i]];
  632. switch (value.get_type()) {
  633. case Variant::ARRAY: {
  634. // Repeat the key with every values
  635. Array values = value;
  636. for (int j = 0; j < values.size(); ++j) {
  637. query += "&" + encoded_key + "=" + String(values[j]).http_escape();
  638. }
  639. break;
  640. }
  641. case Variant::NIL: {
  642. // Add the key with no value
  643. query += "&" + encoded_key;
  644. break;
  645. }
  646. default: {
  647. // Add the key-value pair
  648. query += "&" + encoded_key + "=" + String(value).http_escape();
  649. }
  650. }
  651. }
  652. query.erase(0, 1);
  653. return query;
  654. }
  655. Dictionary HTTPClient::_get_response_headers_as_dictionary() {
  656. List<String> rh;
  657. get_response_headers(&rh);
  658. Dictionary ret;
  659. for (const List<String>::Element *E = rh.front(); E; E = E->next()) {
  660. const String &s = E->get();
  661. int sp = s.find(":");
  662. if (sp == -1)
  663. continue;
  664. String key = s.substr(0, sp).strip_edges();
  665. String value = s.substr(sp + 1, s.length()).strip_edges();
  666. ret[key] = value;
  667. }
  668. return ret;
  669. }
  670. PackedStringArray HTTPClient::_get_response_headers() {
  671. List<String> rh;
  672. get_response_headers(&rh);
  673. PackedStringArray ret;
  674. ret.resize(rh.size());
  675. int idx = 0;
  676. for (const List<String>::Element *E = rh.front(); E; E = E->next()) {
  677. ret.set(idx++, E->get());
  678. }
  679. return ret;
  680. }
  681. void HTTPClient::_bind_methods() {
  682. ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port", "use_ssl", "verify_host"), &HTTPClient::connect_to_host, DEFVAL(-1), DEFVAL(false), DEFVAL(true));
  683. ClassDB::bind_method(D_METHOD("set_connection", "connection"), &HTTPClient::set_connection);
  684. ClassDB::bind_method(D_METHOD("get_connection"), &HTTPClient::get_connection);
  685. ClassDB::bind_method(D_METHOD("request_raw", "method", "url", "headers", "body"), &HTTPClient::request_raw);
  686. ClassDB::bind_method(D_METHOD("request", "method", "url", "headers", "body"), &HTTPClient::request, DEFVAL(String()));
  687. ClassDB::bind_method(D_METHOD("close"), &HTTPClient::close);
  688. ClassDB::bind_method(D_METHOD("has_response"), &HTTPClient::has_response);
  689. ClassDB::bind_method(D_METHOD("is_response_chunked"), &HTTPClient::is_response_chunked);
  690. ClassDB::bind_method(D_METHOD("get_response_code"), &HTTPClient::get_response_code);
  691. ClassDB::bind_method(D_METHOD("get_response_headers"), &HTTPClient::_get_response_headers);
  692. ClassDB::bind_method(D_METHOD("get_response_headers_as_dictionary"), &HTTPClient::_get_response_headers_as_dictionary);
  693. ClassDB::bind_method(D_METHOD("get_response_body_length"), &HTTPClient::get_response_body_length);
  694. ClassDB::bind_method(D_METHOD("read_response_body_chunk"), &HTTPClient::read_response_body_chunk);
  695. ClassDB::bind_method(D_METHOD("set_read_chunk_size", "bytes"), &HTTPClient::set_read_chunk_size);
  696. ClassDB::bind_method(D_METHOD("get_read_chunk_size"), &HTTPClient::get_read_chunk_size);
  697. ClassDB::bind_method(D_METHOD("set_blocking_mode", "enabled"), &HTTPClient::set_blocking_mode);
  698. ClassDB::bind_method(D_METHOD("is_blocking_mode_enabled"), &HTTPClient::is_blocking_mode_enabled);
  699. ClassDB::bind_method(D_METHOD("get_status"), &HTTPClient::get_status);
  700. ClassDB::bind_method(D_METHOD("poll"), &HTTPClient::poll);
  701. ClassDB::bind_method(D_METHOD("query_string_from_dict", "fields"), &HTTPClient::query_string_from_dict);
  702. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_mode_enabled"), "set_blocking_mode", "is_blocking_mode_enabled");
  703. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "connection", PROPERTY_HINT_RESOURCE_TYPE, "StreamPeer", 0), "set_connection", "get_connection");
  704. ADD_PROPERTY(PropertyInfo(Variant::INT, "read_chunk_size", PROPERTY_HINT_RANGE, "256,16777216"), "set_read_chunk_size", "get_read_chunk_size");
  705. BIND_ENUM_CONSTANT(METHOD_GET);
  706. BIND_ENUM_CONSTANT(METHOD_HEAD);
  707. BIND_ENUM_CONSTANT(METHOD_POST);
  708. BIND_ENUM_CONSTANT(METHOD_PUT);
  709. BIND_ENUM_CONSTANT(METHOD_DELETE);
  710. BIND_ENUM_CONSTANT(METHOD_OPTIONS);
  711. BIND_ENUM_CONSTANT(METHOD_TRACE);
  712. BIND_ENUM_CONSTANT(METHOD_CONNECT);
  713. BIND_ENUM_CONSTANT(METHOD_PATCH);
  714. BIND_ENUM_CONSTANT(METHOD_MAX);
  715. BIND_ENUM_CONSTANT(STATUS_DISCONNECTED);
  716. BIND_ENUM_CONSTANT(STATUS_RESOLVING); // Resolving hostname (if hostname was passed in)
  717. BIND_ENUM_CONSTANT(STATUS_CANT_RESOLVE);
  718. BIND_ENUM_CONSTANT(STATUS_CONNECTING); // Connecting to IP
  719. BIND_ENUM_CONSTANT(STATUS_CANT_CONNECT);
  720. BIND_ENUM_CONSTANT(STATUS_CONNECTED); // Connected, now accepting requests
  721. BIND_ENUM_CONSTANT(STATUS_REQUESTING); // Request in progress
  722. BIND_ENUM_CONSTANT(STATUS_BODY); // Request resulted in body which must be read
  723. BIND_ENUM_CONSTANT(STATUS_CONNECTION_ERROR);
  724. BIND_ENUM_CONSTANT(STATUS_SSL_HANDSHAKE_ERROR);
  725. BIND_ENUM_CONSTANT(RESPONSE_CONTINUE);
  726. BIND_ENUM_CONSTANT(RESPONSE_SWITCHING_PROTOCOLS);
  727. BIND_ENUM_CONSTANT(RESPONSE_PROCESSING);
  728. // 2xx successful
  729. BIND_ENUM_CONSTANT(RESPONSE_OK);
  730. BIND_ENUM_CONSTANT(RESPONSE_CREATED);
  731. BIND_ENUM_CONSTANT(RESPONSE_ACCEPTED);
  732. BIND_ENUM_CONSTANT(RESPONSE_NON_AUTHORITATIVE_INFORMATION);
  733. BIND_ENUM_CONSTANT(RESPONSE_NO_CONTENT);
  734. BIND_ENUM_CONSTANT(RESPONSE_RESET_CONTENT);
  735. BIND_ENUM_CONSTANT(RESPONSE_PARTIAL_CONTENT);
  736. BIND_ENUM_CONSTANT(RESPONSE_MULTI_STATUS);
  737. BIND_ENUM_CONSTANT(RESPONSE_ALREADY_REPORTED);
  738. BIND_ENUM_CONSTANT(RESPONSE_IM_USED);
  739. // 3xx redirection
  740. BIND_ENUM_CONSTANT(RESPONSE_MULTIPLE_CHOICES);
  741. BIND_ENUM_CONSTANT(RESPONSE_MOVED_PERMANENTLY);
  742. BIND_ENUM_CONSTANT(RESPONSE_FOUND);
  743. BIND_ENUM_CONSTANT(RESPONSE_SEE_OTHER);
  744. BIND_ENUM_CONSTANT(RESPONSE_NOT_MODIFIED);
  745. BIND_ENUM_CONSTANT(RESPONSE_USE_PROXY);
  746. BIND_ENUM_CONSTANT(RESPONSE_SWITCH_PROXY);
  747. BIND_ENUM_CONSTANT(RESPONSE_TEMPORARY_REDIRECT);
  748. BIND_ENUM_CONSTANT(RESPONSE_PERMANENT_REDIRECT);
  749. // 4xx client error
  750. BIND_ENUM_CONSTANT(RESPONSE_BAD_REQUEST);
  751. BIND_ENUM_CONSTANT(RESPONSE_UNAUTHORIZED);
  752. BIND_ENUM_CONSTANT(RESPONSE_PAYMENT_REQUIRED);
  753. BIND_ENUM_CONSTANT(RESPONSE_FORBIDDEN);
  754. BIND_ENUM_CONSTANT(RESPONSE_NOT_FOUND);
  755. BIND_ENUM_CONSTANT(RESPONSE_METHOD_NOT_ALLOWED);
  756. BIND_ENUM_CONSTANT(RESPONSE_NOT_ACCEPTABLE);
  757. BIND_ENUM_CONSTANT(RESPONSE_PROXY_AUTHENTICATION_REQUIRED);
  758. BIND_ENUM_CONSTANT(RESPONSE_REQUEST_TIMEOUT);
  759. BIND_ENUM_CONSTANT(RESPONSE_CONFLICT);
  760. BIND_ENUM_CONSTANT(RESPONSE_GONE);
  761. BIND_ENUM_CONSTANT(RESPONSE_LENGTH_REQUIRED);
  762. BIND_ENUM_CONSTANT(RESPONSE_PRECONDITION_FAILED);
  763. BIND_ENUM_CONSTANT(RESPONSE_REQUEST_ENTITY_TOO_LARGE);
  764. BIND_ENUM_CONSTANT(RESPONSE_REQUEST_URI_TOO_LONG);
  765. BIND_ENUM_CONSTANT(RESPONSE_UNSUPPORTED_MEDIA_TYPE);
  766. BIND_ENUM_CONSTANT(RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE);
  767. BIND_ENUM_CONSTANT(RESPONSE_EXPECTATION_FAILED);
  768. BIND_ENUM_CONSTANT(RESPONSE_IM_A_TEAPOT);
  769. BIND_ENUM_CONSTANT(RESPONSE_MISDIRECTED_REQUEST);
  770. BIND_ENUM_CONSTANT(RESPONSE_UNPROCESSABLE_ENTITY);
  771. BIND_ENUM_CONSTANT(RESPONSE_LOCKED);
  772. BIND_ENUM_CONSTANT(RESPONSE_FAILED_DEPENDENCY);
  773. BIND_ENUM_CONSTANT(RESPONSE_UPGRADE_REQUIRED);
  774. BIND_ENUM_CONSTANT(RESPONSE_PRECONDITION_REQUIRED);
  775. BIND_ENUM_CONSTANT(RESPONSE_TOO_MANY_REQUESTS);
  776. BIND_ENUM_CONSTANT(RESPONSE_REQUEST_HEADER_FIELDS_TOO_LARGE);
  777. BIND_ENUM_CONSTANT(RESPONSE_UNAVAILABLE_FOR_LEGAL_REASONS);
  778. // 5xx server error
  779. BIND_ENUM_CONSTANT(RESPONSE_INTERNAL_SERVER_ERROR);
  780. BIND_ENUM_CONSTANT(RESPONSE_NOT_IMPLEMENTED);
  781. BIND_ENUM_CONSTANT(RESPONSE_BAD_GATEWAY);
  782. BIND_ENUM_CONSTANT(RESPONSE_SERVICE_UNAVAILABLE);
  783. BIND_ENUM_CONSTANT(RESPONSE_GATEWAY_TIMEOUT);
  784. BIND_ENUM_CONSTANT(RESPONSE_HTTP_VERSION_NOT_SUPPORTED);
  785. BIND_ENUM_CONSTANT(RESPONSE_VARIANT_ALSO_NEGOTIATES);
  786. BIND_ENUM_CONSTANT(RESPONSE_INSUFFICIENT_STORAGE);
  787. BIND_ENUM_CONSTANT(RESPONSE_LOOP_DETECTED);
  788. BIND_ENUM_CONSTANT(RESPONSE_NOT_EXTENDED);
  789. BIND_ENUM_CONSTANT(RESPONSE_NETWORK_AUTH_REQUIRED);
  790. }