clockwerk-opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | /* |
2 | * Copyright (c) Contributors, http://opensimulator.org/ |
||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions are met: |
||
7 | * * Redistributions of source code must retain the above copyright |
||
8 | * notice, this list of conditions and the following disclaimer. |
||
9 | * * Redistributions in binary form must reproduce the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer in the |
||
11 | * documentation and/or other materials provided with the distribution. |
||
12 | * * Neither the name of the OpenSimulator Project nor the |
||
13 | * names of its contributors may be used to endorse or promote products |
||
14 | * derived from this software without specific prior written permission. |
||
15 | * |
||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY |
||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY |
||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
26 | */ |
||
27 | |||
28 | using System.IO; |
||
29 | using System.Net; |
||
30 | using System.Text; |
||
31 | using HttpServer; |
||
32 | |||
33 | namespace OpenSim.Framework.Servers.HttpServer |
||
34 | { |
||
35 | /// <summary> |
||
36 | /// OSHttpResponse is the OpenSim representation of an HTTP |
||
37 | /// response. |
||
38 | /// </summary> |
||
39 | public class OSHttpResponse : IOSHttpResponse |
||
40 | { |
||
41 | /// <summary> |
||
42 | /// Content type property. |
||
43 | /// </summary> |
||
44 | /// <remarks> |
||
45 | /// Setting this property will also set IsContentTypeSet to |
||
46 | /// true. |
||
47 | /// </remarks> |
||
48 | public virtual string ContentType |
||
49 | { |
||
50 | get |
||
51 | { |
||
52 | return _httpResponse.ContentType; |
||
53 | } |
||
54 | |||
55 | set |
||
56 | { |
||
57 | _httpResponse.ContentType = value; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /// <summary> |
||
62 | /// Boolean property indicating whether the content type |
||
63 | /// property actively has been set. |
||
64 | /// </summary> |
||
65 | /// <remarks> |
||
66 | /// IsContentTypeSet will go away together with .NET base. |
||
67 | /// </remarks> |
||
68 | // public bool IsContentTypeSet |
||
69 | // { |
||
70 | // get { return _contentTypeSet; } |
||
71 | // } |
||
72 | // private bool _contentTypeSet; |
||
73 | |||
74 | |||
75 | /// <summary> |
||
76 | /// Length of the body content; 0 if there is no body. |
||
77 | /// </summary> |
||
78 | public long ContentLength |
||
79 | { |
||
80 | get |
||
81 | { |
||
82 | return _httpResponse.ContentLength; |
||
83 | } |
||
84 | |||
85 | set |
||
86 | { |
||
87 | _httpResponse.ContentLength = value; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /// <summary> |
||
92 | /// Alias for ContentLength. |
||
93 | /// </summary> |
||
94 | public long ContentLength64 |
||
95 | { |
||
96 | get { return ContentLength; } |
||
97 | set { ContentLength = value; } |
||
98 | } |
||
99 | |||
100 | /// <summary> |
||
101 | /// Encoding of the body content. |
||
102 | /// </summary> |
||
103 | public Encoding ContentEncoding |
||
104 | { |
||
105 | get |
||
106 | { |
||
107 | return _httpResponse.Encoding; |
||
108 | } |
||
109 | |||
110 | set |
||
111 | { |
||
112 | _httpResponse.Encoding = value; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | public bool KeepAlive |
||
117 | { |
||
118 | get |
||
119 | { |
||
120 | return _httpResponse.Connection == ConnectionType.KeepAlive; |
||
121 | } |
||
122 | |||
123 | set |
||
124 | { |
||
125 | if (value) |
||
126 | _httpResponse.Connection = ConnectionType.KeepAlive; |
||
127 | else |
||
128 | _httpResponse.Connection = ConnectionType.Close; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /// <summary> |
||
133 | /// Get or set the keep alive timeout property (default is |
||
134 | /// 20). Setting this to 0 also disables KeepAlive. Setting |
||
135 | /// this to something else but 0 also enable KeepAlive. |
||
136 | /// </summary> |
||
137 | public int KeepAliveTimeout |
||
138 | { |
||
139 | get |
||
140 | { |
||
141 | return _httpResponse.KeepAlive; |
||
142 | } |
||
143 | |||
144 | set |
||
145 | { |
||
146 | if (value == 0) |
||
147 | { |
||
148 | _httpResponse.Connection = ConnectionType.Close; |
||
149 | _httpResponse.KeepAlive = 0; |
||
150 | } |
||
151 | else |
||
152 | { |
||
153 | _httpResponse.Connection = ConnectionType.KeepAlive; |
||
154 | _httpResponse.KeepAlive = value; |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /// <summary> |
||
160 | /// Return the output stream feeding the body. |
||
161 | /// </summary> |
||
162 | /// <remarks> |
||
163 | /// On its way out... |
||
164 | /// </remarks> |
||
165 | public Stream OutputStream |
||
166 | { |
||
167 | get |
||
168 | { |
||
169 | return _httpResponse.Body; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | public string ProtocolVersion |
||
174 | { |
||
175 | get |
||
176 | { |
||
177 | return _httpResponse.ProtocolVersion; |
||
178 | } |
||
179 | |||
180 | set |
||
181 | { |
||
182 | _httpResponse.ProtocolVersion = value; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /// <summary> |
||
187 | /// Return the output stream feeding the body. |
||
188 | /// </summary> |
||
189 | public Stream Body |
||
190 | { |
||
191 | get |
||
192 | { |
||
193 | return _httpResponse.Body; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /// <summary> |
||
198 | /// Set a redirct location. |
||
199 | /// </summary> |
||
200 | public string RedirectLocation |
||
201 | { |
||
202 | // get { return _redirectLocation; } |
||
203 | set |
||
204 | { |
||
205 | _httpResponse.Redirect(value); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | |||
210 | /// <summary> |
||
211 | /// Chunk transfers. |
||
212 | /// </summary> |
||
213 | public bool SendChunked |
||
214 | { |
||
215 | get |
||
216 | { |
||
217 | return _httpResponse.Chunked; |
||
218 | } |
||
219 | |||
220 | set |
||
221 | { |
||
222 | _httpResponse.Chunked = value; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /// <summary> |
||
227 | /// HTTP status code. |
||
228 | /// </summary> |
||
229 | public virtual int StatusCode |
||
230 | { |
||
231 | get |
||
232 | { |
||
233 | return (int)_httpResponse.Status; |
||
234 | } |
||
235 | |||
236 | set |
||
237 | { |
||
238 | _httpResponse.Status = (HttpStatusCode)value; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | |||
243 | /// <summary> |
||
244 | /// HTTP status description. |
||
245 | /// </summary> |
||
246 | public string StatusDescription |
||
247 | { |
||
248 | get |
||
249 | { |
||
250 | return _httpResponse.Reason; |
||
251 | } |
||
252 | |||
253 | set |
||
254 | { |
||
255 | _httpResponse.Reason = value; |
||
256 | } |
||
257 | } |
||
258 | |||
259 | public bool ReuseContext |
||
260 | { |
||
261 | get |
||
262 | { |
||
263 | if (_httpClientContext != null) |
||
264 | { |
||
265 | return !_httpClientContext.EndWhenDone; |
||
266 | } |
||
267 | return true; |
||
268 | } |
||
269 | set |
||
270 | { |
||
271 | if (_httpClientContext != null) |
||
272 | { |
||
273 | _httpClientContext.EndWhenDone = !value; |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | |||
278 | protected IHttpResponse _httpResponse; |
||
279 | private IHttpClientContext _httpClientContext; |
||
280 | |||
281 | public OSHttpResponse() {} |
||
282 | |||
283 | public OSHttpResponse(IHttpResponse resp) |
||
284 | { |
||
285 | _httpResponse = resp; |
||
286 | } |
||
287 | |||
288 | /// <summary> |
||
289 | /// Instantiate an OSHttpResponse object from an OSHttpRequest |
||
290 | /// object. |
||
291 | /// </summary |
||
292 | /// <param name="req">Incoming OSHttpRequest to which we are |
||
293 | /// replying</param> |
||
294 | public OSHttpResponse(OSHttpRequest req) |
||
295 | { |
||
296 | _httpResponse = new HttpResponse(req.IHttpClientContext, req.IHttpRequest); |
||
297 | _httpClientContext = req.IHttpClientContext; |
||
298 | } |
||
299 | public OSHttpResponse(HttpResponse resp, IHttpClientContext clientContext) |
||
300 | { |
||
301 | _httpResponse = resp; |
||
302 | _httpClientContext = clientContext; |
||
303 | } |
||
304 | |||
305 | /// <summary> |
||
306 | /// Add a header field and content to the response. |
||
307 | /// </summary> |
||
308 | /// <param name="key">string containing the header field |
||
309 | /// name</param> |
||
310 | /// <param name="value">string containing the header field |
||
311 | /// value</param> |
||
312 | public void AddHeader(string key, string value) |
||
313 | { |
||
314 | _httpResponse.AddHeader(key, value); |
||
315 | } |
||
316 | |||
317 | /// <summary> |
||
318 | /// Send the response back to the remote client |
||
319 | /// </summary> |
||
320 | public void Send() |
||
321 | { |
||
322 | _httpResponse.Body.Flush(); |
||
323 | _httpResponse.Send(); |
||
324 | } |
||
325 | |||
326 | public void FreeContext() |
||
327 | { |
||
328 | if (_httpClientContext != null) |
||
329 | _httpClientContext.Close(); |
||
330 | } |
||
331 | } |
||
332 | } |