Skip to content

Commit 1fdf0f9

Browse files
authored
Requests support (#18)
replace lib curl (dlopen) to dlang-requests
1 parent 2b31996 commit 1fdf0f9

File tree

4 files changed

+65
-43
lines changed

4 files changed

+65
-43
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,26 @@ jobs:
3535
with:
3636
compiler: ldc-latest
3737
- uses: ilammy/msvc-dev-cmd@v1
38-
if: matrix.os == 'windows-11-arm'
38+
if: runner.os == 'Windows'
3939
with:
40-
arch: amd64_${{ matrix.arch }}
40+
arch: ${{ matrix.arch }}
41+
- name: Set CURL_CA_BUNDLE for Windows
42+
if: runner.os == 'Windows'
43+
shell: pwsh
44+
run: |
45+
Invoke-WebRequest -Uri https://curl.se/ca/cacert.pem -OutFile curl-ca-bundle.crt
46+
echo "CURL_CA_BUNDLE=$($PWD.Path)\curl-ca-bundle.crt" >> $env:GITHUB_ENV
4147
- name: Set DUB_ARCH for windows-11-arm
4248
if: matrix.os == 'windows-11-arm'
4349
shell: bash
4450
run: |
4551
echo "DUB_ARCH=--arch=arm64-windows-msvc" >> $GITHUB_ENV
4652
echo "CC=cl.exe" >> $GITHUB_ENV
47-
echo "CXX=cl.exe" >> $GITHUB_ENV
53+
echo "CXX=cl.exe" >> $GITHUB_ENV
4854
- name: Build
4955
run: |
5056
dub -b release ${{ env.DUB_ARCH }}
5157
- name: Tests
52-
if: matrix.os != 'windows-11-arm'
5358
run: |
5459
dub -b release ${{ env.DUB_ARCH }} -- install -v
5560
dub -b release ${{ env.DUB_ARCH }} -- install ldc2-master -v
@@ -99,16 +104,16 @@ jobs:
99104
dub -b release -- uninstall ldc2-1.41.0 -v
100105
dub -b release -- list -v
101106
dub -b release -- run -v -- --version
102-
tar -cJf bin/ldcup-freebsd14.2-${{ matrix.arch }}.tar.xz -C bin .
107+
tar -cJf bin/ldcup-freebsd14.3-${{ matrix.arch }}.tar.xz -C bin .
103108
- uses: actions/upload-artifact@v4
104109
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')
105110
with:
106-
name: ldcup-freebsd-14.2-amd64
107-
path: bin/ldcup-freebsd14.2-${{ matrix.arch }}.tar.xz
111+
name: ldcup-freebsd-14.3-amd64
112+
path: bin/ldcup-freebsd14.3-${{ matrix.arch }}.tar.xz
108113
- uses: softprops/action-gh-release@v2
109114
if: startsWith(github.ref, 'refs/tags/')
110115
with:
111-
files: 'bin/ldcup-freebsd14.2-${{ matrix.arch }}.tar.xz'
116+
files: 'bin/ldcup-freebsd14.3-${{ matrix.arch }}.tar.xz'
112117
prerelease: true
113118
token: ${{ secrets.GITHUB_TOKEN }}
114119

@@ -130,6 +135,9 @@ jobs:
130135
run: |
131136
apk update
132137
apk add --no-cache ldc dub clang xz
138+
- name: "Build Flags environment"
139+
run: |
140+
echo "LDFLAGS=-static" >> $GITHUB_ENV
133141
- name: Build
134142
run: |
135143
dub -b release
@@ -141,7 +149,6 @@ jobs:
141149
source $HOME/.profile
142150
dub -b release -- install redub -v
143151
dub -b release -- run -v -- --version
144-
redub --version
145152
- name: Compress artifacts
146153
run: |
147154
cd bin && tar -cJf ldcup-alpine-${{ matrix.arch }}.tar.xz *

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ bin/
1717

1818
# DUB
1919
.dub
20+
dub.*.json
2021
docs.json
2122
__dummy.html
2223
docs/

dub.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"authors": [
88
"Matheus Catarino França"
99
],
10+
"dependencies": {
11+
"requests": "~>2.2.0"
12+
},
1013
"dflags": [
1114
"-preview=dip1000",
1215
"-preview=dip1008",
@@ -44,4 +47,4 @@
4447
]
4548
}
4649
}
47-
}
50+
}

source/impl.d

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module impl;
22

3+
import requests;
4+
35
public import std;
46

57
enum OS : string
@@ -147,7 +149,8 @@ public:
147149
auto rootPath = environment.get("LDC2_PATH", compilerPath);
148150
log("Installing redub to %s", rootPath);
149151

