2
0

uclient-http.c 20 KB

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