1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

import: allow file:// in addition to HTTP(S)

Previously we only allows http/https urls, let's open this up a bit.
Why? Because it makes testing *so* *much* *easier* as we don't need to
run a HTTP server all the time.

CURL mostly abstracts the differences of http/https away from us, hence
we can get away with very little extra work.
This commit is contained in:
Lennart Poettering 2021-08-19 18:15:37 +02:00
parent 55b90ee00b
commit c456862f87
8 changed files with 67 additions and 41 deletions

View File

@ -928,7 +928,7 @@ static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_er
if (r < 0) if (r < 0)
return r; return r;
if (!http_url_is_valid(remote)) if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"URL %s is invalid", remote); "URL %s is invalid", remote);

View File

@ -117,7 +117,7 @@ static int pull_job_restart(PullJob *j, const char *new_url) {
void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) { void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
PullJob *j = NULL; PullJob *j = NULL;
CURLcode code; CURLcode code;
long status; long protocol;
int r; int r;
if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK) if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK)
@ -131,50 +131,62 @@ void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
goto finish; goto finish;
} }
code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status); code = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
if (code != CURLE_OK) { if (code != CURLE_OK) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code)); r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish; goto finish;
} else if (status == 304) { }
log_info("Image already downloaded. Skipping download.");
j->etag_exists = true;
r = 0;
goto finish;
} else if (status >= 300) {
if (status == 404 && j->on_not_found) { if (IN_SET(protocol, CURLPROTO_HTTP, CURLPROTO_HTTPS)) {
_cleanup_free_ char *new_url = NULL; long status;
/* This resource wasn't found, but the implementor wants to maybe let us know a new URL, query for it. */ code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
r = j->on_not_found(j, &new_url); if (code != CURLE_OK) {
if (r < 0) r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish; goto finish;
}
if (r > 0) { /* A new url to use */ if (status == 304) {
assert(new_url); log_info("Image already downloaded. Skipping download.");
j->etag_exists = true;
r = 0;
goto finish;
} else if (status >= 300) {
r = pull_job_restart(j, new_url); if (status == 404 && j->on_not_found) {
_cleanup_free_ char *new_url = NULL;
/* This resource wasn't found, but the implementor wants to maybe let us know a new URL, query for it. */
r = j->on_not_found(j, &new_url);
if (r < 0) if (r < 0)
goto finish; goto finish;
code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status); if (r > 0) { /* A new url to use */
if (code != CURLE_OK) { assert(new_url);
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish; r = pull_job_restart(j, new_url);
if (r < 0)
goto finish;
code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status);
if (code != CURLE_OK) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish;
}
if (status == 0)
return;
} }
if (status == 0)
return;
} }
}
r = log_error_errno( r = log_error_errno(
status == 404 ? SYNTHETIC_ERRNO(ENOMEDIUM) : SYNTHETIC_ERRNO(EIO), /* Make the most common error recognizable */ status == 404 ? SYNTHETIC_ERRNO(ENOMEDIUM) : SYNTHETIC_ERRNO(EIO), /* Make the most common error recognizable */
"HTTP request to %s failed with code %li.", j->url, status); "HTTP request to %s failed with code %li.", j->url, status);
goto finish; goto finish;
} else if (status < 200) { } else if (status < 200) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status); r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status);
goto finish; goto finish;
}
} }
if (j->state != PULL_JOB_RUNNING) { if (j->state != PULL_JOB_RUNNING) {

View File

@ -835,7 +835,7 @@ int raw_pull_start(
assert(!(flags & (PULL_SETTINGS|PULL_ROOTHASH|PULL_ROOTHASH_SIGNATURE|PULL_VERITY)) || !(flags & PULL_DIRECT)); assert(!(flags & (PULL_SETTINGS|PULL_ROOTHASH|PULL_ROOTHASH_SIGNATURE|PULL_VERITY)) || !(flags & PULL_DIRECT));
assert(!(flags & (PULL_SETTINGS|PULL_ROOTHASH|PULL_ROOTHASH_SIGNATURE|PULL_VERITY)) || !checksum); assert(!(flags & (PULL_SETTINGS|PULL_ROOTHASH|PULL_ROOTHASH_SIGNATURE|PULL_VERITY)) || !checksum);
if (!http_url_is_valid(url)) if (!http_url_is_valid(url) && !file_url_is_valid(url))
return -EINVAL; return -EINVAL;
if (local && !pull_validate_local(local, flags)) if (local && !pull_validate_local(local, flags))

View File

@ -597,7 +597,7 @@ int tar_pull_start(
assert(!(flags & PULL_SETTINGS) || !(flags & PULL_DIRECT)); assert(!(flags & PULL_SETTINGS) || !(flags & PULL_DIRECT));
assert(!(flags & PULL_SETTINGS) || !checksum); assert(!(flags & PULL_SETTINGS) || !checksum);
if (!http_url_is_valid(url)) if (!http_url_is_valid(url) && !file_url_is_valid(url))
return -EINVAL; return -EINVAL;
if (local && !pull_validate_local(local, flags)) if (local && !pull_validate_local(local, flags))

View File

@ -110,7 +110,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
int r; int r;
url = argv[1]; url = argv[1];
if (!http_url_is_valid(url)) if (!http_url_is_valid(url) && !file_url_is_valid(url))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url); return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url);
if (argc >= 3) if (argc >= 3)
@ -183,7 +183,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
int r; int r;
url = argv[1]; url = argv[1];
if (!http_url_is_valid(url)) if (!http_url_is_valid(url) && !file_url_is_valid(url))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url); return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url);
if (argc >= 3) if (argc >= 3)

View File

@ -2125,7 +2125,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
remote = argv[1]; remote = argv[1];
if (!http_url_is_valid(remote)) if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"URL '%s' is not valid.", remote); "URL '%s' is not valid.", remote);
@ -2181,7 +2181,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
remote = argv[1]; remote = argv[1];
if (!http_url_is_valid(remote)) if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"URL '%s' is not valid.", remote); "URL '%s' is not valid.", remote);

View File

@ -36,16 +36,29 @@ bool http_url_is_valid(const char *url) {
return ascii_is_valid(p); return ascii_is_valid(p);
} }
bool file_url_is_valid(const char *url) {
const char *p;
if (isempty(url))
return false;
p = startswith(url, "file:/");
if (isempty(p))
return false;
return ascii_is_valid(p);
}
bool documentation_url_is_valid(const char *url) { bool documentation_url_is_valid(const char *url) {
const char *p; const char *p;
if (isempty(url)) if (isempty(url))
return false; return false;
if (http_url_is_valid(url)) if (http_url_is_valid(url) || file_url_is_valid(url))
return true; return true;
p = STARTSWITH_SET(url, "file:/", "info:", "man:"); p = STARTSWITH_SET(url, "info:", "man:");
if (isempty(p)) if (isempty(p))
return false; return false;

View File

@ -6,6 +6,7 @@
#include "macro.h" #include "macro.h"
bool http_url_is_valid(const char *url) _pure_; bool http_url_is_valid(const char *url) _pure_;
bool file_url_is_valid(const char *url) _pure_;
bool documentation_url_is_valid(const char *url) _pure_; bool documentation_url_is_valid(const char *url) _pure_;