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