forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResult.cs
More file actions
247 lines (199 loc) · 7.87 KB
/
HttpResult.cs
File metadata and controls
247 lines (199 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#if !SILVERLIGHT
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using ServiceStack.Service;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
namespace ServiceStack.Common.Web
{
public class HttpResult
: IHttpResult, IStreamWriter
{
public HttpResult()
: this((object)null, null)
{
}
public HttpResult(object response)
: this(response, null)
{
}
public HttpResult(object response, string contentType)
: this(response, contentType, HttpStatusCode.OK)
{
}
public HttpResult(HttpStatusCode statusCode, string statusDescription)
: this()
{
StatusCode = statusCode;
StatusDescription = statusDescription;
}
public HttpResult(object response, HttpStatusCode statusCode)
: this(response, null, statusCode) { }
public HttpResult(object response, string contentType, HttpStatusCode statusCode)
{
this.Headers = new Dictionary<string, string>();
this.ResponseFilter = HttpResponseFilter.Instance;
this.Response = response;
this.ContentType = contentType;
this.StatusCode = statusCode;
}
public HttpResult(FileInfo fileResponse)
: this(fileResponse, false, MimeTypes.GetMimeType(fileResponse.Name)) { }
public HttpResult(FileInfo fileResponse, bool asAttachment)
: this(fileResponse, asAttachment, MimeTypes.GetMimeType(fileResponse.Name)) { }
public HttpResult(FileInfo fileResponse, bool asAttachment, string contentType)
: this(null, contentType, HttpStatusCode.OK)
{
this.FileInfo = fileResponse;
if (!asAttachment) return;
var headerValue =
"attachment; " +
"filename=\"" + fileResponse.Name + "\"; " +
"size=" + fileResponse.Length + "; " +
"creation-date=" + fileResponse.CreationTimeUtc.ToString("R").Replace(",", "") + "; " +
"modification-date=" + fileResponse.LastWriteTimeUtc.ToString("R").Replace(",", "") + "; " +
"read-date=" + fileResponse.LastAccessTimeUtc.ToString("R").Replace(",", "");
this.Headers = new Dictionary<string, string> {
{ HttpHeaders.ContentDisposition, headerValue },
};
}
public HttpResult(Stream responseStream, string contentType)
: this(null, contentType, HttpStatusCode.OK)
{
this.ResponseStream = responseStream;
}
public HttpResult(string responseText, string contentType)
: this(null, contentType, HttpStatusCode.OK)
{
this.ResponseText = responseText;
}
public string ResponseText { get; private set; }
public Stream ResponseStream { get; private set; }
public FileInfo FileInfo { get; private set; }
public string ContentType { get; set; }
public Dictionary<string, string> Headers { get; private set; }
public DateTime LastModified
{
set
{
this.Headers[HttpHeaders.LastModified] = value.ToUniversalTime().ToString("r");
}
}
public string Location
{
set
{
if (StatusCode == HttpStatusCode.OK)
StatusCode = HttpStatusCode.Redirect;
this.Headers[HttpHeaders.Location] = value;
}
}
public void SetPermanentCookie(string name, string value)
{
SetCookie(name, value, DateTime.UtcNow.AddYears(20), null);
}
public void SetPermanentCookie(string name, string value, string path)
{
SetCookie(name, value, DateTime.UtcNow.AddYears(20), path);
}
public void SetSessionCookie(string name, string value)
{
SetSessionCookie(name, value, null);
}
public void SetSessionCookie(string name, string value, string path)
{
path = path ?? "/";
this.Headers[HttpHeaders.SetCookie] = string.Format("{0}={1};path=" + path, name, value);
}
public void SetCookie(string name, string value, TimeSpan expiresIn, string path)
{
var expiresAt = DateTime.UtcNow.Add(expiresIn);
SetCookie(name, value, expiresAt, path);
}
public void SetCookie(string name, string value, DateTime expiresAt, string path)
{
path = path ?? "/";
var cookie = string.Format("{0}={1};expires={2};path={3}", name, value, expiresAt.ToString("R"), path);
this.Headers[HttpHeaders.SetCookie] = cookie;
}
public void DeleteCookie(string name)
{
var cookie = string.Format("{0}=;expires={1};path=/", name, DateTime.UtcNow.AddDays(-1).ToString("R"));
this.Headers[HttpHeaders.SetCookie] = cookie;
}
public IDictionary<string, string> Options
{
get { return this.Headers; }
}
public int Status { get; set; }
public HttpStatusCode StatusCode
{
get { return (HttpStatusCode) Status; }
set { Status = (int) value; }
}
public string StatusDescription { get; set; }
public object Response { get; set; }
public IContentTypeWriter ResponseFilter { get; set; }
public IRequestContext RequestContext { get; set; }
public string View { get; set; }
public string Template { get; set; }
public void WriteTo(Stream responseStream)
{
if (this.FileInfo != null)
{
using (var fs = this.FileInfo.OpenRead())
{
fs.WriteTo(responseStream);
responseStream.Flush();
}
return;
}
if (this.ResponseStream != null)
{
this.ResponseStream.WriteTo(responseStream);
responseStream.Flush();
try
{
this.ResponseStream.Dispose();
}
catch { /*ignore*/ }
return;
}
if (this.ResponseText != null)
{
var bytes = System.Text.Encoding.UTF8.GetBytes(this.ResponseText);
responseStream.Write(bytes, 0, bytes.Length);
responseStream.Flush();
return;
}
if (this.ResponseFilter == null)
throw new ArgumentNullException("ResponseFilter");
if (this.RequestContext == null)
throw new ArgumentNullException("RequestContext");
var bytesResponse = this.Response as byte[];
if (bytesResponse != null)
{
responseStream.Write(bytesResponse, 0, bytesResponse.Length);
return;
}
if (View != null)
RequestContext.SetItem("View", View);
if (Template != null)
RequestContext.SetItem("Template", Template);
ResponseFilter.SerializeToStream(this.RequestContext, this.Response, responseStream);
}
public static HttpResult Status201Created(object response, string newLocationUri)
{
return new HttpResult(response) {
StatusCode = HttpStatusCode.Created,
Headers =
{
{ HttpHeaders.Location, newLocationUri },
}
};
}
}
}
#endif