OSHttpResponse.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. using System.IO;
  28. using System.Net;
  29. using System.Text;
  30. using HttpServer;
  31. namespace OpenSim.Framework.Servers.HttpServer
  32. {
  33. /// <summary>
  34. /// OSHttpResponse is the OpenSim representation of an HTTP
  35. /// response.
  36. /// </summary>
  37. public class OSHttpResponse : IOSHttpResponse
  38. {
  39. /// <summary>
  40. /// Content type property.
  41. /// </summary>
  42. /// <remarks>
  43. /// Setting this property will also set IsContentTypeSet to
  44. /// true.
  45. /// </remarks>
  46. public virtual string ContentType
  47. {
  48. get
  49. {
  50. return _httpResponse.ContentType;
  51. }
  52. set
  53. {
  54. _httpResponse.ContentType = value;
  55. }
  56. }
  57. /// <summary>
  58. /// Boolean property indicating whether the content type
  59. /// property actively has been set.
  60. /// </summary>
  61. /// <remarks>
  62. /// IsContentTypeSet will go away together with .NET base.
  63. /// </remarks>
  64. // public bool IsContentTypeSet
  65. // {
  66. // get { return _contentTypeSet; }
  67. // }
  68. // private bool _contentTypeSet;
  69. /// <summary>
  70. /// Length of the body content; 0 if there is no body.
  71. /// </summary>
  72. public long ContentLength
  73. {
  74. get
  75. {
  76. return _httpResponse.ContentLength;
  77. }
  78. set
  79. {
  80. _httpResponse.ContentLength = value;
  81. }
  82. }
  83. /// <summary>
  84. /// Alias for ContentLength.
  85. /// </summary>
  86. public long ContentLength64
  87. {
  88. get { return ContentLength; }
  89. set { ContentLength = value; }
  90. }
  91. /// <summary>
  92. /// Encoding of the body content.
  93. /// </summary>
  94. public Encoding ContentEncoding
  95. {
  96. get
  97. {
  98. return _httpResponse.Encoding;
  99. }
  100. set
  101. {
  102. _httpResponse.Encoding = value;
  103. }
  104. }
  105. public bool KeepAlive
  106. {
  107. get
  108. {
  109. return _httpResponse.Connection == ConnectionType.KeepAlive;
  110. }
  111. set
  112. {
  113. if (value)
  114. _httpResponse.Connection = ConnectionType.KeepAlive;
  115. else
  116. _httpResponse.Connection = ConnectionType.Close;
  117. }
  118. }
  119. /// <summary>
  120. /// Get or set the keep alive timeout property (default is
  121. /// 20). Setting this to 0 also disables KeepAlive. Setting
  122. /// this to something else but 0 also enable KeepAlive.
  123. /// </summary>
  124. public int KeepAliveTimeout
  125. {
  126. get
  127. {
  128. return _httpResponse.KeepAlive;
  129. }
  130. set
  131. {
  132. if (value == 0)
  133. {
  134. _httpResponse.Connection = ConnectionType.Close;
  135. _httpResponse.KeepAlive = 0;
  136. }
  137. else
  138. {
  139. _httpResponse.Connection = ConnectionType.KeepAlive;
  140. _httpResponse.KeepAlive = value;
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// Return the output stream feeding the body.
  146. /// </summary>
  147. /// <remarks>
  148. /// On its way out...
  149. /// </remarks>
  150. public Stream OutputStream
  151. {
  152. get
  153. {
  154. return _httpResponse.Body;
  155. }
  156. }
  157. public string ProtocolVersion
  158. {
  159. get
  160. {
  161. return _httpResponse.ProtocolVersion;
  162. }
  163. set
  164. {
  165. _httpResponse.ProtocolVersion = value;
  166. }
  167. }
  168. /// <summary>
  169. /// Return the output stream feeding the body.
  170. /// </summary>
  171. public Stream Body
  172. {
  173. get
  174. {
  175. return _httpResponse.Body;
  176. }
  177. }
  178. /// <summary>
  179. /// Set a redirct location.
  180. /// </summary>
  181. public string RedirectLocation
  182. {
  183. // get { return _redirectLocation; }
  184. set
  185. {
  186. _httpResponse.Redirect(value);
  187. }
  188. }
  189. /// <summary>
  190. /// Chunk transfers.
  191. /// </summary>
  192. public bool SendChunked
  193. {
  194. get
  195. {
  196. return _httpResponse.Chunked;
  197. }
  198. set
  199. {
  200. _httpResponse.Chunked = value;
  201. }
  202. }
  203. /// <summary>
  204. /// HTTP status code.
  205. /// </summary>
  206. public virtual int StatusCode
  207. {
  208. get
  209. {
  210. return (int)_httpResponse.Status;
  211. }
  212. set
  213. {
  214. _httpResponse.Status = (HttpStatusCode)value;
  215. }
  216. }
  217. /// <summary>
  218. /// HTTP status description.
  219. /// </summary>
  220. public string StatusDescription
  221. {
  222. get
  223. {
  224. return _httpResponse.Reason;
  225. }
  226. set
  227. {
  228. _httpResponse.Reason = value;
  229. }
  230. }
  231. public bool ReuseContext
  232. {
  233. get
  234. {
  235. if (_httpClientContext != null)
  236. {
  237. return !_httpClientContext.EndWhenDone;
  238. }
  239. return true;
  240. }
  241. set
  242. {
  243. if (_httpClientContext != null)
  244. {
  245. _httpClientContext.EndWhenDone = !value;
  246. }
  247. }
  248. }
  249. protected IHttpResponse _httpResponse;
  250. private IHttpClientContext _httpClientContext;
  251. public OSHttpResponse() {}
  252. public OSHttpResponse(IHttpResponse resp)
  253. {
  254. _httpResponse = resp;
  255. }
  256. /// <summary>
  257. /// Instantiate an OSHttpResponse object from an OSHttpRequest
  258. /// object.
  259. /// </summary
  260. /// <param name="req">Incoming OSHttpRequest to which we are
  261. /// replying</param>
  262. public OSHttpResponse(OSHttpRequest req)
  263. {
  264. _httpResponse = new HttpResponse(req.IHttpClientContext, req.IHttpRequest);
  265. _httpClientContext = req.IHttpClientContext;
  266. }
  267. public OSHttpResponse(HttpResponse resp, IHttpClientContext clientContext)
  268. {
  269. _httpResponse = resp;
  270. _httpClientContext = clientContext;
  271. }
  272. /// <summary>
  273. /// Add a header field and content to the response.
  274. /// </summary>
  275. /// <param name="key">string containing the header field
  276. /// name</param>
  277. /// <param name="value">string containing the header field
  278. /// value</param>
  279. public void AddHeader(string key, string value)
  280. {
  281. _httpResponse.AddHeader(key, value);
  282. }
  283. /// <summary>
  284. /// Send the response back to the remote client
  285. /// </summary>
  286. public void Send()
  287. {
  288. _httpResponse.Body.Flush();
  289. // disable this till they are safe to use
  290. _httpResponse.Connection = ConnectionType.Close;
  291. _httpResponse.Chunked = false;
  292. _httpResponse.Send();
  293. }
  294. public void FreeContext()
  295. {
  296. if (_httpClientContext != null)
  297. _httpClientContext.Close();
  298. }
  299. }
  300. }