Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit d6a1d24

Browse files
jiacai2050Copilot
andauthored
fix: more robustness in http handling (#4)
* fix: more robustness in http handling * Update lib/utils.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add missing import * fix timeout * readme --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5f3dedc commit d6a1d24

File tree

4 files changed

+26
-50
lines changed

4 files changed

+26
-50
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

.github/workflows/semantic-pr.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# asdf-zig [![Build](https://github.com/zigcc/asdf-zig/actions/workflows/build.yml/badge.svg)](https://github.com/zigcc/asdf-zig/actions/workflows/build.yml)
44

5-
[asdf-zig](https://github.com/zigcc/asdf-zig) plugin for the [asdf version manager](https://asdf-vm.com).
5+
[Zig](http://ziglang.org/) plugin for the [asdf version manager](https://asdf-vm.com).
66

77
</div>
88

@@ -13,7 +13,7 @@
1313

1414
# Install
1515

16-
First add asdf-zig as plugin:
16+
After installing [asdf](https://asdf-vm.com/guide/getting-started.html), install the plugin by running:
1717

1818
```shell
1919
asdf plugin add zig https://github.com/zigcc/asdf-zig.git

lib/utils.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import sys
88
import urllib.request
99
from urllib.parse import urljoin
10+
from urllib.error import HTTPError
1011
import json
1112
import hashlib
1213
import logging
1314

1415
INDEX_URL = os.getenv("ASDF_ZIG_INDEX_URL", "https://ziglang.org/download/index.json")
1516
HTTP_TIMEOUT = int(os.getenv("ASDF_ZIG_HTTP_TIMEOUT", "30"))
17+
USER_AGENT = "asdf-zig (https://github.com/zigcc/asdf-zig)"
1618

1719
# https://github.com/mlugg/setup-zig/blob/main/mirrors.json
1820
# If any of these mirrors are down, please open an issue!
@@ -32,17 +34,30 @@
3234
"arm64": "aarch64",
3335
}
3436

37+
class HTTPAccessError(Exception):
38+
def __init__(self, url, code, reason, body):
39+
super().__init__(f"{url} access failed, code:{code}, reason:{reason}, body:{body}")
40+
self.url = url
41+
self.code = code
42+
self.reason = reason
43+
self.body = body
44+
45+
def http_get(url, timeout=HTTP_TIMEOUT):
46+
try:
47+
req = urllib.request.Request(url, headers={'User-Agent': USER_AGENT})
48+
return urllib.request.urlopen(req, timeout=timeout)
49+
except HTTPError as e:
50+
body = e.read().decode("utf-8")
51+
raise HTTPAccessError(url, e.code, e.reason, body)
3552

3653
def fetch_index():
37-
with urllib.request.urlopen(INDEX_URL, timeout=HTTP_TIMEOUT) as response:
38-
if response.getcode() == 200:
39-
body = response.read().decode("utf-8")
40-
return json.loads(body)
41-
else:
42-
raise Exception(f"Fetch index.json error: {response.getcode()}")
54+
with http_get(INDEX_URL) as response:
55+
body = response.read().decode("utf-8")
56+
return json.loads(body)
4357

4458

45-
def all_versions(index=fetch_index()):
59+
def all_versions():
60+
index = fetch_index()
4661
versions = [k for k in index.keys() if k != "master"]
4762
versions.sort(key=lambda v: tuple(map(int, v.split("."))))
4863
return versions
@@ -52,10 +67,7 @@ def download_and_check(url, out_file, expected_shasum, total_size):
5267
logging.info(f"Begin download tarball({total_size}) from {url} to {out_file}...")
5368
chunk_size = 1024 * 1024 # 1M chunks
5469
sha256_hash = hashlib.sha256()
55-
with urllib.request.urlopen(url, timeout=HTTP_TIMEOUT) as response:
56-
if response.getcode() != 200:
57-
raise Exception(f"Fetch index.json error: {response.getcode()}")
58-
70+
with http_get(url) as response:
5971
read_size = 0
6072
with open(out_file, "wb") as f:
6173
while True:
@@ -113,7 +125,7 @@ def download(version, out_file):
113125

114126

115127
def main(args):
116-
command = args[0] if args else "default"
128+
command = args[0] if args else "all-versions"
117129
if command == "all-versions":
118130
versions = all_versions()
119131
print(" ".join(versions))

0 commit comments

Comments
 (0)