uclient-http.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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 <sys/socket.h>
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <unistd.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <fcntl.h>
  25. #include <libubox/ustream.h>
  26. #include <libubox/ustream-ssl.h>
  27. #include <libubox/usock.h>
  28. #include <libubox/blobmsg.h>
  29. #include "uclient.h"
  30. #include "uclient-utils.h"
  31. #include "uclient-backend.h"
  32. enum auth_type {
  33. AUTH_TYPE_UNKNOWN,
  34. AUTH_TYPE_NONE,
  35. AUTH_TYPE_BASIC,
  36. AUTH_TYPE_DIGEST,
  37. };
  38. enum request_type {
  39. REQ_GET,
  40. REQ_HEAD,
  41. REQ_POST,
  42. REQ_PUT,
  43. REQ_DELETE,
  44. __REQ_MAX
  45. };
  46. enum http_state {
  47. HTTP_STATE_INIT,
  48. HTTP_STATE_HEADERS_SENT,
  49. HTTP_STATE_REQUEST_DONE,
  50. HTTP_STATE_RECV_HEADERS,
  51. HTTP_STATE_RECV_DATA,
  52. HTTP_STATE_ERROR,
  53. };
  54. static const char * const request_types[__REQ_MAX] = {
  55. [REQ_GET] = "GET",
  56. [REQ_HEAD] = "HEAD",
  57. [REQ_POST] = "POST",
  58. [REQ_PUT] = "PUT",
  59. [REQ_DELETE] = "DELETE",
  60. };
  61. struct uclient_http {
  62. struct uclient uc;
  63. const struct ustream_ssl_ops *ssl_ops;
  64. struct ustream_ssl_ctx *ssl_ctx;
  65. struct ustream *us;
  66. union {
  67. struct ustream_fd ufd;
  68. struct ustream_ssl ussl;
  69. };
  70. struct uloop_timeout disconnect_t;
  71. unsigned int seq;
  72. int fd;
  73. bool ssl_require_validation;
  74. bool ssl;
  75. bool eof;
  76. bool connection_close;
  77. bool disconnect;
  78. enum request_type req_type;
  79. enum http_state state;
  80. enum auth_type auth_type;
  81. char *auth_str;
  82. long read_chunked;
  83. long content_length;
  84. int usock_flags;
  85. uint32_t nc;
  86. struct blob_buf headers;
  87. struct blob_buf meta;
  88. };
  89. enum {
  90. PREFIX_HTTP,
  91. PREFIX_HTTPS,
  92. __PREFIX_MAX,
  93. };
  94. static const char * const uclient_http_prefix[] = {
  95. [PREFIX_HTTP] = "http://",
  96. [PREFIX_HTTPS] = "https://",
  97. [__PREFIX_MAX] = NULL
  98. };
  99. static int uclient_http_connect(struct uclient *cl);
  100. static int uclient_do_connect(struct uclient_http *uh, const char *port)
  101. {
  102. socklen_t sl;
  103. int fd;
  104. if (uh->uc.url->port)
  105. port = uh->uc.url->port;
  106. memset(&uh->uc.remote_addr, 0, sizeof(uh->uc.remote_addr));
  107. fd = usock_inet_timeout(USOCK_TCP | USOCK_NONBLOCK | uh->usock_flags,
  108. uh->uc.url->host, port, &uh->uc.remote_addr,
  109. uh->uc.timeout_msecs);
  110. if (fd < 0)
  111. return -1;
  112. fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
  113. uh->fd = fd;
  114. sl = sizeof(uh->uc.local_addr);
  115. memset(&uh->uc.local_addr, 0, sl);
  116. getsockname(fd, &uh->uc.local_addr.sa, &sl);
  117. return 0;
  118. }
  119. static void uclient_http_disconnect(struct uclient_http *uh)
  120. {
  121. uloop_timeout_cancel(&uh->disconnect_t);
  122. if (!uh->us)
  123. return;
  124. if (uh->ssl)
  125. ustream_free(&uh->ussl.stream);
  126. else
  127. ustream_free(&uh->ufd.stream);
  128. if(uh->fd >= 0)
  129. close(uh->fd);
  130. uh->us = NULL;
  131. }
  132. static void uclient_http_free_url_state(struct uclient *cl)
  133. {
  134. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  135. uh->auth_type = AUTH_TYPE_UNKNOWN;
  136. free(uh->auth_str);
  137. uh->auth_str = NULL;
  138. uclient_http_disconnect(uh);
  139. }
  140. static void uclient_http_error(struct uclient_http *uh, int code)
  141. {
  142. uh->state = HTTP_STATE_ERROR;
  143. uh->us->eof = true;
  144. ustream_state_change(uh->us);
  145. uclient_backend_set_error(&uh->uc, code);
  146. }
  147. static void uclient_http_request_disconnect(struct uclient *cl)
  148. {
  149. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  150. if (!uh->us)
  151. return;
  152. uh->eof = true;
  153. uh->disconnect = true;
  154. uloop_timeout_set(&uh->disconnect_t, 1);
  155. }
  156. static void uclient_notify_eof(struct uclient_http *uh)
  157. {
  158. struct ustream *us = uh->us;
  159. if (uh->disconnect)
  160. return;
  161. if (!uh->eof) {
  162. if (!us->eof && !us->write_error)
  163. return;
  164. if (ustream_pending_data(us, false))
  165. return;
  166. }
  167. if ((uh->content_length < 0 && uh->read_chunked >= 0) ||
  168. uh->content_length == 0)
  169. uh->uc.data_eof = true;
  170. uclient_backend_set_eof(&uh->uc);
  171. if (uh->connection_close)
  172. uclient_http_request_disconnect(&uh->uc);
  173. }
  174. static void uclient_http_reset_state(struct uclient_http *uh)
  175. {
  176. uh->seq++;
  177. uclient_backend_reset_state(&uh->uc);
  178. uh->read_chunked = -1;
  179. uh->content_length = -1;
  180. uh->eof = false;
  181. uh->disconnect = false;
  182. uh->connection_close = false;
  183. uh->state = HTTP_STATE_INIT;
  184. if (uh->auth_type == AUTH_TYPE_UNKNOWN && !uh->uc.url->auth)
  185. uh->auth_type = AUTH_TYPE_NONE;
  186. }
  187. static void uclient_http_init_request(struct uclient_http *uh)
  188. {
  189. uh->seq++;
  190. uclient_http_reset_state(uh);
  191. blob_buf_init(&uh->meta, 0);
  192. }
  193. static enum auth_type
  194. uclient_http_update_auth_type(struct uclient_http *uh)
  195. {
  196. if (!uh->auth_str)
  197. return AUTH_TYPE_NONE;
  198. if (!strncasecmp(uh->auth_str, "basic", 5))
  199. return AUTH_TYPE_BASIC;
  200. if (!strncasecmp(uh->auth_str, "digest", 6))
  201. return AUTH_TYPE_DIGEST;
  202. return AUTH_TYPE_NONE;
  203. }
  204. static void uclient_http_process_headers(struct uclient_http *uh)
  205. {
  206. enum {
  207. HTTP_HDR_TRANSFER_ENCODING,
  208. HTTP_HDR_CONNECTION,
  209. HTTP_HDR_CONTENT_LENGTH,
  210. HTTP_HDR_AUTH,
  211. __HTTP_HDR_MAX,
  212. };
  213. static const struct blobmsg_policy hdr_policy[__HTTP_HDR_MAX] = {
  214. #define hdr(_name) { .name = _name, .type = BLOBMSG_TYPE_STRING }
  215. [HTTP_HDR_TRANSFER_ENCODING] = hdr("transfer-encoding"),
  216. [HTTP_HDR_CONNECTION] = hdr("connection"),
  217. [HTTP_HDR_CONTENT_LENGTH] = hdr("content-length"),
  218. [HTTP_HDR_AUTH] = hdr("www-authenticate"),
  219. #undef hdr
  220. };
  221. struct blob_attr *tb[__HTTP_HDR_MAX];
  222. struct blob_attr *cur;
  223. blobmsg_parse(hdr_policy, __HTTP_HDR_MAX, tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
  224. cur = tb[HTTP_HDR_TRANSFER_ENCODING];
  225. if (cur && strstr(blobmsg_data(cur), "chunked"))
  226. uh->read_chunked = 0;
  227. cur = tb[HTTP_HDR_CONNECTION];
  228. if (cur && strstr(blobmsg_data(cur), "close"))
  229. uh->connection_close = true;
  230. cur = tb[HTTP_HDR_CONTENT_LENGTH];
  231. if (cur)
  232. uh->content_length = strtoul(blobmsg_data(cur), NULL, 10);
  233. cur = tb[HTTP_HDR_AUTH];
  234. if (cur) {
  235. free(uh->auth_str);
  236. uh->auth_str = strdup(blobmsg_data(cur));
  237. }
  238. uh->auth_type = uclient_http_update_auth_type(uh);
  239. }
  240. static bool uclient_request_supports_body(enum request_type req_type)
  241. {
  242. switch (req_type) {
  243. case REQ_POST:
  244. case REQ_PUT:
  245. case REQ_DELETE:
  246. return true;
  247. default:
  248. return false;
  249. }
  250. }
  251. static int
  252. uclient_http_add_auth_basic(struct uclient_http *uh)
  253. {
  254. struct uclient_url *url = uh->uc.url;
  255. int auth_len = strlen(url->auth);
  256. char *auth_buf;
  257. if (auth_len > 512)
  258. return -EINVAL;
  259. auth_buf = alloca(base64_len(auth_len) + 1);
  260. if (!auth_buf)
  261. return -ENOMEM;
  262. base64_encode(url->auth, auth_len, auth_buf);
  263. ustream_printf(uh->us, "Authorization: Basic %s\r\n", auth_buf);
  264. return 0;
  265. }
  266. static char *digest_unquote_sep(char **str)
  267. {
  268. char *cur = *str + 1;
  269. char *start = cur;
  270. char *out;
  271. if (**str != '"')
  272. return NULL;
  273. out = cur;
  274. while (1) {
  275. if (!*cur)
  276. return NULL;
  277. if (*cur == '"') {
  278. cur++;
  279. break;
  280. }
  281. if (*cur == '\\')
  282. cur++;
  283. *(out++) = *(cur++);
  284. }
  285. if (*cur == ',')
  286. cur++;
  287. *out = 0;
  288. *str = cur;
  289. return start;
  290. }
  291. static char *digest_sep(char **str)
  292. {
  293. char *cur, *next;
  294. cur = *str;
  295. next = strchr(*str, ',');
  296. if (next) {
  297. *str = next + 1;
  298. *next = 0;
  299. } else {
  300. *str += strlen(*str);
  301. }
  302. return cur;
  303. }
  304. static bool strmatch(char **str, const char *prefix)
  305. {
  306. int len = strlen(prefix);
  307. if (strncmp(*str, prefix, len) != 0 || (*str)[len] != '=')
  308. return false;
  309. *str += len + 1;
  310. return true;
  311. }
  312. static void
  313. get_cnonce(char *dest)
  314. {
  315. uint32_t val = 0;
  316. FILE *f;
  317. size_t n;
  318. f = fopen("/dev/urandom", "r");
  319. if (f) {
  320. n = fread(&val, sizeof(val), 1, f);
  321. fclose(f);
  322. if (n != 1)
  323. return;
  324. }
  325. bin_to_hex(dest, &val, sizeof(val));
  326. }
  327. static void add_field(char **buf, int *ofs, int *len, const char *name, const char *val)
  328. {
  329. int available = *len - *ofs;
  330. int required;
  331. const char *next;
  332. char *cur;
  333. if (*len && !*buf)
  334. return;
  335. required = strlen(name) + 4 + strlen(val) * 2;
  336. if (required > available)
  337. *len += required - available + 64;
  338. *buf = realloc(*buf, *len);
  339. if (!*buf)
  340. return;
  341. cur = *buf + *ofs;
  342. cur += sprintf(cur, ", %s=\"", name);
  343. while ((next = strchr(val, '"'))) {
  344. if (next > val) {
  345. memcpy(cur, val, next - val);
  346. cur += next - val;
  347. }
  348. cur += sprintf(cur, "\\\"");
  349. val = next + 1;
  350. }
  351. cur += sprintf(cur, "%s\"", val);
  352. *ofs = cur - *buf;
  353. }
  354. static int
  355. uclient_http_add_auth_digest(struct uclient_http *uh)
  356. {
  357. struct uclient_url *url = uh->uc.url;
  358. const char *realm = NULL, *opaque = NULL;
  359. const char *user, *password;
  360. char *buf, *next;
  361. int len, ofs;
  362. int err = 0;
  363. char cnonce_str[9];
  364. char nc_str[9];
  365. char ahash[33];
  366. char hash[33];
  367. struct http_digest_data data = {
  368. .nc = nc_str,
  369. .cnonce = cnonce_str,
  370. .auth_hash = ahash,
  371. };
  372. len = strlen(uh->auth_str) + 1;
  373. if (len > 512) {
  374. err = -EINVAL;
  375. goto fail;
  376. }
  377. buf = alloca(len);
  378. if (!buf) {
  379. err = -ENOMEM;
  380. goto fail;
  381. }
  382. strcpy(buf, uh->auth_str);
  383. /* skip auth type */
  384. strsep(&buf, " ");
  385. next = buf;
  386. while (*next) {
  387. const char **dest = NULL;
  388. const char *tmp;
  389. while (*next && isspace(*next))
  390. next++;
  391. if (strmatch(&next, "realm"))
  392. dest = &realm;
  393. else if (strmatch(&next, "qop"))
  394. dest = &data.qop;
  395. else if (strmatch(&next, "nonce"))
  396. dest = &data.nonce;
  397. else if (strmatch(&next, "opaque"))
  398. dest = &opaque;
  399. else if (strmatch(&next, "stale") ||
  400. strmatch(&next, "algorithm") ||
  401. strmatch(&next, "auth-param")) {
  402. digest_sep(&next);
  403. continue;
  404. } else if (strmatch(&next, "domain") ||
  405. strmatch(&next, "qop-options"))
  406. dest = &tmp;
  407. else {
  408. digest_sep(&next);
  409. continue;
  410. }
  411. *dest = digest_unquote_sep(&next);
  412. }
  413. if (!realm || !data.qop || !data.nonce) {
  414. err = -EINVAL;
  415. goto fail;
  416. }
  417. sprintf(nc_str, "%08x", uh->nc++);
  418. get_cnonce(cnonce_str);
  419. data.qop = "auth";
  420. data.uri = url->location;
  421. data.method = request_types[uh->req_type];
  422. password = strchr(url->auth, ':');
  423. if (password) {
  424. char *user_buf;
  425. len = password - url->auth;
  426. if (len > 256) {
  427. err = -EINVAL;
  428. goto fail;
  429. }
  430. user_buf = alloca(len + 1);
  431. if (!user_buf) {
  432. err = -ENOMEM;
  433. goto fail;
  434. }
  435. strncpy(user_buf, url->auth, len);
  436. user_buf[len] = 0;
  437. user = user_buf;
  438. password++;
  439. } else {
  440. user = url->auth;
  441. password = "";
  442. }
  443. http_digest_calculate_auth_hash(ahash, user, realm, password);
  444. http_digest_calculate_response(hash, &data);
  445. buf = NULL;
  446. len = 0;
  447. ofs = 0;
  448. add_field(&buf, &ofs, &len, "username", user);
  449. add_field(&buf, &ofs, &len, "realm", realm);
  450. add_field(&buf, &ofs, &len, "nonce", data.nonce);
  451. add_field(&buf, &ofs, &len, "uri", data.uri);
  452. add_field(&buf, &ofs, &len, "cnonce", data.cnonce);
  453. add_field(&buf, &ofs, &len, "response", hash);
  454. if (opaque)
  455. add_field(&buf, &ofs, &len, "opaque", opaque);
  456. ustream_printf(uh->us, "Authorization: Digest nc=%s, qop=%s%s\r\n", data.nc, data.qop, buf);
  457. free(buf);
  458. return 0;
  459. fail:
  460. return err;
  461. }
  462. static int
  463. uclient_http_add_auth_header(struct uclient_http *uh)
  464. {
  465. if (!uh->uc.url->auth)
  466. return 0;
  467. switch (uh->auth_type) {
  468. case AUTH_TYPE_UNKNOWN:
  469. case AUTH_TYPE_NONE:
  470. break;
  471. case AUTH_TYPE_BASIC:
  472. return uclient_http_add_auth_basic(uh);
  473. case AUTH_TYPE_DIGEST:
  474. return uclient_http_add_auth_digest(uh);
  475. }
  476. return 0;
  477. }
  478. static int
  479. uclient_http_send_headers(struct uclient_http *uh)
  480. {
  481. struct uclient_url *url = uh->uc.url;
  482. struct blob_attr *cur;
  483. enum request_type req_type = uh->req_type;
  484. bool literal_ipv6;
  485. int err;
  486. size_t rem;
  487. if (uh->state >= HTTP_STATE_HEADERS_SENT)
  488. return 0;
  489. if (uh->uc.proxy_url)
  490. url = uh->uc.proxy_url;
  491. literal_ipv6 = strchr(url->host, ':');
  492. ustream_printf(uh->us,
  493. "%s %s HTTP/1.1\r\n"
  494. "Host: %s%s%s%s%s\r\n",
  495. request_types[req_type], url->location,
  496. literal_ipv6 ? "[" : "",
  497. url->host,
  498. literal_ipv6 ? "]" : "",
  499. url->port ? ":" : "",
  500. url->port ? url->port : "");
  501. blobmsg_for_each_attr(cur, uh->headers.head, rem)
  502. ustream_printf(uh->us, "%s: %s\r\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
  503. if (uclient_request_supports_body(uh->req_type))
  504. ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
  505. err = uclient_http_add_auth_header(uh);
  506. if (err)
  507. return err;
  508. ustream_printf(uh->us, "\r\n");
  509. uh->state = HTTP_STATE_HEADERS_SENT;
  510. return 0;
  511. }
  512. static void uclient_http_headers_complete(struct uclient_http *uh)
  513. {
  514. enum auth_type auth_type = uh->auth_type;
  515. int seq = uh->uc.seq;
  516. uh->state = HTTP_STATE_RECV_DATA;
  517. uh->uc.meta = uh->meta.head;
  518. uclient_http_process_headers(uh);
  519. if (auth_type == AUTH_TYPE_UNKNOWN && uh->uc.status_code == 401 &&
  520. (uh->req_type == REQ_HEAD || uh->req_type == REQ_GET)) {
  521. uclient_http_connect(&uh->uc);
  522. uclient_http_send_headers(uh);
  523. uh->state = HTTP_STATE_REQUEST_DONE;
  524. return;
  525. }
  526. if (uh->uc.cb->header_done)
  527. uh->uc.cb->header_done(&uh->uc);
  528. if (uh->eof || seq != uh->uc.seq)
  529. return;
  530. if (uh->req_type == REQ_HEAD || uh->uc.status_code == 204 ||
  531. uh->content_length == 0) {
  532. uh->eof = true;
  533. uclient_notify_eof(uh);
  534. }
  535. }
  536. static void uclient_parse_http_line(struct uclient_http *uh, char *data)
  537. {
  538. char *name;
  539. char *sep;
  540. if (uh->state == HTTP_STATE_REQUEST_DONE) {
  541. char *code;
  542. if (!strlen(data))
  543. return;
  544. /* HTTP/1.1 */
  545. strsep(&data, " ");
  546. code = strsep(&data, " ");
  547. if (!code)
  548. goto error;
  549. uh->uc.status_code = strtoul(code, &sep, 10);
  550. if (sep && *sep)
  551. goto error;
  552. uh->state = HTTP_STATE_RECV_HEADERS;
  553. return;
  554. }
  555. if (!*data) {
  556. uclient_http_headers_complete(uh);
  557. return;
  558. }
  559. sep = strchr(data, ':');
  560. if (!sep)
  561. return;
  562. *(sep++) = 0;
  563. for (name = data; *name; name++)
  564. *name = tolower(*name);
  565. name = data;
  566. while (isspace(*sep))
  567. sep++;
  568. blobmsg_add_string(&uh->meta, name, sep);
  569. return;
  570. error:
  571. uh->uc.status_code = 400;
  572. uh->eof = true;
  573. uclient_notify_eof(uh);
  574. }
  575. static void __uclient_notify_read(struct uclient_http *uh)
  576. {
  577. struct uclient *uc = &uh->uc;
  578. unsigned int seq = uh->seq;
  579. char *data;
  580. int len;
  581. if (uh->state < HTTP_STATE_REQUEST_DONE || uh->state == HTTP_STATE_ERROR)
  582. return;
  583. data = ustream_get_read_buf(uh->us, &len);
  584. if (!data || !len)
  585. return;
  586. if (uh->state < HTTP_STATE_RECV_DATA) {
  587. char *sep, *next;
  588. int cur_len;
  589. do {
  590. sep = strchr(data, '\n');
  591. if (!sep)
  592. break;
  593. next = sep + 1;
  594. if (sep > data && sep[-1] == '\r')
  595. sep--;
  596. /* Check for multi-line HTTP headers */
  597. if (sep > data) {
  598. if (!*next)
  599. return;
  600. if (isspace(*next) && *next != '\r' && *next != '\n') {
  601. sep[0] = ' ';
  602. if (sep + 1 < next)
  603. sep[1] = ' ';
  604. continue;
  605. }
  606. }
  607. *sep = 0;
  608. cur_len = next - data;
  609. uclient_parse_http_line(uh, data);
  610. if (seq != uh->seq)
  611. return;
  612. ustream_consume(uh->us, cur_len);
  613. len -= cur_len;
  614. if (uh->eof)
  615. return;
  616. data = ustream_get_read_buf(uh->us, &len);
  617. } while (data && uh->state < HTTP_STATE_RECV_DATA);
  618. if (!len)
  619. return;
  620. }
  621. if (uh->eof)
  622. return;
  623. if (uh->state == HTTP_STATE_RECV_DATA) {
  624. /* Now it's uclient user turn to read some data */
  625. uloop_timeout_cancel(&uc->connection_timeout);
  626. uclient_backend_read_notify(uc);
  627. }
  628. }
  629. static void __uclient_notify_write(struct uclient_http *uh)
  630. {
  631. struct uclient *uc = &uh->uc;
  632. if (uc->cb->data_sent)
  633. uc->cb->data_sent(uc);
  634. }
  635. static void uclient_notify_read(struct ustream *us, int bytes)
  636. {
  637. struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
  638. __uclient_notify_read(uh);
  639. }
  640. static void uclient_notify_write(struct ustream *us, int bytes)
  641. {
  642. struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
  643. __uclient_notify_write(uh);
  644. }
  645. static void uclient_notify_state(struct ustream *us)
  646. {
  647. struct uclient_http *uh = container_of(us, struct uclient_http, ufd.stream);
  648. if (uh->ufd.stream.write_error) {
  649. uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
  650. return;
  651. }
  652. uclient_notify_eof(uh);
  653. }
  654. static int uclient_setup_http(struct uclient_http *uh)
  655. {
  656. struct ustream *us = &uh->ufd.stream;
  657. int ret;
  658. memset(&uh->ufd, 0, sizeof(uh->ufd));
  659. uh->us = us;
  660. uh->ssl = false;
  661. us->string_data = true;
  662. us->notify_state = uclient_notify_state;
  663. us->notify_read = uclient_notify_read;
  664. us->notify_write = uclient_notify_write;
  665. ret = uclient_do_connect(uh, "80");
  666. if (ret)
  667. return UCLIENT_ERROR_CONNECT;
  668. ustream_fd_init(&uh->ufd, uh->fd);
  669. return 0;
  670. }
  671. static void uclient_ssl_notify_read(struct ustream *us, int bytes)
  672. {
  673. struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
  674. __uclient_notify_read(uh);
  675. }
  676. static void uclient_ssl_notify_write(struct ustream *us, int bytes)
  677. {
  678. struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
  679. __uclient_notify_write(uh);
  680. }
  681. static void uclient_ssl_notify_state(struct ustream *us)
  682. {
  683. struct uclient_http *uh = container_of(us, struct uclient_http, ussl.stream);
  684. uclient_notify_eof(uh);
  685. }
  686. static void uclient_ssl_notify_error(struct ustream_ssl *ssl, int error, const char *str)
  687. {
  688. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  689. struct uclient *uc = &uh->uc;
  690. if (uc->cb->log_msg)
  691. uc->cb->log_msg(uc, UCLIENT_LOG_SSL_ERROR, str);
  692. uclient_http_error(uh, UCLIENT_ERROR_CONNECT);
  693. }
  694. static void uclient_ssl_notify_verify_error(struct ustream_ssl *ssl, int error, const char *str)
  695. {
  696. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  697. struct uclient *uc = &uh->uc;
  698. if (!uh->ssl_require_validation)
  699. return;
  700. if (uc->cb->log_msg)
  701. uc->cb->log_msg(uc, UCLIENT_LOG_SSL_VERIFY_ERROR, str);
  702. uclient_http_error(uh, UCLIENT_ERROR_SSL_INVALID_CERT);
  703. }
  704. static void uclient_ssl_notify_connected(struct ustream_ssl *ssl)
  705. {
  706. struct uclient_http *uh = container_of(ssl, struct uclient_http, ussl);
  707. if (!uh->ssl_require_validation)
  708. return;
  709. if (!uh->ussl.valid_cn)
  710. uclient_http_error(uh, UCLIENT_ERROR_SSL_CN_MISMATCH);
  711. }
  712. static int uclient_setup_https(struct uclient_http *uh)
  713. {
  714. struct ustream *us = &uh->ussl.stream;
  715. int ret;
  716. memset(&uh->ussl, 0, sizeof(uh->ussl));
  717. uh->ssl = true;
  718. uh->us = us;
  719. if (!uh->ssl_ctx)
  720. return UCLIENT_ERROR_MISSING_SSL_CONTEXT;
  721. ret = uclient_do_connect(uh, "443");
  722. if (ret)
  723. return UCLIENT_ERROR_CONNECT;
  724. us->string_data = true;
  725. us->notify_state = uclient_ssl_notify_state;
  726. us->notify_read = uclient_ssl_notify_read;
  727. us->notify_write = uclient_ssl_notify_write;
  728. uh->ussl.notify_error = uclient_ssl_notify_error;
  729. uh->ussl.notify_verify_error = uclient_ssl_notify_verify_error;
  730. uh->ussl.notify_connected = uclient_ssl_notify_connected;
  731. uh->ussl.server_name = uh->uc.url->host;
  732. uh->ssl_ops->init_fd(&uh->ussl, uh->fd, uh->ssl_ctx, false);
  733. uh->ssl_ops->set_peer_cn(&uh->ussl, uh->uc.url->host);
  734. return 0;
  735. }
  736. static int uclient_http_connect(struct uclient *cl)
  737. {
  738. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  739. int ret;
  740. if (!cl->eof || uh->disconnect || uh->connection_close)
  741. uclient_http_disconnect(uh);
  742. uclient_http_init_request(uh);
  743. if (uh->us)
  744. return 0;
  745. uh->ssl = cl->url->prefix == PREFIX_HTTPS;
  746. if (uh->ssl)
  747. ret = uclient_setup_https(uh);
  748. else
  749. ret = uclient_setup_http(uh);
  750. return ret;
  751. }
  752. static void uclient_http_disconnect_cb(struct uloop_timeout *timeout)
  753. {
  754. struct uclient_http *uh = container_of(timeout, struct uclient_http, disconnect_t);
  755. uclient_http_disconnect(uh);
  756. }
  757. static struct uclient *uclient_http_alloc(void)
  758. {
  759. struct uclient_http *uh;
  760. uh = calloc_a(sizeof(*uh));
  761. if (!uh)
  762. return NULL;
  763. uh->disconnect_t.cb = uclient_http_disconnect_cb;
  764. blob_buf_init(&uh->headers, 0);
  765. return &uh->uc;
  766. }
  767. static void uclient_http_free_ssl_ctx(struct uclient_http *uh)
  768. {
  769. uh->ssl_ops = NULL;
  770. uh->ssl_ctx = NULL;
  771. }
  772. static void uclient_http_free(struct uclient *cl)
  773. {
  774. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  775. uclient_http_free_url_state(cl);
  776. uclient_http_free_ssl_ctx(uh);
  777. blob_buf_free(&uh->headers);
  778. blob_buf_free(&uh->meta);
  779. free(uh);
  780. }
  781. int
  782. uclient_http_set_request_type(struct uclient *cl, const char *type)
  783. {
  784. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  785. unsigned int i;
  786. if (cl->backend != &uclient_backend_http)
  787. return -1;
  788. if (uh->state > HTTP_STATE_INIT)
  789. return -1;
  790. for (i = 0; i < ARRAY_SIZE(request_types); i++) {
  791. if (strcmp(request_types[i], type) != 0)
  792. continue;
  793. uh->req_type = i;
  794. return 0;
  795. }
  796. return -1;
  797. }
  798. int
  799. uclient_http_reset_headers(struct uclient *cl)
  800. {
  801. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  802. blob_buf_init(&uh->headers, 0);
  803. return 0;
  804. }
  805. int
  806. uclient_http_set_header(struct uclient *cl, const char *name, const char *value)
  807. {
  808. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  809. if (cl->backend != &uclient_backend_http)
  810. return -1;
  811. if (uh->state > HTTP_STATE_INIT)
  812. return -1;
  813. blobmsg_add_string(&uh->headers, name, value);
  814. return 0;
  815. }
  816. static int
  817. uclient_http_send_data(struct uclient *cl, const char *buf, unsigned int len)
  818. {
  819. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  820. int err;
  821. if (uh->state >= HTTP_STATE_REQUEST_DONE)
  822. return -1;
  823. err = uclient_http_send_headers(uh);
  824. if (err)
  825. return err;
  826. if (len > 0) {
  827. ustream_printf(uh->us, "%X\r\n", len);
  828. ustream_write(uh->us, buf, len, false);
  829. ustream_printf(uh->us, "\r\n");
  830. }
  831. return len;
  832. }
  833. static int
  834. uclient_http_request_done(struct uclient *cl)
  835. {
  836. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  837. int err;
  838. if (uh->state >= HTTP_STATE_REQUEST_DONE)
  839. return -1;
  840. err = uclient_http_send_headers(uh);
  841. if (err)
  842. return err;
  843. if (uclient_request_supports_body(uh->req_type))
  844. ustream_printf(uh->us, "0\r\n\r\n");
  845. uh->state = HTTP_STATE_REQUEST_DONE;
  846. return 0;
  847. }
  848. static int
  849. uclient_http_read(struct uclient *cl, char *buf, unsigned int len)
  850. {
  851. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  852. int read_len = 0;
  853. char *data, *data_end;
  854. if (uh->state < HTTP_STATE_RECV_DATA || !uh->us)
  855. return 0;
  856. data = ustream_get_read_buf(uh->us, &read_len);
  857. if (!data || !read_len) {
  858. ustream_poll(uh->us);
  859. data = ustream_get_read_buf(uh->us, &read_len);
  860. if (!data || !read_len)
  861. return 0;
  862. }
  863. data_end = data + read_len;
  864. read_len = 0;
  865. if (uh->read_chunked == 0) {
  866. char *sep;
  867. if (data[0] == '\r' && data[1] == '\n') {
  868. data += 2;
  869. read_len += 2;
  870. }
  871. sep = strstr(data, "\r\n");
  872. if (!sep)
  873. return 0;
  874. *sep = 0;
  875. uh->read_chunked = strtoul(data, NULL, 16);
  876. read_len += sep + 2 - data;
  877. data = sep + 2;
  878. if (!uh->read_chunked) {
  879. uh->eof = true;
  880. uh->uc.data_eof = true;
  881. }
  882. }
  883. unsigned int diff = data_end - data;
  884. if (len > diff)
  885. len = diff;
  886. if (uh->read_chunked >= 0) {
  887. if (len > (unsigned long) uh->read_chunked)
  888. len = uh->read_chunked;
  889. uh->read_chunked -= len;
  890. } else if (uh->content_length >= 0) {
  891. if (len > (unsigned long) uh->content_length)
  892. len = uh->content_length;
  893. uh->content_length -= len;
  894. if (!uh->content_length) {
  895. uh->eof = true;
  896. uh->uc.data_eof = true;
  897. }
  898. }
  899. if (len > 0) {
  900. read_len += len;
  901. memcpy(buf, data, len);
  902. }
  903. if (read_len > 0)
  904. ustream_consume(uh->us, read_len);
  905. uclient_notify_eof(uh);
  906. /* Now that we consumed something and if this isn't EOF, start timer again */
  907. if (!uh->uc.eof && !cl->connection_timeout.pending)
  908. uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs);
  909. return len;
  910. }
  911. int uclient_http_redirect(struct uclient *cl)
  912. {
  913. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  914. struct blobmsg_policy location = {
  915. .name = "location",
  916. .type = BLOBMSG_TYPE_STRING,
  917. };
  918. struct uclient_url *url = cl->url;
  919. struct blob_attr *tb;
  920. if (cl->backend != &uclient_backend_http)
  921. return false;
  922. if (!uclient_http_status_redirect(cl))
  923. return false;
  924. blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
  925. if (!tb)
  926. return false;
  927. url = uclient_get_url_location(url, blobmsg_data(tb));
  928. if (!url)
  929. return false;
  930. if (cl->proxy_url) {
  931. free(cl->proxy_url);
  932. cl->proxy_url = url;
  933. }
  934. else {
  935. free(cl->url);
  936. cl->url = url;
  937. }
  938. if (uclient_http_connect(cl))
  939. return -1;
  940. uclient_http_request_done(cl);
  941. return true;
  942. }
  943. int uclient_http_set_ssl_ctx(struct uclient *cl, const struct ustream_ssl_ops *ops,
  944. struct ustream_ssl_ctx *ctx, bool require_validation)
  945. {
  946. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  947. if (cl->backend != &uclient_backend_http)
  948. return -1;
  949. uclient_http_free_url_state(cl);
  950. uclient_http_free_ssl_ctx(uh);
  951. uh->ssl_ops = ops;
  952. uh->ssl_ctx = ctx;
  953. uh->ssl_require_validation = !!ctx && require_validation;
  954. return 0;
  955. }
  956. int uclient_http_set_address_family(struct uclient *cl, int af)
  957. {
  958. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  959. if (cl->backend != &uclient_backend_http)
  960. return -1;
  961. switch (af) {
  962. case AF_INET:
  963. uh->usock_flags = USOCK_IPV4ONLY;
  964. break;
  965. case AF_INET6:
  966. uh->usock_flags = USOCK_IPV6ONLY;
  967. break;
  968. default:
  969. uh->usock_flags = 0;
  970. break;
  971. }
  972. return 0;
  973. }
  974. static int
  975. uclient_http_pending_bytes(struct uclient *cl, bool write)
  976. {
  977. struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
  978. return ustream_pending_data(uh->us, write);
  979. }
  980. const struct uclient_backend uclient_backend_http = {
  981. .prefix = uclient_http_prefix,
  982. .alloc = uclient_http_alloc,
  983. .free = uclient_http_free,
  984. .connect = uclient_http_connect,
  985. .disconnect = uclient_http_request_disconnect,
  986. .update_url = uclient_http_free_url_state,
  987. .update_proxy_url = uclient_http_free_url_state,
  988. .read = uclient_http_read,
  989. .write = uclient_http_send_data,
  990. .request = uclient_http_request_done,
  991. .pending_bytes = uclient_http_pending_bytes,
  992. };