OSHttpRequest.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.IO;
  32. using System.Net;
  33. using System.Reflection;
  34. using System.Text;
  35. using HttpServer;
  36. using log4net;
  37. namespace OpenSim.Framework.Servers
  38. {
  39. public class OSHttpRequest
  40. {
  41. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. protected IHttpRequest _request = null;
  43. protected IHttpClientContext _context = null;
  44. public string[] AcceptTypes
  45. {
  46. get { return _request.AcceptTypes; }
  47. }
  48. public Encoding ContentEncoding
  49. {
  50. get { return _contentEncoding; }
  51. }
  52. private Encoding _contentEncoding;
  53. public long ContentLength
  54. {
  55. get { return _request.ContentLength; }
  56. }
  57. public long ContentLength64
  58. {
  59. get { return ContentLength; }
  60. }
  61. public string ContentType
  62. {
  63. get { return _contentType; }
  64. }
  65. private string _contentType;
  66. public bool HasEntityBody
  67. {
  68. get { return _request.ContentLength != 0; }
  69. }
  70. public NameValueCollection Headers
  71. {
  72. get { return _request.Headers; }
  73. }
  74. public string HttpMethod
  75. {
  76. get { return _request.Method; }
  77. }
  78. public Stream InputStream
  79. {
  80. get { return _request.Body; }
  81. }
  82. public bool IsSecured
  83. {
  84. get { return _context.Secured; }
  85. }
  86. public bool KeepAlive
  87. {
  88. get { return ConnectionType.KeepAlive == _request.Connection; }
  89. }
  90. public NameValueCollection QueryString
  91. {
  92. get { return _queryString; }
  93. }
  94. private NameValueCollection _queryString;
  95. public Hashtable Query
  96. {
  97. get { return _query; }
  98. }
  99. private Hashtable _query;
  100. public string RawUrl
  101. {
  102. get { return _request.Uri.AbsolutePath; }
  103. }
  104. public IPEndPoint RemoteIPEndPoint
  105. {
  106. get { return _remoteIPEndPoint; }
  107. }
  108. private IPEndPoint _remoteIPEndPoint;
  109. public Uri Url
  110. {
  111. get { return _request.Uri; }
  112. }
  113. public string UserAgent
  114. {
  115. get { return _userAgent; }
  116. }
  117. private string _userAgent;
  118. internal IHttpRequest IHttpRequest
  119. {
  120. get { return _request; }
  121. }
  122. internal IHttpClientContext IHttpClientContext
  123. {
  124. get { return _context; }
  125. }
  126. /// <summary>
  127. /// Internal whiteboard for handlers to store temporary stuff
  128. /// into.
  129. /// </summary>
  130. internal Dictionary<string, object> Whiteboard
  131. {
  132. get { return _whiteboard; }
  133. }
  134. private Dictionary<string, object> _whiteboard = new Dictionary<string, object>();
  135. public OSHttpRequest() {}
  136. public OSHttpRequest(IHttpClientContext context, IHttpRequest req)
  137. {
  138. _request = req;
  139. _context = context;
  140. if (null != req.Headers["content-encoding"])
  141. _contentEncoding = Encoding.GetEncoding(_request.Headers["content-encoding"]);
  142. if (null != req.Headers["content-type"])
  143. _contentType = _request.Headers["content-type"];
  144. if (null != req.Headers["user-agent"])
  145. _userAgent = req.Headers["user-agent"];
  146. if (null != req.Headers["remote_addr"])
  147. {
  148. try
  149. {
  150. IPAddress addr = IPAddress.Parse(req.Headers["remote_addr"]);
  151. int port = Int32.Parse(req.Headers["remote_port"]);
  152. _remoteIPEndPoint = new IPEndPoint(addr, port);
  153. }
  154. catch (FormatException)
  155. {
  156. _log.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
  157. req.Headers["remote_addr"], req.Headers["remote_port"]);
  158. }
  159. }
  160. _queryString = new NameValueCollection();
  161. _query = new Hashtable();
  162. try
  163. {
  164. foreach (HttpInputItem item in req.QueryString)
  165. {
  166. try
  167. {
  168. _queryString.Add(item.Name, item.Value);
  169. _query[item.Name] = item.Value;
  170. }
  171. catch (InvalidCastException)
  172. {
  173. _log.DebugFormat("[OSHttpRequest]: error parsing {0} query item, skipping it", item.Name);
  174. continue;
  175. }
  176. }
  177. }
  178. catch (Exception)
  179. {
  180. _log.ErrorFormat("[OSHttpRequest]: Error parsing querystring");
  181. }
  182. }
  183. public override string ToString()
  184. {
  185. StringBuilder me = new StringBuilder();
  186. me.Append(String.Format("OSHttpRequest: {0} {1}\n", HttpMethod, RawUrl));
  187. foreach (string k in Headers.AllKeys)
  188. {
  189. me.Append(String.Format(" {0}: {1}\n", k, Headers[k]));
  190. }
  191. if (null != RemoteIPEndPoint)
  192. {
  193. me.Append(String.Format(" IP: {0}\n", RemoteIPEndPoint));
  194. }
  195. return me.ToString();
  196. }
  197. }
  198. }