http_client.cpp 28 KB

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