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