11module impl ;
22
3+ import requests;
4+
35public import std;
46
57enum 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(" \n Download 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