+ cur = *buf + *ofs;
+ cur += sprintf(cur, ", %s=\"", name);
+
+ while ((next = strchr(val, '"'))) {
+ if (next > val) {
+ memcpy(cur, val, next - val);
+ cur += next - val;
+ }
+
+ cur += sprintf(cur, "\\\"");
+ val = next + 1;
+ }
+
+ cur += sprintf(cur, "%s\"", val);
+ *ofs = cur - *buf;
+}
+
+static void
+uclient_http_add_auth_digest(struct uclient_http *uh)
+{
+ struct uclient_url *url = uh->uc.url;
+ const char *realm = NULL, *opaque = NULL;
+ const char *user, *password;
+ char *buf, *next;
+ int len, ofs;
+
+ char cnonce_str[9];
+ char nc_str[9];
+ char ahash[33];
+ char hash[33];
+
+ struct http_digest_data data = {
+ .nc = nc_str,
+ .cnonce = cnonce_str,
+ .auth_hash = ahash,
+ };
+
+ len = strlen(uh->auth_str) + 1;
+ if (len > 512)
+ return;
+
+ buf = alloca(len);
+ strcpy(buf, uh->auth_str);
+
+ /* skip auth type */
+ strsep(&buf, " ");
+
+ next = buf;
+ while (*next) {
+ const char **dest = NULL;
+
+ while (isspace(*next))
+ next++;
+
+ if (strmatch(&next, "realm"))
+ dest = &realm;
+ else if (strmatch(&next, "qop"))
+ dest = &data.qop;
+ else if (strmatch(&next, "nonce"))
+ dest = &data.nonce;
+ else if (strmatch(&next, "opaque"))
+ dest = &opaque;
+ else
+ return;
+
+ *dest = digest_unquote_sep(&next);
+ }
+
+ if (!realm || !data.qop || !data.nonce)
+ return;
+
+ sprintf(nc_str, "%08x", uh->nc++);
+ get_cnonce(cnonce_str);
+
+ data.qop = "auth";
+ data.uri = url->location;
+ data.method = request_types[uh->req_type];
+
+ password = strchr(url->auth, ':');
+ if (password) {
+ char *user_buf;
+
+ len = password - url->auth;
+ if (len > 256)
+ return;
+
+ user_buf = alloca(len + 1);
+ strncpy(user_buf, url->auth, len);
+ user_buf[len] = 0;
+ user = user_buf;
+ password++;
+ } else {
+ user = url->auth;
+ password = "";
+ }
+
+ http_digest_calculate_auth_hash(ahash, user, realm, password);
+ http_digest_calculate_response(hash, &data);
+
+ buf = NULL;
+ len = 0;
+ ofs = 0;
+
+ add_field(&buf, &ofs, &len, "username", user);
+ add_field(&buf, &ofs, &len, "realm", realm);
+ add_field(&buf, &ofs, &len, "nonce", data.nonce);
+ add_field(&buf, &ofs, &len, "uri", data.uri);
+ add_field(&buf, &ofs, &len, "cnonce", data.cnonce);
+ add_field(&buf, &ofs, &len, "response", hash);
+ if (opaque)
+ add_field(&buf, &ofs, &len, "opaque", opaque);
+
+ ustream_printf(uh->us, "Authorization: Digest nc=%s, qop=%s%s\r\n", data.nc, data.qop, buf);
+ free(buf);
+}
+
+static void
+uclient_http_add_auth_header(struct uclient_http *uh)
+{
+ if (!uh->uc.url->auth)
+ return;
+
+ switch (uh->auth_type) {
+ case AUTH_TYPE_UNKNOWN:
+ case AUTH_TYPE_NONE:
+ break;
+ case AUTH_TYPE_BASIC:
+ uclient_http_add_auth_basic(uh);
+ break;
+ case AUTH_TYPE_DIGEST:
+ uclient_http_add_auth_digest(uh);
+ break;
+ }
+}
+
+static void
+uclient_http_send_headers(struct uclient_http *uh)
+{
+ struct uclient_url *url = uh->uc.url;
+ struct blob_attr *cur;
+ enum request_type req_type = uh->req_type;
+ int rem;
+
+ if (uh->state >= HTTP_STATE_HEADERS_SENT)
+ return;
+
+ if (uh->auth_type == AUTH_TYPE_UNKNOWN)
+ req_type = REQ_HEAD;
+
+ ustream_printf(uh->us,
+ "%s %s HTTP/1.1\r\n"
+ "Host: %s\r\n",
+ request_types[req_type],
+ url->location, url->host);
+
+ blobmsg_for_each_attr(cur, uh->headers.head, rem)
+ ustream_printf(uh->us, "%s: %s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
+
+ if (uh->req_type == REQ_POST)
+ ustream_printf(uh->us, "Transfer-Encoding: chunked\r\n");
+
+ uclient_http_add_auth_header(uh);
+
+ ustream_printf(uh->us, "\r\n");
+
+ uh->state = HTTP_STATE_HEADERS_SENT;
+}
+
+static void uclient_http_headers_complete(struct uclient_http *uh)
+{
+ enum auth_type auth_type = uh->auth_type;
+
+ uh->state = HTTP_STATE_RECV_DATA;
+ uh->uc.meta = uh->meta.head;
+ uclient_http_process_headers(uh);
+
+ if (auth_type == AUTH_TYPE_UNKNOWN) {
+ uclient_http_init_request(uh);
+ uclient_http_send_headers(uh);
+ uh->state = HTTP_STATE_REQUEST_DONE;
+ return;
+ }
+
+ if (uh->uc.cb->header_done)
+ uh->uc.cb->header_done(&uh->uc);
+
+ if (uh->req_type == REQ_HEAD) {
+ uh->eof = true;
+ uclient_notify_eof(uh);
+ }