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