Detect an existing URL scheme instead of searching for "http"

CK
4mo ago
+13-1

clean_server_url adds a missing scheme with if "http" not in base_url. That is a substring test against the whole string, so any host whose name happens to contain those four characters is read as already schemed and comes back without one.

example.com                            -> http://example.com
myhttpd.local                          -> myhttpd.local
http2.example.com/general/v0/general   -> http2.example.com/general/v0/general

Adding the scheme is the thing the hook exists to do. The function docstring is "Fix url scheme" and the class docstring is "fixing common mistakes by users in defining server_url". urlparse then reads the schemeless string as a path, so netloc is empty and the cleanup below it works on nothing.

This matches a real scheme instead, <letter><letter|digit|+|-|.>* followed by :// per RFC 3986. Requiring the :// is what keeps localhost:8000 unschemed, and it is why urlparse cannot do the check here, since it reads localhost as the scheme.

test_unit_clean_server_url_fixes_non_unst_domain_url is already parameterised for exactly this and none of its cases uses a host containing "http", which is why the suite stayed green. Three added that do.

Verified in a clean venv with the package installed editable. Reverting the one source line, keeping the new cases, gives exactly those 3 failures and nothing else moves. Full unit suite is 274 passed, 1 xfailed with the change, against 271 and 1 without it. Five test dependencies your pyproject.toml declares were missing from my environment at first and 43 tests failed for that reason alone, so that baseline is after installing them.

Low severity. It needs a hostname containing "http", which is unusual, and only self-hosted users pass a bare host at all.

I did not test against a running server.