http_client_tcp.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /**************************************************************************/
  2. /* http_client_tcp.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef WEB_ENABLED
  31. #include "http_client_tcp.h"
  32. #include "core/io/stream_peer_tls.h"
  33. #include "core/version.h"
  34. HTTPClient *HTTPClientTCP::_create_func() {
  35. return memnew(HTTPClientTCP);
  36. }
  37. Error HTTPClientTCP::connect_to_host(const String &p_host, int p_port, bool p_tls, bool p_verify_host) {
  38. close();
  39. conn_port = p_port;
  40. conn_host = p_host;
  41. ip_candidates.clear();
  42. tls = p_tls;
  43. tls_verify_host = p_verify_host;
  44. String host_lower = conn_host.to_lower();
  45. if (host_lower.begins_with("http://")) {
  46. conn_host = conn_host.substr(7, conn_host.length() - 7);
  47. } else if (host_lower.begins_with("https://")) {
  48. tls = true;
  49. conn_host = conn_host.substr(8, conn_host.length() - 8);
  50. }
  51. ERR_FAIL_COND_V(conn_host.length() < HOST_MIN_LEN, ERR_INVALID_PARAMETER);
  52. if (conn_port < 0) {
  53. if (tls) {
  54. conn_port = PORT_HTTPS;
  55. } else {
  56. conn_port = PORT_HTTP;
  57. }
  58. }
  59. connection = tcp_connection;
  60. if (tls && https_proxy_port != -1) {
  61. proxy_client.instantiate(); // Needs proxy negotiation.
  62. server_host = https_proxy_host;
  63. server_port = https_proxy_port;
  64. } else if (!tls && http_proxy_port != -1) {
  65. server_host = http_proxy_host;
  66. server_port = http_proxy_port;
  67. } else {
  68. server_host = conn_host;
  69. server_port = conn_port;
  70. }
  71. if (server_host.is_valid_ip_address()) {
  72. // Host contains valid IP.
  73. Error err = tcp_connection->connect_to_host(IPAddress(server_host), server_port);
  74. if (err) {
  75. status = STATUS_CANT_CONNECT;
  76. return err;
  77. }
  78. status = STATUS_CONNECTING;
  79. } else {
  80. // Host contains hostname and needs to be resolved to IP.
  81. resolving = IP::get_singleton()->resolve_hostname_queue_item(server_host);
  82. if (resolving == IP::RESOLVER_INVALID_ID) {
  83. status = STATUS_CANT_RESOLVE;
  84. return ERR_CANT_RESOLVE;
  85. }
  86. status = STATUS_RESOLVING;
  87. }
  88. return OK;
  89. }
  90. void HTTPClientTCP::set_connection(const Ref<StreamPeer> &p_connection) {
  91. ERR_FAIL_COND_MSG(p_connection.is_null(), "Connection is not a reference to a valid StreamPeer object.");
  92. if (tls) {
  93. ERR_FAIL_NULL_MSG(Object::cast_to<StreamPeerTLS>(p_connection.ptr()),
  94. "Connection is not a reference to a valid StreamPeerTLS object.");
  95. }
  96. if (connection == p_connection) {
  97. return;
  98. }
  99. close();
  100. connection = p_connection;
  101. status = STATUS_CONNECTED;
  102. }
  103. Ref<StreamPeer> HTTPClientTCP::get_connection() const {
  104. return connection;
  105. }
  106. static bool _check_request_url(HTTPClientTCP::Method p_method, const String &p_url) {
  107. switch (p_method) {
  108. case HTTPClientTCP::METHOD_CONNECT: {
  109. // Authority in host:port format, as in RFC7231.
  110. int pos = p_url.find_char(':');
  111. return 0 < pos && pos < p_url.length() - 1;
  112. }
  113. case HTTPClientTCP::METHOD_OPTIONS: {
  114. if (p_url == "*") {
  115. return true;
  116. }
  117. [[fallthrough]];
  118. }
  119. default:
  120. // Absolute path or absolute URL.
  121. return p_url.begins_with("/") || p_url.begins_with("http://") || p_url.begins_with("https://");
  122. }
  123. }
  124. Error HTTPClientTCP::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const uint8_t *p_body, int p_body_size) {
  125. ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
  126. ERR_FAIL_COND_V(!_check_request_url(p_method, p_url), ERR_INVALID_PARAMETER);
  127. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
  128. ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA);
  129. Error err = verify_headers(p_headers);
  130. if (err) {
  131. return err;
  132. }
  133. String uri = p_url;
  134. if (!tls && http_proxy_port != -1) {
  135. uri = vformat("http://%s:%d%s", conn_host, conn_port, p_url);
  136. }
  137. String request = String(_methods[p_method]) + " " + uri + " HTTP/1.1\r\n";
  138. bool add_host = true;
  139. bool add_clen = p_body_size > 0;
  140. bool add_uagent = true;
  141. bool add_accept = true;
  142. for (int i = 0; i < p_headers.size(); i++) {
  143. request += p_headers[i] + "\r\n";
  144. if (add_host && p_headers[i].findn("Host:") == 0) {
  145. add_host = false;
  146. }
  147. if (add_clen && p_headers[i].findn("Content-Length:") == 0) {
  148. add_clen = false;
  149. }
  150. if (add_uagent && p_headers[i].findn("User-Agent:") == 0) {
  151. add_uagent = false;
  152. }
  153. if (add_accept && p_headers[i].findn("Accept:") == 0) {
  154. add_accept = false;
  155. }
  156. }
  157. if (add_host) {
  158. if ((tls && conn_port == PORT_HTTPS) || (!tls && conn_port == PORT_HTTP)) {
  159. // Don't append the standard ports.
  160. request += "Host: " + conn_host + "\r\n";
  161. } else {
  162. request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n";
  163. }
  164. }
  165. if (add_clen) {
  166. request += "Content-Length: " + itos(p_body_size) + "\r\n";
  167. // Should it add utf8 encoding?
  168. }
  169. if (add_uagent) {
  170. request += "User-Agent: GodotEngine/" + String(VERSION_FULL_BUILD) + " (" + OS::get_singleton()->get_name() + ")\r\n";
  171. }
  172. if (add_accept) {
  173. request += "Accept: */*\r\n";
  174. }
  175. request += "\r\n";
  176. CharString cs = request.utf8();
  177. request_buffer->clear();
  178. request_buffer->put_data((const uint8_t *)cs.get_data(), cs.length());
  179. if (p_body_size > 0) {
  180. request_buffer->put_data(p_body, p_body_size);
  181. }
  182. request_buffer->seek(0);
  183. status = STATUS_REQUESTING;
  184. head_request = p_method == METHOD_HEAD;
  185. return OK;
  186. }
  187. bool HTTPClientTCP::has_response() const {
  188. return response_headers.size() != 0;
  189. }
  190. bool HTTPClientTCP::is_response_chunked() const {
  191. return chunked;
  192. }
  193. int HTTPClientTCP::get_response_code() const {
  194. return response_num;
  195. }
  196. Error HTTPClientTCP::get_response_headers(List<String> *r_response) {
  197. if (!response_headers.size()) {
  198. return ERR_INVALID_PARAMETER;
  199. }
  200. for (int i = 0; i < response_headers.size(); i++) {
  201. r_response->push_back(response_headers[i]);
  202. }
  203. response_headers.clear();
  204. return OK;
  205. }
  206. void HTTPClientTCP::close() {
  207. if (tcp_connection->get_status() != StreamPeerTCP::STATUS_NONE) {
  208. tcp_connection->disconnect_from_host();
  209. }
  210. connection.unref();
  211. proxy_client.unref();
  212. status = STATUS_DISCONNECTED;
  213. head_request = false;
  214. if (resolving != IP::RESOLVER_INVALID_ID) {
  215. IP::get_singleton()->erase_resolve_item(resolving);
  216. resolving = IP::RESOLVER_INVALID_ID;
  217. }
  218. ip_candidates.clear();
  219. response_headers.clear();
  220. response_str.clear();
  221. request_buffer->clear();
  222. body_size = -1;
  223. body_left = 0;
  224. chunk_left = 0;
  225. chunk_trailer_part = false;
  226. read_until_eof = false;
  227. response_num = 0;
  228. handshaking = false;
  229. }
  230. Error HTTPClientTCP::poll() {
  231. if (tcp_connection.is_valid()) {
  232. tcp_connection->poll();
  233. }
  234. switch (status) {
  235. case STATUS_RESOLVING: {
  236. ERR_FAIL_COND_V(resolving == IP::RESOLVER_INVALID_ID, ERR_BUG);
  237. IP::ResolverStatus rstatus = IP::get_singleton()->get_resolve_item_status(resolving);
  238. switch (rstatus) {
  239. case IP::RESOLVER_STATUS_WAITING:
  240. return OK; // Still resolving.
  241. case IP::RESOLVER_STATUS_DONE: {
  242. ip_candidates = IP::get_singleton()->get_resolve_item_addresses(resolving);
  243. IP::get_singleton()->erase_resolve_item(resolving);
  244. resolving = IP::RESOLVER_INVALID_ID;
  245. Error err = ERR_BUG; // Should be at least one entry.
  246. while (ip_candidates.size() > 0) {
  247. err = tcp_connection->connect_to_host(ip_candidates.pop_front(), server_port);
  248. if (err == OK) {
  249. break;
  250. }
  251. }
  252. if (err) {
  253. status = STATUS_CANT_CONNECT;
  254. return err;
  255. }
  256. status = STATUS_CONNECTING;
  257. } break;
  258. case IP::RESOLVER_STATUS_NONE:
  259. case IP::RESOLVER_STATUS_ERROR: {
  260. IP::get_singleton()->erase_resolve_item(resolving);
  261. resolving = IP::RESOLVER_INVALID_ID;
  262. close();
  263. status = STATUS_CANT_RESOLVE;
  264. return ERR_CANT_RESOLVE;
  265. } break;
  266. }
  267. } break;
  268. case STATUS_CONNECTING: {
  269. StreamPeerTCP::Status s = tcp_connection->get_status();
  270. switch (s) {
  271. case StreamPeerTCP::STATUS_CONNECTING: {
  272. return OK;
  273. } break;
  274. case StreamPeerTCP::STATUS_CONNECTED: {
  275. if (tls && proxy_client.is_valid()) {
  276. Error err = proxy_client->poll();
  277. if (err == ERR_UNCONFIGURED) {
  278. proxy_client->set_connection(tcp_connection);
  279. const Vector<String> headers;
  280. err = proxy_client->request(METHOD_CONNECT, vformat("%s:%d", conn_host, conn_port), headers, nullptr, 0);
  281. if (err != OK) {
  282. status = STATUS_CANT_CONNECT;
  283. return err;
  284. }
  285. } else if (err != OK) {
  286. status = STATUS_CANT_CONNECT;
  287. return err;
  288. }
  289. switch (proxy_client->get_status()) {
  290. case STATUS_REQUESTING: {
  291. return OK;
  292. } break;
  293. case STATUS_BODY: {
  294. proxy_client->read_response_body_chunk();
  295. return OK;
  296. } break;
  297. case STATUS_CONNECTED: {
  298. if (proxy_client->get_response_code() != RESPONSE_OK) {
  299. status = STATUS_CANT_CONNECT;
  300. return ERR_CANT_CONNECT;
  301. }
  302. proxy_client.unref();
  303. return OK;
  304. }
  305. case STATUS_DISCONNECTED:
  306. case STATUS_RESOLVING:
  307. case STATUS_CONNECTING: {
  308. status = STATUS_CANT_CONNECT;
  309. ERR_FAIL_V(ERR_BUG);
  310. } break;
  311. default: {
  312. status = STATUS_CANT_CONNECT;
  313. return ERR_CANT_CONNECT;
  314. } break;
  315. }
  316. } else if (tls) {
  317. Ref<StreamPeerTLS> tls_conn;
  318. if (!handshaking) {
  319. // Connect the StreamPeerTLS and start handshaking.
  320. tls_conn = Ref<StreamPeerTLS>(StreamPeerTLS::create());
  321. tls_conn->set_blocking_handshake_enabled(false);
  322. Error err = tls_conn->connect_to_stream(tcp_connection, tls_verify_host, conn_host);
  323. if (err != OK) {
  324. close();
  325. status = STATUS_TLS_HANDSHAKE_ERROR;
  326. return ERR_CANT_CONNECT;
  327. }
  328. connection = tls_conn;
  329. handshaking = true;
  330. } else {
  331. // We are already handshaking, which means we can use your already active TLS connection.
  332. tls_conn = static_cast<Ref<StreamPeerTLS>>(connection);
  333. if (tls_conn.is_null()) {
  334. close();
  335. status = STATUS_TLS_HANDSHAKE_ERROR;
  336. return ERR_CANT_CONNECT;
  337. }
  338. tls_conn->poll(); // Try to finish the handshake.
  339. }
  340. if (tls_conn->get_status() == StreamPeerTLS::STATUS_CONNECTED) {
  341. // Handshake has been successful.
  342. handshaking = false;
  343. ip_candidates.clear();
  344. status = STATUS_CONNECTED;
  345. return OK;
  346. } else if (tls_conn->get_status() != StreamPeerTLS::STATUS_HANDSHAKING) {
  347. // Handshake has failed.
  348. close();
  349. status = STATUS_TLS_HANDSHAKE_ERROR;
  350. return ERR_CANT_CONNECT;
  351. }
  352. // ... we will need to poll more for handshake to finish.
  353. } else {
  354. ip_candidates.clear();
  355. status = STATUS_CONNECTED;
  356. }
  357. return OK;
  358. } break;
  359. case StreamPeerTCP::STATUS_ERROR:
  360. case StreamPeerTCP::STATUS_NONE: {
  361. Error err = ERR_CANT_CONNECT;
  362. while (ip_candidates.size() > 0) {
  363. tcp_connection->disconnect_from_host();
  364. err = tcp_connection->connect_to_host(ip_candidates.pop_front(), server_port);
  365. if (err == OK) {
  366. return OK;
  367. }
  368. }
  369. close();
  370. status = STATUS_CANT_CONNECT;
  371. return err;
  372. } break;
  373. }
  374. } break;
  375. case STATUS_BODY:
  376. case STATUS_CONNECTED: {
  377. // Check if we are still connected.
  378. if (tls) {
  379. Ref<StreamPeerTLS> tmp = connection;
  380. tmp->poll();
  381. if (tmp->get_status() != StreamPeerTLS::STATUS_CONNECTED) {
  382. status = STATUS_CONNECTION_ERROR;
  383. return ERR_CONNECTION_ERROR;
  384. }
  385. } else if (tcp_connection->get_status() != StreamPeerTCP::STATUS_CONNECTED) {
  386. status = STATUS_CONNECTION_ERROR;
  387. return ERR_CONNECTION_ERROR;
  388. }
  389. // Connection established, requests can now be made.
  390. return OK;
  391. } break;
  392. case STATUS_REQUESTING: {
  393. if (request_buffer->get_available_bytes()) {
  394. int avail = request_buffer->get_available_bytes();
  395. int pos = request_buffer->get_position();
  396. const Vector<uint8_t> data = request_buffer->get_data_array();
  397. int wrote = 0;
  398. Error err;
  399. if (blocking) {
  400. err = connection->put_data(data.ptr() + pos, avail);
  401. wrote += avail;
  402. } else {
  403. err = connection->put_partial_data(data.ptr() + pos, avail, wrote);
  404. }
  405. if (err != OK) {
  406. close();
  407. status = STATUS_CONNECTION_ERROR;
  408. return ERR_CONNECTION_ERROR;
  409. }
  410. pos += wrote;
  411. request_buffer->seek(pos);
  412. if (avail - wrote > 0) {
  413. return OK;
  414. }
  415. request_buffer->clear();
  416. }
  417. while (true) {
  418. uint8_t byte;
  419. int rec = 0;
  420. Error err = _get_http_data(&byte, 1, rec);
  421. if (err != OK) {
  422. close();
  423. status = STATUS_CONNECTION_ERROR;
  424. return ERR_CONNECTION_ERROR;
  425. }
  426. if (rec == 0) {
  427. return OK; // Still requesting, keep trying!
  428. }
  429. response_str.push_back(byte);
  430. int rs = response_str.size();
  431. if (
  432. (rs >= 2 && response_str[rs - 2] == '\n' && response_str[rs - 1] == '\n') ||
  433. (rs >= 4 && response_str[rs - 4] == '\r' && response_str[rs - 3] == '\n' && response_str[rs - 2] == '\r' && response_str[rs - 1] == '\n')) {
  434. // End of response, parse.
  435. response_str.push_back(0);
  436. String response;
  437. response.parse_utf8((const char *)response_str.ptr());
  438. Vector<String> responses = response.split("\n");
  439. body_size = -1;
  440. chunked = false;
  441. body_left = 0;
  442. chunk_left = 0;
  443. chunk_trailer_part = false;
  444. read_until_eof = false;
  445. response_str.clear();
  446. response_headers.clear();
  447. response_num = RESPONSE_OK;
  448. // Per the HTTP 1.1 spec, keep-alive is the default.
  449. // Not following that specification breaks standard implementations.
  450. // Broken web servers should be fixed.
  451. bool keep_alive = true;
  452. for (int i = 0; i < responses.size(); i++) {
  453. String header = responses[i].strip_edges();
  454. String s = header.to_lower();
  455. if (s.length() == 0) {
  456. continue;
  457. }
  458. if (s.begins_with("content-length:")) {
  459. body_size = s.substr(s.find(":") + 1, s.length()).strip_edges().to_int();
  460. body_left = body_size;
  461. } else if (s.begins_with("transfer-encoding:")) {
  462. String encoding = header.substr(header.find(":") + 1, header.length()).strip_edges();
  463. if (encoding == "chunked") {
  464. chunked = true;
  465. }
  466. } else if (s.begins_with("connection: close")) {
  467. keep_alive = false;
  468. }
  469. if (i == 0 && responses[i].begins_with("HTTP")) {
  470. String num = responses[i].get_slicec(' ', 1);
  471. response_num = num.to_int();
  472. } else {
  473. response_headers.push_back(header);
  474. }
  475. }
  476. // This is a HEAD request, we won't receive anything.
  477. if (head_request) {
  478. body_size = 0;
  479. body_left = 0;
  480. }
  481. if (body_size != -1 || chunked) {
  482. status = STATUS_BODY;
  483. } else if (!keep_alive) {
  484. read_until_eof = true;
  485. status = STATUS_BODY;
  486. } else {
  487. status = STATUS_CONNECTED;
  488. }
  489. return OK;
  490. }
  491. }
  492. } break;
  493. case STATUS_DISCONNECTED: {
  494. return ERR_UNCONFIGURED;
  495. } break;
  496. case STATUS_CONNECTION_ERROR:
  497. case STATUS_TLS_HANDSHAKE_ERROR: {
  498. return ERR_CONNECTION_ERROR;
  499. } break;
  500. case STATUS_CANT_CONNECT: {
  501. return ERR_CANT_CONNECT;
  502. } break;
  503. case STATUS_CANT_RESOLVE: {
  504. return ERR_CANT_RESOLVE;
  505. } break;
  506. }
  507. return OK;
  508. }
  509. int64_t HTTPClientTCP::get_response_body_length() const {
  510. return body_size;
  511. }
  512. PackedByteArray HTTPClientTCP::read_response_body_chunk() {
  513. ERR_FAIL_COND_V(status != STATUS_BODY, PackedByteArray());
  514. PackedByteArray ret;
  515. Error err = OK;
  516. if (chunked) {
  517. while (true) {
  518. if (chunk_trailer_part) {
  519. // We need to consume the trailer part too or keep-alive will break.
  520. uint8_t b;
  521. int rec = 0;
  522. err = _get_http_data(&b, 1, rec);
  523. if (rec == 0) {
  524. break;
  525. }
  526. chunk.push_back(b);
  527. int cs = chunk.size();
  528. if ((cs >= 2 && chunk[cs - 2] == '\r' && chunk[cs - 1] == '\n')) {
  529. if (cs == 2) {
  530. // Finally over.
  531. chunk_trailer_part = false;
  532. status = STATUS_CONNECTED;
  533. chunk.clear();
  534. break;
  535. } else {
  536. // We do not process nor return the trailer data.
  537. chunk.clear();
  538. }
  539. }
  540. } else if (chunk_left == 0) {
  541. // Reading length.
  542. uint8_t b;
  543. int rec = 0;
  544. err = _get_http_data(&b, 1, rec);
  545. if (rec == 0) {
  546. break;
  547. }
  548. chunk.push_back(b);
  549. if (chunk.size() > 32) {
  550. ERR_PRINT("HTTP Invalid chunk hex len");
  551. status = STATUS_CONNECTION_ERROR;
  552. break;
  553. }
  554. if (chunk.size() > 2 && chunk[chunk.size() - 2] == '\r' && chunk[chunk.size() - 1] == '\n') {
  555. int len = 0;
  556. for (int i = 0; i < chunk.size() - 2; i++) {
  557. char c = chunk[i];
  558. int v = 0;
  559. if (is_digit(c)) {
  560. v = c - '0';
  561. } else if (c >= 'a' && c <= 'f') {
  562. v = c - 'a' + 10;
  563. } else if (c >= 'A' && c <= 'F') {
  564. v = c - 'A' + 10;
  565. } else {
  566. ERR_PRINT("HTTP Chunk len not in hex!!");
  567. status = STATUS_CONNECTION_ERROR;
  568. break;
  569. }
  570. len <<= 4;
  571. len |= v;
  572. if (len > (1 << 24)) {
  573. ERR_PRINT("HTTP Chunk too big!! >16mb");
  574. status = STATUS_CONNECTION_ERROR;
  575. break;
  576. }
  577. }
  578. if (len == 0) {
  579. // End reached!
  580. chunk_trailer_part = true;
  581. chunk.clear();
  582. break;
  583. }
  584. chunk_left = len + 2;
  585. chunk.resize(chunk_left);
  586. }
  587. } else {
  588. int rec = 0;
  589. err = _get_http_data(&chunk.write[chunk.size() - chunk_left], chunk_left, rec);
  590. if (rec == 0) {
  591. break;
  592. }
  593. chunk_left -= rec;
  594. if (chunk_left == 0) {
  595. if (chunk[chunk.size() - 2] != '\r' || chunk[chunk.size() - 1] != '\n') {
  596. ERR_PRINT("HTTP Invalid chunk terminator (not \\r\\n)");
  597. status = STATUS_CONNECTION_ERROR;
  598. break;
  599. }
  600. ret.resize(chunk.size() - 2);
  601. uint8_t *w = ret.ptrw();
  602. memcpy(w, chunk.ptr(), chunk.size() - 2);
  603. chunk.clear();
  604. }
  605. break;
  606. }
  607. }
  608. } else {
  609. int to_read = !read_until_eof ? MIN(body_left, read_chunk_size) : read_chunk_size;
  610. ret.resize(to_read);
  611. int _offset = 0;
  612. while (to_read > 0) {
  613. int rec = 0;
  614. {
  615. uint8_t *w = ret.ptrw();
  616. err = _get_http_data(w + _offset, to_read, rec);
  617. }
  618. if (rec <= 0) { // Ended up reading less.
  619. ret.resize(_offset);
  620. break;
  621. } else {
  622. _offset += rec;
  623. to_read -= rec;
  624. if (!read_until_eof) {
  625. body_left -= rec;
  626. }
  627. }
  628. if (err != OK) {
  629. ret.resize(_offset);
  630. break;
  631. }
  632. }
  633. }
  634. if (err != OK) {
  635. close();
  636. if (err == ERR_FILE_EOF) {
  637. status = STATUS_DISCONNECTED; // Server disconnected.
  638. } else {
  639. status = STATUS_CONNECTION_ERROR;
  640. }
  641. } else if (body_left == 0 && !chunked && !read_until_eof) {
  642. status = STATUS_CONNECTED;
  643. }
  644. return ret;
  645. }
  646. HTTPClientTCP::Status HTTPClientTCP::get_status() const {
  647. return status;
  648. }
  649. void HTTPClientTCP::set_blocking_mode(bool p_enable) {
  650. blocking = p_enable;
  651. }
  652. bool HTTPClientTCP::is_blocking_mode_enabled() const {
  653. return blocking;
  654. }
  655. Error HTTPClientTCP::_get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  656. if (blocking) {
  657. // We can't use StreamPeer.get_data, since when reaching EOF we will get an
  658. // error without knowing how many bytes we received.
  659. Error err = ERR_FILE_EOF;
  660. int read = 0;
  661. int left = p_bytes;
  662. r_received = 0;
  663. while (left > 0) {
  664. err = connection->get_partial_data(p_buffer + r_received, left, read);
  665. if (err == OK) {
  666. r_received += read;
  667. } else if (err == ERR_FILE_EOF) {
  668. r_received += read;
  669. return err;
  670. } else {
  671. return err;
  672. }
  673. left -= read;
  674. }
  675. return err;
  676. } else {
  677. return connection->get_partial_data(p_buffer, p_bytes, r_received);
  678. }
  679. }
  680. void HTTPClientTCP::set_read_chunk_size(int p_size) {
  681. ERR_FAIL_COND(p_size < 256 || p_size > (1 << 24));
  682. read_chunk_size = p_size;
  683. }
  684. int HTTPClientTCP::get_read_chunk_size() const {
  685. return read_chunk_size;
  686. }
  687. void HTTPClientTCP::set_http_proxy(const String &p_host, int p_port) {
  688. if (p_host.is_empty() || p_port == -1) {
  689. http_proxy_host = "";
  690. http_proxy_port = -1;
  691. } else {
  692. http_proxy_host = p_host;
  693. http_proxy_port = p_port;
  694. }
  695. }
  696. void HTTPClientTCP::set_https_proxy(const String &p_host, int p_port) {
  697. if (p_host.is_empty() || p_port == -1) {
  698. https_proxy_host = "";
  699. https_proxy_port = -1;
  700. } else {
  701. https_proxy_host = p_host;
  702. https_proxy_port = p_port;
  703. }
  704. }
  705. HTTPClientTCP::HTTPClientTCP() {
  706. tcp_connection.instantiate();
  707. request_buffer.instantiate();
  708. }
  709. HTTPClient *(*HTTPClient::_create)() = HTTPClientTCP::_create_func;
  710. #endif // WEB_ENABLED