150-
version (AArch64) currentArch = Arch.arm64;
152+
version (AArch64)
153+
currentArch = Arch.arm64;
151154

152155
string redubFile;
153156
if (currentOS == OS.freebsd)
@@ -502,10 +505,17 @@ public:
502505

503506
try
504507
{
505-
auto response = get(url);
508+
auto rq = Request();
509+
version (Windows)
510+
rq.sslSetCaCert(environment.get("CURL_CA_BUNDLE"));
511+
else
512+
rq.sslSetVerifyPeer(false);
513+
auto res = rq.get(url);
514+
enforce(res.code / 100 == 2, format("HTTP request returned status code %s", res.code));
515+
string response = cast(string) res.responseBody.data;
506516
string dversion = releaseType == ReleaseType.nightly ?
507517
response.split("<id>tag:github.com,2008:Grit::Commit/")[1].split(
508-
"</id>")[0][0 .. 8].to!string : response.to!string.strip;
518+
"</id>")[0][0 .. 8].to!string : response.strip;
509519
log("Resolved %s version: ldc2-%s", releaseType, dversion);
510520
return "ldc2-" ~ dversion;
511521
}
@@ -538,26 +548,30 @@ public:
538548
throw new Exception("Unknown compiler: %s".format(compilerSpec));
539549
}
540550

541-
private void download(ref string url, string fileName) @trusted
551+
private void download(string url, string fileName) @trusted
542552
{
543553
log("Downloading from URL: " ~ url);
544-
auto buf = appender!(ubyte[])();
545-
size_t contentLength;
546-
547-
auto http = HTTP(url); // unsafe/@system (need libcurl)
548-
http.method = HTTP.Method.get;
549-
http.onReceiveHeader((in k, in v) {
550-
if (k == "content-length")
551-
contentLength = to!size_t(v);
552-
});
554+
auto rq = Request();
555+
rq.useStreaming = true;
556+
version (Windows)
557+
rq.sslSetCaCert(environment.get("CURL_CA_BUNDLE"));
558+
else
559+
rq.sslSetVerifyPeer(false);
560+
auto res = rq.get(url);
561+
enforce(res.code / 100 == 2, format("HTTP request returned status code %s", res.code));
562+
size_t contentLength = res.contentLength;
553563

554-
// Progress bar
564+
auto file = File(fileName, "wb");
565+
size_t received = 0;
555566
int barWidth = 50;
556-
http.onReceive((data) {
557-
buf.put(data);
567+
568+
foreach (ubyte[] data; res.receiveAsRange())
569+
{
570+
file.rawWrite(data);
571+
received += data.length;
558572
if (contentLength > 0)
559573
{
560-
float progress = cast(float) buf.data.length / contentLength;
574+
float progress = cast(float) received / contentLength;
561575
int pos = cast(int)(barWidth * progress);
562576

563577
write("\r[");
@@ -570,23 +584,13 @@ public:
570584
else
571585
write(" ");
572586
}
587+
573588
writef("] %d%%", cast(int)(progress * 100));
574589
stdout.flush();
575590
}
576-
return data.length;
577-
});
578-
579-
http.dataTimeout = dur!"msecs"(0);
580-
http.perform();
581-
immutable sc = http.statusLine().code;
582-
enforce(sc / 100 == 2 || sc == 302,
583-
format("HTTP request returned status code %s", sc));
584-
log("\nDownload complete");
585-
586-
auto file = File(fileName, "wb");
587-
scope (success)
588-
file.close();
589-
file.rawWrite(buf.data);
591+
}
592+
writeln();
593+
log("Download complete");
590594
}
591595

592596
void downloadAndExtract(string url, string targetPath) @safe
@@ -661,14 +665,21 @@ public:
661665
int page = 1;
662666
while (true)
663667
{
664-
auto response = get(format("%s?per_page=100&page=%s", baseURL, page++));
665-
auto json = parseJSON(response).array;
668+
auto rq = Request();
669+
version (Windows)
670+
rq.sslSetCaCert(environment.get("CURL_CA_BUNDLE"));
671+
else
672+
rq.sslSetVerifyPeer(false);
673+
auto res = rq.get(format("%s?per_page=100&page=%s", baseURL, page++));
674+
enforce(res.code / 100 == 2, format("HTTP request returned status code %s", res.code));
675+
auto json = parseJSON(cast(string) res.responseBody.data).array;
666676
if (json.empty)
667677
break;
668678
results ~= json;
669679
if (json.length < 100)
670680
break;
671681
}
682+
672683
return results;
673684
}
674685

0 commit comments

Comments
 (0)