From a53cea6b7e4094ea51339c80ab5fe160a19a9f6b Mon Sep 17 00:00:00 2001 From: Dr Scofield Date: Tue, 20 May 2008 16:51:45 +0000 Subject: [PATCH] i'm extending the RestStreamHandler.Handler(...) signature to actually provide OSHttpRequest and OSHttpResponse to our REST handler. also, this adds proper RestPlugin.IsGod() checking against the X-OpenSim-Godkey HTTP request header. last, i added XML doc comments to RestPlugin.cs --- .../Rest/Regions/GETRestRegionPlugin.cs | 10 +++- OpenSim/ApplicationPlugins/Rest/RestPlugin.cs | 58 +++++++++++++++++-- .../Framework/Communications/CAPSService.cs | 3 +- .../Communications/Capabilities/Caps.cs | 13 ++++- OpenSim/Framework/Servers/RestMethod.cs | 3 +- .../Framework/Servers/RestStreamHandler.cs | 2 +- OpenSim/Grid/GridServer/GridManager.cs | 24 ++++++-- OpenSim/Grid/UserServer/UserManager.cs | 6 +- .../Region/DataSnapshot/DataRequestHandler.cs | 3 +- .../Voice/AsterixVoice/AsteriskVoiceModule.cs | 6 +- .../Avatar/Voice/SIPVoice/SIPVoiceModule.cs | 6 +- 11 files changed, 110 insertions(+), 24 deletions(-) diff --git a/OpenSim/ApplicationPlugins/Rest/Regions/GETRestRegionPlugin.cs b/OpenSim/ApplicationPlugins/Rest/Regions/GETRestRegionPlugin.cs index 2b006e0dd8..a319a8b5af 100644 --- a/OpenSim/ApplicationPlugins/Rest/Regions/GETRestRegionPlugin.cs +++ b/OpenSim/ApplicationPlugins/Rest/Regions/GETRestRegionPlugin.cs @@ -54,10 +54,16 @@ namespace OpenSim.ApplicationPlugins.Rest.Regions public partial class RestRegionPlugin : RestPlugin { #region GET methods - public string GetHandler(string request, string path, string param) + public string GetHandler(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { + // foreach (string h in httpRequest.Headers.AllKeys) + // foreach (string v in httpRequest.Headers.GetValues(h)) + // m_log.DebugFormat("{0} IsGod: {1} -> {2}", MsgID, h, v); + + MsgID = RequestID; m_log.DebugFormat("{0} GET path {1} param {2}", MsgID, path, param); - + try { // param empty: regions list diff --git a/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs b/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs index 8c370ed3f5..4b8cdc114d 100644 --- a/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs +++ b/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs @@ -240,9 +240,15 @@ namespace OpenSim.ApplicationPlugins.Rest } } - private List _handlers = new List(); + /// + /// Add a REST stream handler to the underlying HTTP server. + /// + /// GET/PUT/POST/DELETE or + /// similar + /// URL prefix + /// RestMethod handler doing the actual work public virtual void AddRestStreamHandler(string httpMethod, string path, RestMethod method) { if (!IsEnabled) return; @@ -259,20 +265,50 @@ namespace OpenSim.ApplicationPlugins.Rest m_log.DebugFormat("{0} Added REST handler {1} {2}", MsgID, httpMethod, path); } - public bool AddAgentHandler(string agentname, IHttpAgentHandler handler) + /// + /// Add a powerful Agent handler to the underlying HTTP + /// server. + /// + /// name of agent handler + /// agent handler method + /// true when the plugin is disabled or the agent + /// handler could not be added.. + public bool AddAgentHandler(string agentName, IHttpAgentHandler handler) { if (!IsEnabled) return false; - return _httpd.AddAgentHandler(agentname, handler); + return _httpd.AddAgentHandler(agentName, handler); } + /// + /// Check whether the HTTP request came from god; that is, is + /// the god_key as configured in the config section supplied + /// via X-OpenSim-Godkey? + /// + /// HTTP request header + /// true when the HTTP request came from god. protected bool IsGod(OSHttpRequest request) { - string[] keys = request.Headers.GetValues("x-opensim-godkey"); + string[] keys = request.Headers.GetValues("X-OpenSim-Godkey"); if (null == keys) return false; + // we take the last key supplied return keys[keys.Length-1] == _godkey; } + /// + /// Checks wether the X-OpenSim-Password value provided in the + /// HTTP header is indeed the password on file for the avatar + /// specified by the UUID + /// + protected bool IsVerifiedUser(OSHttpRequest request, LLUUID uuid) + { + // XXX under construction + return false; + } + + /// + /// Clean up and remove all handlers that were added earlier. + /// public virtual void Close() { foreach (RestStreamHandler h in _handlers) @@ -282,12 +318,26 @@ namespace OpenSim.ApplicationPlugins.Rest _handlers = null; } + /// + /// Return a failure message. + /// + /// origin of the failure message + /// {0}", message); } + /// + /// Return a failure message. + /// + /// origin of the failure message + /// exception causing the failure message + /// This should probably set a return code as + /// well. (?) public string Failure(string method, Exception e) { m_log.DebugFormat("{0} {1} failed: {2}", MsgID, method, e.ToString()); diff --git a/OpenSim/Framework/Communications/CAPSService.cs b/OpenSim/Framework/Communications/CAPSService.cs index caf96ff1fb..07bea9d246 100644 --- a/OpenSim/Framework/Communications/CAPSService.cs +++ b/OpenSim/Framework/Communications/CAPSService.cs @@ -45,7 +45,8 @@ namespace OpenSim.Framework.Communications m_server.AddStreamHandler(new RestStreamHandler("POST", path, restMethod)); } - public string CapsRequest(string request, string path, string param) + public string CapsRequest(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { System.Console.WriteLine("new caps request " + request + " from path " + path); return String.Empty; diff --git a/OpenSim/Framework/Communications/Capabilities/Caps.cs b/OpenSim/Framework/Communications/Capabilities/Caps.cs index 9725c94fbc..8d8f945c4d 100644 --- a/OpenSim/Framework/Communications/Capabilities/Caps.cs +++ b/OpenSim/Framework/Communications/Capabilities/Caps.cs @@ -208,8 +208,11 @@ namespace OpenSim.Framework.Communications.Capabilities /// /// /// + /// HTTP request header object + /// HTTP response header object /// - public string CapsRequest(string request, string path, string param) + public string CapsRequest(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { m_log.Debug("[CAPS]: Seed Caps Request in region: " + m_regionName); //Console.WriteLine("caps request " + request); @@ -503,8 +506,11 @@ namespace OpenSim.Framework.Communications.Capabilities /// /// /// + /// HTTP request header object + /// HTTP response header object /// - public string ScriptTaskInventory(string request, string path, string param) + public string ScriptTaskInventory(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { try { @@ -558,7 +564,8 @@ namespace OpenSim.Framework.Communications.Capabilities /// /// /// - public string NoteCardAgentInventory(string request, string path, string param) + public string NoteCardAgentInventory(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { m_log.Debug("[CAPS]: NoteCardAgentInventory Request in region: " + m_regionName); //libsecondlife.StructuredData.LLSDMap hash = (libsecondlife.StructuredData.LLSDMap)libsecondlife.StructuredData.LLSDParser.DeserializeBinary(Helpers.StringToField(request)); diff --git a/OpenSim/Framework/Servers/RestMethod.cs b/OpenSim/Framework/Servers/RestMethod.cs index 1a31ef155a..6bd34f54bb 100644 --- a/OpenSim/Framework/Servers/RestMethod.cs +++ b/OpenSim/Framework/Servers/RestMethod.cs @@ -27,5 +27,6 @@ namespace OpenSim.Framework.Servers { - public delegate string RestMethod(string request, string path, string param); + public delegate string RestMethod(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse); } diff --git a/OpenSim/Framework/Servers/RestStreamHandler.cs b/OpenSim/Framework/Servers/RestStreamHandler.cs index 301b0a99ed..44c9dfc800 100644 --- a/OpenSim/Framework/Servers/RestStreamHandler.cs +++ b/OpenSim/Framework/Servers/RestStreamHandler.cs @@ -49,7 +49,7 @@ namespace OpenSim.Framework.Servers streamReader.Close(); string param = GetParam(path); - string responseString = m_restMethod(requestBody, path, param); + string responseString = m_restMethod(requestBody, path, param, httpRequest, httpResponse); return Encoding.UTF8.GetBytes(responseString); } diff --git a/OpenSim/Grid/GridServer/GridManager.cs b/OpenSim/Grid/GridServer/GridManager.cs index 83b531918a..5a6a3280ce 100644 --- a/OpenSim/Grid/GridServer/GridManager.cs +++ b/OpenSim/Grid/GridServer/GridManager.cs @@ -877,10 +877,13 @@ namespace OpenSim.Grid.GridServer /// /// /// + /// HTTP request header object + /// HTTP response header object /// - public string RestGetRegionMethod(string request, string path, string param) + public string RestGetRegionMethod(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { - return RestGetSimMethod(String.Empty, "/sims/", param); + return RestGetSimMethod(String.Empty, "/sims/", param, httpRequest, httpResponse); } /// @@ -889,10 +892,13 @@ namespace OpenSim.Grid.GridServer /// /// /// + /// HTTP request header object + /// HTTP response header object /// - public string RestSetRegionMethod(string request, string path, string param) + public string RestSetRegionMethod(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { - return RestSetSimMethod(String.Empty, "/sims/", param); + return RestSetSimMethod(String.Empty, "/sims/", param, httpRequest, httpResponse); } /// @@ -901,8 +907,11 @@ namespace OpenSim.Grid.GridServer /// /// /// A string representing the sim's UUID + /// HTTP request header object + /// HTTP response header object /// Information about the sim in XML - public string RestGetSimMethod(string request, string path, string param) + public string RestGetSimMethod(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { string respstring = String.Empty; @@ -946,8 +955,11 @@ namespace OpenSim.Grid.GridServer /// /// /// + /// HTTP request header object + /// HTTP response header object /// "OK" or an error - public string RestSetSimMethod(string request, string path, string param) + public string RestSetSimMethod(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { Console.WriteLine("Processing region update via REST method"); RegionProfileData theSim; diff --git a/OpenSim/Grid/UserServer/UserManager.cs b/OpenSim/Grid/UserServer/UserManager.cs index 1cf2a48e9e..706f461505 100644 --- a/OpenSim/Grid/UserServer/UserManager.cs +++ b/OpenSim/Grid/UserServer/UserManager.cs @@ -35,6 +35,7 @@ using log4net; using Nwc.XmlRpc; using OpenSim.Framework; using OpenSim.Framework.Communications; +using OpenSim.Framework.Servers; namespace OpenSim.Grid.UserServer { @@ -53,8 +54,11 @@ namespace OpenSim.Grid.UserServer /// The request /// The path (eg /bork/narf/test) /// Parameters sent + /// HTTP request header object + /// HTTP response header object /// Success "OK" else error - public string RestDeleteUserSessionMethod(string request, string path, string param) + public string RestDeleteUserSessionMethod(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { // TODO! Important! diff --git a/OpenSim/Region/DataSnapshot/DataRequestHandler.cs b/OpenSim/Region/DataSnapshot/DataRequestHandler.cs index abf2c1ad33..6ecae06bb0 100644 --- a/OpenSim/Region/DataSnapshot/DataRequestHandler.cs +++ b/OpenSim/Region/DataSnapshot/DataRequestHandler.cs @@ -72,7 +72,8 @@ namespace OpenSim.Region.DataSnapshot new RestStreamHandler("POST", capsBase + m_discoveryPath, OnDiscoveryAttempt)); } - public string OnDiscoveryAttempt(string request, string path, string param) + public string OnDiscoveryAttempt(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { //Very static for now, flexible enough to add new formats LLSDDiscoveryResponse llsd_response = new LLSDDiscoveryResponse(); diff --git a/OpenSim/Region/Environment/Modules/Avatar/Voice/AsterixVoice/AsteriskVoiceModule.cs b/OpenSim/Region/Environment/Modules/Avatar/Voice/AsterixVoice/AsteriskVoiceModule.cs index f8651a3062..2e8a8dace3 100644 --- a/OpenSim/Region/Environment/Modules/Avatar/Voice/AsterixVoice/AsteriskVoiceModule.cs +++ b/OpenSim/Region/Environment/Modules/Avatar/Voice/AsterixVoice/AsteriskVoiceModule.cs @@ -131,14 +131,16 @@ namespace OpenSim.Region.Environment.Modules.Avatar.Voice.AsterixVoice string capsBase = "/CAPS/" + caps.CapsObjectPath; caps.RegisterHandler("ParcelVoiceInfoRequest", new RestStreamHandler("POST", capsBase + m_parcelVoiceInfoRequestPath, - delegate(string request, string path, string param) + delegate(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { return ParcelVoiceInfoRequest(request, path, param, agentID, caps); })); caps.RegisterHandler("ProvisionVoiceAccountRequest", new RestStreamHandler("POST", capsBase + m_provisionVoiceAccountRequestPath, - delegate(string request, string path, string param) + delegate(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { return ProvisionVoiceAccountRequest(request, path, param, agentID, caps); diff --git a/OpenSim/Region/Environment/Modules/Avatar/Voice/SIPVoice/SIPVoiceModule.cs b/OpenSim/Region/Environment/Modules/Avatar/Voice/SIPVoice/SIPVoiceModule.cs index feb716e0a9..77f761f280 100644 --- a/OpenSim/Region/Environment/Modules/Avatar/Voice/SIPVoice/SIPVoiceModule.cs +++ b/OpenSim/Region/Environment/Modules/Avatar/Voice/SIPVoice/SIPVoiceModule.cs @@ -104,14 +104,16 @@ namespace OpenSim.Region.Environment.Modules.Avatar.Voice.SIPVoice string capsBase = "/CAPS/" + caps.CapsObjectPath; caps.RegisterHandler("ParcelVoiceInfoRequest", new RestStreamHandler("POST", capsBase + m_parcelVoiceInfoRequestPath, - delegate(string request, string path, string param) + delegate(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { return ParcelVoiceInfoRequest(request, path, param, agentID, caps); })); caps.RegisterHandler("ProvisionVoiceAccountRequest", new RestStreamHandler("POST", capsBase + m_provisionVoiceAccountRequestPath, - delegate(string request, string path, string param) + delegate(string request, string path, string param, + OSHttpRequest httpRequest, OSHttpResponse httpResponse) { return ProvisionVoiceAccountRequest(request, path, param, agentID, caps);