1 /** 2 * ae.sys.net implementation using std.net.curl 3 * Note: std.net.curl requires libcurl. 4 * 5 * License: 6 * This Source Code Form is subject to the terms of 7 * the Mozilla Public License, v. 2.0. If a copy of 8 * the MPL was not distributed with this file, You 9 * can obtain one at http://mozilla.org/MPL/2.0/. 10 * 11 * Authors: 12 * Vladimir Panteleev <vladimir@thecybershadow.net> 13 */ 14 15 module ae.sys.net.curl; 16 17 import std.file; 18 import std.net.curl; 19 import std.string; 20 21 import ae.net.ietf.url; 22 import ae.sys.net; 23 24 class CurlNetwork : Network 25 { 26 override void downloadFile(string url, string target) 27 { 28 std.file.write(target, getFile(url)); 29 } 30 31 override void[] getFile(string url) 32 { 33 return get!(AutoProtocol, ubyte)(url); 34 } 35 36 override bool urlOK(string url) 37 { 38 try 39 { 40 auto http = HTTP(url); 41 http.method = HTTP.Method.head; 42 http.perform(); 43 return http.statusLine.code == 200; // OK 44 } 45 catch (Exception e) 46 return false; 47 } 48 49 override string resolveRedirect(string url) 50 { 51 string result = null; 52 53 auto http = HTTP(url); 54 http.method = HTTP.Method.head; 55 http.onReceiveHeader = 56 (in char[] key, in char[] value) 57 { 58 if (icmp(key, "Location")==0) 59 { 60 result = value.idup; 61 if (result) 62 result = url.applyRelativeURL(result); 63 } 64 }; 65 http.perform(); 66 67 return result; 68 } 69 } 70 71 static this() 72 { 73 net = new CurlNetwork(); 74 }