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 	/// Check if the resource exists and is downloadable.
40 	/// E.g. the HTTP status code for a HEAD request should be 200.
41 	bool urlOK(string url)
42 	{
43 		notImplemented();
44 		assert(false);
45 	}
46 
47 	/// Get the destination of an HTTP redirect.
48 	string resolveRedirect(string url)
49 	{
50 		notImplemented();
51 		assert(false);
52 	}
53 
54 	private final void notImplemented()
55 	{
56 		assert(false, "Not implemented or Network implementation not set");
57 	}
58 }
59 
60 /// The instance of the selected Network implementation.
61 Network net;
62 
63 static this()
64 {
65 	assert(!net);
66 	net = new Network();
67 }
68 
69 /// UFCS-able global synonym functions.
70 void downloadFile(string url, string target) { net.downloadFile(url, target); }
71 void[] getFile(string url) { return net.getFile(url); } /// ditto
72 bool urlOK(string url) { return net.urlOK(url); } /// ditto
73 string resolveRedirect(string url) { return net.resolveRedirect(url); } /// ditto