SOLIDIFY
NAME
solidify - download a web site, turning it to static HTML
SYNOPSIS
solidify
[options]
out-dir
https://site.dom/path
[https://site.dom/path...]
DESCRIPTION
Solidify
downloads all URL's on the command line
and saves them in
out-dir.
It also scans all downloaded URL's for more URL's
that live under
the directories the command line URL's live in,
and downloads them as well.
A command line URL ending in a
/
is a directory.
If the last component of a URL's path has a
.
in it (for example
https://site.dom/index.php)
it is a page, and it lives in the directory containing it
(https://site.dom/).
Otherwise it is assumed to be a directory missing its trailing
/
(for example
https://site.dom/blog
lives in
https://site.dom/blog/).
The objective is to turn the site into static HTML
so it can served by a web server
using just the files downloaded.
All URL's in downloaded files that point to other downloaded files
are made relative,
and pages and URL's may be altered so they can be served under any URL
from the static files.
In addition to downloading the site from it's home server
solidify
can also download foreign files the site depends on.
Javascript, css and images are often hosted on foreign servers,
and downloading them ensures the site will still work
when those foreign servers disappear.
OPTIONS
- --abort=status[,...]
-
Abort immediately
if the server returns one of the numeric HTTP status codes in
the comma separated list.
Normally all pages are saved and scanned for URL's.
--stop
is an obsolete name for this option.
- --assets[=asset-url]
-
Download page assets that live under URL
asset-url
in addition to the normal behaviour of
downloading assets that live under
one of the URL's listed on the command line.
If
asset-url
is omitted download all page assets.
This option may be repeated.
Page assets are URL's a page's HTML or css loads to render properly:
css, images, fonts, javascript and the like.
Links
(<a href=...>,
<link rel=next ...>,
and so on)
are not assets,
nor are URL's found inside javascript.
Embedded documents
(<iframe>,
<embed>,
<object>)
are also treated as links,
because on a foreign server they are almost always
someone else's application
(a video player, a map, a social media widget)
that won't work from a static copy.
Assets are scanned for URL's like any other download,
so the images and fonts a foreign css file needs are downloaded too.
Using an
asset-url
(for example
--assets=https://cdn.site.dom/)
rather than downloading all page assets
avoids collecting large amounts of junk from third party sites.
-
When it finishes
solidify
lists the hosts holding page assets it didn't download,
as the
--assets
options that would download them.
The static site still depends on those hosts.
With
--progress
each URL is listed too.
Downloaded assets may in turn use assets on other hosts
(for example a font service's css loading its fonts from another host),
so it may take a few runs to collect everything.
As
--assets
can't be changed when restarting, each run must start afresh.
URL's that javascript computes as it runs aren't seen, so aren't listed.
- --foreign=foreign-dir
-
Store foreign URL's (ie, URL's that aren't under the
root)
in
out-dir/foreign-dir/host/path,
where
host
includes the port if it isn't the default.
The default
foreign-dir
is "_".
It must not contain a "/".
- --header=H:V
-
Add a http header
H: V
to each page fetch request.
Adding a
Cookie
header can be used to bypass login credentials,
adding a
User-Agent
header can make some picky sites accept requests.
A header given here replaces the one
solidify
would otherwise send;
by default it sends
User-Agent: solidify/version.
- --keep-work
-
Don't delete the
out-dir/#.tmp
work directory when finished.
- --progress
-
Print progress messages to stdout.
- --rewrite=filename
-
Before scanning and saving HTML pages,
apply the search/replaces listed in
filename.
This option may be repeated.
See
MODIFYING THE DOWNLOADED PAGES
below.
--replace
is an obsolete name for this option.
- --root=[URL]
-
The root URL is sometimes called the site root.
It defaults to the directory the first URL on the command line lives in.
If a URL lives under the root
its path relative to the root becomes the filename it is stored in
under
out-dir.
URL's not under the root URL are called foreign URL's.
They are stored in a filename based on the full URL (including the host name)
and stored under the
foreign-dir
directory.
Omitting
URL
causes all URL's to be treated as foreign.
If you omit it, you should probably set
foreign-dir
to ".".
- --skip=status[,...]
-
Don't save or scan a page for URL's
if the server returns one of the numeric HTTP status codes in
the comma separated list.
Normally all pages are saved and scanned.
- --threads=count
-
Download up to
count
pages concurrently.
The default is 5, unless
--wait
is also present in which case 1 must be used.
- --wait=S[:T]
-
Wait between
S
seconds between each page fetch.
If
T
is supplied, wait a random amount of time between
S
and
T
seconds.
-
Separately from this,
a fetch that times out or gets a 429, 500, 502, 503 or 504 response
is retried twice,
after waiting 3 and then 15 seconds
(longer, up to 2 minutes, if the server's
Retry-After
header asks for it).
Sites that limit how fast they can be fetched
may need a slower
--wait
as well.
RESTARTING
Solidify
goes through multiple phases, looking at the entire site during each phase.
The first phase, which is the longest by far,
is downloading all pages in the site to a temporary area.
If interrupted this first phase can be restarted by rerunning the program
with the same parameters.
More URL's may be added to the command line when restarting,
but the
--root
and
--assets
options must not change.
Once the first phase is complete the run can not be restarted.
MODIFYING THE DOWNLOADED PAGES
The --rewrite=filename
option modifies the downloaded HTML pages
before they are scanned for URL's and saved.
Since this happens before
solidify
processes the page it can be used to change what URL's
solidify
sees and then downloads.
The search and replace is done using Python's
re.sub(search, replace, HTML)
function.
This means the search text is a Python regular expression,
and the replacement can include text found by the search.
If you aren't familiar with this function
you will need to consult the Python
re
module's documentation before using this option.
Each search in
filename
is introduced by a line containing the exact string:
-
.....Search-for:
That string may be proceeded by blank lines
and comment lines starting with a
#
sign.
The lines following contain the Python regular expression to search for.
Regular expressions can be made more readable
(for example, they can have comments)
by starting them with
(?x)
which turns the re.VERBOSE mode.
If present the replace string
must immediately follow the search string it replaces
and must be introduced with a line containing exactly:
-
.....Replace-with:
The line must be immediately followed
with the text that will replace the search string.
If no replace string is present the text found by the search is deleted.
Example:
#
# Don't include revision diff's.
#
.....Search-for:
(?sx)
<input\stype="radio"\sname="rev[^>]*>
<input\stype="radio"[^>]*>
<a\shref="[^"]*[?]action=diff&[^>]*>[^<]*</a>
.....Replace-with:
<font color=grey>[diff elided]</font>
#
# Don't include the login page.
#
.....Search-for:
(?sx)
<a\s[^>]*href="[^"]*[?]action=login\b[^>]*>Login</a>
.....Replace-with:
<font color=grey>[Login elided]</font>
Warning:
The time python's
re
module takes to process some input is usually roughly proportional to
the size of the input.
Proportional means if it takes N seconds to scan a file for a regex
it will take 100 times longer to process something 100 times bigger.
Unfortunately, the word "usually" must be there because
for some regular expressions Python's
re
module takes exponentially long to scan some inputs.
You may not notice you've exponential case for a small file,
but for a file 100 times larger you will
still be waiting for it to complete
when in 5 billion years time
the sun morphs into a red giant and ends all life on earth.
In concrete terms, this means while your
--rewrite
might work on one site, it could cause
solidify
to freeze, soaking up 100% of the CPU on another.
The root cause of this problem is the
re
module uses a Non-deterministic Finite Automata (NFA)
to recognise regular expressions.
The only way I can fix that is to replace the inbuilt
re
module with one that uses a Deterministic Finite Automata (DFA),
however I haven't found a suitable Python DFA regexp library.
MESSY DETAILS AND LIMITATIONS
Solidify
works by downloading the supplied URL's.
It discovers additional URL's to download by scanning html and css
for additional URL's with the same site.
In html that includes javascript embedded in
<script>
elements and
on...=
attributes,
and the URL a
<meta http-equiv=refresh>
redirects to.
It does not scan other file types (for example, javascript or json).
The HTML, css and javascript downloaded isn't parsed,
just quickly scanned for strings that look like URL's.
Solidify
tries hard not to miss URL's,
but that comes at the expense of mistaking random strings for URL's.
To limit the damage,
a string found in javascript or a
data-
attribute that has no
/
in it is only taken to be a URL if it ends in a file extension listed in
/etc/mime.types
or a common server side one like
.php.
Even so this can generate unexpected 404's (http page not found errors).
404 errors are mostly harmless.
If they really bother you the
#http-error=404
suffix appended to the filename makes it's easy to find and
delete the resulting downloads.
Javascript is not run, so dynamic content the browser fetches with AJAX
will not be seen.
All that means the translation process is not an exact science;
it's more of a best effort.
Usually
solidify
preserves URL's and page contents,
but in some circumstances URL's must be changed
so a static site will behave similarly to the dynamic one,
regardless of what base URL it is served from:
- -
-
All intra site URL's (ie URL's pointing to other files downloaded)
are made relative, so the site can be serviced from any base URL.
This includes foreign URL's.
- -
-
As web servers strip the query component queries are converted into
path components. For example,
https://some.dom/path/file.js?query
is turned into
https://some.dom/path/file.%3fquery.js.
- -
-
If the file extension does not match the Content-Type
as new file extension that does match will be appended.
For example,
https://some.dom/path/file.php?foo=bar&x=y
might be turned into:
https://some.dom/path/file.%3ffoo=bar&x=y.php.html
- -
-
A URL ending with a '/' is changed so it fetches a file called 'index'
with an extension matching the http Content-Type.
For example,
https://some.dom/path/
might be turned into:
https://some.dom/path/index.html.
- -
-
If a URL is both a page and has pages underneath it
(that is, it is also a directory), then the page will be renamed.
For example, if both
https://site.com/dir.html/foo.html
and
https://site.com/dir.html
exist then the latter will be renamed to:
https://site.com/dir~1.html.
- -
-
Pages that redirect are saved as html pages with a
<meta http-equiv=Refresh content=0;redirect-url>
to achieve the same effect.
URL's in the downloaded pages pointing to a page that redirects
are changed to point to where it redirects to,
as page assets like images and css can't be redirected that way.
- -
-
https://site.com/a-very-long-filename.html
(that is, filenames too long for the filesystem) are truncated.
For example, to:
https://site.com/a-very-long-file~truncated.html.
- -
-
https://site.com/i-dont-exist.html
(that is, any page that doesn't return a http OK or redirect)
gets saved to a file with
#http-error=xxx
appended, for example
https://site.com/i-dont-exist.html#http-error=404.
Unlike the previous cases URL's in the downloaded pages
aren't adjusted to this new form,
so internal URL's that returned a http errors
when fetched
will returned "404 Not Found" in the downloaded site.
All downloaded text is re-encoded as UTF-8,
and html pages have a
<meta http-equiv=Content-Type content=text/html; charset=UTF-8>
added to reflect that.
AUTHOR
Russell Stuart, <russell+solidify@stuart.id.au>.