return status == 304;
}
+static int http_status_need_authentication(CURLcode status) {
+ return status == 401;
+}
+
void pull_job_close_disk_fd(PullJob *j) {
if (!j)
return;
if (j->free_userdata)
j->free_userdata(j->userdata);
free(j->description);
+ free(j->authentication_challenge);
return mfree(j);
}
j->etag_exists = true;
r = 0;
goto finish;
+ } else if (http_status_need_authentication(status)) {
+ log_info("Access to image requires authentication.");
+ r = -ENOKEY;
+ goto finish;
} else if (status >= 300) {
if (status == 404 && j->on_not_found) {
goto fail;
}
+ if (http_status_need_authentication(status)) {
+ _cleanup_free_ char *challenge = NULL;
+
+ r = curl_header_strdup(contents, sz, "WWW-Authenticate:", &challenge);
+ if (r < 0) {
+ log_oom();
+ goto fail;
+ }
+ if (r > 0)
+ free_and_replace(j->authentication_challenge, challenge);
+ return sz;
+ }
+
if (http_status_ok(status) || http_status_etag_exists(status)) {
/* Check Etag on OK and etag exists responses. */
return pull_job_add_request_header(j, f);
}
+
+int pull_job_set_bearer_token(PullJob *j, const char *token) {
+ assert(j);
+
+ _cleanup_free_ char *f = strjoin("Authorization: Bearer ", token);
+ if (!f)
+ return -ENOMEM;
+
+ return pull_job_add_request_header(j, f);
+}
bool sync;
bool force_memory;
+
+ char *authentication_challenge;
} PullJob;
int pull_job_new(PullJob **ret, const char *url, CurlGlue *glue, void *userdata);
int pull_job_add_request_header(PullJob *j, const char *hdr);
int pull_job_set_accept(PullJob *j, char * const *l);
+int pull_job_set_bearer_token(PullJob *j, const char *token);
DEFINE_TRIVIAL_CLEANUP_FUNC(PullJob*, pull_job_unref);