OSHttpRequest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.Net;
  32. using System.IO;
  33. using System.Text;
  34. using HttpServer;
  35. namespace OpenSim.Framework.Servers
  36. {
  37. public class OSHttpRequest
  38. {
  39. public string[] AcceptTypes
  40. {
  41. get { return _acceptTypes; }
  42. }
  43. private string[] _acceptTypes;
  44. public Encoding ContentEncoding
  45. {
  46. get { return _contentEncoding; }
  47. }
  48. private Encoding _contentEncoding;
  49. public long ContentLength
  50. {
  51. get { return _contentLength64; }
  52. }
  53. private long _contentLength64;
  54. public long ContentLength64
  55. {
  56. get { return ContentLength; }
  57. }
  58. public string ContentType
  59. {
  60. get { return _contentType; }
  61. }
  62. private string _contentType;
  63. // public CookieCollection Cookies
  64. // {
  65. // get { return _cookies; }
  66. // }
  67. // private CookieCollection _cookies;
  68. public NameValueCollection Headers
  69. {
  70. get { return _headers; }
  71. }
  72. private NameValueCollection _headers;
  73. public string HttpMethod
  74. {
  75. get { return _httpMethod; }
  76. }
  77. private string _httpMethod;
  78. public Stream InputStream
  79. {
  80. get { return _inputStream; }
  81. }
  82. private Stream _inputStream;
  83. // public bool IsSecureConnection
  84. // {
  85. // get { return _isSecureConnection; }
  86. // }
  87. // private bool _isSecureConnection;
  88. // public bool IsAuthenticated
  89. // {
  90. // get { return _isAuthenticated; }
  91. // }
  92. // private bool _isAuthenticated;
  93. public bool HasEntityBody
  94. {
  95. get { return _hasbody; }
  96. }
  97. private bool _hasbody;
  98. public bool KeepAlive
  99. {
  100. get { return _keepAlive; }
  101. }
  102. private bool _keepAlive;
  103. public string RawUrl
  104. {
  105. get { return _rawUrl; }
  106. }
  107. private string _rawUrl;
  108. public Uri Url
  109. {
  110. get { return _url; }
  111. }
  112. private Uri _url;
  113. public string UserAgent
  114. {
  115. get { return _userAgent; }
  116. }
  117. private string _userAgent;
  118. public NameValueCollection QueryString
  119. {
  120. get { return _queryString; }
  121. }
  122. private NameValueCollection _queryString;
  123. public Hashtable Query
  124. {
  125. get { return _query; }
  126. }
  127. private Hashtable _query;
  128. public IPEndPoint RemoteIPEndPoint
  129. {
  130. get { return _ipEndPoint; }
  131. }
  132. private IPEndPoint _ipEndPoint;
  133. internal HttpRequest HttpRequest
  134. {
  135. get { return _request; }
  136. }
  137. private HttpRequest _request;
  138. internal HttpClientContext HttpClientContext
  139. {
  140. get { return _context; }
  141. }
  142. private HttpClientContext _context;
  143. /// <summary>
  144. /// Internal whiteboard for handlers to store temporary stuff
  145. /// into.
  146. /// </summary>
  147. internal Dictionary<string, object> Whiteboard
  148. {
  149. get { return _whiteboard; }
  150. }
  151. private Dictionary<string, object> _whiteboard = new Dictionary<string, object>();
  152. public OSHttpRequest()
  153. {
  154. }
  155. public OSHttpRequest(HttpListenerRequest req)
  156. {
  157. _acceptTypes = req.AcceptTypes;
  158. _contentEncoding = req.ContentEncoding;
  159. _contentLength64 = req.ContentLength64;
  160. _contentType = req.ContentType;
  161. _headers = req.Headers;
  162. _httpMethod = req.HttpMethod;
  163. _hasbody = req.HasEntityBody;
  164. _inputStream = req.InputStream;
  165. _keepAlive = req.KeepAlive;
  166. _rawUrl = req.RawUrl;
  167. _url = req.Url;
  168. _queryString = req.QueryString;
  169. _userAgent = req.UserAgent;
  170. _ipEndPoint = req.RemoteEndPoint;
  171. // _cookies = req.Cookies;
  172. // _isSecureConnection = req.IsSecureConnection;
  173. // _isAuthenticated = req.IsAuthenticated;
  174. }
  175. public OSHttpRequest(HttpClientContext context, HttpRequest req)
  176. {
  177. _context = context;
  178. _request = req;
  179. _acceptTypes = req.AcceptTypes;
  180. if (null != req.Headers["content-encoding"])
  181. _contentEncoding = Encoding.GetEncoding(_request.Headers["content-encoding"]);
  182. _contentLength64 = req.ContentLength;
  183. if (null != req.Headers["content-type"])
  184. _contentType = _request.Headers["content-type"];
  185. _headers = req.Headers;
  186. _httpMethod = req.Method;
  187. _hasbody = req.ContentLength != 0;
  188. _inputStream = req.Body;
  189. _keepAlive = ConnectionType.KeepAlive == req.Connection;
  190. _rawUrl = req.Uri.AbsolutePath;
  191. _url = req.Uri;
  192. if (null != req.Headers["user-agent"])
  193. _userAgent = req.Headers["user-agent"];
  194. _queryString = new NameValueCollection();
  195. _query = new Hashtable();
  196. foreach (KeyValuePair<string, HttpInputItem> q in req.QueryString)
  197. {
  198. _queryString.Add(q.Key, q.Value.Value);
  199. _query[q.Key] = q.Value.Value;
  200. }
  201. // TODO: requires change to HttpServer.HttpRequest
  202. _ipEndPoint = null;
  203. // _cookies = req.Cookies;
  204. // _isSecureConnection = req.IsSecureConnection;
  205. // _isAuthenticated = req.IsAuthenticated;
  206. }
  207. public override string ToString()
  208. {
  209. StringBuilder me = new StringBuilder();
  210. me.Append(String.Format("OSHttpRequest: {0} {1}\n", HttpMethod, RawUrl));
  211. foreach (string k in Headers.AllKeys)
  212. {
  213. me.Append(String.Format(" {0}: {1}\n", k, Headers[k]));
  214. }
  215. if (null != RemoteIPEndPoint)
  216. {
  217. me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint.ToString()));
  218. }
  219. return me.ToString();
  220. }
  221. }
  222. }