aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/ApplicationPlugins/Rest/Inventory/Rest.cs
diff options
context:
space:
mode:
authorDr Scofield2008-07-02 09:02:30 +0000
committerDr Scofield2008-07-02 09:02:30 +0000
commitd40bea4a8e09be1f8e87cf41405aaa60fa8826cb (patch)
tree91ef9356d8b284ac6fa5f0d588fedebe723b69ad /OpenSim/ApplicationPlugins/Rest/Inventory/Rest.cs
parentMantis#1643. Thank you Melanie for a patch that: (diff)
downloadopensim-SC_OLD-d40bea4a8e09be1f8e87cf41405aaa60fa8826cb.zip
opensim-SC_OLD-d40bea4a8e09be1f8e87cf41405aaa60fa8826cb.tar.gz
opensim-SC_OLD-d40bea4a8e09be1f8e87cf41405aaa60fa8826cb.tar.bz2
opensim-SC_OLD-d40bea4a8e09be1f8e87cf41405aaa60fa8826cb.tar.xz
From: Alan M Webb <awebb@vnet.ibm.com>
This adds REST services for inventory access. It also allows inventory uploads.
Diffstat (limited to '')
-rw-r--r--OpenSim/ApplicationPlugins/Rest/Inventory/Rest.cs479
1 files changed, 479 insertions, 0 deletions
diff --git a/OpenSim/ApplicationPlugins/Rest/Inventory/Rest.cs b/OpenSim/ApplicationPlugins/Rest/Inventory/Rest.cs
new file mode 100644
index 0000000..e88c54d
--- /dev/null
+++ b/OpenSim/ApplicationPlugins/Rest/Inventory/Rest.cs
@@ -0,0 +1,479 @@
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
29using System;
30using System.Collections.Generic;
31using System.Reflection;
32using System.Text;
33using OpenSim.Framework;
34using OpenSim.Framework.Servers;
35using OpenSim.Framework.Communications;
36using OpenSim.Framework.Communications.Cache;
37using Nini.Config;
38
39namespace OpenSim.ApplicationPlugins.Rest.Inventory
40{
41
42 public class Rest
43 {
44
45 internal static readonly log4net.ILog Log =
46 log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47
48 internal static bool DEBUG = Log.IsDebugEnabled;
49
50 /// <summary>
51 /// These values have a single value for the whole
52 /// domain and lifetime of the plugin handler. We
53 /// make them static for ease of reference within
54 /// the assembly. These are initialized by the
55 /// RestHandler class during start-up.
56 /// </summary>
57
58 internal static RestHandler Plugin = null;
59 internal static OpenSimBase main = null;
60 internal static CommunicationsManager Comms = null;
61 internal static IInventoryServices InventoryServices = null;
62 internal static IUserService UserServices = null;
63 internal static AssetCache AssetServices = null;
64 internal static string Prefix = null;
65 internal static IConfig Config = null;
66 internal static string GodKey = null;
67 internal static bool Authenticate = true;
68 internal static bool Secure = true;
69 internal static bool ExtendedEscape = true;
70 internal static bool DumpAsset = false;
71 internal static string Realm = "REST";
72 internal static Dictionary<string,string> Domains = new Dictionary<string,string>();
73 internal static int CreationDate = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
74 internal static int DumpLineSize = 32; // Should be a multiple of 16 or (possibly) 4
75
76 internal static string MsgId
77 {
78 get { return Plugin.MsgId; }
79 }
80
81 internal static string RequestId
82 {
83 get { return Plugin.RequestId; }
84 }
85
86 internal static Encoding Encoding = Encoding.UTF8;
87
88 /// <summary>
89 /// Version control for REST implementation. This
90 /// refers to the overall infrastructure represented
91 /// by the following classes
92 /// RequestData
93 /// RequestInventoryPlugin
94 /// Rest
95 /// It does no describe implementation classes such as
96 /// RestInventoryServices, which may morph much more
97 /// often. Such classes ARE dependent upon this however
98 /// and should check it in their Initialize method.
99 /// </summary>
100
101 public static readonly float Version = 1.0F;
102 public const string Name = "REST 1.0";
103
104 /// <summary>
105 /// Currently defined HTTP methods.
106 /// Only GET and HEAD are required to be
107 /// supported by all servers. See Respond
108 /// to see how these are handled.
109 /// </summary>
110
111 // REST AGENT 1.0 interpretations
112 public const string GET = "get"; // information retrieval - server state unchanged
113 public const string HEAD = "head"; // same as get except only the headers are returned.
114 public const string POST = "post"; // Replace the URI designated resource with the entity.
115 public const string PUT = "put"; // Add the entity to the context represented by the URI
116 public const string DELETE = "delete"; // Remove the URI designated resource from the server.
117
118 public const string OPTIONS = "options"; //
119 public const string TRACE = "trace"; //
120 public const string CONNECT = "connect"; //
121
122 // Define this in one place...
123
124 public const string UrlPathSeparator = "/";
125 public const string UrlMethodSeparator = ":";
126
127 // Redirection qualifications
128
129 public const bool PERMANENT = false;
130 public const bool TEMPORARY = true;
131
132 // Constant arrays used by String.Split
133
134 public static readonly char C_SPACE = ' ';
135 public static readonly char C_SLASH = '/';
136 public static readonly char C_PATHSEP = '/';
137 public static readonly char C_COLON = ':';
138 public static readonly char C_PLUS = '+';
139 public static readonly char C_PERIOD = '.';
140 public static readonly char C_COMMA = ',';
141 public static readonly char C_DQUOTE = '"';
142
143 public static readonly string CS_SPACE = " ";
144 public static readonly string CS_SLASH = "/";
145 public static readonly string CS_PATHSEP = "/";
146 public static readonly string CS_COLON = ":";
147 public static readonly string CS_PLUS = "+";
148 public static readonly string CS_PERIOD = ".";
149 public static readonly string CS_COMMA = ",";
150 public static readonly string CS_DQUOTE = "\"";
151
152 public static readonly char[] CA_SPACE = { C_SPACE };
153 public static readonly char[] CA_SLASH = { C_SLASH };
154 public static readonly char[] CA_PATHSEP = { C_PATHSEP };
155 public static readonly char[] CA_COLON = { C_COLON };
156 public static readonly char[] CA_PERIOD = { C_PERIOD };
157 public static readonly char[] CA_PLUS = { C_PLUS };
158 public static readonly char[] CA_COMMA = { C_COMMA };
159 public static readonly char[] CA_DQUOTE = { C_DQUOTE };
160
161 // HTTP Code Values (in value order)
162
163 public const int HttpStatusCodeContinue = 100;
164 public const int HttpStatusCodeSwitchingProtocols = 101;
165
166 public const int HttpStatusCodeOK = 200;
167 public const int HttpStatusCodeCreated = 201;
168 public const int HttpStatusCodeAccepted = 202;
169 public const int HttpStatusCodeNonAuthoritative = 203;
170 public const int HttpStatusCodeNoContent = 204;
171 public const int HttpStatusCodeResetContent = 205;
172 public const int HttpStatusCodePartialContent = 206;
173
174 public const int HttpStatusCodeMultipleChoices = 300;
175 public const int HttpStatusCodePermanentRedirect = 301;
176 public const int HttpStatusCodeFound = 302;
177 public const int HttpStatusCodeSeeOther = 303;
178 public const int HttpStatusCodeNotModified = 304;
179 public const int HttpStatusCodeUseProxy = 305;
180 public const int HttpStatusCodeReserved306 = 306;
181 public const int HttpStatusCodeTemporaryRedirect = 307;
182
183 public const int HttpStatusCodeBadRequest = 400;
184 public const int HttpStatusCodeNotAuthorized = 401;
185 public const int HttpStatusCodePaymentRequired = 402;
186 public const int HttpStatusCodeForbidden = 403;
187 public const int HttpStatusCodeNotFound = 404;
188 public const int HttpStatusCodeMethodNotAllowed = 405;
189 public const int HttpStatusCodeNotAcceptable = 406;
190 public const int HttpStatusCodeProxyAuthenticate = 407;
191 public const int HttpStatusCodeTimeOut = 408;
192 public const int HttpStatusCodeConflict = 409;
193 public const int HttpStatusCodeGone = 410;
194 public const int HttpStatusCodeLengthRequired = 411;
195 public const int HttpStatusCodePreconditionFailed = 412;
196 public const int HttpStatusCodeEntityTooLarge = 413;
197 public const int HttpStatusCodeUriTooLarge = 414;
198 public const int HttpStatusCodeUnsupportedMedia = 415;
199 public const int HttpStatusCodeRangeNotSatsified = 416;
200 public const int HttpStatusCodeExpectationFailed = 417;
201
202 public const int HttpStatusCodeServerError = 500;
203 public const int HttpStatusCodeNotImplemented = 501;
204 public const int HttpStatusCodeBadGateway = 502;
205 public const int HttpStatusCodeServiceUnavailable = 503;
206 public const int HttpStatusCodeGatewayTimeout = 504;
207 public const int HttpStatusCodeHttpVersionError = 505;
208
209 // HTTP Status Descriptions (in status code order)
210
211 public const string HttpStatusDescContinue = "Continue Request"; // 100
212 public const string HttpStatusDescSwitchingProtocols = "Switching Protocols"; // 101
213
214 public const string HttpStatusDescOK = "OK";
215 public const string HttpStatusDescCreated = "CREATED";
216 public const string HttpStatusDescAccepted = "ACCEPTED";
217 public const string HttpStatusDescNonAuthoritative = "NON-AUTHORITATIVE INFORMATION";
218 public const string HttpStatusDescNoContent = "NO CONTENT";
219 public const string HttpStatusDescResetContent = "RESET CONTENT";
220 public const string HttpStatusDescPartialContent = "PARTIAL CONTENT";
221
222 public const string HttpStatusDescMultipleChoices = "MULTIPLE CHOICES";
223 public const string HttpStatusDescPermanentRedirect = "PERMANENT REDIRECT";
224 public const string HttpStatusDescFound = "FOUND";
225 public const string HttpStatusDescSeeOther = "SEE OTHER";
226 public const string HttpStatusDescNotModified = "NOT MODIFIED";
227 public const string HttpStatusDescUseProxy = "USE PROXY";
228 public const string HttpStatusDescReserved306 = "RESERVED CODE 306";
229 public const string HttpStatusDescTemporaryRedirect = "TEMPORARY REDIRECT";
230
231 public const string HttpStatusDescBadRequest = "BAD REQUEST";
232 public const string HttpStatusDescNotAuthorized = "NOT AUTHORIZED";
233 public const string HttpStatusDescPaymentRequired = "PAYMENT REQUIRED";
234 public const string HttpStatusDescForbidden = "FORBIDDEN";
235 public const string HttpStatusDescNotFound = "NOT FOUND";
236 public const string HttpStatusDescMethodNotAllowed = "METHOD NOT ALLOWED";
237 public const string HttpStatusDescNotAcceptable = "NOT ACCEPTABLE";
238 public const string HttpStatusDescProxyAuthenticate = "PROXY AUTHENTICATION REQUIRED";
239 public const string HttpStatusDescTimeOut = "TIMEOUT";
240 public const string HttpStatusDescConflict = "CONFLICT";
241 public const string HttpStatusDescGone = "GONE";
242 public const string HttpStatusDescLengthRequired = "LENGTH REQUIRED";
243 public const string HttpStatusDescPreconditionFailed = "PRECONDITION FAILED";
244 public const string HttpStatusDescEntityTooLarge = "ENTITY TOO LARGE";
245 public const string HttpStatusDescUriTooLarge = "URI TOO LARGE";
246 public const string HttpStatusDescUnsupportedMedia = "UNSUPPORTED MEDIA";
247 public const string HttpStatusDescRangeNotSatisfied = "RANGE NOT SATISFIED";
248 public const string HttpStatusDescExpectationFailed = "EXPECTATION FAILED";
249
250 public const string HttpStatusDescServerError = "SERVER ERROR";
251 public const string HttpStatusDescNotImplemented = "NOT IMPLEMENTED";
252 public const string HttpStatusDescBadGateway = "BAD GATEWAY";
253 public const string HttpStatusDescServiceUnavailable = "SERVICE UNAVAILABLE";
254 public const string HttpStatusDescGatewayTimeout = "GATEWAY TIMEOUT";
255 public const string HttpStatusDescHttpVersionError = "HTTP VERSION NOT SUPPORTED";
256
257 // HTTP Headers
258
259 public const string HttpHeaderAccept = "Accept";
260 public const string HttpHeaderAcceptCharset = "Accept-Charset";
261 public const string HttpHeaderAcceptEncoding = "Accept-Encoding";
262 public const string HttpHeaderAcceptLanguage = "Accept-Language";
263 public const string HttpHeaderAcceptRanges = "Accept-Ranges";
264 public const string HttpHeaderAge = "Age";
265 public const string HttpHeaderAllow = "Allow";
266 public const string HttpHeaderAuthorization = "Authorization";
267 public const string HttpHeaderCacheControl = "Cache-Control";
268 public const string HttpHeaderConnection = "Connection";
269 public const string HttpHeaderContentEncoding = "Content-Encoding";
270 public const string HttpHeaderContentLanguage = "Content-Language";
271 public const string HttpHeaderContentLength = "Content-Length";
272 public const string HttpHeaderContentLocation = "Content-Location";
273 public const string HttpHeaderContentMD5 = "Content-MD5";
274 public const string HttpHeaderContentRange = "Content-Range";
275 public const string HttpHeaderContentType = "Content-Type";
276 public const string HttpHeaderDate = "Date";
277 public const string HttpHeaderETag = "ETag";
278 public const string HttpHeaderExpect = "Expect";
279 public const string HttpHeaderExpires = "Expires";
280 public const string HttpHeaderFrom = "From";
281 public const string HttpHeaderHost = "Host";
282 public const string HttpHeaderIfMatch = "If-Match";
283 public const string HttpHeaderIfModifiedSince = "If-Modified-Since";
284 public const string HttpHeaderIfNoneMatch = "If-None-Match";
285 public const string HttpHeaderIfRange = "If-Range";
286 public const string HttpHeaderIfUnmodifiedSince = "If-Unmodified-Since";
287 public const string HttpHeaderLastModified = "Last-Modified";
288 public const string HttpHeaderLocation = "Location";
289 public const string HttpHeaderMaxForwards = "Max-Forwards";
290 public const string HttpHeaderPragma = "Pragma";
291 public const string HttpHeaderProxyAuthenticate = "Proxy-Authenticate";
292 public const string HttpHeaderProxyAuthorization = "Proxy-Authorization";
293 public const string HttpHeaderRange = "Range";
294 public const string HttpHeaderReferer = "Referer";
295 public const string HttpHeaderRetryAfter = "Retry-After";
296 public const string HttpHeaderServer = "Server";
297 public const string HttpHeaderTE = "TE";
298 public const string HttpHeaderTrailer = "Trailer";
299 public const string HttpHeaderTransferEncoding = "Transfer-Encoding";
300 public const string HttpHeaderUpgrade = "Upgrade";
301 public const string HttpHeaderUserAgent = "User-Agent";
302 public const string HttpHeaderVary = "Vary";
303 public const string HttpHeaderVia = "Via";
304 public const string HttpHeaderWarning = "Warning";
305 public const string HttpHeaderWWWAuthenticate = "WWW-Authenticate";
306
307 /// <summary>
308 /// Supported authentication schemes
309 /// </summary>
310
311 public const string AS_BASIC = "Basic";
312 public const string AS_DIGEST = "Digest";
313
314 /// Supported Digest algorithms
315
316 public const string Digest_MD5 = "MD5"; // assumedd efault if omitted
317 public const string Digest_MD5Sess = "MD5-sess";
318
319 public const string Qop_Auth = "auth";
320 public const string Qop_Int = "auth-int";
321
322 /// Utility routines
323
324 public static string StringToBase64(string str)
325 {
326 try
327 {
328 byte[] encData_byte = new byte[str.Length];
329 encData_byte = Encoding.UTF8.GetBytes(str);
330 return Convert.ToBase64String(encData_byte);
331 }
332 catch
333 {
334 return String.Empty;
335 }
336 }
337
338 public static string Base64ToString(string str)
339 {
340 UTF8Encoding encoder = new UTF8Encoding();
341 Decoder utf8Decode = encoder.GetDecoder();
342 try
343 {
344 byte[] todecode_byte = Convert.FromBase64String(str);
345 int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
346 char[] decoded_char = new char[charCount];
347 utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
348 return new String(decoded_char);
349 }
350 catch
351 {
352 return String.Empty;
353 }
354 }
355
356 private const string hvals = "0123456789abcdef";
357
358 public static int Hex2Int(string hex)
359 {
360 int val = 0;
361 int sum = 0;
362 string tmp = null;
363
364 if (hex != null)
365 {
366 tmp = hex.ToLower();
367 for (int i = 0; i < tmp.Length; i++)
368 {
369 val = hvals.IndexOf(tmp[i]);
370 if (val == -1)
371 break;
372 sum *= 16;
373 sum += val;
374 }
375 }
376
377 return sum;
378
379 }
380
381 public static string Int2Hex8(int val)
382 {
383 string res = String.Empty;
384 for (int i = 0; i < 8; i++)
385 {
386 res = (val % 16) + res;
387 val = val / 16;
388 }
389 return res;
390 }
391
392 public static string ToHex32(int val)
393 {
394 return String.Empty;
395 }
396
397 public static string ToHex32(string val)
398 {
399 return String.Empty;
400 }
401
402 // Nonce management
403
404 public static string NonceGenerator()
405 {
406 return StringToBase64(Guid.NewGuid().ToString());
407 }
408
409 // Dump he specified data stream;
410
411 public static void Dump(byte[] data)
412 {
413
414 char[] buffer = new char[Rest.DumpLineSize];
415 int cc = 0;
416
417 for (int i = 0; i < data.Length; i++)
418 {
419
420 if (i % Rest.DumpLineSize == 0) Console.Write("\n{0}: ",i.ToString("d8"));
421
422 if (i % 4 == 0) Console.Write(" ");
423// if (i%16 == 0) Console.Write(" ");
424
425 Console.Write("{0}",data[i].ToString("x2"));
426
427 if (data[i] < 127 && data[i] > 31)
428 buffer[i % Rest.DumpLineSize] = (char) data[i];
429 else
430 buffer[i % Rest.DumpLineSize] = '.';
431
432 cc++;
433
434 if (i != 0 && (i + 1) % Rest.DumpLineSize == 0)
435 {
436 Console.Write(" |"+(new String(buffer))+"|");
437 cc = 0;
438 }
439
440 }
441
442 // Finish off any incomplete line
443
444 if (cc != 0)
445 {
446 for (int i = cc ; i < Rest.DumpLineSize; i++)
447 {
448 if (i % 4 == 0) Console.Write(" ");
449 // if (i%16 == 0) Console.Write(" ");
450 Console.Write(" ");
451 buffer[i % Rest.DumpLineSize] = ' ';
452 }
453 Console.WriteLine(" |"+(new String(buffer))+"|");
454 }
455 else
456 {
457 Console.Write("\n");
458 }
459
460 }
461
462 }
463
464 // Local exception type
465
466 public class RestException : Exception
467 {
468
469 internal int statusCode;
470 internal string statusDesc;
471 internal string httpmethod;
472 internal string httppath;
473
474 public RestException(string msg) : base(msg)
475 {
476 }
477 }
478
479}