1 module hunt.http.HttpResponse;
2 
3 import hunt.http.HttpField;
4 import hunt.http.HttpFields;
5 import hunt.http.HttpHeader;
6 import hunt.http.HttpMetaData;
7 import hunt.http.HttpScheme;
8 import hunt.http.HttpVersion;
9 
10 import hunt.collection;
11 import hunt.Functions;
12 import hunt.Exceptions;
13 import hunt.logging;
14 import hunt.net.util.HttpURI;
15 import hunt.text.Common;
16 import hunt.util.StringBuilder;
17 import hunt.util.Common;
18 import hunt.util.MimeType;
19 
20 import std.ascii;
21 import std.conv;
22 import std.format;
23 import std.range;
24 import std.traits;
25 
26 /**
27  * 
28  */
29 class HttpResponse : HttpMetaData {
30     protected int _status;
31     protected string _reason;
32 
33     this() {
34         this(HttpVersion.Null, 0, new HttpFields());
35     }
36 
37     this(HttpVersion ver, int status, HttpFields fields) {
38         this(ver, status, fields, long.min);
39     }
40 
41     this(HttpVersion ver, int status, HttpFields fields, long contentLength) {
42         super(ver, fields, contentLength);
43         _status = status;
44     }
45 
46     this(HttpVersion ver, int status, string reason, HttpFields fields, long contentLength) {
47         super(ver, fields, contentLength);
48         _reason = reason;
49         _status = status;
50     }
51 
52     override bool isResponse() {
53         return true;
54     }
55 
56     /**
57      * @return the HTTP status
58      */
59     int getStatus() {
60         return _status;
61     }
62 
63     /**
64     * @return the HTTP reason
65     */
66     string getReason() {
67         return _reason;
68     }
69 
70     /**
71     * @param status the HTTP status to set
72     */
73     void setStatus(int status) {
74         _status = status;
75     }
76 
77     /**
78     * @param reason the HTTP reason to set
79     */
80     void setReason(string reason) {
81         _reason = reason;
82     }
83 
84     HttpResponse header(T)(string header, T value) {
85         getFields().put(header, value);
86         return this;
87     }
88 
89     HttpResponse header(T)(HttpHeader header, T value) {
90         getFields().put(header, value);
91         return this;
92     }    
93 
94     HttpResponse headers(T = string)(T[string] value) {
95         foreach (string k, T v; value) {
96             getFields().add(k, v);
97         }
98         return this;
99     }
100 
101     override HttpFields headers() {
102         return super.headers();
103     }
104 
105     override string[] headers(string name) {
106         return super.headers(name);
107     }
108 
109     override string[] headers(HttpHeader header) {
110         return super.headers(header);
111     }
112 
113     
114     /**
115     * Returns true if the code is in [200..300), which means the request was successfully received,
116     * understood, and accepted.
117     */
118     bool isSuccessful() {
119         return _status >= 200 && _status < 300;
120     }
121 
122     override string toString() {
123         HttpFields fields = getFields();
124         return format("%s{s=%d,h=%d,cl=%d}", getHttpVersion(), getStatus(), 
125             fields is null ? -1 : fields.size(), getContentLength());
126     }
127 
128 // dmftoff helper api
129     alias code = getStatus;
130     alias message = getReason;
131     
132     alias withHeader = header;
133     alias withHeaders = headers;
134 
135     deprecated("Using header instead.")
136     alias setHeader = header;
137 // dmfton
138 
139 }