http_client.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*************************************************************************/
  2. /* http_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "http_client.h"
  31. #include "io/stream_peer_ssl.h"
  32. Error HTTPClient::connect(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) {
  33. close();
  34. conn_port = p_port;
  35. conn_host = p_host;
  36. if (conn_host.begins_with("http://")) {
  37. conn_host = conn_host.replace_first("http://", "");
  38. } else if (conn_host.begins_with("https://")) {
  39. //use https
  40. conn_host = conn_host.replace_first("https://", "");
  41. }
  42. ssl = p_ssl;
  43. ssl_verify_host = p_verify_host;
  44. connection = tcp_connection;
  45. if (conn_host.is_valid_ip_address()) {
  46. //is ip
  47. Error err = tcp_connection->connect(IP_Address(conn_host), p_port);
  48. if (err) {
  49. status = STATUS_CANT_CONNECT;
  50. return err;
  51. }
  52. status = STATUS_CONNECTING;
  53. } else {
  54. //is hostname
  55. resolving = IP::get_singleton()->resolve_hostname_queue_item(conn_host);
  56. status = STATUS_RESOLVING;
  57. }
  58. return OK;
  59. }
  60. void HTTPClient::set_connection(const Ref<StreamPeer> &p_connection) {
  61. close();
  62. connection = p_connection;
  63. status = STATUS_CONNECTED;
  64. }
  65. Ref<StreamPeer> HTTPClient::get_connection() const {
  66. return connection;
  67. }
  68. Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const DVector<uint8_t> &p_body) {
  69. ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
  70. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
  71. ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA);
  72. static const char *_methods[METHOD_MAX] = {
  73. "GET",
  74. "HEAD",
  75. "POST",
  76. "PUT",
  77. "DELETE",
  78. "OPTIONS",
  79. "TRACE",
  80. "CONNECT"
  81. };
  82. String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n";
  83. if ((ssl && conn_port == 443) || (!ssl && conn_port == 80)) {
  84. // don't append the standard ports
  85. request += "Host: " + conn_host + "\r\n";
  86. } else {
  87. request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n";
  88. }
  89. bool add_clen = p_body.size() > 0;
  90. for (int i = 0; i < p_headers.size(); i++) {
  91. request += p_headers[i] + "\r\n";
  92. if (add_clen && p_headers[i].find("Content-Length:") == 0) {
  93. add_clen = false;
  94. }
  95. }
  96. if (add_clen) {
  97. request += "Content-Length: " + itos(p_body.size()) + "\r\n";
  98. //should it add utf8 encoding? not sure
  99. }
  100. request += "\r\n";
  101. CharString cs = request.utf8();
  102. DVector<uint8_t> data;
  103. //Maybe this goes faster somehow?
  104. for (int i = 0; i < cs.length(); i++) {
  105. data.append(cs[i]);
  106. }
  107. data.append_array(p_body);
  108. DVector<uint8_t>::Read r = data.read();
  109. Error err = connection->put_data(&r[0], data.size());
  110. if (err) {
  111. close();
  112. status = STATUS_CONNECTION_ERROR;
  113. return err;
  114. }
  115. status = STATUS_REQUESTING;
  116. return OK;
  117. }
  118. Error HTTPClient::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body) {
  119. ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER);
  120. ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER);
  121. ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA);
  122. static const char *_methods[METHOD_MAX] = {
  123. "GET",
  124. "HEAD",
  125. "POST",
  126. "PUT",
  127. "DELETE",
  128. "OPTIONS",
  129. "TRACE",
  130. "CONNECT"
  131. };
  132. String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n";
  133. if ((ssl && conn_port == 443) || (!ssl && conn_port == 80)) {
  134. // don't append the standard ports
  135. request += "Host: " + conn_host + "\r\n";
  136. } else {
  137. request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n";
  138. }
  139. bool add_clen = p_body.length() > 0;
  140. for (int i = 0; i < p_headers.size(); i++) {
  141. request += p_headers[i] + "\r\n";
  142. if (add_clen && p_headers[i].find("Content-Length:") == 0) {
  143. add_clen = false;
  144. }
  145. }
  146. if (add_clen) {
  147. request += "Content-Length: " + itos(p_body.utf8().length()) + "\r\n";
  148. //should it add utf8 encoding? not sure
  149. }
  150. request += "\r\n";
  151. request += p_body;
  152. CharString cs = request.utf8();
  153. Error err = connection->put_data((const uint8_t *)cs.ptr(), cs.length());
  154. if (err) {
  155. close();
  156. status = STATUS_CONNECTION_ERROR;
  157. return err;
  158. }
  159. status = STATUS_REQUESTING;
  160. return OK;
  161. }
  162. Error HTTPClient::send_body_text(const String &p_body) {
  163. return OK;
  164. }
  165. Error HTTPClient::send_body_data(const ByteArray &p_body) {
  166. return OK;
  167. }
  168. bool HTTPClient::has_response() const {
  169. return response_headers.size() != 0;
  170. }
  171. bool HTTPClient::is_response_chunked() const {
  172. return chunked;
  173. }
  174. int HTTPClient::get_response_code() const {
  175. return response_num;
  176. }
  177. Error HTTPClient::get_response_headers(List<String> *r_response) {
  178. if (!response_headers.size())
  179. return ERR_INVALID_PARAMETER;
  180. for (int i = 0; i < response_headers.size(); i++) {
  181. r_response->push_back(response_headers[i]);
  182. }
  183. response_headers.clear();
  184. return OK;
  185. }
  186. void HTTPClient::close() {
  187. if (tcp_connection->get_status() != StreamPeerTCP::STATUS_NONE)
  188. tcp_connection->disconnect();
  189. connection.unref();
  190. status = STATUS_DISCONNECTED;
  191. if (resolving != IP::RESOLVER_INVALID_ID) {
  192. IP::get_singleton()->erase_resolve_item(resolving);
  193. resolving = IP::RESOLVER_INVALID_ID;
  194. }
  195. response_headers.clear();
  196. response_str.clear();
  197. body_size = 0;
  198. body_left = 0;
  199. chunk_left = 0;
  200. response_num = 0;
  201. }
  202. Error HTTPClient::poll() {
  203. switch (status) {
  204. case STATUS_RESOLVING: {
  205. ERR_FAIL_COND_V(resolving == IP::RESOLVER_INVALID_ID, ERR_BUG);
  206. IP::ResolverStatus rstatus = IP::get_singleton()->get_resolve_item_status(resolving);
  207. switch (rstatus) {
  208. case IP::RESOLVER_STATUS_WAITING:
  209. return OK; //still resolving
  210. case IP::RESOLVER_STATUS_DONE: {
  211. IP_Address host = IP::get_singleton()->get_resolve_item_address(resolving);
  212. Error err = tcp_connection->connect(host, conn_port);
  213. IP::get_singleton()->erase_resolve_item(resolving);
  214. resolving = IP::RESOLVER_INVALID_ID;
  215. if (err) {
  216. status = STATUS_CANT_CONNECT;
  217. return err;
  218. }
  219. status = STATUS_CONNECTING;
  220. } break;
  221. case IP::RESOLVER_STATUS_NONE:
  222. case IP::RESOLVER_STATUS_ERROR: {
  223. IP::get_singleton()->erase_resolve_item(resolving);
  224. resolving = IP::RESOLVER_INVALID_ID;
  225. close();
  226. status = STATUS_CANT_RESOLVE;
  227. return ERR_CANT_RESOLVE;
  228. } break;
  229. }
  230. } break;
  231. case STATUS_CONNECTING: {
  232. StreamPeerTCP::Status s = tcp_connection->get_status();
  233. switch (s) {
  234. case StreamPeerTCP::STATUS_CONNECTING: {
  235. return OK; //do none
  236. } break;
  237. case StreamPeerTCP::STATUS_CONNECTED: {
  238. if (ssl) {
  239. Ref<StreamPeerSSL> ssl = StreamPeerSSL::create();
  240. Error err = ssl->connect(tcp_connection, true, ssl_verify_host ? conn_host : String());
  241. if (err != OK) {
  242. close();
  243. status = STATUS_SSL_HANDSHAKE_ERROR;
  244. return ERR_CANT_CONNECT;
  245. }
  246. //print_line("SSL! TURNED ON!");
  247. connection = ssl;
  248. }
  249. status = STATUS_CONNECTED;
  250. return OK;
  251. } break;
  252. case StreamPeerTCP::STATUS_ERROR:
  253. case StreamPeerTCP::STATUS_NONE: {
  254. close();
  255. status = STATUS_CANT_CONNECT;
  256. return ERR_CANT_CONNECT;
  257. } break;
  258. }
  259. } break;
  260. case STATUS_CONNECTED: {
  261. //request something please
  262. return OK;
  263. } break;
  264. case STATUS_REQUESTING: {
  265. while (true) {
  266. uint8_t byte;
  267. int rec = 0;
  268. Error err = _get_http_data(&byte, 1, rec);
  269. if (err != OK) {
  270. close();
  271. status = STATUS_CONNECTION_ERROR;
  272. return ERR_CONNECTION_ERROR;
  273. }
  274. if (rec == 0)
  275. return OK; //keep trying!
  276. response_str.push_back(byte);
  277. int rs = response_str.size();
  278. if (
  279. (rs >= 2 && response_str[rs - 2] == '\n' && response_str[rs - 1] == '\n') ||
  280. (rs >= 4 && response_str[rs - 4] == '\r' && response_str[rs - 3] == '\n' && rs >= 4 && response_str[rs - 2] == '\r' && response_str[rs - 1] == '\n')) {
  281. //end of response, parse.
  282. response_str.push_back(0);
  283. String response;
  284. response.parse_utf8((const char *)response_str.ptr());
  285. //print_line("END OF RESPONSE? :\n"+response+"\n------");
  286. Vector<String> responses = response.split("\n");
  287. body_size = 0;
  288. chunked = false;
  289. body_left = 0;
  290. chunk_left = 0;
  291. response_str.clear();
  292. response_headers.clear();
  293. response_num = RESPONSE_OK;
  294. for (int i = 0; i < responses.size(); i++) {
  295. String header = responses[i].strip_edges();
  296. String s = header.to_lower();
  297. if (s.length() == 0)
  298. continue;
  299. if (s.begins_with("content-length:")) {
  300. body_size = s.substr(s.find(":") + 1, s.length()).strip_edges().to_int();
  301. body_left = body_size;
  302. }
  303. if (s.begins_with("transfer-encoding:")) {
  304. String encoding = header.substr(header.find(":") + 1, header.length()).strip_edges();
  305. //print_line("TRANSFER ENCODING: "+encoding);
  306. if (encoding == "chunked") {
  307. chunked = true;
  308. }
  309. }
  310. if (i == 0 && responses[i].begins_with("HTTP")) {
  311. String num = responses[i].get_slicec(' ', 1);
  312. response_num = num.to_int();
  313. } else {
  314. response_headers.push_back(header);
  315. }
  316. }
  317. if (body_size == 0 && !chunked) {
  318. status = STATUS_CONNECTED; //ask for something again?
  319. } else {
  320. status = STATUS_BODY;
  321. }
  322. return OK;
  323. }
  324. }
  325. //wait for response
  326. return OK;
  327. } break;
  328. case STATUS_DISCONNECTED: {
  329. return ERR_UNCONFIGURED;
  330. } break;
  331. case STATUS_CONNECTION_ERROR: {
  332. return ERR_CONNECTION_ERROR;
  333. } break;
  334. case STATUS_CANT_CONNECT: {
  335. return ERR_CANT_CONNECT;
  336. } break;
  337. case STATUS_CANT_RESOLVE: {
  338. return ERR_CANT_RESOLVE;
  339. } break;
  340. }
  341. return OK;
  342. }
  343. Dictionary HTTPClient::_get_response_headers_as_dictionary() {
  344. List<String> rh;
  345. get_response_headers(&rh);
  346. Dictionary ret;
  347. for (const List<String>::Element *E = rh.front(); E; E = E->next()) {
  348. String s = E->get();
  349. int sp = s.find(":");
  350. if (sp == -1)
  351. continue;
  352. String key = s.substr(0, sp).strip_edges();
  353. String value = s.substr(sp + 1, s.length()).strip_edges();
  354. ret[key] = value;
  355. }
  356. return ret;
  357. }
  358. StringArray HTTPClient::_get_response_headers() {
  359. List<String> rh;
  360. get_response_headers(&rh);
  361. StringArray ret;
  362. ret.resize(rh.size());
  363. int idx = 0;
  364. for (const List<String>::Element *E = rh.front(); E; E = E->next()) {
  365. ret.set(idx++, E->get());
  366. }
  367. return ret;
  368. }
  369. int HTTPClient::get_response_body_length() const {
  370. return body_size;
  371. }
  372. ByteArray HTTPClient::read_response_body_chunk() {
  373. ERR_FAIL_COND_V(status != STATUS_BODY, ByteArray());
  374. Error err = OK;
  375. if (chunked) {
  376. while (true) {
  377. if (chunk_left == 0) {
  378. //reading len
  379. uint8_t b;
  380. int rec = 0;
  381. err = _get_http_data(&b, 1, rec);
  382. if (rec == 0)
  383. break;
  384. chunk.push_back(b);
  385. if (chunk.size() > 32) {
  386. ERR_PRINT("HTTP Invalid chunk hex len");
  387. status = STATUS_CONNECTION_ERROR;
  388. return ByteArray();
  389. }
  390. if (chunk.size() > 2 && chunk[chunk.size() - 2] == '\r' && chunk[chunk.size() - 1] == '\n') {
  391. int len = 0;
  392. for (int i = 0; i < chunk.size() - 2; i++) {
  393. char c = chunk[i];
  394. int v = 0;
  395. if (c >= '0' && c <= '9')
  396. v = c - '0';
  397. else if (c >= 'a' && c <= 'f')
  398. v = c - 'a' + 10;
  399. else if (c >= 'A' && c <= 'F')
  400. v = c - 'A' + 10;
  401. else {
  402. ERR_PRINT("HTTP Chunk len not in hex!!");
  403. status = STATUS_CONNECTION_ERROR;
  404. return ByteArray();
  405. }
  406. len <<= 4;
  407. len |= v;
  408. if (len > (1 << 24)) {
  409. ERR_PRINT("HTTP Chunk too big!! >16mb");
  410. status = STATUS_CONNECTION_ERROR;
  411. return ByteArray();
  412. }
  413. }
  414. if (len == 0) {
  415. //end!
  416. status = STATUS_CONNECTED;
  417. chunk.clear();
  418. return ByteArray();
  419. }
  420. chunk_left = len + 2;
  421. chunk.resize(chunk_left);
  422. }
  423. } else {
  424. int rec = 0;
  425. err = _get_http_data(&chunk[chunk.size() - chunk_left], chunk_left, rec);
  426. if (rec == 0) {
  427. break;
  428. }
  429. chunk_left -= rec;
  430. if (chunk_left == 0) {
  431. if (chunk[chunk.size() - 2] != '\r' || chunk[chunk.size() - 1] != '\n') {
  432. ERR_PRINT("HTTP Invalid chunk terminator (not \\r\\n)");
  433. status = STATUS_CONNECTION_ERROR;
  434. return ByteArray();
  435. }
  436. ByteArray ret;
  437. ret.resize(chunk.size() - 2);
  438. {
  439. ByteArray::Write w = ret.write();
  440. copymem(w.ptr(), chunk.ptr(), chunk.size() - 2);
  441. }
  442. chunk.clear();
  443. return ret;
  444. }
  445. break;
  446. }
  447. }
  448. } else {
  449. int to_read = MIN(body_left, read_chunk_size);
  450. ByteArray ret;
  451. ret.resize(to_read);
  452. int _offset = 0;
  453. while (to_read > 0) {
  454. int rec = 0;
  455. {
  456. ByteArray::Write w = ret.write();
  457. err = _get_http_data(w.ptr() + _offset, to_read, rec);
  458. }
  459. if (rec > 0) {
  460. body_left -= rec;
  461. to_read -= rec;
  462. _offset += rec;
  463. } else {
  464. if (to_read > 0) //ended up reading less
  465. ret.resize(_offset);
  466. break;
  467. }
  468. }
  469. if (body_left == 0) {
  470. status = STATUS_CONNECTED;
  471. }
  472. return ret;
  473. }
  474. if (err != OK) {
  475. close();
  476. if (err == ERR_FILE_EOF) {
  477. status = STATUS_DISCONNECTED; //server disconnected
  478. } else {
  479. status = STATUS_CONNECTION_ERROR;
  480. }
  481. } else if (body_left == 0 && !chunked) {
  482. status = STATUS_CONNECTED;
  483. }
  484. return ByteArray();
  485. }
  486. HTTPClient::Status HTTPClient::get_status() const {
  487. return status;
  488. }
  489. void HTTPClient::set_blocking_mode(bool p_enable) {
  490. blocking = p_enable;
  491. }
  492. bool HTTPClient::is_blocking_mode_enabled() const {
  493. return blocking;
  494. }
  495. Error HTTPClient::_get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  496. if (blocking) {
  497. Error err = connection->get_data(p_buffer, p_bytes);
  498. if (err == OK)
  499. r_received = p_bytes;
  500. else
  501. r_received = 0;
  502. return err;
  503. } else {
  504. return connection->get_partial_data(p_buffer, p_bytes, r_received);
  505. }
  506. }
  507. void HTTPClient::_bind_methods() {
  508. ObjectTypeDB::bind_method(_MD("connect:Error", "host", "port", "use_ssl", "verify_host"), &HTTPClient::connect, DEFVAL(false), DEFVAL(true));
  509. ObjectTypeDB::bind_method(_MD("set_connection", "connection:StreamPeer"), &HTTPClient::set_connection);
  510. ObjectTypeDB::bind_method(_MD("get_connection:StreamPeer"), &HTTPClient::get_connection);
  511. ObjectTypeDB::bind_method(_MD("request_raw", "method", "url", "headers", "body"), &HTTPClient::request_raw);
  512. ObjectTypeDB::bind_method(_MD("request", "method", "url", "headers", "body"), &HTTPClient::request, DEFVAL(String()));
  513. ObjectTypeDB::bind_method(_MD("send_body_text", "body"), &HTTPClient::send_body_text);
  514. ObjectTypeDB::bind_method(_MD("send_body_data", "body"), &HTTPClient::send_body_data);
  515. ObjectTypeDB::bind_method(_MD("close"), &HTTPClient::close);
  516. ObjectTypeDB::bind_method(_MD("has_response"), &HTTPClient::has_response);
  517. ObjectTypeDB::bind_method(_MD("is_response_chunked"), &HTTPClient::is_response_chunked);
  518. ObjectTypeDB::bind_method(_MD("get_response_code"), &HTTPClient::get_response_code);
  519. ObjectTypeDB::bind_method(_MD("get_response_headers"), &HTTPClient::_get_response_headers);
  520. ObjectTypeDB::bind_method(_MD("get_response_headers_as_dictionary"), &HTTPClient::_get_response_headers_as_dictionary);
  521. ObjectTypeDB::bind_method(_MD("get_response_body_length"), &HTTPClient::get_response_body_length);
  522. ObjectTypeDB::bind_method(_MD("read_response_body_chunk"), &HTTPClient::read_response_body_chunk);
  523. ObjectTypeDB::bind_method(_MD("set_read_chunk_size", "bytes"), &HTTPClient::set_read_chunk_size);
  524. ObjectTypeDB::bind_method(_MD("set_blocking_mode", "enabled"), &HTTPClient::set_blocking_mode);
  525. ObjectTypeDB::bind_method(_MD("is_blocking_mode_enabled"), &HTTPClient::is_blocking_mode_enabled);
  526. ObjectTypeDB::bind_method(_MD("get_status"), &HTTPClient::get_status);
  527. ObjectTypeDB::bind_method(_MD("poll:Error"), &HTTPClient::poll);
  528. ObjectTypeDB::bind_method(_MD("query_string_from_dict:String", "fields"), &HTTPClient::query_string_from_dict);
  529. BIND_CONSTANT(METHOD_GET);
  530. BIND_CONSTANT(METHOD_HEAD);
  531. BIND_CONSTANT(METHOD_POST);
  532. BIND_CONSTANT(METHOD_PUT);
  533. BIND_CONSTANT(METHOD_DELETE);
  534. BIND_CONSTANT(METHOD_OPTIONS);
  535. BIND_CONSTANT(METHOD_TRACE);
  536. BIND_CONSTANT(METHOD_CONNECT);
  537. BIND_CONSTANT(METHOD_MAX);
  538. BIND_CONSTANT(STATUS_DISCONNECTED);
  539. BIND_CONSTANT(STATUS_RESOLVING); //resolving hostname (if passed a hostname)
  540. BIND_CONSTANT(STATUS_CANT_RESOLVE);
  541. BIND_CONSTANT(STATUS_CONNECTING); //connecting to ip
  542. BIND_CONSTANT(STATUS_CANT_CONNECT);
  543. BIND_CONSTANT(STATUS_CONNECTED); //connected ); requests only accepted here
  544. BIND_CONSTANT(STATUS_REQUESTING); // request in progress
  545. BIND_CONSTANT(STATUS_BODY); // request resulted in body ); which must be read
  546. BIND_CONSTANT(STATUS_CONNECTION_ERROR);
  547. BIND_CONSTANT(STATUS_SSL_HANDSHAKE_ERROR);
  548. BIND_CONSTANT(RESPONSE_CONTINUE);
  549. BIND_CONSTANT(RESPONSE_SWITCHING_PROTOCOLS);
  550. BIND_CONSTANT(RESPONSE_PROCESSING);
  551. // 2xx successful
  552. BIND_CONSTANT(RESPONSE_OK);
  553. BIND_CONSTANT(RESPONSE_CREATED);
  554. BIND_CONSTANT(RESPONSE_ACCEPTED);
  555. BIND_CONSTANT(RESPONSE_NON_AUTHORITATIVE_INFORMATION);
  556. BIND_CONSTANT(RESPONSE_NO_CONTENT);
  557. BIND_CONSTANT(RESPONSE_RESET_CONTENT);
  558. BIND_CONSTANT(RESPONSE_PARTIAL_CONTENT);
  559. BIND_CONSTANT(RESPONSE_MULTI_STATUS);
  560. BIND_CONSTANT(RESPONSE_IM_USED);
  561. // 3xx redirection
  562. BIND_CONSTANT(RESPONSE_MULTIPLE_CHOICES);
  563. BIND_CONSTANT(RESPONSE_MOVED_PERMANENTLY);
  564. BIND_CONSTANT(RESPONSE_FOUND);
  565. BIND_CONSTANT(RESPONSE_SEE_OTHER);
  566. BIND_CONSTANT(RESPONSE_NOT_MODIFIED);
  567. BIND_CONSTANT(RESPONSE_USE_PROXY);
  568. BIND_CONSTANT(RESPONSE_TEMPORARY_REDIRECT);
  569. // 4xx client error
  570. BIND_CONSTANT(RESPONSE_BAD_REQUEST);
  571. BIND_CONSTANT(RESPONSE_UNAUTHORIZED);
  572. BIND_CONSTANT(RESPONSE_PAYMENT_REQUIRED);
  573. BIND_CONSTANT(RESPONSE_FORBIDDEN);
  574. BIND_CONSTANT(RESPONSE_NOT_FOUND);
  575. BIND_CONSTANT(RESPONSE_METHOD_NOT_ALLOWED);
  576. BIND_CONSTANT(RESPONSE_NOT_ACCEPTABLE);
  577. BIND_CONSTANT(RESPONSE_PROXY_AUTHENTICATION_REQUIRED);
  578. BIND_CONSTANT(RESPONSE_REQUEST_TIMEOUT);
  579. BIND_CONSTANT(RESPONSE_CONFLICT);
  580. BIND_CONSTANT(RESPONSE_GONE);
  581. BIND_CONSTANT(RESPONSE_LENGTH_REQUIRED);
  582. BIND_CONSTANT(RESPONSE_PRECONDITION_FAILED);
  583. BIND_CONSTANT(RESPONSE_REQUEST_ENTITY_TOO_LARGE);
  584. BIND_CONSTANT(RESPONSE_REQUEST_URI_TOO_LONG);
  585. BIND_CONSTANT(RESPONSE_UNSUPPORTED_MEDIA_TYPE);
  586. BIND_CONSTANT(RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE);
  587. BIND_CONSTANT(RESPONSE_EXPECTATION_FAILED);
  588. BIND_CONSTANT(RESPONSE_UNPROCESSABLE_ENTITY);
  589. BIND_CONSTANT(RESPONSE_LOCKED);
  590. BIND_CONSTANT(RESPONSE_FAILED_DEPENDENCY);
  591. BIND_CONSTANT(RESPONSE_UPGRADE_REQUIRED);
  592. // 5xx server error
  593. BIND_CONSTANT(RESPONSE_INTERNAL_SERVER_ERROR);
  594. BIND_CONSTANT(RESPONSE_NOT_IMPLEMENTED);
  595. BIND_CONSTANT(RESPONSE_BAD_GATEWAY);
  596. BIND_CONSTANT(RESPONSE_SERVICE_UNAVAILABLE);
  597. BIND_CONSTANT(RESPONSE_GATEWAY_TIMEOUT);
  598. BIND_CONSTANT(RESPONSE_HTTP_VERSION_NOT_SUPPORTED);
  599. BIND_CONSTANT(RESPONSE_INSUFFICIENT_STORAGE);
  600. BIND_CONSTANT(RESPONSE_NOT_EXTENDED);
  601. }
  602. void HTTPClient::set_read_chunk_size(int p_size) {
  603. ERR_FAIL_COND(p_size < 256 || p_size > (1 << 24));
  604. read_chunk_size = p_size;
  605. }
  606. String HTTPClient::query_string_from_dict(const Dictionary &p_dict) {
  607. String query = "";
  608. Array keys = p_dict.keys();
  609. for (int i = 0; i < keys.size(); ++i) {
  610. query += "&" + String(keys[i]).http_escape() + "=" + String(p_dict[keys[i]]).http_escape();
  611. }
  612. query.erase(0, 1);
  613. return query;
  614. }
  615. HTTPClient::HTTPClient() {
  616. tcp_connection = StreamPeerTCP::create_ref();
  617. resolving = IP::RESOLVER_INVALID_ID;
  618. status = STATUS_DISCONNECTED;
  619. conn_port = 80;
  620. body_size = 0;
  621. chunked = false;
  622. body_left = 0;
  623. chunk_left = 0;
  624. response_num = 0;
  625. ssl = false;
  626. blocking = false;
  627. read_chunk_size = 4096;
  628. }
  629. HTTPClient::~HTTPClient() {
  630. }