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 void[] post(string url, in void[] data) 37 { 38 return .post!ubyte(url, data); 39 } 40 41 override bool urlOK(string url) 42 { 43 try 44 { 45 auto http = HTTP(url); 46 http.method = HTTP.Method.head; 47 http.perform(); 48 return http.statusLine.code == 200; // OK 49 } 50 catch (Exception e) 51 return false; 52 } 53 54 override string resolveRedirect(string url) 55 { 56 string result = null; 57 58 auto http = HTTP(url); 59 http.method = HTTP.Method.head; 60 http.onReceiveHeader = 61 (in char[] key, in char[] value) 62 { 63 if (icmp(key, "Location")==0) 64 { 65 result = value.idup; 66 if (result) 67 result = url.applyRelativeURL(result); 68 } 69 }; 70 http.perform(); 71 72 return result; 73 } 74 } 75 76 static this() 77 { 78 net = new CurlNetwork(); 79 }