![]() |
URLs, resolution and concretization | ![]() |
This page describes the C++ class url of
System/Classes/url.hpp
and
url.cpp
. The user level view and the Scheme routines are documented in the URL system; the treatment of tmfs URLs in internals of the TeXmacs file system.
A url is a reference counted handle (CONCRETE) on a url_rep, whose only field is a tree t. Two URLs are equal if their trees are equal; u[i] returns the i-th child of the tree as a URL and as_tree and as_url convert in both directions. The tree has one of the following forms:
One component of a name, such as "doc" or "a.tm". The special atoms "." (url_here), ".." (url_parent) and "…" (url_ancestor) denote the current directory, the parent directory and any ancestor directory; the empty atom marks a trailing slash.
The concatenation u1/u2. Concatenations are kept right nested: a/b/c is (concat a (concat b c)).
A search path: the first of u1 and u2 which exists, or all of them, depending on the operation. Also right nested.
The root of a name space: default (the local file system), file, http, https, ftp, doi, mailto, tmfs, blank (a name starting with //) and, on Android, content. (root ramdisc contents) is a ramdisc, a pseudo-file whose contents are stored in the URL itself.
Any sequence of components (printed **).
One component matching a pattern with *.
The empty URL (url_none, printed {}), which is also the result of failed operations. The default constructor url () returns it.
So /usr/share is (concat (root default) (concat usr share)) and https://www.texmacs.org/x is (concat (root https) (concat www.texmacs.org x)).
The predicates of
url.hpp
test these forms (is_none, is_atomic, is_concat, is_or, is_root, is_wildcard, …) and the root of a name (is_rooted, is_rooted_web, is_rooted_tmfs, is_rooted_blank, is_ramdisc). is_name is true for rootless names without paths or wildcards and is_rooted_name for a root followed by such a name; the latter is the form of a resolved URL.
URLs are parsed from strings by url_general (name, type), where type is one of
The conventions of the operating system: components are separated by / (and also by \ on Windows), and search paths by : (; on Windows). Used by url_system, for names which come from the system (command line, environment variables, file dialogs).
Unix conventions on all systems. Used by url_unix and by the constructors url (const char*), url (string) and url (dir, name), that is, for all names written in the source code.
Web conventions (url_standard).
Like URL_UNIX, but without the heuristics described below.
In the system and Unix formats, a component ~ is replaced by the value of $HOME and a component $VAR by the value of the environment variable, which is itself parsed in the system format (so a variable holding a search path yields an or). An unset or empty variable yields url_none, which makes the whole URL empty. Components containing * become wildcards.
url_general first recognizes explicit protocols (local:, file://, http://, https://, ftp://, doi:, mailto:, tmfs://, // and, on Android, content://). It then applies heuristics, in this order: a string containing the path separator is a search path; a string starting with / (on Windows: a drive letter or \\) is an absolute local name; on Windows, /c/… in the Unix format is the drive c:; except in the clean Unix format, names starting with www. or ftp. are web addresses. Anything else is a relative name.
as_string (u, type) prints a URL in one of the formats above (the default is the system format). Components of non-local roots are always printed with /; non-trivial subexpressions are put between braces, so that a/{b:c} denotes a/b:a/c. The shortcuts as_system_string, as_unix_string and as_standard_string fix the format; operator << on a tm_ostream uses the system format. On Windows, local names are printed with drive letters (c:\…) or as UNC names (\\host\…).
u1 * u2 (also with a string or const char* on the right, parsed in the Unix format). If u2 is rooted, the result is u2, except that a local absolute name or a // name after a web URL stays on the same host and protocol. A trailing .. removes the last component; the parent of a root, of a web host or (on Windows) of a drive is itself. . is neutral and url_none is absorbing.
u1 | u2, which removes immediate duplicates; an empty side (url_none) is simply dropped. expand distributes concatenations over or and replaces … by the list of all ancestors; factor is its inverse and also sorts; sort sorts the alternatives.
head (the directory, u * ".."), tail (the last component), suffix (lower case, without a trailing ~ or #, empty for names without a dot or starting with one), basename, glue and unglue (add or remove characters at the end of the last component), unblank (remove a trailing slash).
relative (base, u) is head (base) * u; delta (base, u) computes a relative name such that relative (base, delta (base, u)) == u when both are on the same root and host, and returns u itself otherwise.
get_root, unroot, reroot (u, protocol).
descends (u, base) tests whether u lies below one of the alternatives of base. is_secure (u) tests whether u lies below $TEXMACS_SECURE_PATH, which the boot sets to its previous value followed by $TEXMACS_PATH and $TEXMACS_HOME_PATH (see paths, directories and settings). In documents from secure locations, the typesetter executes scripts (such as extern) without first asking the Scheme predicate secure? (
Typeset/Env/env_exec.cpp
); the environment computes this flag from the file name of the document (edit_env_rep::secure). new_buffer_rep::secure is initialized in the same way but not read anywhere.
An URL with search paths, wildcards or a relative name denotes a set of candidate resources. Resolution finds the existing ones:
All candidates which pass the filter, as an or of rooted names. Relative names are interpreted with respect to $PWD (or $HOME if PWD is not set).
The first candidate only, or url_none. The default filter is "fr": a readable regular file. Alternatives of an or are tried in order, so the order of a search path is the order of precedence.
The same as resolve (u, "r") being non-empty; has_permission (u, filter) is the general version.
Look for an executable program, with the shell command which if it works on the system (use_which, determined at boot time), and otherwise in $PATH (also in $TEXMACS_PATH/bin on Windows and Android; on Windows, exists_in_path tries the suffixes .bat, .exe and .com).
All subdirectories of the directories of a path, used for the style and package menus and for the style search paths.
The filter is a string of letters which are all tested by is_of_type (files and caches): f regular file, d directory, l symbolic link, r readable, w writable, x executable. The empty filter accepts everything, so that resolve (u, "") just builds the first candidate name; this is how save_string chooses where to write a file which does not exist yet. Local candidates are returned with the default root even if they were written with file://.
Wildcards can only be expanded for local files (anything else fails with an error): ** matches any sequence of directories, including the empty one, and a pattern matches one directory entry. Directory listings come from read_directory and are therefore cached for the system directories.
A resolved URL is turned into a name which the operating system understands by
Local names (roots default, file, blank) are rerooted to default; web URLs are downloaded with get_from_web, tmfs URLs are fetched from their Scheme handler with get_from_server, ramdiscs are written to a temporary file with get_from_ramdisc; . and .. become $PWD and its parent. Anything else yields url_none.
The same, printed as a system string; a warning is printed on failure unless quiet is set.
Resolution followed by concretization.
(
file.hpp
) The concretized name quoted for the shell, used by the system (cmd, u, …) helpers.
For remote resources the result is a temporary file in the session's temporary directory, so concretized names must be used at once and not stored. The three fetch functions live in
System/Files/web_files.cpp
:
doi: names are first rewritten to https://www.doi.org/…. With Qt 6 the file is downloaded with qt_download_file; otherwise with wget or, if it is not installed, curl. An empty or missing result means failure.
Calls tmfs-load and saves the result in a temporary file; see the tmfs internals.
Writes the contents stored in the URL to a temporary file.
Web and ramdisc results are kept in a small in-memory cache which maps the URL to its temporary file and remembers the 25 most recently used entries; web_cache_invalidate (u) forgets one entry, so that the next access downloads the file again. tmfs results are not cached. The cache is not saved between sessions, but generated files which should survive a session, such as downloaded images, go through make_file (files and caches).
Environment variables and ~ are expanded when the URL is constructed, not when it is resolved. A static url initialized before the boot has set TEXMACS_PATH and the other variables is empty or wrong. Conversely, url ("$TEXMACS_PATH/…") in a function picks up the current value each time it is called.
In the Unix format, used by all string constants and by url (string), a colon separates search paths. A relative name containing a colon is therefore parsed as a path: use url_system or build the name component by component.
Testing a web URL (exists, is_of_type) downloads the whole file.
Without Qt 6, web files are downloaded with wget --no-check-certificate when wget is available, so the TLS certificate of the server is not checked (the curl command lines, including those of the HTTP requests in programs, web requests, messages and timing, do not disable the check).
file://host/path is concretized as the local name /host/path; host names in file URLs are not supported.
The in-memory web cache does not check that its temporary file still exists. make_file moves the temporary file of a downloaded resource into system/make, after which the cache entry points to a missing file: a later load_string or concretize of the same web URL in the same session fails instead of downloading it again.
concretize of a single wildcard component returns the raw pattern instead of failing.