OSHttpRequest.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 _request = null;
  44. protected IHttpClientContext _context = null;
  45. public string[] AcceptTypes
  46. {
  47. get { return _request.AcceptTypes; }
  48. }
  49. public Encoding ContentEncoding
  50. {
  51. get { return _contentEncoding; }
  52. }
  53. private Encoding _contentEncoding;
  54. public long ContentLength
  55. {
  56. get { return _request.ContentLength; }
  57. }
  58. public long ContentLength64
  59. {
  60. get { return ContentLength; }
  61. }
  62. public string ContentType
  63. {
  64. get { return _contentType; }
  65. }
  66. private string _contentType;
  67. public HttpCookieCollection Cookies
  68. {
  69. get
  70. {
  71. RequestCookies cookies = _request.Cookies;
  72. HttpCookieCollection httpCookies = new HttpCookieCollection();
  73. foreach (RequestCookie cookie in cookies)
  74. httpCookies.Add(new HttpCookie(cookie.Name, cookie.Value));
  75. return httpCookies;
  76. }
  77. }
  78. public bool HasEntityBody
  79. {
  80. get { return _request.ContentLength != 0; }
  81. }
  82. public NameValueCollection Headers
  83. {
  84. get { return _request.Headers; }
  85. }
  86. public string HttpMethod
  87. {
  88. get { return _request.Method; }
  89. }
  90. public Stream InputStream
  91. {
  92. get { return _request.Body; }
  93. }
  94. public bool IsSecured
  95. {
  96. get { return _context.IsSecured; }
  97. }
  98. public bool KeepAlive
  99. {
  100. get { return ConnectionType.KeepAlive == _request.Connection; }
  101. }
  102. public NameValueCollection QueryString
  103. {
  104. get { return _queryString; }
  105. }
  106. private NameValueCollection _queryString;
  107. public Hashtable Query
  108. {
  109. get { return _query; }
  110. }
  111. private Hashtable _query;
  112. /// <value>
  113. /// POST request values, if applicable
  114. /// </value>
  115. // public Hashtable Form { get; private set; }
  116. public string RawUrl
  117. {
  118. get { return _request.Uri.AbsolutePath; }
  119. }
  120. public IPEndPoint RemoteIPEndPoint
  121. {
  122. get { return _remoteIPEndPoint; }
  123. }
  124. private IPEndPoint _remoteIPEndPoint;
  125. public Uri Url
  126. {
  127. get { return _request.Uri; }
  128. }
  129. public string UserAgent
  130. {
  131. get { return _userAgent; }
  132. }
  133. private string _userAgent;
  134. internal IHttpRequest IHttpRequest
  135. {
  136. get { return _request; }
  137. }
  138. internal IHttpClientContext IHttpClientContext
  139. {
  140. get { return _context; }
  141. }
  142. /// <summary>
  143. /// Internal whiteboard for handlers to store temporary stuff
  144. /// into.
  145. /// </summary>
  146. internal Dictionary<string, object> Whiteboard
  147. {
  148. get { return _whiteboard; }
  149. }
  150. private Dictionary<string, object> _whiteboard = new Dictionary<string, object>();
  151. public OSHttpRequest() {}
  152. public OSHttpRequest(IHttpClientContext context, IHttpRequest req)
  153. {
  154. _request = req;
  155. _context = context;
  156. if (null != req.Headers["content-encoding"])
  157. {
  158. try
  159. {
  160. _contentEncoding = Encoding.GetEncoding(_request.Headers["content-encoding"]);
  161. }
  162. catch (Exception)
  163. {
  164. // ignore
  165. }
  166. }
  167. if (null != req.Headers["content-type"])
  168. _contentType = _request.Headers["content-type"];
  169. if (null != req.Headers["user-agent"])
  170. _userAgent = req.Headers["user-agent"];
  171. if (null != req.Headers["remote_addr"])
  172. {
  173. try
  174. {
  175. IPAddress addr = IPAddress.Parse(req.Headers["remote_addr"]);
  176. // sometimes req.Headers["remote_port"] returns a comma separated list, so use
  177. // the first one in the list and log it
  178. string[] strPorts = req.Headers["remote_port"].Split(new char[] { ',' });
  179. if (strPorts.Length > 1)
  180. {
  181. _log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
  182. req.Headers["remote_addr"], req.Headers["remote_port"]);
  183. }
  184. int port = Int32.Parse(strPorts[0]);
  185. _remoteIPEndPoint = new IPEndPoint(addr, port);
  186. }
  187. catch (FormatException)
  188. {
  189. _log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
  190. req.Headers["remote_addr"], req.Headers["remote_port"]);
  191. }
  192. }
  193. _queryString = new NameValueCollection();
  194. _query = new Hashtable();
  195. try
  196. {
  197. foreach (HttpInputItem item in req.QueryString)
  198. {
  199. try
  200. {
  201. _queryString.Add(item.Name, item.Value);
  202. _query[item.Name] = item.Value;
  203. }
  204. catch (InvalidCastException)
  205. {
  206. _log.DebugFormat("[OSHttpRequest]: error parsing {0} query item, skipping it", item.Name);
  207. continue;
  208. }
  209. }
  210. }
  211. catch (Exception)
  212. {
  213. _log.ErrorFormat("[OSHttpRequest]: Error parsing querystring");
  214. }
  215. // Form = new Hashtable();
  216. // foreach (HttpInputItem item in req.Form)
  217. // {
  218. // _log.DebugFormat("[OSHttpRequest]: Got form item {0}={1}", item.Name, item.Value);
  219. // Form.Add(item.Name, item.Value);
  220. // }
  221. }
  222. public override string ToString()
  223. {
  224. StringBuilder me = new StringBuilder();
  225. me.Append(String.Format("OSHttpRequest: {0} {1}\n", HttpMethod, RawUrl));
  226. foreach (string k in Headers.AllKeys)
  227. {
  228. me.Append(String.Format(" {0}: {1}\n", k, Headers[k]));
  229. }
  230. if (null != RemoteIPEndPoint)
  231. {
  232. me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint));
  233. }
  234. return me.ToString();
  235. }
  236. }
  237. }