http_client_tcp.cpp 22 KB

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