diff options
author | Dr Scofield | 2008-07-14 12:18:32 +0000 |
---|---|---|
committer | Dr Scofield | 2008-07-14 12:18:32 +0000 |
commit | 7692f3e18f49fc57b63efbe54b7ac71de2fb7e16 (patch) | |
tree | 4ee9a4279ff904a8c5fc7e2bd9f85f55bf8b52ba /OpenSim/Framework/Servers/OSHttpXmlRpcHandler.cs | |
parent | adds a default value of true to the new IRC bridge configuration option "nick... (diff) | |
download | opensim-SC_OLD-7692f3e18f49fc57b63efbe54b7ac71de2fb7e16.zip opensim-SC_OLD-7692f3e18f49fc57b63efbe54b7ac71de2fb7e16.tar.gz opensim-SC_OLD-7692f3e18f49fc57b63efbe54b7ac71de2fb7e16.tar.bz2 opensim-SC_OLD-7692f3e18f49fc57b63efbe54b7ac71de2fb7e16.tar.xz |
further work in progress on the HttpServer side: XmlRpc handler path
almost complete and soon to be ready for testing; OSHttpResponse code
out.
Diffstat (limited to 'OpenSim/Framework/Servers/OSHttpXmlRpcHandler.cs')
-rw-r--r-- | OpenSim/Framework/Servers/OSHttpXmlRpcHandler.cs | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/OSHttpXmlRpcHandler.cs b/OpenSim/Framework/Servers/OSHttpXmlRpcHandler.cs new file mode 100644 index 0000000..4205547 --- /dev/null +++ b/OpenSim/Framework/Servers/OSHttpXmlRpcHandler.cs | |||
@@ -0,0 +1,222 @@ | |||
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.Generic; | ||
30 | using System.IO; | ||
31 | using System.Net; | ||
32 | using System.Reflection; | ||
33 | using System.Text; | ||
34 | using System.Text.RegularExpressions; | ||
35 | using System.Xml; | ||
36 | using log4net; | ||
37 | using Nwc.XmlRpc; | ||
38 | |||
39 | namespace OpenSim.Framework.Servers | ||
40 | { | ||
41 | public delegate XmlRpcResponse OSHttpXmlRpcProcessor(XmlRpcRequest request); | ||
42 | |||
43 | public class OSHttpXmlRpcHandler: OSHttpHandler | ||
44 | { | ||
45 | private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
46 | |||
47 | /// <summary> | ||
48 | /// Regular expression used to match against path of incoming | ||
49 | /// HTTP request. If you want to match any string either use | ||
50 | /// '.*' or null. To match for the emtpy string use '^$' | ||
51 | /// </summary> | ||
52 | public Regex Path | ||
53 | { | ||
54 | get { return _pathsRegex; } | ||
55 | } | ||
56 | private Regex _pathsRegex; | ||
57 | |||
58 | /// <summary> | ||
59 | /// Dictionary of (header name, regular expression) tuples, | ||
60 | /// allowing us to match on HTTP header fields. | ||
61 | /// </summary> | ||
62 | public Dictionary<string, Regex> Headers | ||
63 | { | ||
64 | get { return _headers; } | ||
65 | } | ||
66 | private Dictionary<string, Regex> _headers; | ||
67 | |||
68 | /// <summary> | ||
69 | /// Regex to whitelist IP end points. A null value disables | ||
70 | /// checking of IP end points. | ||
71 | /// </summary> | ||
72 | /// <remarks> | ||
73 | /// This feature is currently not implemented as it requires | ||
74 | /// (trivial) changes to HttpServer.HttpListener that have not | ||
75 | /// been implemented. | ||
76 | /// </remarks> | ||
77 | public Regex IPEndPointWhitelist | ||
78 | { | ||
79 | get { return _ipEndPointRegex; } | ||
80 | } | ||
81 | private Regex _ipEndPointRegex; | ||
82 | |||
83 | /// <summary> | ||
84 | /// An OSHttpHandler that matches on the "content-type" header can | ||
85 | /// supply an OSHttpContentTypeChecker delegate which will be | ||
86 | /// invoked by the request matcher in OSHttpRequestPump. | ||
87 | /// </summary> | ||
88 | /// <returns>true if the handler is interested in the content; | ||
89 | /// false otherwise</returns> | ||
90 | public OSHttpContentTypeChecker ContentTypeChecker | ||
91 | { | ||
92 | get | ||
93 | { | ||
94 | return delegate(OSHttpRequest req) | ||
95 | { | ||
96 | XmlRpcRequest xmlRpcRequest = null; | ||
97 | |||
98 | // check whether req is already reified | ||
99 | // if not: reify (and post to whiteboard) | ||
100 | try | ||
101 | { | ||
102 | if (req.Whiteboard.ContainsKey("xmlrequest")) | ||
103 | { | ||
104 | xmlRpcRequest = req.Whiteboard["xmlrequest"] as XmlRpcRequest; | ||
105 | } | ||
106 | else | ||
107 | { | ||
108 | StreamReader body = new StreamReader(req.InputStream); | ||
109 | string requestBody = body.ReadToEnd(); | ||
110 | xmlRpcRequest = (XmlRpcRequest)(new XmlRpcRequestDeserializer()).Deserialize(requestBody); | ||
111 | req.Whiteboard["xmlrequest"] = xmlRpcRequest; | ||
112 | } | ||
113 | } | ||
114 | catch (XmlException) | ||
115 | { | ||
116 | _log.ErrorFormat("[OSHttpXmlRpcHandler] failed to deserialize XmlRpcRequest from {0}", req.ToString()); | ||
117 | return false; | ||
118 | } | ||
119 | |||
120 | // check against methodName | ||
121 | if ((null != xmlRpcRequest) | ||
122 | && !String.IsNullOrEmpty(xmlRpcRequest.MethodName) | ||
123 | && xmlRpcRequest.MethodName == _methodName) | ||
124 | { | ||
125 | _log.DebugFormat("[OSHttpXmlRpcHandler] located handler {0} for {1}", _methodName, req.ToString()); | ||
126 | return true; | ||
127 | } | ||
128 | |||
129 | return false; | ||
130 | }; | ||
131 | } | ||
132 | } | ||
133 | |||
134 | // contains handler for processing XmlRpc Request | ||
135 | private OSHttpXmlRpcProcessor _handler; | ||
136 | |||
137 | // contains XmlRpc method name | ||
138 | private string _methodName; | ||
139 | |||
140 | |||
141 | /// <summary> | ||
142 | /// Instantiate an XmlRpc handler. | ||
143 | /// </summary> | ||
144 | /// <param name="handler">OSHttpXmlRpcProcessor | ||
145 | /// delegate</param> | ||
146 | /// <param name="methodName">XmlRpc method name</param> | ||
147 | /// <param name="path">XmlRpc path prefix (regular expression)</param> | ||
148 | /// <param name="headers">Dictionary with header names and | ||
149 | /// regular expressions to match content of headers</param> | ||
150 | /// <param name="whitelist">IP whitelist of remote end points | ||
151 | /// to accept (regular expression)</param> | ||
152 | /// <remarks> | ||
153 | /// Except for handler and methodName, all other parameters | ||
154 | /// can be null, in which case they are not taken into account | ||
155 | /// when the handler is being looked up. | ||
156 | /// </remarks> | ||
157 | public OSHttpXmlRpcHandler(OSHttpXmlRpcProcessor handler, string methodName, Regex path, | ||
158 | Dictionary<string, Regex> headers, Regex whitelist) | ||
159 | { | ||
160 | _handler = handler; | ||
161 | _pathsRegex = path; | ||
162 | _methodName = methodName; | ||
163 | |||
164 | if (null == _headers) _headers = new Dictionary<string, Regex>(); | ||
165 | _headers.Add("content-type", new Regex(@"^(text|application)/xml", RegexOptions.IgnoreCase | | ||
166 | RegexOptions.Compiled)); | ||
167 | |||
168 | _ipEndPointRegex = whitelist; | ||
169 | } | ||
170 | |||
171 | |||
172 | /// <summary> | ||
173 | /// Instantiate an XmlRpc handler. | ||
174 | /// </summary> | ||
175 | /// <param name="handler">OSHttpXmlRpcProcessor | ||
176 | /// delegate</param> | ||
177 | /// <param name="methodName">XmlRpc method name</param> | ||
178 | public OSHttpXmlRpcHandler(OSHttpXmlRpcProcessor handler, string methodName) | ||
179 | : this(handler, methodName, null, null, null) | ||
180 | { | ||
181 | } | ||
182 | |||
183 | |||
184 | /// <summary> | ||
185 | /// Invoked by OSHttpRequestPump. | ||
186 | /// </summary> | ||
187 | public OSHttpHandlerResult Process(OSHttpRequest request) | ||
188 | { | ||
189 | XmlRpcResponse xmlRpcResponse; | ||
190 | string responseString; | ||
191 | |||
192 | OSHttpResponse resp = new OSHttpResponse(request); | ||
193 | |||
194 | try | ||
195 | { | ||
196 | // reified XmlRpcRequest must still be on the whiteboard | ||
197 | XmlRpcRequest xmlRpcRequest = request.Whiteboard["xmlrequest"] as XmlRpcRequest; | ||
198 | xmlRpcResponse = _handler(xmlRpcRequest); | ||
199 | responseString = XmlRpcResponseSerializer.Singleton.Serialize(xmlRpcResponse); | ||
200 | |||
201 | resp.ContentType = "text/xml"; | ||
202 | byte[] buffer = Encoding.UTF8.GetBytes(responseString); | ||
203 | |||
204 | resp.SendChunked = false; | ||
205 | resp.ContentLength = buffer.Length; | ||
206 | resp.ContentEncoding = Encoding.UTF8; | ||
207 | |||
208 | resp.Body.Write(buffer, 0, buffer.Length); | ||
209 | resp.Body.Flush(); | ||
210 | |||
211 | resp.Send(); | ||
212 | |||
213 | } | ||
214 | catch (Exception ex) | ||
215 | { | ||
216 | _log.WarnFormat("[OSHttpXmlRpcHandler]: Error: {0}", ex.Message); | ||
217 | return OSHttpHandlerResult.Pass; | ||
218 | } | ||
219 | return OSHttpHandlerResult.Done; | ||
220 | } | ||
221 | } | ||
222 | } \ No newline at end of file | ||