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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package_control/commands/add_channel_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def run(self, url=None, unattended=False):
if isinstance(url, str):
url = url.strip()

if re.match(r'https?://', url, re.I) is None:
if re.match(r'^(?:file:///|https?://)', url, re.I) is None:
output_fn = console_write if unattended else show_error
output_fn(
'''
Expand Down
2 changes: 1 addition & 1 deletion package_control/commands/add_repository_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def run(self, url=None, unattended=False):
if isinstance(url, str):
url = url.strip()

if re.match(r'https?://', url, re.I) is None:
if re.match(r'^(?:file:///|https?://)', url, re.I) is None:
output_fn = console_write if unattended else show_error
output_fn(
'''
Expand Down
2 changes: 1 addition & 1 deletion package_control/commands/existing_packages_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def run(self):
default_packages = manager.list_default_packages()
default_version = 'built-in v' + sublime.version()

url_pattern = re.compile(r'^https?://')
url_pattern = re.compile(r'^(?:file:///|https?://)')

package_list = []
for package in sorted(self.list_packages(manager), key=lambda s: s.lower()):
Expand Down
32 changes: 12 additions & 20 deletions package_control/download_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import socket
import sys
from threading import Lock, Timer
from urllib.parse import urljoin, urlparse
from urllib.parse import unquote, urljoin, urlparse

from . import __version__
from . import text
Expand Down Expand Up @@ -68,6 +68,13 @@ def http_get(url, settings, error_message='', prefer_cached=False):
The string contents of the URL
"""

if url[:8].lower() == "file:///":
try:
with open(unquote(url[8:]), "rb") as f:
return f.read()
except OSError as e:
raise DownloaderException(str(e))

manager = None
result = None

Expand Down Expand Up @@ -172,11 +179,7 @@ def resolve_urls(root_url, uris):
A generator of resolved URLs
"""

scheme_match = re.match(r'(https?:)//', root_url, re.I)
if scheme_match is None:
root_dir = os.path.dirname(root_url)
else:
root_dir = ''
scheme_match = re.match(r'^(file:/|https?:)//', root_url, re.I)

for url in uris:
if not url:
Expand All @@ -190,10 +193,7 @@ def resolve_urls(root_url, uris):
# We don't allow absolute repositories
continue
elif url.startswith('./') or url.startswith('../'):
if root_dir:
url = os.path.normpath(os.path.join(root_dir, url))
else:
url = urljoin(root_url, url)
url = urljoin(root_url, url)
yield url


Expand All @@ -214,23 +214,15 @@ def resolve_url(root_url, url):
if not url:
return url

scheme_match = re.match(r'(https?:)//', root_url, re.I)
if scheme_match is None:
root_dir = os.path.dirname(root_url)
else:
root_dir = ''

if url.startswith('//'):
scheme_match = re.match(r'^(file:/|https?:)//', root_url, re.I)
if scheme_match is not None:
return scheme_match.group(1) + url
else:
return 'https:' + url

elif url.startswith('./') or url.startswith('../'):
if root_dir:
return os.path.normpath(os.path.join(root_dir, url))
else:
return urljoin(root_url, url)
return urljoin(root_url, url)

return url

Expand Down
20 changes: 1 addition & 19 deletions package_control/providers/channel_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,7 @@ def fetch(self):
if self.repositories is not None:
return

if re.match(r'https?://', self.channel_url, re.I):
json_string = http_get(self.channel_url, self.settings, 'Error downloading channel.')

# All other channels are expected to be filesystem paths
else:
if not os.path.exists(self.channel_url):
raise ProviderException('Error, file %s does not exist' % self.channel_url)

if self.settings.get('debug'):
console_write(
'''
Loading %s as a channel
''',
self.channel_url
)

# We open as binary so we get bytes like the DownloadManager
with open(self.channel_url, 'rb') as f:
json_string = f.read()
json_string = http_get(self.channel_url, self.settings, 'Error downloading channel.')

try:
channel_info = json.loads(json_string.decode('utf-8'))
Expand Down
20 changes: 1 addition & 19 deletions package_control/providers/json_repository_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,7 @@ def fetch_repo(self, location):

self.included_urls.add(location)

if re.match(r'https?://', location, re.I):
json_string = http_get(location, self.settings, 'Error downloading repository.')

# Anything that is not a URL is expected to be a filesystem path
else:
if not os.path.exists(location):
raise ProviderException('Error, file %s does not exist' % location)

if self.settings.get('debug'):
console_write(
'''
Loading %s as a repository
''',
location
)

# We open as binary so we get bytes like the DownloadManager
with open(location, 'rb') as f:
json_string = f.read()
json_string = http_get(location, self.settings, 'Error downloading repository.')

try:
repo_info = json.loads(json_string.decode('utf-8'))
Expand Down
0