uclient-http.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. }
  400. static void uclient_http_headers_complete(struct uclient_http *uh)
  401. {
  402. enum auth_type auth_type = uh->auth_type;
  403. uh->state = HTTP_STATE_RECV_DATA;
  404. uh->uc.meta = uh->meta.head;
  405. uclient_http_process_headers(uh);
  406. if (auth_type == AUTH_TYPE_UNKNOWN) {
  407. uclient_http_init_request(uh);
  408. uclient_http_send_headers(uh);
  409. uh->state = HTTP_STATE_REQUEST_DONE;
  410. return;
  411. }
  412. if (uh->uc.cb->header_done)
  413. uh->uc.cb->header_done(&uh->uc);
  414. if (uh->req_type == REQ_HEAD) {
  415. uh->eof = true;
  416. uclient_notify_eof(uh);
  417. }
  418. }
  419. static void uclient_parse_http_line(struct uclient_http *uh, char *data)
  420. {
  421. char *name;
  422. char *sep;
  423. if (uh->state == HTTP_STATE_REQUEST_DONE) {
  424. char *code;
  425. /* HTTP/1.1 */
  426. strsep(&data, " ");
  427. code = strsep(&data, " ");
  428. if (!code)
  429. goto error;
  430. uh->uc.status_code = strtoul(code, &sep, 10);
  431. if (sep && *sep)
  432. goto error;
  433. uh->state = HTTP_STATE_RECV_HEADERS;
  434. return;
  435. }
  436. if (!*data) {
  437. uclient_http_headers_complete(uh);
  438. return;
  439. }
  440. sep = strchr(data, ':');
  441. if (!sep)
  442. return;
  443. *(sep++) = 0;
  444. for (name = data; *name; name++)
  445. *name = tolower(*name);
  446. name = data;
  447. while (isspace(*sep))
  448. sep++;
  449. blobmsg_add_string(&uh->meta, name, sep);
  450. return;
  451. error:
  452. uh->uc.status_code = 400;
  453. uh->eof = true;
  454. uclient_notify_eof(uh);
  455. }
  456. static void __uclient_notify_read(struct uclient_http *uh)
  457. {
  458. struct uclient *uc = &uh->uc;
  459. char *data;
  460. int len;
  461. if (uh->state < HTTP_STATE_REQUEST_DONE || uh->state == HTTP_STATE_ERROR)
  462. return;
  463. data = ustream_get_read_buf(uh->us, &len);
  464. if (!data || !len)
  465. return;
  466. if (uh->state < HTTP_STATE_RECV_DATA) {
  467. char *sep;
  468. int cur_len;
  469. do {
  470. sep = strstr(data, "\r\n");
  471. if (!sep)
  472. break;
  473. /* Check for multi-line HTTP headers */
  474. if (sep > data) {
  475. if (!sep[2])
  476. return;
  477. if (isspace(sep[2]) && sep[2] != '\r') {
  478. sep[0] = ' ';
  479. sep[1] = ' ';
  480. continue;
  481. }
  482. }
  483. *sep = 0;
  484. cur_len = sep + 2 - data;
  485. uclient_parse_http_line(uh, data);
  486. ustream_consume(uh->us, cur_len);
  487. len -= cur_len;
  488. data = ustream_get_read_buf(uh->us, &len);
  489. } while (data && uh->state < HTTP_STATE_RECV_DATA);
  490. if (!len)
  491. return;
  492. }
  493. if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read)
  494. uc->cb->data_read(uc);
  495. }
  496. static void uclient_notify_read(struct ustream *us, int bytes)
  497. {
  498. struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
  499. __uclient_notify_read(uh);
  500. }
  501. static void uclient_notify_state(struct ustream *us)
  502. {
  503. struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
  504. uclient_notify_eof(uh);
  505. }
  506. static int uclient_setup_http(struct uclient_http *uh)
  507. {
  508. struct ustream *us = &uh->ufd.stream;
  509. int ret;
  510. uh->us = us;
  511. us->string_data = true;
  512. us->notify_state = uclient_notify_state;
  513. us->notify_read = uclient_notify_read;
  514. ret = uclient_do_connect(uh, "80");
  515. if (ret)
  516. return ret;
  517. return 0;
  518. }
  519. static void uclient_ssl_notify_read(struct ustream *us, int bytes)
  520. {
  521. struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
  522. __uclient_notify_read(uh);
  523. }
  524. static void uclient_ssl_notify_state(struct ustream *us)
  525. {
  526. struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
  527. uclient_notify_eof(uh);
  528. }
  529. static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
  530. {
  531. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  532. uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
  533. }
  534. static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
  535. {
  536. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  537. if (!uh->ssl_require_validation)
  538. return;
  539. uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
  540. }
  541. static void uclient_ssl_notify_connected(struct ustream_ssl *ssl)
  542. {
  543. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  544. if (!uh->ssl_require_validation)
  545. return;
  546. if (!uh->ussl.valid_cn)
  547. uclient_http_error(uh, UCLIENT_ERROR_SSL_CN_MISMATCH);
  548. }
  549. static int uclient_setup_https(struct uclient_http *uh)
  550. {
  551. struct ustream *us = &uh->ussl.stream;
  552. int ret;
  553. uh->ssl = true;
  554. uh->us = us;
  555. ret = uclient_do_connect(uh, "443");
  556. if (ret)
  557. return ret;
  558. if (!uh->ssl_ctx)
  559. uh->ssl_ctx = ustream_ssl_context_new(false);
  560. us->string_data = true;
  561. us->notify_state = uclient_ssl_notify_state;
  562. us->notify_read = uclient_ssl_notify_read;
  563. uh->ussl.notify_error = uclient_ssl_notify_error;
  564. uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error;
  565. uh->ussl.notify_connected = uclient_ssl_notify_connected;
  566. ustream_ssl_init(&uh->ussl, &uh->ufd.stream, uh->ssl_ctx, false);
  567. ustream_ssl_set_peer_cn(&uh->ussl, uh->uc.url->host);
  568. return 0;
  569. }
  570. static int uclient_http_connect(struct uclient *cl)
  571. {
  572. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  573. int ret;
  574. uclient_http_init_request(uh);
  575. if (uh->us)
  576. return 0;
  577. uh->ssl = cl->url->prefix == PREFIX_HTTPS;
  578. if (uh->ssl)
  579. ret = uclient_setup_https(uh);
  580. else
  581. ret = uclient_setup_http(uh);
  582. if (ret)
  583. uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
  584. return ret;
  585. }
  586. static struct uclient *uclient_http_alloc(void)
  587. {
  588. struct uclient_http *uh;
  589. uh = calloc_a(sizeof(*uh));
  590. blob_buf_init(&uh->headers, 0);
  591. return &uh->uc;
  592. }
  593. static void uclient_http_free_ssl_ctx(struct uclient_http *uh)
  594. {
  595. if (uh->ssl_ctx && !uh->ssl_ctx_ext)
  596. ustream_ssl_context_free(uh->ssl_ctx);
  597. uh->ssl_ctx_ext = false;
  598. }
  599. static void uclient_http_free(struct uclient *cl)
  600. {
  601. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  602. uclient_http_free_ssl_ctx(uh);
  603. uclient_http_free_url_state(cl);
  604. blob_buf_free(&uh->headers);
  605. blob_buf_free(&uh->meta);
  606. free(uh);
  607. }
  608. int
  609. uclient_http_set_request_type(struct uclient *cl, const char *type)
  610. {
  611. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  612. int i;
  613. if (cl->backend != &uclient_backend_http)
  614. return -1;
  615. if (uh->state > HTTP_STATE_INIT)
  616. return -1;
  617. for (i = 0; i < ARRAY_SIZE(request_types); i++) {
  618. if (strcmp(request_types[i], type) != 0)
  619. continue;
  620. uh->req_type = i;
  621. return 0;
  622. }
  623. return -1;
  624. }
  625. int
  626. uclient_http_reset_headers(struct uclient *cl, const char *name, const char *value)
  627. {
  628. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  629. blob_buf_init(&uh->headers, 0);
  630. return 0;
  631. }
  632. int
  633. uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
  634. {
  635. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  636. if (cl->backend != &uclient_backend_http)
  637. return -1;
  638. if (uh->state > HTTP_STATE_INIT)
  639. return -1;
  640. blobmsg_add_string(&uh->headers, name, value);
  641. return 0;
  642. }
  643. static int
  644. uclient_http_send_data(struct uclient *cl, char *buf, unsigned int len)
  645. {
  646. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  647. if (uh->state >= HTTP_STATE_REQUEST_DONE)
  648. return -1;
  649. uclient_http_send_headers(uh);
  650. ustream_printf(uh->us, "%X\r\n", len);
  651. if (len > 0)
  652. ustream_write(uh->us, buf, len, false);
  653. ustream_printf(uh->us, "\r\n");
  654. return len;
  655. }
  656. static int
  657. uclient_http_request_done(struct uclient *cl)
  658. {
  659. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  660. if (uh->state >= HTTP_STATE_REQUEST_DONE)
  661. return -1;
  662. uclient_http_send_headers(uh);
  663. uh->state = HTTP_STATE_REQUEST_DONE;
  664. return 0;
  665. }
  666. static int
  667. uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
  668. {
  669. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  670. int read_len = 0;
  671. char *data, *data_end;
  672. if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
  673. return 0;
  674. data = ustream_get_read_buf(uh->us, &read_len);
  675. if (!data || !read_len)
  676. return 0;
  677. data_end = data + read_len;
  678. read_len = 0;
  679. if (uh->read_chunked == 0) {
  680. char *sep;
  681. if (data[0] == '\r' && data[1] == '\n') {
  682. data += 2;
  683. read_len += 2;
  684. }
  685. sep = strstr(data, "\r\n");
  686. if (!sep)
  687. return 0;
  688. *sep = 0;
  689. uh->read_chunked = strtoul(data, NULL, 16);
  690. read_len += sep + 2 - data;
  691. data = sep + 2;
  692. if (!uh->read_chunked)
  693. uh->eof = true;
  694. }
  695. if (len > data_end - data)
  696. len = data_end - data;
  697. if (uh->read_chunked >= 0) {
  698. if (len > uh->read_chunked)
  699. len = uh->read_chunked;
  700. uh->read_chunked -= len;
  701. } else if (uh->content_length >= 0) {
  702. if (len > uh->content_length)
  703. len = uh->content_length;
  704. uh->content_length -= len;
  705. if (!uh->content_length)
  706. uh->eof = true;
  707. }
  708. if (len > 0) {
  709. read_len += len;
  710. memcpy(buf, data, len);
  711. }
  712. if (read_len > 0)
  713. ustream_consume(uh->us, read_len);
  714. uclient_notify_eof(uh);
  715. return len;
  716. }
  717. bool uclient_http_redirect(struct uclient *cl)
  718. {
  719. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  720. struct blobmsg_policy location = {
  721. .name = "location",
  722. .type = BLOBMSG_TYPE_STRING,
  723. };
  724. struct uclient_url *url = cl->url;
  725. struct blob_attr *tb;
  726. if (cl->backend != &uclient_backend_http)
  727. return false;
  728. switch (cl->status_code) {
  729. case 301:
  730. case 302:
  731. case 307:
  732. break;
  733. default:
  734. return false;
  735. }
  736. blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
  737. if (!tb)
  738. return false;
  739. url = uclient_get_url(blobmsg_data(tb), url->auth);
  740. if (!url)
  741. return false;
  742. free(cl->url);
  743. cl->url = url;
  744. uclient_http_connect(cl);
  745. uclient_http_request_done(cl);
  746. return true;
  747. }
  748. int uclient_http_set_ssl_ctx(struct uclient *cl, struct ustream_ssl_ctx *ctx, bool require_validation)
  749. {
  750. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  751. if (cl->backend != &uclient_backend_http)
  752. return -1;
  753. uclient_http_free_url_state(cl);
  754. uclient_http_free_ssl_ctx(uh);
  755. uh->ssl_ctx = ctx;
  756. uh->ssl_ctx_ext = !!ctx;
  757. uh->ssl_require_validation = !!ctx && require_validation;
  758. return 0;
  759. }
  760. const struct uclient_backend uclient_backend_http = {
  761. .prefix = uclient_http_prefix,
  762. .alloc = uclient_http_alloc,
  763. .free = uclient_http_free,
  764. .connect = uclient_http_connect,
  765. .update_url = uclient_http_free_url_state,
  766. .read = uclient_http_read,
  767. .write = uclient_http_send_data,
  768. .request = uclient_http_request_done,
  769. };