OSHttpRequest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.IO;
  32. using System.Net;
  33. using System.Reflection;
  34. using System.Text;
  35. using System.Web;
  36. using OSHttpServer;
  37. using log4net;
  38. namespace OpenSim.Framework.Servers.HttpServer
  39. {
  40. public class OSHttpRequest : IOSHttpRequest
  41. {
  42. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. protected IHttpRequest m_request = null;
  44. protected IHttpClientContext m_context = null;
  45. public string[] AcceptTypes
  46. {
  47. get { return m_request.AcceptTypes; }
  48. }
  49. public Encoding ContentEncoding
  50. {
  51. get { return m_contentEncoding; }
  52. }
  53. private Encoding m_contentEncoding;
  54. public long ContentLength
  55. {
  56. get { return m_request.ContentLength; }
  57. }
  58. public long ContentLength64
  59. {
  60. get { return ContentLength; }
  61. }
  62. public string ContentType
  63. {
  64. get { return m_contentType; }
  65. }
  66. private string m_contentType;
  67. public HttpCookieCollection Cookies
  68. {
  69. get
  70. {
  71. RequestCookies cookies = m_request.Cookies;
  72. HttpCookieCollection httpCookies = new HttpCookieCollection();
  73. if(cookies != null)
  74. {
  75. foreach (RequestCookie cookie in cookies)
  76. httpCookies.Add(new HttpCookie(cookie.Name, cookie.Value));
  77. }
  78. return httpCookies;
  79. }
  80. }
  81. public bool HasEntityBody
  82. {
  83. get { return m_request.ContentLength != 0; }
  84. }
  85. public NameValueCollection Headers
  86. {
  87. get { return m_request.Headers; }
  88. }
  89. public string HttpMethod
  90. {
  91. get { return m_request.Method; }
  92. }
  93. public Stream InputStream
  94. {
  95. get { return m_request.Body; }
  96. }
  97. public bool IsSecured
  98. {
  99. get { return m_context.IsSecured; }
  100. }
  101. public bool KeepAlive
  102. {
  103. get { return ConnectionType.KeepAlive == m_request.Connection; }
  104. }
  105. public NameValueCollection QueryString
  106. {
  107. get { return m_request.QueryString;}
  108. }
  109. private Hashtable m_queryAsHashtable = null;
  110. public Hashtable Query
  111. {
  112. get
  113. {
  114. if (m_queryAsHashtable == null)
  115. BuildQueryHashtable();
  116. return m_queryAsHashtable;
  117. }
  118. }
  119. //faster than Query
  120. private Dictionary<string, string> _queryAsDictionay = null;
  121. public Dictionary<string,string> QueryAsDictionary
  122. {
  123. get
  124. {
  125. if (_queryAsDictionay == null)
  126. BuildQueryDictionary();
  127. return _queryAsDictionay;
  128. }
  129. }
  130. private HashSet<string> m_queryFlags = null;
  131. public HashSet<string> QueryFlags
  132. {
  133. get
  134. {
  135. if (m_queryFlags == null)
  136. BuildQueryDictionary();
  137. return m_queryFlags;
  138. }
  139. }
  140. /// <value>
  141. /// POST request values, if applicable
  142. /// </value>
  143. // public Hashtable Form { get; private set; }
  144. public string RawUrl
  145. {
  146. get { return m_request.Uri.AbsolutePath; }
  147. }
  148. public IPEndPoint RemoteIPEndPoint
  149. {
  150. get { return m_request.RemoteIPEndPoint; }
  151. }
  152. public IPEndPoint LocalIPEndPoint
  153. {
  154. get { return m_request.LocalIPEndPoint; }
  155. }
  156. public Uri Url
  157. {
  158. get { return m_request.Uri; }
  159. }
  160. public string UriPath
  161. {
  162. get { return m_request.UriPath; }
  163. }
  164. public string UserAgent
  165. {
  166. get { return m_userAgent; }
  167. }
  168. private string m_userAgent;
  169. public double ArrivalTS
  170. {
  171. get { return m_request.ArrivalTS;}
  172. }
  173. internal IHttpRequest IHttpRequest
  174. {
  175. get { return m_request; }
  176. }
  177. internal IHttpClientContext IHttpClientContext
  178. {
  179. get { return m_context; }
  180. }
  181. /// <summary>
  182. /// Internal whiteboard for handlers to store temporary stuff
  183. /// into.
  184. /// </summary>
  185. internal Dictionary<string, object> Whiteboard
  186. {
  187. get { return _whiteboard; }
  188. }
  189. private Dictionary<string, object> _whiteboard = new Dictionary<string, object>();
  190. public OSHttpRequest() {}
  191. public OSHttpRequest(IHttpRequest req)
  192. {
  193. m_request = req;
  194. m_context = req.Context;
  195. if (null != req.Headers["content-encoding"])
  196. {
  197. try
  198. {
  199. m_contentEncoding = Encoding.GetEncoding(m_request.Headers["content-encoding"]);
  200. }
  201. catch
  202. {
  203. // ignore
  204. }
  205. }
  206. if (null != req.Headers["content-type"])
  207. m_contentType = m_request.Headers["content-type"];
  208. if (null != req.Headers["user-agent"])
  209. m_userAgent = req.Headers["user-agent"];
  210. // Form = new Hashtable();
  211. // foreach (HttpInputItem item in req.Form)
  212. // {
  213. // _log.DebugFormat("[OSHttpRequest]: Got form item {0}={1}", item.Name, item.Value);
  214. // Form.Add(item.Name, item.Value);
  215. // }
  216. }
  217. private void BuildQueryDictionary()
  218. {
  219. NameValueCollection q = m_request.QueryString;
  220. _queryAsDictionay = new Dictionary<string, string>();
  221. m_queryFlags = new HashSet<string>();
  222. for(int i = 0; i <q.Count; ++i)
  223. {
  224. try
  225. {
  226. var name = q.GetKey(i);
  227. if(!string.IsNullOrEmpty(name))
  228. _queryAsDictionay[name] = q[i];
  229. else
  230. m_queryFlags.Add(q[i]);
  231. }
  232. catch {}
  233. }
  234. }
  235. private void BuildQueryHashtable()
  236. {
  237. NameValueCollection q = m_request.QueryString;
  238. m_queryAsHashtable = new Hashtable();
  239. m_queryFlags = new HashSet<string>();
  240. for (int i = 0; i < q.Count; ++i)
  241. {
  242. try
  243. {
  244. var name = q.GetKey(i);
  245. if (!string.IsNullOrEmpty(name))
  246. m_queryAsHashtable[name] = q[i];
  247. else
  248. m_queryFlags.Add(q[i]);
  249. }
  250. catch { }
  251. }
  252. }
  253. public override string ToString()
  254. {
  255. StringBuilder me = new StringBuilder();
  256. me.Append(String.Format("OSHttpRequest: {0} {1}\n", HttpMethod, RawUrl));
  257. foreach (string k in Headers.AllKeys)
  258. {
  259. me.Append(String.Format(" {0}: {1}\n", k, Headers[k]));
  260. }
  261. if (null != RemoteIPEndPoint)
  262. {
  263. me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint));
  264. }
  265. return me.ToString();
  266. }
  267. }
  268. }