diff options
author | BlueWall | 2013-01-15 10:04:16 -0500 |
---|---|---|
committer | BlueWall | 2013-01-15 10:04:16 -0500 |
commit | ca3e0d67d230277bd5992582248c81b796375384 (patch) | |
tree | 3d5f2fb2e567a857149b38e338928a818299eb5e | |
parent | BulletSim: fix the 'No recognised physics mesh found ...' error spew by remem... (diff) | |
download | opensim-SC-ca3e0d67d230277bd5992582248c81b796375384.zip opensim-SC-ca3e0d67d230277bd5992582248c81b796375384.tar.gz opensim-SC-ca3e0d67d230277bd5992582248c81b796375384.tar.bz2 opensim-SC-ca3e0d67d230277bd5992582248c81b796375384.tar.xz |
Add Json-Rpc 2.0 To Registered Handlers
Added registration json-rpc handlers in the http server. Covers version 2.0
See: http://www.jsonrpc.org/specification
5 files changed, 298 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"); |
diff --git a/OpenSim/Framework/Servers/HttpServer/Interfaces/IHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/Interfaces/IHttpServer.cs index 0bd3aae..13b5dd3 100644 --- a/OpenSim/Framework/Servers/HttpServer/Interfaces/IHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/Interfaces/IHttpServer.cs | |||
@@ -97,6 +97,8 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
97 | bool AddXmlRPCHandler(string method, XmlRpcMethod handler); | 97 | bool AddXmlRPCHandler(string method, XmlRpcMethod handler); |
98 | bool AddXmlRPCHandler(string method, XmlRpcMethod handler, bool keepAlive); | 98 | bool AddXmlRPCHandler(string method, XmlRpcMethod handler, bool keepAlive); |
99 | 99 | ||
100 | bool AddJsonRPCHandler(string method, JsonRPCMethod handler); | ||
101 | |||
100 | /// <summary> | 102 | /// <summary> |
101 | /// Gets the XML RPC handler for given method name | 103 | /// Gets the XML RPC handler for given method name |
102 | /// </summary> | 104 | /// </summary> |
diff --git a/OpenSim/Framework/Servers/HttpServer/JsonRPCMethod.cs b/OpenSim/Framework/Servers/HttpServer/JsonRPCMethod.cs new file mode 100644 index 0000000..7334049 --- /dev/null +++ b/OpenSim/Framework/Servers/HttpServer/JsonRPCMethod.cs | |||
@@ -0,0 +1,34 @@ | |||
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 OpenSimulator 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 | using System.Net; | ||
29 | using OpenMetaverse.StructuredData; | ||
30 | |||
31 | namespace OpenSim.Framework.Servers.HttpServer | ||
32 | { | ||
33 | public delegate void JsonRPCMethod(OSDMap jsonRpcRequest, ref JsonRpcResponse response); | ||
34 | } | ||
diff --git a/OpenSim/Framework/Servers/HttpServer/JsonRpcResponse.cs b/OpenSim/Framework/Servers/HttpServer/JsonRpcResponse.cs new file mode 100644 index 0000000..2c50587 --- /dev/null +++ b/OpenSim/Framework/Servers/HttpServer/JsonRpcResponse.cs | |||
@@ -0,0 +1,150 @@ | |||
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 OpenSimulator 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 | using System; | ||
28 | using System.Net; | ||
29 | using OpenMetaverse.StructuredData; | ||
30 | |||
31 | namespace OpenSim.Framework.Servers.HttpServer | ||
32 | { | ||
33 | public sealed class ErrorCode | ||
34 | { | ||
35 | private ErrorCode() {} | ||
36 | |||
37 | public const int ParseError = -32700; | ||
38 | public const int InvalidRequest = -32600; | ||
39 | public const int MethodNotFound = -32601; | ||
40 | public const int InvalidParams = -32602; | ||
41 | public const int InternalError = -32604; | ||
42 | |||
43 | } | ||
44 | |||
45 | public class JsonRpcError | ||
46 | { | ||
47 | internal OSDMap Error = new OSDMap(); | ||
48 | |||
49 | public int Code | ||
50 | { | ||
51 | get | ||
52 | { | ||
53 | if (Error.ContainsKey("code")) | ||
54 | return Error["code"].AsInteger(); | ||
55 | else | ||
56 | return 0; | ||
57 | } | ||
58 | set | ||
59 | { | ||
60 | Error["code"] = OSD.FromInteger(value); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | public string Message | ||
65 | { | ||
66 | get | ||
67 | { | ||
68 | if (Error.ContainsKey("message")) | ||
69 | return Error["message"].AsString(); | ||
70 | else | ||
71 | return null; | ||
72 | } | ||
73 | set | ||
74 | { | ||
75 | Error["message"] = OSD.FromString(value); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | public OSD Data | ||
80 | { | ||
81 | get; set; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | public class JsonRpcResponse | ||
86 | { | ||
87 | public string JsonRpc | ||
88 | { | ||
89 | get | ||
90 | { | ||
91 | return Reply["jsonrpc"].AsString(); | ||
92 | } | ||
93 | set | ||
94 | { | ||
95 | Reply["jsonrpc"] = OSD.FromString(value); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | public string Id | ||
100 | { | ||
101 | get | ||
102 | { | ||
103 | return Reply["id"].AsString(); | ||
104 | } | ||
105 | set | ||
106 | { | ||
107 | Reply["id"] = OSD.FromString(value); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | public OSD Result | ||
112 | { | ||
113 | get; set; | ||
114 | } | ||
115 | |||
116 | public JsonRpcError Error | ||
117 | { | ||
118 | get; set; | ||
119 | } | ||
120 | |||
121 | public OSDMap Reply = new OSDMap(); | ||
122 | |||
123 | public JsonRpcResponse() | ||
124 | { | ||
125 | Error = new JsonRpcError(); | ||
126 | } | ||
127 | |||
128 | public string Serialize() | ||
129 | { | ||
130 | if (Result != null) | ||
131 | Reply["result"] = Result; | ||
132 | |||
133 | if (Error.Code != 0) | ||
134 | { | ||
135 | Reply["error"] = (OSD)Error.Error; | ||
136 | } | ||
137 | |||
138 | string result = string.Empty; | ||
139 | try | ||
140 | { | ||
141 | result = OSDParser.SerializeJsonString(Reply); | ||
142 | } | ||
143 | catch | ||
144 | { | ||
145 | |||
146 | } | ||
147 | return result; | ||
148 | } | ||
149 | } | ||
150 | } | ||
diff --git a/OpenSim/Framework/Servers/MainServer.cs b/OpenSim/Framework/Servers/MainServer.cs index ae7d515..293887f 100644 --- a/OpenSim/Framework/Servers/MainServer.cs +++ b/OpenSim/Framework/Servers/MainServer.cs | |||
@@ -230,6 +230,10 @@ namespace OpenSim.Framework.Servers | |||
230 | List<String> poll = httpServer.GetPollServiceHandlerKeys(); | 230 | List<String> poll = httpServer.GetPollServiceHandlerKeys(); |
231 | foreach (String s in httpServer.GetHTTPHandlerKeys()) | 231 | foreach (String s in httpServer.GetHTTPHandlerKeys()) |
232 | handlers.AppendFormat("\t{0} {1}\n", s, (poll.Contains(s) ? "(poll service)" : string.Empty)); | 232 | handlers.AppendFormat("\t{0} {1}\n", s, (poll.Contains(s) ? "(poll service)" : string.Empty)); |
233 | |||
234 | handlers.AppendFormat("* JSONRPC:\n"); | ||
235 | foreach (String s in httpServer.GetJsonRpcHandlerKeys()) | ||
236 | handlers.AppendFormat("\t{0}\n", s); | ||
233 | 237 | ||
234 | // handlers.AppendFormat("* Agent:\n"); | 238 | // handlers.AppendFormat("* Agent:\n"); |
235 | // foreach (String s in httpServer.GetAgentHandlerKeys()) | 239 | // foreach (String s in httpServer.GetAgentHandlerKeys()) |