8000 Teach `http_get` to read "file:///" urls by kaste · Pull Request #1694 · wbond/package_control · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Teach http_get to read "file:///" urls #1694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2025
Merged

Conversation

kaste
Copy link
Contributor
@kaste kaste commented Apr 6, 2025

We can already host local repositories -- i.e. "repositories" in the PC settings understand absolute local paths as additional repositories. But then inside such a repository, we cannot point a release url to a local file but MUST use the http-protocol.

E.g. for testing purposes, that means to create a local file server just to host such release-zip files.

See https://github.com/SublimeLinter/SublimeLinter/blob/029ff3dcf441f5606ecb329d66d76ed7df601e96/scripts/repo.json#L14-L14 Over there, I just use "python -m http.server 8000" to host a folder that contains SublimeLinter releases.

That seems unnecessary as the "file:///"-scheme denotes a local file expressed as an url, and frankly, other fetchers would accept them just as-is. E.g. a simple urllib.request.urlopen(location) would work with both http and file urls.

It doesn't work here because of basic tests in _grab, namely:

        parsed = urlparse(url)
        if not parsed or not parsed.hostname:
            raise DownloaderException('The URL "%s" is malformed' % url)

Don't scratch that test but instead check for the "file:///" scheme at the top, and do a straight, dead-simple file.open instead.

That's the minimal patch to support the usecase.

We can already host local repositories -- i.e. "repositories" in the
PC settings understand absolute local paths as additional repositories.
But then inside such a repository, we cannot point a release url to a
local file but MUST use the http-protocol.

That seems unnecessary as the "file:///"-scheme denotes a local file
expressed as an url, and frankly, other fetchers would accept them just
as-is.  E.g. a simple `urllib.request.urlopen(location)` would work with
both http and file urls.

Here, it doesn't work because of basic tests in `_grab`, e.g.:

```
        parsed = urlparse(url)
        if not parsed or not parsed.hostname:
            raise DownloaderException('The URL "%s" is malformed' % url)
```

Don't scratch these tests but instead check for the "file:///" scheme at
the top, and do a straight, dead-simple `file.open` instead.

That's the minimal patch to support the usecase.
Replace support of absolute local paths on channel/repository level
by global support for file:// protocol to handle local file resources,
including packages and libraries.

Enables adding local channels or repositories via Command Palette.
@deathaxe deathaxe merged commit 8c74722 into wbond:master May 7, 2025
@kaste
Copy link
Contributor Author
kaste commented May 7, 2025

Wow, that's cool. You could delete quite a bit of lines with that small addition.

@kaste
Copy link
Contributor Author
kaste commented May 7, 2025

Should we test this or do you make releases on a specific schedule?

@kaste kaste deleted the file-fetches branch May 7, 2025 20:35
@kaste
Copy link
Contributor Author
kaste commented May 8, 2025

Ah, you made a breaking change here. You dropped support for absolute paths in "repositories" in the PC settings. I think that change should be additive. It is more consistent now but a breaking change.

@deathaxe
Copy link
Collaborator
deathaxe commented May 8, 2025

Which bit of lines do you meand?

Absolute local paths are not officially promoted and not supported by Add Repository / Add Channel Commands. Supporting them, would also need to add another code path in http_get() for exaclty the same thing. Hence I expect it acceptable for users to adjust possible settings accordingly.

@kaste
Copy link
Contributor Author
kaste commented May 8, 2025

Which bit of lines do you meand?

Just generally, the original patch was +7 additive. But you could refactor and use it so that we have -61 in the overall diff now.

Absolute local paths are not officially promoted...

FichteFoll told me that I can use absolute paths years ago, but indeed, it is not in the settings comments.

@FichteFoll
Copy link
Collaborator
FichteFoll commented May 9, 2025

Can't you use standard absolute paths with the file protocol as well? That's what browsers do anyway. It just looks a bit weird because of the triple forward slashes at the beginning (and I honstly don't know about Windows here).

Edit: In fact, that's what all the regular expressions match as well, so not sure I understand the "absolute local path" problem.

@kaste
Copy link
Contributor Author
kaste commented May 9, 2025
< 9EC7 /table>

@deathaxe
Copy link
Collaborator
deathaxe commented May 9, 2025

To add support for the file protocol is the meat of this PR.

In one undocumented part, the PC settings, it was possible to just have standard absolute paths, e.g. c:\Users\..... That is no longer possible. Users must use the file protocol.

The con is really just that -- as the settings are user facing -- it is easier and feels "normal" to add a standard absolute path, as you can just copy and paste that from everywhere.

Browsers prepend file:// when pasting local absolute file paths.

The actual protocol name is file://. Third / is leading slash indicating absolute local path. Without it, url would denote a relative path, which Package Control expects to be denoted by leading ./.

Linux: /www/index.html becomes file:///www/index.html

Windows: C:\www\index.html is converted to file:///C:/www/index.html. The extra / being prepended makes it look like a absolute unix path (to some degree).

PC suppport(ed) adding absolute local paths into "channel" and "repository" settings to support locally hosted channel.json/repository.json by using a separate code path in ChannelProvider and JsonRepositoryProvider to load them from disk.

Any download asset specified via "url" however is downloaded by http_get(), which doesn't support local file paths, even if relative urls of releases were resolved to an absolute local file paths.

This made local file hosting impossible.

Of course we could also teach http_get() to treat anything not starting with https:// or file:// as absolute local file path. But why do we always need hundred ways to do one thing? We'd need to check for https:// vs. file:// vs. local path on all locations changed by this PR.

Official UI / Command Palette entries, didn't support adding local file paths anyway. That was just a somewhat "hidden" expert feature. Now adding local files is possible as well, when using a file:// url. Maybe we can auto-convert local absolute paths to support copy & paste.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0