1 module requests.base;
2 
3 import requests.streams;
4 import requests.utils;
5 import requests.uri;
6 
7 public interface Auth {
8     string[string] authHeaders(string domain);
9     string         userName();
10     string         password();
11 }
12 
13 public class RequestException: Exception {
14     this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null) @safe pure nothrow {
15         super(msg, file, line, next);
16     }
17 }
18 
19 public struct ReceiveAsRange {
20     bool empty() {
21         return data.length == 0;
22     };
23     ubyte[] front() {
24         return data;
25     };
26     void popFront() {
27         if ( read ) {
28             // get new portion
29             data = read();
30         } else {
31             // we can't read any new data
32             data.length = 0;
33         }
34     };
35     package {
36         bool            activated;
37         ubyte[]         data;
38         ubyte[]         delegate() read;
39     }
40 }
41 
42 public class Response {
43     package {
44         ushort           _code;
45         Buffer!ubyte     _responseBody;
46         string[string]   _responseHeaders;
47         /// Initial URI
48         URI              _uri;
49         /// Final URI. Can differ from __URI if request go through redirections.
50         URI              _finalURI;
51         ReceiveAsRange   _receiveAsRange;
52         mixin(Setter!ushort("code"));
53         mixin(Setter!URI("uri"));
54         mixin(Setter!URI("finalURI"));
55     }
56     mixin(Getter!ushort("code"));
57     mixin(Getter!URI("uri"));
58     mixin(Getter!URI("finalURI"));
59     @property auto ref responseBody() @safe nothrow {
60         return _responseBody;
61     }
62     @property auto ref responseHeaders() pure @safe nothrow {
63         return _responseHeaders;
64     }
65     @property auto ref receiveAsRange() pure @safe nothrow {
66         return _receiveAsRange;
67     }
68 }