1
0

OSHttpRequest.cs 9.9 KB

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