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)
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,
"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) {
PullJob *j = NULL;
CURLcode code;
long status;
long protocol;
int r;
if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK)
@ -131,11 +131,22 @@ void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
goto finish;
}
code = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
if (code != CURLE_OK) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish;
}
if (IN_SET(protocol, CURLPROTO_HTTP, CURLPROTO_HTTPS)) {
long status;
code = curl_easy_getinfo(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;
} else if (status == 304) {
}
if (status == 304) {
log_info("Image already downloaded. Skipping download.");
j->etag_exists = true;
r = 0;
@ -176,6 +187,7 @@ void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status);
goto finish;
}
}
if (j->state != PULL_JOB_RUNNING) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Premature connection termination.");

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)) || !checksum);
if (!http_url_is_valid(url))
if (!http_url_is_valid(url) && !file_url_is_valid(url))
return -EINVAL;
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) || !checksum);
if (!http_url_is_valid(url))
if (!http_url_is_valid(url) && !file_url_is_valid(url))
return -EINVAL;
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;
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);
if (argc >= 3)
@ -183,7 +183,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
int r;
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);
if (argc >= 3)

View File

@ -2125,7 +2125,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
assert(bus);
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),
"URL '%s' is not valid.", remote);
@ -2181,7 +2181,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
assert(bus);
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),
"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);
}
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) {
const char *p;
if (isempty(url))
return false;
if (http_url_is_valid(url))
if (http_url_is_valid(url) || file_url_is_valid(url))
return true;
p = STARTSWITH_SET(url, "file:/", "info:", "man:");
p = STARTSWITH_SET(url, "info:", "man:");
if (isempty(p))
return false;

View File

@ -6,6 +6,7 @@
#include "macro.h"
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_;