1
0

OSHttpResponse.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 OSHttpServer;
  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. public byte[] RawBuffer
  179. {
  180. get
  181. {
  182. return _httpResponse.RawBuffer;
  183. }
  184. set
  185. {
  186. _httpResponse.RawBuffer = value;
  187. }
  188. }
  189. public int RawBufferStart
  190. {
  191. get
  192. {
  193. return _httpResponse.RawBufferStart;
  194. }
  195. set
  196. {
  197. _httpResponse.RawBufferStart = value;
  198. }
  199. }
  200. public int RawBufferLen
  201. {
  202. get
  203. {
  204. return _httpResponse.RawBufferLen;
  205. }
  206. set
  207. {
  208. _httpResponse.RawBufferLen = value;
  209. }
  210. }
  211. public int Priority
  212. {
  213. get
  214. {
  215. return _httpResponse.Priority;
  216. }
  217. set
  218. {
  219. _httpResponse.Priority = value;
  220. }
  221. }
  222. /// <summary>
  223. /// Chunk transfers.
  224. /// </summary>
  225. public bool SendChunked
  226. {
  227. get
  228. {
  229. return _httpResponse.Chunked;
  230. }
  231. set
  232. {
  233. _httpResponse.Chunked = value;
  234. }
  235. }
  236. /// <summary>
  237. /// HTTP status code.
  238. /// </summary>
  239. public virtual int StatusCode
  240. {
  241. get
  242. {
  243. return (int)_httpResponse.Status;
  244. }
  245. set
  246. {
  247. _httpResponse.Status = (HttpStatusCode)value;
  248. }
  249. }
  250. public double RequestTS
  251. {
  252. get {return _httpResponse.RequestTS; }
  253. }
  254. /// <summary>
  255. /// HTTP status description.
  256. /// </summary>
  257. public string StatusDescription
  258. {
  259. get
  260. {
  261. return _httpResponse.Reason;
  262. }
  263. set
  264. {
  265. _httpResponse.Reason = value;
  266. }
  267. }
  268. protected IHttpResponse _httpResponse;
  269. public OSHttpResponse() {}
  270. public OSHttpResponse(IHttpResponse resp)
  271. {
  272. _httpResponse = resp;
  273. }
  274. /// <summary>
  275. /// Instantiate an OSHttpResponse object from an OSHttpRequest
  276. /// object.
  277. /// </summary
  278. /// <param name="req">Incoming OSHttpRequest to which we are
  279. /// replying</param>
  280. public OSHttpResponse(OSHttpRequest req)
  281. {
  282. _httpResponse = new HttpResponse(req.IHttpRequest);
  283. }
  284. public OSHttpResponse(HttpResponse resp)
  285. {
  286. _httpResponse = resp;
  287. }
  288. /// <summary>
  289. /// Add a header field and content to the response.
  290. /// </summary>
  291. /// <param name="key">string containing the header field
  292. /// name</param>
  293. /// <param name="value">string containing the header field
  294. /// value</param>
  295. public void AddHeader(string key, string value)
  296. {
  297. _httpResponse.AddHeader(key, value);
  298. }
  299. /// <summary>
  300. /// Send the response back to the remote client
  301. /// </summary>
  302. public void Send()
  303. {
  304. _httpResponse.Chunked = false;
  305. _httpResponse.Send();
  306. }
  307. }
  308. }