Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178176572
D61275.1796198462.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
11 KB
Referenced Files
None
Subscribers
None
D61275.1796198462.diff
View Options
diff --git a/ObsoleteFiles.inc b/ObsoleteFiles.inc
--- a/ObsoleteFiles.inc
+++ b/ObsoleteFiles.inc
@@ -51,6 +51,9 @@
# xargs -n1 | sort | uniq -d;
# done
+# 20261106: libfetch symbol versioning, shared library bumped to version 7
+OLD_LIBS+=usr/lib/libfetch.so.6
+
# 20261020: Regenerate trust store
OLD_FILES+=usr/share/certs/trusted/Atos_TrustedRoot_2011.pem
OLD_FILES+=usr/share/certs/trusted/SecureSign_Root_CA12.pem
diff --git a/lib/libfetch/Makefile b/lib/libfetch/Makefile
--- a/lib/libfetch/Makefile
+++ b/lib/libfetch/Makefile
@@ -24,7 +24,10 @@
CFLAGS+= -DFTP_COMBINE_CWDS
-SHLIB_MAJOR= 6
+SHLIB_MAJOR= 7
+
+VERSION_DEF= ${SRCTOP}/lib/libc/Versions.def
+SYMBOL_MAPS= ${.CURDIR}/Symbol.map
ftperr.h: ftp.errors ${.CURDIR}/Makefile
@echo "static struct fetcherr ftp_errlist[] = {" > ${.TARGET}
diff --git a/lib/libfetch/Symbol.map b/lib/libfetch/Symbol.map
new file mode 100644
--- /dev/null
+++ b/lib/libfetch/Symbol.map
@@ -0,0 +1,37 @@
+FBSD_1.9 {
+ fetchAuthMethod;
+ fetchDebug;
+ fetchFreeURL;
+ fetchGet;
+ fetchGetFTP;
+ fetchGetFile;
+ fetchGetHTTP;
+ fetchGetURL;
+ fetchLastErrCode;
+ fetchLastErrString;
+ fetchList;
+ fetchListFTP;
+ fetchListFile;
+ fetchListHTTP;
+ fetchListURL;
+ fetchMakeURL;
+ fetchParseURL;
+ fetchPut;
+ fetchPutFTP;
+ fetchPutFile;
+ fetchPutHTTP;
+ fetchPutURL;
+ fetchReqHTTP;
+ fetchRestartCalls;
+ fetchStat;
+ fetchStatFTP;
+ fetchStatFile;
+ fetchStatHTTP;
+ fetchStatURL;
+ fetchTimeout;
+ fetchXGet;
+ fetchXGetFTP;
+ fetchXGetFile;
+ fetchXGetHTTP;
+ fetchXGetURL;
+};
diff --git a/lib/libfetch/fetch.h b/lib/libfetch/fetch.h
--- a/lib/libfetch/fetch.h
+++ b/lib/libfetch/fetch.h
@@ -48,6 +48,7 @@
size_t length;
time_t ims_time;
int netrcfd;
+ char *validator;
};
struct url_stat {
@@ -87,6 +88,7 @@
#define FETCH_UNKNOWN 17
#define FETCH_URL 18
#define FETCH_VERBOSE 19
+#define FETCH_RESTART 20
__BEGIN_DECLS
diff --git a/lib/libfetch/fetch.3 b/lib/libfetch/fetch.3
--- a/lib/libfetch/fetch.3
+++ b/lib/libfetch/fetch.3
@@ -24,7 +24,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd June 27, 2026
+.Dd November 11, 2026
.Dt FETCH 3
.Os
.Sh NAME
@@ -170,15 +170,47 @@
off_t offset;
size_t length;
time_t ims_time;
+ int netrcfd;
+ char *validator;
};
.Ed
.Pp
The
+.Va offset
+field requests that the transfer start at the given byte offset rather
+than at the beginning of the document.
+On return, it holds the offset at which the server actually started the
+transfer, which is lower than requested when the server does not
+support, or did not honor, partial transfers.
+.Pp
+The
.Va ims_time
field stores the time value for
.Li If-Modified-Since
HTTP requests.
.Pp
+The
+.Va validator
+field points to the strong entity tag of the document most recently
+retrieved with this
+.Vt "struct url" ,
+or is
+.Dv NULL
+if the server sent none; see the
+.Sx HTTP SCHEME
+section.
+The library allocates it with
+.Xr malloc 3 ,
+replaces it as responses arrive, and frees it in
+.Fn fetchFreeURL ;
+a
+.Vt "struct url"
+which does not come from
+.Fn fetchMakeURL
+or
+.Fn fetchParseURL
+must be zeroed before its first use.
+.Pp
The pointer returned by
.Fn fetchMakeURL
or
@@ -403,6 +435,38 @@
HTTP header to only fetch the content if it is newer than
.Va ims_time .
.Pp
+Every successful HTTP request records the document's strong entity tag
+in the
+.Va validator
+field.
+When the same
+.Vt "struct url"
+is used again with a non-zero
+.Va offset ,
+.Fn fetchXGetHTTP
+and
+.Fn fetchGetHTTP
+send it in an
+.Li If-Range
+HTTP header, so that a server which has changed the document in the
+meantime sends all of it, from offset zero, instead of the tail of a
+different version.
+If the server answers that the range cannot be served and does not
+report that the document ends exactly at
+.Va offset ,
+or sends part of a document with a different entity tag, the request
+fails with
+.Dv FETCH_RESTART
+and should be retried with
+.Va offset
+set to zero.
+The entity tag is that of the document finally retrieved, after any
+redirects; free
+.Va validator
+and set it to
+.Dv NULL
+before reusing the structure for a different document.
+.Pp
The function
.Fn fetchReqHTTP
can be used to make requests with an arbitrary HTTP verb,
@@ -538,6 +602,8 @@
Protocol error
.It Bq Er FETCH_RESOLV
Resolver error
+.It Bq Er FETCH_RESTART
+Transfer must be restarted from the beginning
.It Bq Er FETCH_SERVER
Server error
.It Bq Er FETCH_TEMP
diff --git a/lib/libfetch/fetch.c b/lib/libfetch/fetch.c
--- a/lib/libfetch/fetch.c
+++ b/lib/libfetch/fetch.c
@@ -495,5 +495,6 @@
fetchFreeURL(struct url *u)
{
free(u->doc);
+ free(u->validator);
free(u);
}
diff --git a/lib/libfetch/http.c b/lib/libfetch/http.c
--- a/lib/libfetch/http.c
+++ b/lib/libfetch/http.c
@@ -99,6 +99,9 @@
/* Maximum number of redirects to follow */
#define MAX_REDIRECT 20
+/* Longest entity tag kept as a validator */
+#define MAX_VALIDATOR 256
+
/* Symbolic names for reply codes we care about */
#define HTTP_OK 200
#define HTTP_PARTIAL 206
@@ -367,7 +370,7 @@
* Wrap a file descriptor up
*/
static FILE *
-http_funopen(conn_t *conn, int chunked)
+http_funopen(conn_t *conn, int chunked, bool nobody)
{
struct httpio *io;
FILE *f;
@@ -378,6 +381,7 @@
}
io->conn = conn;
io->chunked = chunked;
+ io->eof = nobody;
f = funopen(io, http_readfn, http_writefn, NULL, http_closefn);
if (f == NULL) {
fetch_syserr();
@@ -400,6 +404,7 @@
hdr_unknown = 1,
hdr_content_length,
hdr_content_range,
+ hdr_etag,
hdr_last_modified,
hdr_location,
hdr_transfer_encoding,
@@ -414,6 +419,7 @@
} hdr_names[] = {
{ hdr_content_length, "Content-Length" },
{ hdr_content_range, "Content-Range" },
+ { hdr_etag, "ETag" },
{ hdr_last_modified, "Last-Modified" },
{ hdr_location, "Location" },
{ hdr_transfer_encoding, "Transfer-Encoding" },
@@ -920,6 +926,28 @@
return (0);
}
+/*
+ * Copy a strong entity tag, the only kind that can validate a range
+ * (RFC 9110, 13.1.5), checking that it can safely be sent back.
+ */
+static int
+http_parse_etag(const char *p, char *etag)
+{
+ size_t len;
+
+ if (p[0] != '"')
+ return (-1);
+ for (len = 1; p[len] != '"'; len++)
+ if ((unsigned char)p[len] < 0x21 || p[len] == 0x7f)
+ return (-1);
+ len++;
+ if (len >= MAX_VALIDATOR || p[len + strspn(p + len, " \t")] != '\0')
+ return (-1);
+ memcpy(etag, p, len);
+ etag[len] = '\0';
+ return (0);
+}
+
/*
* Parse a content-length header
*/
@@ -1605,22 +1633,22 @@
struct url *purl, const char *flags, const char *content_type,
const char *body)
{
+ char hbuf[MAXHOSTNAMELEN + 7], validator[MAX_VALIDATOR];
+ http_auth_challenges_t proxy_challenges, server_challenges;
char timebuf[80];
- char hbuf[MAXHOSTNAMELEN + 7], *host;
+ http_headerbuf_t headerbuf;
+ size_t body_len;
+ off_t clength, length, offset, size;
conn_t *conn;
- struct url *url, *loc;
- int chunked, direct, ims, noredirect, verbose;
- int e, i, n, val;
- off_t offset, clength, length, size;
+ FILE *f;
+ char *host;
+ struct url *loc, *url;
time_t mtime;
const char *p;
- FILE *f;
- hdr_t h;
struct tm *timestruct;
- http_headerbuf_t headerbuf;
- http_auth_challenges_t server_challenges;
- http_auth_challenges_t proxy_challenges;
- size_t body_len;
+ int chunked, direct, e, i, ims, n, noredirect, val, verbose;
+ hdr_t h;
+ bool ifrange, nobody;
/* The following calls don't allocate anything */
init_http_headerbuf(&headerbuf);
@@ -1632,6 +1660,10 @@
verbose = CHECK_FLAG('v');
ims = CHECK_FLAG('i');
+ /* a resumed transfer is conditional on the document being unchanged */
+ ifrange = (URL->offset > 0 && URL->validator != NULL);
+ nobody = false;
+
if (direct && purl) {
fetchFreeURL(purl);
purl = NULL;
@@ -1652,6 +1684,7 @@
length = -1;
size = -1;
mtime = 0;
+ validator[0] = '\0';
/* check port */
if (!url->port)
@@ -1785,8 +1818,15 @@
http_cmd(conn, "User-Agent: %s " _LIBFETCH_VER,
getprogname());
}
- if (url->offset > 0)
+ if (url->offset > 0) {
http_cmd(conn, "Range: bytes=%lld-", (long long)url->offset);
+ if (ifrange) {
+ if (verbose)
+ fetch_info("If-Range: %s",
+ URL->validator);
+ http_cmd(conn, "If-Range: %s", URL->validator);
+ }
+ }
http_cmd(conn, "Connection: close");
if (body) {
@@ -1897,6 +1937,10 @@
case hdr_content_range:
http_parse_range(p, &offset, &length, &size);
break;
+ case hdr_etag:
+ if (http_parse_etag(p, validator) != 0)
+ validator[0] = '\0';
+ break;
case hdr_last_modified:
http_parse_mtime(p, &mtime);
break;
@@ -1984,12 +2028,26 @@
/* requested range not satisfiable */
if (conn->err == HTTP_BAD_RANGE) {
- if (url->offset > 0 && url->length == 0) {
- /* asked for 0 bytes; fake it */
+ /*
+ * URL->length is what the previous response
+ * reported, so a conditional request goes by the
+ * size the server gives instead.
+ */
+ if (url->offset > 0 && (ifrange ?
+ size == url->offset : url->length == 0)) {
+ /* asked for 0 bytes; fake an empty part */
offset = url->offset;
clength = -1;
- conn->err = HTTP_OK;
+ conn->err = HTTP_PARTIAL;
+ /* the reply's body is not the document's */
+ nobody = true;
break;
+ } else if (ifrange) {
+ /* shorter now, or the server does not say */
+ fetchLastErrCode = FETCH_RESTART;
+ strlcpy(fetchLastErrString,
+ "range no longer available", MAXERRSTRING);
+ goto ouch;
} else {
http_seterr(conn->err);
goto ouch;
@@ -2032,6 +2090,16 @@
goto ouch;
}
+ /* a conditional partial response must describe the same document */
+ if (ifrange && conn->err == HTTP_PARTIAL && validator[0] != '\0' &&
+ strcmp(validator, URL->validator) != 0) {
+ fetchLastErrCode = FETCH_RESTART;
+ strlcpy(fetchLastErrString,
+ "document changed since the transfer started",
+ MAXERRSTRING);
+ goto ouch;
+ }
+
/* check for inconsistencies */
if (clength != -1 && length != -1 && clength != length) {
http_seterr(HTTP_PROTOCOL_ERROR);
@@ -2060,12 +2128,24 @@
goto ouch;
}
+ /* remember the validator; a partial response without one keeps it */
+ if (!HTTP_ERROR(conn->err) &&
+ (conn->err != HTTP_PARTIAL || validator[0] != '\0')) {
+ free(URL->validator);
+ URL->validator = NULL;
+ if (validator[0] != '\0' &&
+ (URL->validator = strdup(validator)) == NULL) {
+ fetch_syserr();
+ goto ouch;
+ }
+ }
+
/* report back real offset and size */
URL->offset = offset;
URL->length = clength;
/* wrap it up in a FILE */
- if ((f = http_funopen(conn, chunked)) == NULL) {
+ if ((f = http_funopen(conn, chunked, nobody)) == NULL) {
fetch_syserr();
goto ouch;
}
diff --git a/usr.bin/fetch/fetch.c b/usr.bin/fetch/fetch.c
--- a/usr.bin/fetch/fetch.c
+++ b/usr.bin/fetch/fetch.c
@@ -1189,6 +1189,7 @@
&& fetchLastErrCode != FETCH_URL
&& fetchLastErrCode != FETCH_RESOLV
&& fetchLastErrCode != FETCH_UNKNOWN
+ && fetchLastErrCode != FETCH_RESTART
&& (!is_http || (
fetchLastErrCode != FETCH_PROTO
&& fetchLastErrCode != FETCH_SERVER
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Dec 2, 8:01 AM (14 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42127789
Default Alt Text
D61275.1796198462.diff (11 KB)
Attached To
Mode
D61275: libfetch: Send If-Range with ranged requests
Attached
Detach File
Event Timeline