uclient-http.c 19 KB

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