1 /** 2 * Abstract interface for basic network operations. 3 * Import ae.sys.net.* to select an implementation. 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; 16 17 import std.functional; 18 import std.path; 19 20 import ae.net.ietf.url; 21 import ae.sys.file; 22 23 class Network 24 { 25 /// Download file located at the indicated URL, 26 /// unless the target file already exists. 27 void downloadFile(string url, string target) 28 { 29 notImplemented(); 30 } 31 32 /// Get resource located at the indicated URL. 33 void[] getFile(string url) 34 { 35 notImplemented(); 36 assert(false); 37 } 38 39 /// Post data to the specified URL. 40 // TODO: Content-Type? 41 void[] post(string url, in void[] data) 42 { 43 notImplemented(); 44 assert(false); 45 } 46 47 /// Check if the resource exists and is downloadable. 48 /// E.g. the HTTP status code for a HEAD request should be 200. 49 bool urlOK(string url) 50 { 51 notImplemented(); 52 assert(false); 53 } 54 55 /// Get the destination of an HTTP redirect. 56 string resolveRedirect(string url) 57 { 58 notImplemented(); 59 assert(false); 60 } 61 62 private final void notImplemented() 63 { 64 assert(false, "Not implemented or Network implementation not set"); 65 } 66 } 67 68 /// The instance of the selected Network implementation. 69 Network net; 70 71 static this() 72 { 73 assert(!net); 74 net = new Network(); 75 } 76 77 /// UFCS-able global synonym functions. 78 void downloadFile(string url, string target) { net.downloadFile(url, target); } 79 void[] getFile(string url) { return net.getFile(url); } /// ditto 80 void[] post(string url, void[] data) { return net.post(url, data); } 81 bool urlOK(string url) { return net.urlOK(url); } /// ditto 82 string resolveRedirect(string url) { return net.resolveRedirect(url); } /// ditto