OSHttpRequest.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 HttpServer;
  37. using log4net;
  38. namespace OpenSim.Framework.Servers.HttpServer
  39. {
  40. public class OSHttpRequest
  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.Secured; }
  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. public string RawUrl
  113. {
  114. get { return _request.Uri.AbsolutePath; }
  115. }
  116. public IPEndPoint RemoteIPEndPoint
  117. {
  118. get { return _remoteIPEndPoint; }
  119. }
  120. private IPEndPoint _remoteIPEndPoint;
  121. public Uri Url
  122. {
  123. get { return _request.Uri; }
  124. }
  125. public string UserAgent
  126. {
  127. get { return _userAgent; }
  128. }
  129. private string _userAgent;
  130. internal IHttpRequest IHttpRequest
  131. {
  132. get { return _request; }
  133. }
  134. internal IHttpClientContext IHttpClientContext
  135. {
  136. get { return _context; }
  137. }
  138. /// <summary>
  139. /// Internal whiteboard for handlers to store temporary stuff
  140. /// into.
  141. /// </summary>
  142. internal Dictionary<string, object> Whiteboard
  143. {
  144. get { return _whiteboard; }
  145. }
  146. private Dictionary<string, object> _whiteboard = new Dictionary<string, object>();
  147. public OSHttpRequest() {}
  148. public OSHttpRequest(IHttpClientContext context, IHttpRequest req)
  149. {
  150. _request = req;
  151. _context = context;
  152. if (null != req.Headers["content-encoding"])
  153. _contentEncoding = Encoding.GetEncoding(_request.Headers["content-encoding"]);
  154. if (null != req.Headers["content-type"])
  155. _contentType = _request.Headers["content-type"];
  156. if (null != req.Headers["user-agent"])
  157. _userAgent = req.Headers["user-agent"];
  158. if (null != req.Headers["remote_addr"])
  159. {
  160. try
  161. {
  162. IPAddress addr = IPAddress.Parse(req.Headers["remote_addr"]);
  163. int port = Int32.Parse(req.Headers["remote_port"]);
  164. _remoteIPEndPoint = new IPEndPoint(addr, port);
  165. }
  166. catch (FormatException)
  167. {
  168. _log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
  169. req.Headers["remote_addr"], req.Headers["remote_port"]);
  170. }
  171. }
  172. _queryString = new NameValueCollection();
  173. _query = new Hashtable();
  174. try
  175. {
  176. foreach (HttpInputItem item in req.QueryString)
  177. {
  178. try
  179. {
  180. _queryString.Add(item.Name, item.Value);
  181. _query[item.Name] = item.Value;
  182. }
  183. catch (InvalidCastException)
  184. {
  185. _log.DebugFormat("[OSHttpRequest]: error parsing {0} query item, skipping it", item.Name);
  186. continue;
  187. }
  188. }
  189. }
  190. catch (Exception)
  191. {
  192. _log.ErrorFormat("[OSHttpRequest]: Error parsing querystring");
  193. }
  194. }
  195. public override string ToString()
  196. {
  197. StringBuilder me = new StringBuilder();
  198. me.Append(String.Format("OSHttpRequest: {0} {1}\n", HttpMethod, RawUrl));
  199. foreach (string k in Headers.AllKeys)
  200. {
  201. me.Append(String.Format(" {0}: {1}\n", k, Headers[k]));
  202. }
  203. if (null != RemoteIPEndPoint)
  204. {
  205. me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint));
  206. }
  207. return me.ToString();
  208. }
  209. }
  210. }