vibe.inet.urltransfer 0/0(100%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
/** Downloading and uploading of data from/to URLs. Note that this module is scheduled for deprecation and will be replaced by another module in the future. All functions are defined as templates to avoid this dependency issue when building the library. Copyright: © 2012-2015 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ module vibe.inet.urltransfer; import vibe.core.log; import vibe.core.file; import vibe.inet.url; import vibe.core.stream; import vibe.internal.interfaceproxy : asInterface; import std.exception; import std.string; /** Downloads a file from the specified URL. Any redirects will be followed until the actual file resource is reached or if the redirection limit of 10 is reached. Note that only HTTP(S) is currently supported. */ void download(HTTPClient_ = void*)(URL url, scope void delegate(scope InputStream) callback, HTTPClient_ client_ = null) { import vibe.http.client; assert(url.username.length == 0 && url.password.length == 0, "Auth not supported yet."); assert(url.schema == "http" || url.schema == "https", "Only http(s):// supported for now."); HTTPClient client; static if (is(HTTPClient_ == HTTPClient)) client = client_; if (!client) client = new HTTPClient(); scope (exit) { if (client_ !is null) client.disconnect(); } if (!url.port) url.port = url.defaultPort; foreach( i; 0 .. 10 ){ client.connect(url.host, url.port, url.schema == "https"); logTrace("connect to %s", url.host); bool done = false; client.request( (scope HTTPClientRequest req) { req.requestURL = url.localURI; logTrace("REQUESTING %s!", req.requestURL); }, (scope HTTPClientResponse res) { logTrace("GOT ANSWER!"); switch( res.statusCode ){ default: throw new HTTPStatusException(res.statusCode, "Server responded with "~httpStatusText(res.statusCode)~" for "~url.toString()); case HTTPStatus.OK: done = true; callback(res.bodyReader.asInterface!InputStream); break; case HTTPStatus.movedPermanently: case HTTPStatus.found: case HTTPStatus.seeOther: case HTTPStatus.temporaryRedirect: logTrace("Status code: %s", res.statusCode); auto pv = "Location" in res.headers; enforce(pv !is null, "Server responded with redirect but did not specify the redirect location for "~url.toString()); logDebug("Redirect to '%s'", *pv); if( startsWith((*pv), "http:") || startsWith((*pv), "https:") ){ logTrace("parsing %s", *pv); auto nurl = URL(*pv); if (!nurl.port) nurl.port = nurl.defaultPort; if (url.host != nurl.host || url.schema != nurl.schema || url.port != nurl.port) client.disconnect(); url = nurl; } else url.localURI = *pv; break; } } ); if (done) return; } enforce(false, "Too many redirects!"); assert(false); } /// ditto void download(HTTPClient_ = void*)(string url, scope void delegate(scope InputStream) callback, HTTPClient_ client_ = null) { download(URL(url), callback, client_); } /// ditto void download()(string url, string filename) { download(url, (scope input){ auto fil = openFile(filename, FileMode.createTrunc); scope(exit) fil.close(); fil.write(input); }); } /// ditto void download()(URL url, Path filename) { download(url.toString(), filename.toNativeString()); }