From bdc792d319601caa93790b21c33b3b623a4ac13c Mon Sep 17 00:00:00 2001 From: Dr Scofield Date: Thu, 22 May 2008 12:00:01 +0000 Subject: here are further enhancements to the IHttpAgentHandler and to BaseHttpServer (from awebb) i've added the OSHttpStatusCodes enumeration of HTTP status codes, have adapted BaseHttpServer to use those. then RestPlugin now has proper Failure handling returning proper HTTP status codes. Regions/POSTHandler is work-in-progress. --- OpenSim/ApplicationPlugins/Rest/RestPlugin.cs | 60 +++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 9 deletions(-) (limited to 'OpenSim/ApplicationPlugins/Rest/RestPlugin.cs') diff --git a/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs b/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs index 4b8cdc1..f1ca83d 100644 --- a/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs +++ b/OpenSim/ApplicationPlugins/Rest/RestPlugin.cs @@ -66,7 +66,7 @@ namespace OpenSim.ApplicationPlugins.Rest private string _prefix; // URL prefix below // which all REST URLs // are living - private StringWriter _sw = null; + private StringWriter _sw = null; private XmlTextWriter _xw = null; private string _godkey; @@ -240,7 +240,8 @@ namespace OpenSim.ApplicationPlugins.Rest } } - private List _handlers = new List(); + private List _handlers = new List(); + private Dictionary _agents = new Dictionary(); /// /// Add a REST stream handler to the underlying HTTP server. @@ -271,15 +272,39 @@ namespace OpenSim.ApplicationPlugins.Rest /// /// name of agent handler /// agent handler method - /// true when the plugin is disabled or the agent - /// handler could not be added.. + /// false when the plugin is disabled or the agent + /// handler could not be added. Any generated exceptions are + /// allowed to drop through to the caller, i.e. ArgumentException. + /// public bool AddAgentHandler(string agentName, IHttpAgentHandler handler) { if (!IsEnabled) return false; + _agents.Add(agentName, handler); return _httpd.AddAgentHandler(agentName, handler); } /// + /// Remove a powerful Agent handler from the underlying HTTP + /// server. + /// + /// name of agent handler + /// agent handler method + /// false when the plugin is disabled or the agent + /// handler could not be removed. Any generated exceptions are + /// allowed to drop through to the caller, i.e. KeyNotFound. + /// + public bool RemoveAgentHandler(string agentName, IHttpAgentHandler handler) + { + if (!IsEnabled) return false; + if(_agents[agentName] == handler) + { + _agents.Remove(agentName); + return _httpd.RemoveAgentHandler(agentName, handler); + } + return false; + } + + /// /// 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? @@ -316,19 +341,30 @@ namespace OpenSim.ApplicationPlugins.Rest _httpd.RemoveStreamHandler(h.HttpMethod, h.Path); } _handlers = null; + foreach (KeyValuePair h in _agents) + { + _httpd.RemoveAgentHandler(h.Key,h.Value); + } + _agents = null; } /// /// Return a failure message. /// /// origin of the failure message - /// failure message /// This should probably set a return code as /// well. (?) - protected string Failure(string method, string message) + protected string Failure(OSHttpResponse response, OSHttpStatusCode status, + string method, string format, params string[] msg) { - m_log.ErrorFormat("{0} {1} failed: {2}", MsgID, method, message); - return String.Format("{0}", message); + string m = String.Format(format, msg); + + response.StatusCode = (int)status; + response.StatusDescription = m; + + m_log.ErrorFormat("{0} {1} failed: {2}", MsgID, method, m); + return String.Format("{0}", m); } /// @@ -338,8 +374,14 @@ namespace OpenSim.ApplicationPlugins.Rest /// exception causing the failure message /// This should probably set a return code as /// well. (?) - public string Failure(string method, Exception e) + public string Failure(OSHttpResponse response, OSHttpStatusCode status, + string method, Exception e) { + string m = String.Format("exception occurred: {0}", e.Message); + + response.StatusCode = (int)status; + response.StatusDescription = m; + m_log.DebugFormat("{0} {1} failed: {2}", MsgID, method, e.ToString()); m_log.ErrorFormat("{0} {1} failed: {2}", MsgID, method, e.Message); -- cgit v1.1