OSHttpResponse.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 OpenSim 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. using System;
  28. using System.Collections;
  29. using System.IO;
  30. using System.Net;
  31. using System.Text;
  32. using HttpServer;
  33. namespace OpenSim.Framework.Servers
  34. {
  35. /// <summary>
  36. /// OSHttpResponse is the OpenSim representation of an HTTP
  37. /// response.
  38. /// </summary>
  39. /// <remarks>
  40. /// OSHttpResponse is currently dual "homed" in that it support
  41. /// both the .NET HttpListenerResponse and the HttpServer
  42. /// HttpResponse (similar to OSHttpRequest); this duality is only
  43. /// temporary and the .NET usage will disappear once the switch to
  44. /// HttpServer is completed.
  45. /// </remarks>
  46. public class OSHttpResponse
  47. {
  48. // property code below is a bit messy, will all resolve to
  49. // harmony once we've completed the switch
  50. /// <summary>
  51. /// Content type property.
  52. /// </summary>
  53. /// <remarks>
  54. /// Setting this property will also set IsContentTypeSet to
  55. /// true.
  56. /// </remarks>
  57. public string ContentType
  58. {
  59. get
  60. {
  61. if (HttpServer)
  62. return _httpResponse.ContentType;
  63. else
  64. return _httpListenerResponse.ContentType;
  65. }
  66. set
  67. {
  68. if (HttpServer)
  69. {
  70. _httpResponse.ContentType = value;
  71. }
  72. else
  73. {
  74. _httpListenerResponse.ContentType = value;
  75. _contentTypeSet = true;
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// Boolean property indicating whether the content type
  81. /// property actively has been set.
  82. /// </summary>
  83. /// <remarks>
  84. /// IsContentTypeSet will go away together with .NET base.
  85. /// </remarks>
  86. public bool IsContentTypeSet
  87. {
  88. get { return _contentTypeSet; }
  89. }
  90. private bool _contentTypeSet;
  91. /// <summary>
  92. /// Length of the body content; 0 if there is no body.
  93. /// </summary>
  94. public long ContentLength
  95. {
  96. get
  97. {
  98. if (HttpServer)
  99. return _httpResponse.ContentLength;
  100. else
  101. return _httpListenerResponse.ContentLength64;
  102. }
  103. set
  104. {
  105. if (HttpServer)
  106. _httpResponse.ContentLength = value;
  107. else
  108. _httpListenerResponse.ContentLength64 = value;
  109. }
  110. }
  111. /// <summary>
  112. /// Alias for ContentLength.
  113. /// </summary>
  114. public long ContentLength64
  115. {
  116. get { return ContentLength; }
  117. set { ContentLength = value; }
  118. }
  119. /// <summary>
  120. /// Encoding of the body content.
  121. /// </summary>
  122. public Encoding ContentEncoding
  123. {
  124. get
  125. {
  126. if (HttpServer)
  127. return _httpResponse.Encoding;
  128. else
  129. return _httpListenerResponse.ContentEncoding;
  130. }
  131. set
  132. {
  133. if (HttpServer)
  134. _httpResponse.Encoding = value;
  135. else
  136. _httpListenerResponse.ContentEncoding = value;
  137. }
  138. }
  139. /// <summary>
  140. /// Headers of the response.
  141. /// </summary>
  142. public WebHeaderCollection Headers
  143. {
  144. get
  145. {
  146. if (HttpServer)
  147. return null;
  148. else
  149. return _httpListenerResponse.Headers;
  150. }
  151. }
  152. /// <summary>
  153. /// Get or set the keep alive property.
  154. /// </summary>
  155. public bool KeepAlive
  156. {
  157. get
  158. {
  159. if (HttpServer)
  160. return _httpResponse.Connection == ConnectionType.KeepAlive;
  161. else
  162. return _httpListenerResponse.KeepAlive;
  163. }
  164. set
  165. {
  166. if (HttpServer)
  167. _httpResponse.Connection = ConnectionType.KeepAlive;
  168. else
  169. _httpListenerResponse.KeepAlive = value;
  170. }
  171. }
  172. /// <summary>
  173. /// Return the output stream feeding the body.
  174. /// </summary>
  175. /// <remarks>
  176. /// On its way out...
  177. /// </remarks>
  178. public Stream OutputStream
  179. {
  180. get
  181. {
  182. if (HttpServer)
  183. return _httpResponse.Body;
  184. else
  185. return _httpListenerResponse.OutputStream;
  186. }
  187. }
  188. /// <summary>
  189. /// Return the output stream feeding the body.
  190. /// </summary>
  191. public Stream Body
  192. {
  193. get
  194. {
  195. if (HttpServer)
  196. return _httpResponse.Body;
  197. throw new Exception("[OSHttpResponse] mixed .NET and HttpServer access");
  198. }
  199. }
  200. /// <summary>
  201. /// Set a redirct location.
  202. /// </summary>
  203. public string RedirectLocation
  204. {
  205. // get { return _redirectLocation; }
  206. set
  207. {
  208. if (HttpServer)
  209. _httpResponse.Redirect(value);
  210. else
  211. _httpListenerResponse.RedirectLocation = value;
  212. }
  213. }
  214. /// <summary>
  215. /// Chunk transfers.
  216. /// </summary>
  217. public bool SendChunked
  218. {
  219. get
  220. {
  221. if (HttpServer)
  222. return _httpResponse.Chunked;
  223. else
  224. return _httpListenerResponse.SendChunked;
  225. }
  226. set
  227. {
  228. if (HttpServer)
  229. _httpResponse.Chunked = value;
  230. else
  231. _httpListenerResponse.SendChunked = value;
  232. }
  233. }
  234. /// <summary>
  235. /// HTTP status code.
  236. /// </summary>
  237. public int StatusCode
  238. {
  239. get
  240. {
  241. if (HttpServer)
  242. return (int)_httpResponse.Status;
  243. else
  244. return _httpListenerResponse.StatusCode;
  245. }
  246. set
  247. {
  248. if (HttpServer)
  249. _httpResponse.Status = (HttpStatusCode)value;
  250. else
  251. _httpListenerResponse.StatusCode = value;
  252. }
  253. }
  254. /// <summary>
  255. /// HTTP status description.
  256. /// </summary>
  257. public string StatusDescription
  258. {
  259. get
  260. {
  261. if (HttpServer)
  262. return _httpResponse.Reason;
  263. else
  264. return _httpListenerResponse.StatusDescription;
  265. }
  266. set
  267. {
  268. if (HttpServer)
  269. _httpResponse.Reason = value;
  270. else
  271. _httpListenerResponse.StatusDescription = value;
  272. }
  273. }
  274. internal bool HttpServer
  275. {
  276. get { return null != _httpResponse; }
  277. }
  278. private HttpResponse _httpResponse;
  279. private HttpListenerResponse _httpListenerResponse;
  280. // internal HttpResponse HttpResponse
  281. // {
  282. // get { return _httpResponse; }
  283. // }
  284. public OSHttpResponse()
  285. {
  286. }
  287. /// <summary>
  288. /// Instantiate an OSHttpResponse object based on an
  289. /// underlying .NET HttpListenerResponse.
  290. /// </summary>
  291. /// <remarks>
  292. /// Almost deprecated; will go west to make once HttpServer
  293. /// base takes over.
  294. /// </remarks>
  295. public OSHttpResponse(HttpListenerResponse resp)
  296. {
  297. _httpListenerResponse = resp;
  298. }
  299. /// <summary>
  300. /// Instantiate an OSHttpResponse object from an OSHttpRequest
  301. /// object.
  302. /// </summary
  303. /// <param name="req">Incoming OSHttpRequest to which we are
  304. /// replying</param>
  305. public OSHttpResponse(OSHttpRequest req)
  306. {
  307. _httpResponse = new HttpResponse(req.HttpClientContext, req.HttpRequest);
  308. }
  309. /// <summary>
  310. /// Add a header field and content to the response.
  311. /// </summary>
  312. /// <param name="key">string containing the header field
  313. /// name</param>
  314. /// <param name="value">string containing the header field
  315. /// value</param>
  316. public void AddHeader(string key, string value)
  317. {
  318. if (HttpServer)
  319. _httpResponse.AddHeader(key, value);
  320. else
  321. _httpListenerResponse.Headers.Add(key, value);
  322. }
  323. /// <summary>
  324. /// Send the response back to the remote client
  325. /// </summary>
  326. public void Send()
  327. {
  328. if (HttpServer)
  329. {
  330. _httpResponse.Body.Flush();
  331. _httpResponse.Send();
  332. }
  333. else
  334. {
  335. OutputStream.Close();
  336. }
  337. }
  338. }
  339. }