OSHttpResponse.cs 10 KB

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