Merge commit 'f54b398540698e6e09022fe77b6405624b532f5c' into careminster

This commit is contained in:
Melanie
2013-01-16 01:13:14 +00:00
18 changed files with 414 additions and 60 deletions

View File

@@ -77,6 +77,7 @@ namespace OpenSim.Framework.Servers.HttpServer
// protected HttpListener m_httpListener;
protected CoolHTTPListener m_httpListener2;
protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
protected Dictionary<string, JsonRPCMethod> jsonRpcHandlers = new Dictionary<string, JsonRPCMethod>();
protected Dictionary<string, bool> m_rpcHandlersKeepAlive = new Dictionary<string, bool>();
protected DefaultLLSDMethod m_defaultLlsdHandler = null; // <-- Moving away from the monolithic.. and going to /registered/
protected Dictionary<string, LLSDMethod> m_llsdHandlers = new Dictionary<string, LLSDMethod>();
@@ -217,6 +218,37 @@ namespace OpenSim.Framework.Servers.HttpServer
return new List<string>(m_rpcHandlers.Keys);
}
// JsonRPC
public bool AddJsonRPCHandler(string method, JsonRPCMethod handler)
{
lock(jsonRpcHandlers)
{
jsonRpcHandlers.Add(method, handler);
}
return true;
}
public JsonRPCMethod GetJsonRPCHandler(string method)
{
lock (jsonRpcHandlers)
{
if (jsonRpcHandlers.ContainsKey(method))
{
return jsonRpcHandlers[method];
}
else
{
return null;
}
}
}
public List<string> GetJsonRpcHandlerKeys()
{
lock (jsonRpcHandlers)
return new List<string>(jsonRpcHandlers.Keys);
}
public bool AddHTTPHandler(string methodName, GenericHTTPMethod handler)
{
//m_log.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName);
@@ -557,10 +589,18 @@ namespace OpenSim.Framework.Servers.HttpServer
buffer = HandleLLSDRequests(request, response);
break;
case "application/json-rpc":
if (DebugLevel >= 3)
LogIncomingToContentTypeHandler(request);
buffer = HandleJsonRpcRequests(request, response);
break;
case "text/xml":
case "application/xml":
case "application/json":
default:
//m_log.Info("[Debug BASE HTTP SERVER]: in default handler");
// Point of note.. the DoWeHaveA methods check for an EXACT path
@@ -986,6 +1026,74 @@ namespace OpenSim.Framework.Servers.HttpServer
return buffer;
}
// JsonRpc (v2.0 only)
private byte[] HandleJsonRpcRequests(OSHttpRequest request, OSHttpResponse response)
{
Stream requestStream = request.InputStream;
JsonRpcResponse jsonRpcResponse = new JsonRpcResponse();
OSDMap jsonRpcRequest = null;
try
{
jsonRpcRequest = (OSDMap)OSDParser.DeserializeJson(requestStream);
}
catch (LitJson.JsonException e)
{
jsonRpcResponse.Error.Code = ErrorCode.InternalError;
jsonRpcResponse.Error.Message = e.Message;
}
requestStream.Close();
if (jsonRpcRequest != null)
{
if (jsonRpcRequest.ContainsKey("jsonrpc") || jsonRpcRequest["jsonrpc"].AsString() == "2.0")
{
jsonRpcResponse.JsonRpc = "2.0";
// If we have no id, then it's a "notification"
if (jsonRpcRequest.ContainsKey("id"))
{
jsonRpcResponse.Id = jsonRpcRequest["id"].AsString();
}
string methodname = jsonRpcRequest["method"];
JsonRPCMethod method;
if (jsonRpcHandlers.ContainsKey(methodname))
{
lock(jsonRpcHandlers)
{
jsonRpcHandlers.TryGetValue(methodname, out method);
}
method(jsonRpcRequest, ref jsonRpcResponse);
}
else // Error no hanlder defined for requested method
{
jsonRpcResponse.Error.Code = ErrorCode.InvalidRequest;
jsonRpcResponse.Error.Message = string.Format ("No handler defined for {0}", methodname);
}
}
else // not json-rpc 2.0 could be v1
{
jsonRpcResponse.Error.Code = ErrorCode.InvalidRequest;
jsonRpcResponse.Error.Message = "Must be valid json-rpc 2.0 see: http://www.jsonrpc.org/specification";
if (jsonRpcRequest.ContainsKey("id"))
jsonRpcResponse.Id = jsonRpcRequest["id"].AsString();
}
}
response.KeepAlive = true;
string responseData = string.Empty;
responseData = jsonRpcResponse.Serialize();
byte[] buffer = Encoding.UTF8.GetBytes(responseData);
return buffer;
}
private byte[] HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response)
{
//m_log.Warn("[BASE HTTP SERVER]: We've figured out it's a LLSD Request");