char *sep;
if (uh->state == HTTP_STATE_REQUEST_DONE) {
+ char *code;
+
+ /* HTTP/1.1 */
+ strsep(&data, " ");
+
+ code = strsep(&data, " ");
+ if (!code)
+ goto error;
+
+ uh->uc.status_code = strtoul(code, &sep, 10);
+ if (sep && *sep)
+ goto error;
+
uh->state = HTTP_STATE_RECV_HEADERS;
return;
}
sep++;
blobmsg_add_string(&uh->meta, name, sep);
+ return;
+
+error:
+ uh->uc.status_code = 400;
+ uh->eof = true;
+ uclient_notify_eof(uh);
}
static void __uclient_notify_read(struct uclient_http *uh)
return len;
}
-const struct uclient_backend uclient_backend_http __hidden = {
+bool uclient_http_redirect(struct uclient *cl)
+{
+ struct uclient_http *uh = container_of(cl, struct uclient_http, uc);
+ struct blobmsg_policy location = {
+ .name = "location",
+ .type = BLOBMSG_TYPE_STRING,
+ };
+ struct uclient_url *url = cl->url;
+ struct blob_attr *tb;
+
+ if (cl->backend != &uclient_backend_http)
+ return false;
+
+ switch (cl->status_code) {
+ case 301:
+ case 302:
+ case 307:
+ break;
+ default:
+ return false;
+ }
+
+ blobmsg_parse(&location, 1, &tb, blob_data(uh->meta.head), blob_len(uh->meta.head));
+ if (!tb)
+ return false;
+
+ url = uclient_get_url(blobmsg_data(tb), url->auth);
+ if (!url)
+ return false;
+
+ free(cl->url);
+ cl->url = url;
+ uclient_http_connect(cl);
+ uclient_http_request_done(cl);
+
+ return true;
+}
+
+const struct uclient_backend uclient_backend_http = {
.prefix = uclient_http_prefix,
.alloc = uclient_http_alloc,