uclient-http.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. * uclient - ustream based protocol client library
  3. *
  4. * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <unistd.h>
  21. #include <stdint.h>
  22. #include <libubox/ustream.h>
  23. #include <libubox/ustream-ssl.h>
  24. #include <libubox/usock.h>
  25. #include <libubox/blobmsg.h>
  26. #include "uclient.h"
  27. #include "uclient-utils.h"
  28. #include "uclient-backend.h"
  29. enum auth_type {
  30. AUTH_TYPE_UNKNOWN,
  31. AUTH_TYPE_NONE,
  32. AUTH_TYPE_BASIC,
  33. AUTH_TYPE_DIGEST,
  34. };
  35. enum request_type {
  36. REQ_GET,
  37. REQ_HEAD,
  38. REQ_POST,
  39. __REQ_MAX
  40. };
  41. enum http_state {
  42. HTTP_STATE_INIT,
  43. HTTP_STATE_HEADERS_SENT,
  44. HTTP_STATE_REQUEST_DONE,
  45. HTTP_STATE_RECV_HEADERS,
  46. HTTP_STATE_RECV_DATA,
  47. HTTP_STATE_ERROR,
  48. };
  49. static const char * const request_types[__REQ_MAX] = {
  50. [REQ_GET] = "GET",
  51. [REQ_HEAD] = "HEAD",
  52. [REQ_POST] = "POST",
  53. };
  54. struct uclient_http {
  55. struct uclient uc;
  56. struct ustream_ssl_ctx *ssl_ctx;
  57. struct ustream *us;
  58. struct ustream_fd ufd;
  59. struct ustream_ssl ussl;
  60. bool ssl_require_validation;
  61. bool ssl_ctx_ext;
  62. bool ssl;
  63. bool eof;
  64. bool connection_close;
  65. enum request_type req_type;
  66. enum http_state state;
  67. enum auth_type auth_type;
  68. char *auth_str;
  69. long read_chunked;
  70. long content_length;
  71. uint32_t nc;
  72. struct blob_buf headers;
  73. struct blob_buf meta;
  74. };
  75. enum {
  76. PREFIX_HTTP,
  77. PREFIX_HTTPS,
  78. __PREFIX_MAX,
  79. };
  80. static const char * const uclient_http_prefix[] = {
  81. [PREFIX_HTTP] = "http://",
  82. [PREFIX_HTTPS] = "https://",
  83. [__PREFIX_MAX] = NULL
  84. };
  85. static int uclient_do_connect(struct uclient_http *uh, const char *port)
  86. {
  87. int fd;
  88. if (uh->uc.url->port)
  89. port = uh->uc.url->port;
  90. fd = usock(USOCK_TCP | USOCK_NONBLOCK, uh->uc.url->host, port);
  91. if (fd < 0)
  92. return -1;
  93. ustream_fd_init(&uh->ufd, fd);
  94. return 0;
  95. }
  96. static void uclient_http_disconnect(struct uclient_http *uh)
  97. {
  98. if (!uh->us)
  99. return;
  100. if (uh->ssl)
  101. ustream_free(&uh->ussl.stream);
  102. ustream_free(&uh->ufd.stream);
  103. close(uh->ufd.fd.fd);
  104. uh->us = NULL;
  105. }
  106. static void uclient_http_free_url_state(struct uclient *cl)
  107. {
  108. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  109. uh->auth_type = AUTH_TYPE_UNKNOWN;
  110. free(uh->auth_str);
  111. uh->auth_str = NULL;
  112. uclient_http_disconnect(uh);
  113. }
  114. static void uclient_http_error(struct uclient_http *uh, int code)
  115. {
  116. uh->state = HTTP_STATE_ERROR;
  117. uh->us->eof = true;
  118. ustream_state_change(uh->us);
  119. uclient_backend_set_error(&uh->uc, code);
  120. }
  121. static void uclient_notify_eof(struct uclient_http *uh)
  122. {
  123. struct ustream *us = uh->us;
  124. if (!uh->eof) {
  125. if (!us->eof && !us->write_error)
  126. return;
  127. if (ustream_pending_data(us, false))
  128. return;
  129. }
  130. uclient_backend_set_eof(&uh->uc);
  131. if (uh->connection_close)
  132. uclient_http_disconnect(uh);
  133. }
  134. static void uclient_http_reset_state(struct uclient_http *uh)
  135. {
  136. uclient_backend_reset_state(&uh->uc);
  137. uh->read_chunked = -1;
  138. uh->content_length = -1;
  139. uh->eof = false;
  140. uh->connection_close = false;
  141. uh->state = HTTP_STATE_INIT;
  142. if (uh->auth_type == AUTH_TYPE_UNKNOWN && !uh->uc.url->auth)
  143. uh->auth_type = AUTH_TYPE_NONE;
  144. }
  145. static void uclient_http_init_request(struct uclient_http *uh)
  146. {
  147. uclient_http_reset_state(uh);
  148. blob_buf_init(&uh->meta, 0);
  149. }
  150. static enum auth_type
  151. uclient_http_update_auth_type(struct uclient_http *uh)
  152. {
  153. if (!uh->auth_str)
  154. return AUTH_TYPE_NONE;
  155. if (!strncasecmp(uh->auth_str, "basic", 5))
  156. return AUTH_TYPE_BASIC;
  157. if (!strncasecmp(uh->auth_str, "digest", 6))
  158. return AUTH_TYPE_DIGEST;
  159. return AUTH_TYPE_NONE;
  160. }
  161. static void uclient_http_process_headers(struct uclient_http *uh)
  162. {
  163. enum {
  164. HTTP_HDR_TRANSFER_ENCODING,
  165. HTTP_HDR_CONNECTION,
  166. HTTP_HDR_CONTENT_LENGTH,
  167. HTTP_HDR_AUTH,
  168. __HTTP_HDR_MAX,
  169. };
  170. static const struct blobmsg_policy hdr_policy[__HTTP_HDR_MAX] = {
  171. #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
  172. [HTTP_HDR_TRANSFER_ENCODING] = hdr("transfer-encoding"),
  173. [HTTP_HDR_CONNECTION] = hdr("connection"),
  174. [HTTP_HDR_CONTENT_LENGTH] = hdr("content-length"),
  175. [HTTP_HDR_AUTH] = hdr("www-authenticate"),
  176. #undef hdr
  177. };
  178. struct blob_attr *tb[__HTTP_HDR_MAX];
  179. struct blob_attr *cur;
  180. blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
  181. cur = tb[HTTP_HDR_TRANSFER_ENCODING];
  182. if (cur && strstr(blobmsg_data(cur), "chunked"))
  183. uh->read_chunked = 0;
  184. cur = tb[HTTP_HDR_CONNECTION];
  185. if (cur && strstr(blobmsg_data(cur), "close"))
  186. uh->connection_close = true;
  187. cur = tb[HTTP_HDR_CONTENT_LENGTH];
  188. if (cur)
  189. uh->content_length = strtoul(blobmsg_data(cur), NULL, 10);
  190. cur = tb[HTTP_HDR_AUTH];
  191. if (cur) {
  192. free(uh->auth_str);
  193. uh->auth_str = strdup(blobmsg_data(cur));
  194. }
  195. uh->auth_type = uclient_http_update_auth_type(uh);
  196. }
  197. static void
  198. uclient_http_add_auth_basic(struct uclient_http *uh)
  199. {
  200. struct uclient_url *url = uh->uc.url;
  201. int auth_len = strlen(url->auth);
  202. char *auth_buf;
  203. if (auth_len > 512)
  204. return;
  205. auth_buf = alloca(base64_len(auth_len) + 1);
  206. base64_encode(url->auth, auth_len, auth_buf);
  207. ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
  208. }
  209. static char *digest_unquote_sep(char **str)
  210. {
  211. char *cur = *str + 1;
  212. char *start = cur;
  213. char *out;
  214. if (**str != '"')
  215. return NULL;
  216. out = cur;
  217. while (1) {
  218. if (!*cur)
  219. return NULL;
  220. if (*cur == '"') {
  221. cur++;
  222. break;
  223. }
  224. if (*cur == '\\')
  225. cur++;
  226. *(out++) = *(cur++);
  227. }
  228. if (*cur == ',')
  229. cur++;
  230. *out = 0;
  231. *str = cur;
  232. return start;
  233. }
  234. static bool strmatch(char **str, const char *prefix)
  235. {
  236. int len = strlen(prefix);
  237. if (strncmp(*str, prefix, len) != 0 || (*str)[len] != '=')
  238. return false;
  239. *str += len + 1;
  240. return true;
  241. }
  242. static void
  243. get_cnonce(char *dest)
  244. {
  245. uint32_t val = 0;
  246. FILE *f;
  247. f = fopen("/dev/urandom", "r");
  248. if (f) {
  249. fread(&val, sizeof(val), 1, f);
  250. fclose(f);
  251. }
  252. bin_to_hex(dest, &val, sizeof(val));
  253. }
  254. static void add_field(char **buf, int *ofs, int *len, const char *name, const char *val)
  255. {
  256. int available = *len - *ofs;
  257. int required;
  258. const char *next;
  259. char *cur;
  260. if (*len && !*buf)
  261. return;
  262. required = strlen(name) + 4 + strlen(val) * 2;
  263. if (required > available)
  264. *len += required - available + 64;
  265. *buf = realloc(*buf, *len);
  266. if (!*buf)
  267. return;
  268. cur = *buf + *ofs;
  269. cur += sprintf(cur, ", %s=\"", name);
  270. while ((next = strchr(val, '"'))) {
  271. if (next > val) {
  272. memcpy(cur, val, next - val);
  273. cur += next - val;
  274. }
  275. cur += sprintf(cur, "\\\"");
  276. val = next + 1;
  277. }
  278. cur += sprintf(cur, "%s\"", val);
  279. *ofs = cur - *buf;
  280. }
  281. static void
  282. uclient_http_add_auth_digest(struct uclient_http *uh)
  283. {
  284. struct uclient_url *url = uh->uc.url;
  285. const char *realm = NULL, *opaque = NULL;
  286. const char *user, *password;
  287. char *buf, *next;
  288. int len, ofs;
  289. char cnonce_str[9];
  290. char nc_str[9];
  291. char ahash[33];
  292. char hash[33];
  293. struct http_digest_data data = {
  294. .nc = nc_str,
  295. .cnonce = cnonce_str,
  296. .auth_hash = ahash,
  297. };
  298. len = strlen(uh->auth_str) + 1;
  299. if (len > 512)
  300. return;
  301. buf = alloca(len);
  302. strcpy(buf, uh->auth_str);
  303. /* skip auth type */
  304. strsep(&buf, " ");
  305. next = buf;
  306. while (*next) {
  307. const char **dest = NULL;
  308. while (isspace(*next))
  309. next++;
  310. if (strmatch(&next, "realm"))
  311. dest = &realm;
  312. else if (strmatch(&next, "qop"))
  313. dest = &data.qop;
  314. else if (strmatch(&next, "nonce"))
  315. dest = &data.nonce;
  316. else if (strmatch(&next, "opaque"))
  317. dest = &opaque;
  318. else
  319. return;
  320. *dest = digest_unquote_sep(&next);
  321. }
  322. if (!realm || !data.qop || !data.nonce)
  323. return;
  324. sprintf(nc_str, "%08x", uh->nc++);
  325. get_cnonce(cnonce_str);
  326. data.qop = "auth";
  327. data.uri = url->location;
  328. data.method = request_types[uh->req_type];
  329. password = strchr(url->auth, ':');
  330. if (password) {
  331. char *user_buf;
  332. len = password - url->auth;
  333. if (len > 256)
  334. return;
  335. user_buf = alloca(len + 1);
  336. strncpy(user_buf, url->auth, len);
  337. user_buf[len] = 0;
  338. user = user_buf;
  339. password++;
  340. } else {
  341. user = url->auth;
  342. password = "";
  343. }
  344. http_digest_calculate_auth_hash(ahash, user, realm, password);
  345. http_digest_calculate_response(hash, &data);
  346. buf = NULL;
  347. len = 0;
  348. ofs = 0;
  349. add_field(&buf, &ofs, &len, "username", user);
  350. add_field(&buf, &ofs, &len, "realm", realm);
  351. add_field(&buf, &ofs, &len, "nonce", data.nonce);
  352. add_field(&buf, &ofs, &len, "uri", data.uri);
  353. add_field(&buf, &ofs, &len, "cnonce", data.cnonce);
  354. add_field(&buf, &ofs, &len, "response", hash);
  355. if (opaque)
  356. add_field(&buf, &ofs, &len, "opaque", opaque);
  357. ustream_printf(uh->us, "Authorization: Digest nc=%s, qop=%s%s\r\n", data.nc, data.qop, buf);
  358. free(buf);
  359. }
  360. static void
  361. uclient_http_add_auth_header(struct uclient_http *uh)
  362. {
  363. if (!uh->uc.url->auth)
  364. return;
  365. switch (uh->auth_type) {
  366. case AUTH_TYPE_UNKNOWN:
  367. case AUTH_TYPE_NONE:
  368. break;
  369. case AUTH_TYPE_BASIC:
  370. uclient_http_add_auth_basic(uh);
  371. break;
  372. case AUTH_TYPE_DIGEST:
  373. uclient_http_add_auth_digest(uh);
  374. break;
  375. }
  376. }
  377. static void
  378. uclient_http_send_headers(struct uclient_http *uh)
  379. {
  380. struct uclient_url *url = uh->uc.url;
  381. struct blob_attr *cur;
  382. enum request_type req_type = uh->req_type;
  383. int rem;
  384. if (uh->state >= HTTP_STATE_HEADERS_SENT)
  385. return;
  386. if (uh->auth_type == AUTH_TYPE_UNKNOWN)
  387. req_type = REQ_HEAD;
  388. ustream_printf(uh->us,
  389. "%s %s HTTP/1.1\r\n"
  390. "Host: %s\r\n",
  391. request_types[req_type],
  392. url->location, url->host);
  393. blobmsg_for_each_attr(cur, uh->headers.head, rem)
  394. ustream_printf(uh->us, "%s: %s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
  395. if (uh->req_type == REQ_POST)
  396. ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
  397. uclient_http_add_auth_header(uh);
  398. ustream_printf(uh->us, "\r\n");
  399. uh->state = HTTP_STATE_HEADERS_SENT;
  400. }
  401. static void uclient_http_headers_complete(struct uclient_http *uh)
  402. {
  403. enum auth_type auth_type = uh->auth_type;
  404. uh->state = HTTP_STATE_RECV_DATA;
  405. uh->uc.meta = uh->meta.head;
  406. uclient_http_process_headers(uh);
  407. if (auth_type == AUTH_TYPE_UNKNOWN) {
  408. uclient_http_init_request(uh);
  409. uclient_http_send_headers(uh);
  410. uh->state = HTTP_STATE_REQUEST_DONE;
  411. return;
  412. }
  413. if (uh->uc.cb->header_done)
  414. uh->uc.cb->header_done(&uh->uc);
  415. if (uh->req_type == REQ_HEAD) {
  416. uh->eof = true;
  417. uclient_notify_eof(uh);
  418. }
  419. }
  420. static void uclient_parse_http_line(struct uclient_http *uh, char *data)
  421. {
  422. char *name;
  423. char *sep;
  424. if (uh->state == HTTP_STATE_REQUEST_DONE) {
  425. char *code;
  426. /* HTTP/1.1 */
  427. strsep(&data, " ");
  428. code = strsep(&data, " ");
  429. if (!code)
  430. goto error;
  431. uh->uc.status_code = strtoul(code, &sep, 10);
  432. if (sep && *sep)
  433. goto error;
  434. uh->state = HTTP_STATE_RECV_HEADERS;
  435. return;
  436. }
  437. if (!*data) {
  438. uclient_http_headers_complete(uh);
  439. return;
  440. }
  441. sep = strchr(data, ':');
  442. if (!sep)
  443. return;
  444. *(sep++) = 0;
  445. for (name = data; *name; name++)
  446. *name = tolower(*name);
  447. name = data;
  448. while (isspace(*sep))
  449. sep++;
  450. blobmsg_add_string(&uh->meta, name, sep);
  451. return;
  452. error:
  453. uh->uc.status_code = 400;
  454. uh->eof = true;
  455. uclient_notify_eof(uh);
  456. }
  457. static void __uclient_notify_read(struct uclient_http *uh)
  458. {
  459. struct uclient *uc = &uh->uc;
  460. char *data;
  461. int len;
  462. if (uh->state < HTTP_STATE_REQUEST_DONE || uh->state == HTTP_STATE_ERROR)
  463. return;
  464. data = ustream_get_read_buf(uh->us, &len);
  465. if (!data || !len)
  466. return;
  467. if (uh->state < HTTP_STATE_RECV_DATA) {
  468. char *sep;
  469. int cur_len;
  470. do {
  471. sep = strstr(data, "\r\n");
  472. if (!sep)
  473. break;
  474. /* Check for multi-line HTTP headers */
  475. if (sep > data) {
  476. if (!sep[2])
  477. return;
  478. if (isspace(sep[2]) && sep[2] != '\r') {
  479. sep[0] = ' ';
  480. sep[1] = ' ';
  481. continue;
  482. }
  483. }
  484. *sep = 0;
  485. cur_len = sep + 2 - data;
  486. uclient_parse_http_line(uh, data);
  487. ustream_consume(uh->us, cur_len);
  488. len -= cur_len;
  489. data = ustream_get_read_buf(uh->us, &len);
  490. } while (data && uh->state < HTTP_STATE_RECV_DATA);
  491. if (!len)
  492. return;
  493. }
  494. if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
  495. uc->cb->data_read(uc);
  496. }
  497. static void uclient_notify_read(struct ustream *us, int bytes)
  498. {
  499. struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
  500. __uclient_notify_read(uh);
  501. }
  502. static void uclient_notify_state(struct ustream *us)
  503. {
  504. struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
  505. uclient_notify_eof(uh);
  506. }
  507. static int uclient_setup_http(struct uclient_http *uh)
  508. {
  509. struct ustream *us = &uh->ufd.stream;
  510. int ret;
  511. uh->us = us;
  512. us->string_data = true;
  513. us->notify_state = uclient_notify_state;
  514. us->notify_read = uclient_notify_read;
  515. ret = uclient_do_connect(uh, "80");
  516. if (ret)
  517. return ret;
  518. return 0;
  519. }
  520. static void uclient_ssl_notify_read(struct ustream *us, int bytes)
  521. {
  522. struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
  523. __uclient_notify_read(uh);
  524. }
  525. static void uclient_ssl_notify_state(struct ustream *us)
  526. {
  527. struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
  528. uclient_notify_eof(uh);
  529. }
  530. static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
  531. {
  532. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  533. uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
  534. }
  535. static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
  536. {
  537. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  538. if (!uh->ssl_require_validation)
  539. return;
  540. uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
  541. }
  542. static void uclient_ssl_notify_connected(struct ustream_ssl *ssl)
  543. {
  544. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  545. if (!uh->ssl_require_validation)
  546. return;
  547. if (!uh->ussl.valid_cn)
  548. uclient_http_error(uh, UCLIENT_ERROR_SSL_CN_MISMATCH);
  549. }
  550. static int uclient_setup_https(struct uclient_http *uh)
  551. {
  552. struct ustream *us = &uh->ussl.stream;
  553. int ret;
  554. uh->ssl = true;
  555. uh->us = us;
  556. ret = uclient_do_connect(uh, "443");
  557. if (ret)
  558. return ret;
  559. if (!uh->ssl_ctx)
  560. uh->ssl_ctx = ustream_ssl_context_new(false);
  561. us->string_data = true;
  562. us->notify_state = uclient_ssl_notify_state;
  563. us->notify_read = uclient_ssl_notify_read;
  564. uh->ussl.notify_error = uclient_ssl_notify_error;
  565. uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error;
  566. uh->ussl.notify_connected = uclient_ssl_notify_connected;
  567. ustream_ssl_init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false);
  568. ustream_ssl_set_peer_cn(&uh->ussl, uh->uc.url->host);
  569. return 0;
  570. }
  571. static int uclient_http_connect(struct uclient *cl)
  572. {
  573. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  574. int ret;
  575. uclient_http_init_request(uh);
  576. if (uh->us)
  577. return 0;
  578. uh->ssl = cl->url->prefix == PREFIX_HTTPS;
  579. if (uh->ssl)
  580. ret = uclient_setup_https(uh);
  581. else
  582. ret = uclient_setup_http(uh);
  583. if (ret)
  584. uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
  585. return ret;
  586. }
  587. static struct uclient *uclient_http_alloc(void)
  588. {
  589. struct uclient_http *uh;
  590. uh = calloc_a(sizeof(*uh));
  591. blob_buf_init(&uh->headers, 0);
  592. return &uh->uc;
  593. }
  594. static void uclient_http_free_ssl_ctx(struct uclient_http *uh)
  595. {
  596. if (uh->ssl_ctx && !uh->ssl_ctx_ext)
  597. ustream_ssl_context_free(uh->ssl_ctx);
  598. uh->ssl_ctx_ext = false;
  599. }
  600. static void uclient_http_free(struct uclient *cl)
  601. {
  602. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  603. uclient_http_free_ssl_ctx(uh);
  604. uclient_http_free_url_state(cl);
  605. blob_buf_free(&uh->headers);
  606. blob_buf_free(&uh->meta);
  607. free(uh);
  608. }
  609. int
  610. uclient_http_set_request_type(struct uclient *cl, const char *type)
  611. {
  612. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  613. int i;
  614. if (cl->backend != &uclient_backend_http)
  615. return -1;
  616. if (uh->state > HTTP_STATE_INIT)
  617. return -1;
  618. for (i = 0; i < ARRAY_SIZE(request_types); i++) {
  619. if (strcmp(request_types[i], type) != 0)
  620. continue;
  621. uh->req_type = i;
  622. return 0;
  623. }
  624. return -1;
  625. }
  626. int
  627. uclient_http_reset_headers(struct uclient *cl)
  628. {
  629. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  630. blob_buf_init(&uh->headers, 0);
  631. return 0;
  632. }
  633. int
  634. uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
  635. {
  636. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  637. if (cl->backend != &uclient_backend_http)
  638. return -1;
  639. if (uh->state > HTTP_STATE_INIT)
  640. return -1;
  641. blobmsg_add_string(&uh->headers, name, value);
  642. return 0;
  643. }
  644. static int
  645. uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
  646. {
  647. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  648. if (uh->state >= HTTP_STATE_REQUEST_DONE)
  649. return -1;
  650. uclient_http_send_headers(uh);
  651. ustream_printf(uh->us, "%X\r\n", len);
  652. if (len > 0)
  653. ustream_write(uh->us, buf, len, false);
  654. ustream_printf(uh->us, "\r\n");
  655. return len;
  656. }
  657. static int
  658. uclient_http_request_done(struct uclient *cl)
  659. {
  660. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  661. if (uh->state >= HTTP_STATE_REQUEST_DONE)
  662. return -1;
  663. uclient_http_send_headers(uh);
  664. if (uh->req_type == REQ_POST)
  665. ustream_printf(uh->us, "0\r\n\r\n");
  666. uh->state = HTTP_STATE_REQUEST_DONE;
  667. return 0;
  668. }
  669. static int
  670. uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
  671. {
  672. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  673. int read_len = 0;
  674. char *data, *data_end;
  675. if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
  676. return 0;
  677. data = ustream_get_read_buf(uh->us, &read_len);
  678. if (!data || !read_len)
  679. return 0;
  680. data_end = data + read_len;
  681. read_len = 0;
  682. if (uh->read_chunked == 0) {
  683. char *sep;
  684. if (data[0] == '\r' && data[1] == '\n') {
  685. data += 2;
  686. read_len += 2;
  687. }
  688. sep = strstr(data, "\r\n");
  689. if (!sep)
  690. return 0;
  691. *sep = 0;
  692. uh->read_chunked = strtoul(data, NULL, 16);
  693. read_len += sep + 2 - data;
  694. data = sep + 2;
  695. if (!uh->read_chunked)
  696. uh->eof = true;
  697. }
  698. if (len > data_end - data)
  699. len = data_end - data;
  700. if (uh->read_chunked >= 0) {
  701. if (len > uh->read_chunked)
  702. len = uh->read_chunked;
  703. uh->read_chunked -= len;
  704. } else if (uh->content_length >= 0) {
  705. if (len > uh->content_length)
  706. len = uh->content_length;
  707. uh->content_length -= len;
  708. if (!uh->content_length)
  709. uh->eof = true;
  710. }
  711. if (len > 0) {
  712. read_len += len;
  713. memcpy(buf, data, len);
  714. }
  715. if (read_len > 0)
  716. ustream_consume(uh->us, read_len);
  717. uclient_notify_eof(uh);
  718. return len;
  719. }
  720. bool uclient_http_redirect(struct uclient *cl)
  721. {
  722. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  723. struct blobmsg_policy location = {
  724. .name = "location",
  725. .type = BLOBMSG_TYPE_STRING,
  726. };
  727. struct uclient_url *url = cl->url;
  728. struct blob_attr *tb;
  729. if (cl->backend != &uclient_backend_http)
  730. return false;
  731. switch (cl->status_code) {
  732. case 301:
  733. case 302:
  734. case 307:
  735. break;
  736. default:
  737. return false;
  738. }
  739. blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
  740. if (!tb)
  741. return false;
  742. url = uclient_get_url(blobmsg_data(tb), url->auth);
  743. if (!url)
  744. return false;
  745. free(cl->url);
  746. cl->url = url;
  747. uclient_http_connect(cl);
  748. uclient_http_request_done(cl);
  749. return true;
  750. }
  751. int uclient_http_set_ssl_ctx(struct uclient *cl, struct ustream_ssl_ctx *ctx, bool require_validation)
  752. {
  753. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  754. if (cl->backend != &uclient_backend_http)
  755. return -1;
  756. uclient_http_free_url_state(cl);
  757. uclient_http_free_ssl_ctx(uh);
  758. uh->ssl_ctx = ctx;
  759. uh->ssl_ctx_ext = !!ctx;
  760. uh->ssl_require_validation = !!ctx && require_validation;
  761. return 0;
  762. }
  763. const struct uclient_backend uclient_backend_http = {
  764. .prefix = uclient_http_prefix,
  765. .alloc = uclient_http_alloc,
  766. .free = uclient_http_free,
  767. .connect = uclient_http_connect,
  768. .update_url = uclient_http_free_url_state,
  769. .read = uclient_http_read,
  770. .write = uclient_http_send_data,
  771. .request = uclient_http_request_done,
  772. };