http_client.cpp 28 KB

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