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