OSHttpResponse.cs 8.0 KB

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