diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs index 8a0340f..85b19c0 100644 --- a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs | |||
@@ -77,6 +77,7 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
77 | // protected HttpListener m_httpListener; | 77 | // protected HttpListener m_httpListener; |
78 | protected CoolHTTPListener m_httpListener2; | 78 | protected CoolHTTPListener m_httpListener2; |
79 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); | 79 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); |
80 | protected Dictionary<string, JsonRPCMethod> jsonRpcHandlers = new Dictionary<string, JsonRPCMethod>(); | ||
80 | protected Dictionary<string, bool> m_rpcHandlersKeepAlive = new Dictionary<string, bool>(); | 81 | protected Dictionary<string, bool> m_rpcHandlersKeepAlive = new Dictionary<string, bool>(); |
81 | protected DefaultLLSDMethod m_defaultLlsdHandler = null; // <-- Moving away from the monolithic.. and going to /registered/ | 82 | protected DefaultLLSDMethod m_defaultLlsdHandler = null; // <-- Moving away from the monolithic.. and going to /registered/ |
82 | protected Dictionary<string, LLSDMethod> m_llsdHandlers = new Dictionary<string, LLSDMethod>(); | 83 | protected Dictionary<string, LLSDMethod> m_llsdHandlers = new Dictionary<string, LLSDMethod>(); |
@@ -217,6 +218,37 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
217 | return new List<string>(m_rpcHandlers.Keys); | 218 | return new List<string>(m_rpcHandlers.Keys); |
218 | } | 219 | } |
219 | 220 | ||
221 | // JsonRPC | ||
222 | public bool AddJsonRPCHandler(string method, JsonRPCMethod handler) | ||
223 | { | ||
224 | lock(jsonRpcHandlers) | ||
225 | { | ||
226 | jsonRpcHandlers.Add(method, handler); | ||
227 | } | ||
228 | return true; | ||
229 | } | ||
230 | |||
231 | public JsonRPCMethod GetJsonRPCHandler(string method) | ||
232 | { | ||
233 | lock (jsonRpcHandlers) | ||
234 | { | ||
235 | if (jsonRpcHandlers.ContainsKey(method)) | ||
236 | { | ||
237 | return jsonRpcHandlers[method]; | ||
238 | } | ||
239 | else | ||
240 | { | ||
241 | return null; | ||
242 | } | ||
243 | } | ||
244 | } | ||
245 | |||
246 | public List<string> GetJsonRpcHandlerKeys() | ||
247 | { | ||
248 | lock (jsonRpcHandlers) | ||
249 | return new List<string>(jsonRpcHandlers.Keys); | ||
250 | } | ||
251 | |||
220 | public bool AddHTTPHandler(string methodName, GenericHTTPMethod handler) | 252 | public bool AddHTTPHandler(string methodName, GenericHTTPMethod handler) |
221 | { | 253 | { |
222 | //m_log.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName); | 254 | //m_log.DebugFormat("[BASE HTTP SERVER]: Registering {0}", methodName); |
@@ -556,10 +588,18 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
556 | 588 | ||
557 | buffer = HandleLLSDRequests(request, response); | 589 | buffer = HandleLLSDRequests(request, response); |
558 | break; | 590 | break; |
591 | |||
592 | case "application/json-rpc": | ||
593 | if (DebugLevel >= 3) | ||
594 | LogIncomingToContentTypeHandler(request); | ||
595 | |||
596 | buffer = HandleJsonRpcRequests(request, response); | ||
597 | break; | ||
559 | 598 | ||
560 | case "text/xml": | 599 | case "text/xml": |
561 | case "application/xml": | 600 | case "application/xml": |
562 | case "application/json": | 601 | case "application/json": |
602 | |||
563 | default: | 603 | default: |
564 | //m_log.Info("[Debug BASE HTTP SERVER]: in default handler"); | 604 | //m_log.Info("[Debug BASE HTTP SERVER]: in default handler"); |
565 | // Point of note.. the DoWeHaveA methods check for an EXACT path | 605 | // Point of note.. the DoWeHaveA methods check for an EXACT path |
@@ -985,6 +1025,74 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
985 | return buffer; | 1025 | return buffer; |
986 | } | 1026 | } |
987 | 1027 | ||
1028 | // JsonRpc (v2.0 only) | ||
1029 | private byte[] HandleJsonRpcRequests(OSHttpRequest request, OSHttpResponse response) | ||
1030 | { | ||
1031 | Stream requestStream = request.InputStream; | ||
1032 | JsonRpcResponse jsonRpcResponse = new JsonRpcResponse(); | ||
1033 | OSDMap jsonRpcRequest = null; | ||
1034 | |||
1035 | try | ||
1036 | { | ||
1037 | jsonRpcRequest = (OSDMap)OSDParser.DeserializeJson(requestStream); | ||
1038 | } | ||
1039 | catch (LitJson.JsonException e) | ||
1040 | { | ||
1041 | jsonRpcResponse.Error.Code = ErrorCode.InternalError; | ||
1042 | jsonRpcResponse.Error.Message = e.Message; | ||
1043 | } | ||
1044 | |||
1045 | requestStream.Close(); | ||
1046 | |||
1047 | if (jsonRpcRequest != null) | ||
1048 | { | ||
1049 | if (jsonRpcRequest.ContainsKey("jsonrpc") || jsonRpcRequest["jsonrpc"].AsString() == "2.0") | ||
1050 | { | ||
1051 | jsonRpcResponse.JsonRpc = "2.0"; | ||
1052 | |||
1053 | // If we have no id, then it's a "notification" | ||
1054 | if (jsonRpcRequest.ContainsKey("id")) | ||
1055 | { | ||
1056 | jsonRpcResponse.Id = jsonRpcRequest["id"].AsString(); | ||
1057 | } | ||
1058 | |||
1059 | string methodname = jsonRpcRequest["method"]; | ||
1060 | JsonRPCMethod method; | ||
1061 | |||
1062 | if (jsonRpcHandlers.ContainsKey(methodname)) | ||
1063 | { | ||
1064 | lock(jsonRpcHandlers) | ||
1065 | { | ||
1066 | jsonRpcHandlers.TryGetValue(methodname, out method); | ||
1067 | } | ||
1068 | |||
1069 | method(jsonRpcRequest, ref jsonRpcResponse); | ||
1070 | } | ||
1071 | else // Error no hanlder defined for requested method | ||
1072 | { | ||
1073 | jsonRpcResponse.Error.Code = ErrorCode.InvalidRequest; | ||
1074 | jsonRpcResponse.Error.Message = string.Format ("No handler defined for {0}", methodname); | ||
1075 | } | ||
1076 | } | ||
1077 | else // not json-rpc 2.0 could be v1 | ||
1078 | { | ||
1079 | jsonRpcResponse.Error.Code = ErrorCode.InvalidRequest; | ||
1080 | jsonRpcResponse.Error.Message = "Must be valid json-rpc 2.0 see: http://www.jsonrpc.org/specification"; | ||
1081 | |||
1082 | if (jsonRpcRequest.ContainsKey("id")) | ||
1083 | jsonRpcResponse.Id = jsonRpcRequest["id"].AsString(); | ||
1084 | } | ||
1085 | } | ||
1086 | |||
1087 | response.KeepAlive = true; | ||
1088 | string responseData = string.Empty; | ||
1089 | |||
1090 | responseData = jsonRpcResponse.Serialize(); | ||
1091 | |||
1092 | byte[] buffer = Encoding.UTF8.GetBytes(responseData); | ||
1093 | return buffer; | ||
1094 | } | ||
1095 | |||
988 | private byte[] HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response) | 1096 | private byte[] HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response) |
989 | { | 1097 | { |
990 | //m_log.Warn("[BASE HTTP SERVER]: We've figured out it's a LLSD Request"); | 1098 | //m_log.Warn("[BASE HTTP SERVER]: We've figured out it's a LLSD Request"); |