diff options
author | Johan Berntsson | 2008-07-23 07:27:11 +0000 |
---|---|---|
committer | Johan Berntsson | 2008-07-23 07:27:11 +0000 |
commit | 3b35332957e0d122cdd063ad14d3795856bcd8e5 (patch) | |
tree | 8fbabfa0cdb049fd9151f6cd887c6897e8d89604 /OpenSim/Framework/Servers/RestSessionService.cs | |
parent | thanks lulurun for a security patch that blocks unathorized access to the inv... (diff) | |
download | opensim-SC-3b35332957e0d122cdd063ad14d3795856bcd8e5.zip opensim-SC-3b35332957e0d122cdd063ad14d3795856bcd8e5.tar.gz opensim-SC-3b35332957e0d122cdd063ad14d3795856bcd8e5.tar.bz2 opensim-SC-3b35332957e0d122cdd063ad14d3795856bcd8e5.tar.xz |
adding files that were not included in r5589
Diffstat (limited to 'OpenSim/Framework/Servers/RestSessionService.cs')
-rw-r--r-- | OpenSim/Framework/Servers/RestSessionService.cs | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/RestSessionService.cs b/OpenSim/Framework/Servers/RestSessionService.cs new file mode 100644 index 0000000..3c79844 --- /dev/null +++ b/OpenSim/Framework/Servers/RestSessionService.cs | |||
@@ -0,0 +1,223 @@ | |||
1 | using System; | ||
2 | using System.IO; | ||
3 | using System.Net; | ||
4 | using System.Collections.Generic; | ||
5 | using System.Text; | ||
6 | using System.Xml; | ||
7 | using System.Xml.Serialization; | ||
8 | |||
9 | namespace OpenSim.Framework.Servers | ||
10 | { | ||
11 | public class RestSessionObject<TRequest> | ||
12 | { | ||
13 | private string sid; | ||
14 | private string aid; | ||
15 | private TRequest request_body; | ||
16 | |||
17 | public string SessionID | ||
18 | { | ||
19 | get { return sid; } | ||
20 | set { sid = value; } | ||
21 | } | ||
22 | |||
23 | public string AvatarID | ||
24 | { | ||
25 | get { return aid; } | ||
26 | set { aid = value; } | ||
27 | } | ||
28 | |||
29 | public TRequest Body | ||
30 | { | ||
31 | get { return request_body; } | ||
32 | set { request_body = value; } | ||
33 | } | ||
34 | } | ||
35 | |||
36 | public class SynchronousRestSessionObjectPoster<TRequest, TResponse> | ||
37 | { | ||
38 | public static TResponse BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid) | ||
39 | { | ||
40 | RestSessionObject<TRequest> sobj = new RestSessionObject<TRequest>(); | ||
41 | sobj.SessionID = sid; | ||
42 | sobj.AvatarID = aid; | ||
43 | sobj.Body = obj; | ||
44 | |||
45 | Type type = typeof(RestSessionObject<TRequest>); | ||
46 | |||
47 | WebRequest request = WebRequest.Create(requestUrl); | ||
48 | request.Method = verb; | ||
49 | request.ContentType = "text/xml"; | ||
50 | |||
51 | MemoryStream buffer = new MemoryStream(); | ||
52 | |||
53 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
54 | settings.Encoding = Encoding.UTF8; | ||
55 | |||
56 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
57 | { | ||
58 | XmlSerializer serializer = new XmlSerializer(type); | ||
59 | serializer.Serialize(writer, sobj); | ||
60 | writer.Flush(); | ||
61 | } | ||
62 | |||
63 | int length = (int)buffer.Length; | ||
64 | request.ContentLength = length; | ||
65 | |||
66 | Stream requestStream = request.GetRequestStream(); | ||
67 | requestStream.Write(buffer.ToArray(), 0, length); | ||
68 | TResponse deserial = default(TResponse); | ||
69 | using (WebResponse resp = request.GetResponse()) | ||
70 | { | ||
71 | XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); | ||
72 | deserial = (TResponse)deserializer.Deserialize(resp.GetResponseStream()); | ||
73 | } | ||
74 | return deserial; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | public class RestSessionObjectPosterResponse<TRequest, TResponse> | ||
79 | { | ||
80 | |||
81 | public ReturnResponse<TResponse> ResponseCallback; | ||
82 | |||
83 | public void BeginPostObject(string requestUrl, TRequest obj, string sid, string aid) | ||
84 | { | ||
85 | BeginPostObject("POST", requestUrl, obj, sid, aid); | ||
86 | } | ||
87 | |||
88 | public void BeginPostObject(string verb, string requestUrl, TRequest obj, string sid, string aid) | ||
89 | { | ||
90 | RestSessionObject<TRequest> sobj = new RestSessionObject<TRequest>(); | ||
91 | sobj.SessionID = sid; | ||
92 | sobj.AvatarID = aid; | ||
93 | sobj.Body = obj; | ||
94 | |||
95 | Type type = typeof(RestSessionObject<TRequest>); | ||
96 | |||
97 | WebRequest request = WebRequest.Create(requestUrl); | ||
98 | request.Method = verb; | ||
99 | request.ContentType = "text/xml"; | ||
100 | |||
101 | MemoryStream buffer = new MemoryStream(); | ||
102 | |||
103 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
104 | settings.Encoding = Encoding.UTF8; | ||
105 | |||
106 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
107 | { | ||
108 | XmlSerializer serializer = new XmlSerializer(type); | ||
109 | serializer.Serialize(writer, sobj); | ||
110 | writer.Flush(); | ||
111 | } | ||
112 | |||
113 | int length = (int)buffer.Length; | ||
114 | request.ContentLength = length; | ||
115 | |||
116 | Stream requestStream = request.GetRequestStream(); | ||
117 | requestStream.Write(buffer.ToArray(), 0, length); | ||
118 | // IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); | ||
119 | request.BeginGetResponse(AsyncCallback, request); | ||
120 | } | ||
121 | |||
122 | private void AsyncCallback(IAsyncResult result) | ||
123 | { | ||
124 | WebRequest request = (WebRequest)result.AsyncState; | ||
125 | using (WebResponse resp = request.EndGetResponse(result)) | ||
126 | { | ||
127 | TResponse deserial; | ||
128 | XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); | ||
129 | Stream stream = resp.GetResponseStream(); | ||
130 | |||
131 | // This is currently a bad debug stanza since it gobbles us the response... | ||
132 | // StreamReader reader = new StreamReader(stream); | ||
133 | // m_log.DebugFormat("[REST OBJECT POSTER RESPONSE]: Received {0}", reader.ReadToEnd()); | ||
134 | |||
135 | deserial = (TResponse)deserializer.Deserialize(stream); | ||
136 | |||
137 | if (deserial != null && ResponseCallback != null) | ||
138 | { | ||
139 | ResponseCallback(deserial); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | public delegate bool CheckIdentityMethod(string sid, string aid); | ||
146 | |||
147 | public class RestDeserialiseSecureHandler<TRequest, TResponse> : BaseRequestHandler, IStreamHandler | ||
148 | where TRequest : new() | ||
149 | { | ||
150 | private RestDeserialiseMethod<TRequest, TResponse> m_method; | ||
151 | private CheckIdentityMethod m_smethod; | ||
152 | |||
153 | public RestDeserialiseSecureHandler(string httpMethod, string path, RestDeserialiseMethod<TRequest, TResponse> method, CheckIdentityMethod smethod) | ||
154 | : base(httpMethod, path) | ||
155 | { | ||
156 | m_smethod = smethod; | ||
157 | m_method = method; | ||
158 | } | ||
159 | |||
160 | public void Handle(string path, Stream request, Stream responseStream, | ||
161 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
162 | { | ||
163 | RestSessionObject<TRequest> deserial; | ||
164 | using (XmlTextReader xmlReader = new XmlTextReader(request)) | ||
165 | { | ||
166 | XmlSerializer deserializer = new XmlSerializer(typeof(RestSessionObject<TRequest>)); | ||
167 | deserial = (RestSessionObject<TRequest>)deserializer.Deserialize(xmlReader); | ||
168 | } | ||
169 | |||
170 | TResponse response = default(TResponse); | ||
171 | if (m_smethod(deserial.SessionID, deserial.AvatarID)) | ||
172 | { | ||
173 | response = m_method(deserial.Body); | ||
174 | } | ||
175 | |||
176 | using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream)) | ||
177 | { | ||
178 | XmlSerializer serializer = new XmlSerializer(typeof(TResponse)); | ||
179 | serializer.Serialize(xmlWriter, response); | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | |||
184 | public delegate bool CheckTrustedSourceMethod(IPEndPoint peer); | ||
185 | |||
186 | public class RestDeserialiseTrustedHandler<TRequest, TResponse> : BaseRequestHandler, IStreamHandler | ||
187 | where TRequest : new() | ||
188 | { | ||
189 | private RestDeserialiseMethod<TRequest, TResponse> m_method; | ||
190 | private CheckTrustedSourceMethod m_tmethod; | ||
191 | |||
192 | public RestDeserialiseTrustedHandler(string httpMethod, string path, RestDeserialiseMethod<TRequest, TResponse> method, CheckTrustedSourceMethod tmethod) | ||
193 | : base(httpMethod, path) | ||
194 | { | ||
195 | m_tmethod = tmethod; | ||
196 | m_method = method; | ||
197 | } | ||
198 | |||
199 | public void Handle(string path, Stream request, Stream responseStream, | ||
200 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
201 | { | ||
202 | TRequest deserial; | ||
203 | using (XmlTextReader xmlReader = new XmlTextReader(request)) | ||
204 | { | ||
205 | XmlSerializer deserializer = new XmlSerializer(typeof(TRequest)); | ||
206 | deserial = (TRequest)deserializer.Deserialize(xmlReader); | ||
207 | } | ||
208 | |||
209 | TResponse response = default(TResponse); | ||
210 | if (m_tmethod(httpRequest.RemoteIPEndPoint)) | ||
211 | { | ||
212 | response = m_method(deserial); | ||
213 | } | ||
214 | |||
215 | using (XmlWriter xmlWriter = XmlTextWriter.Create(responseStream)) | ||
216 | { | ||
217 | XmlSerializer serializer = new XmlSerializer(typeof(TResponse)); | ||
218 | serializer.Serialize(xmlWriter, response); | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | |||
223 | } | ||