http_client_tcp.cpp 22 KB

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