diff options
author | Oren Hurvitz | 2014-04-24 14:19:03 +0300 |
---|---|---|
committer | Oren Hurvitz | 2014-04-24 15:58:41 +0100 |
commit | d15a3b10a3031b9552d67d2e2d435a689b448e2f (patch) | |
tree | 647c987c479a475ab247784cedef12779e92e7ff /OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs | |
parent | Fixed: hypergrid-linking stopped accepting the following format: "http://grid... (diff) | |
download | opensim-SC_OLD-d15a3b10a3031b9552d67d2e2d435a689b448e2f.zip opensim-SC_OLD-d15a3b10a3031b9552d67d2e2d435a689b448e2f.tar.gz opensim-SC_OLD-d15a3b10a3031b9552d67d2e2d435a689b448e2f.tar.bz2 opensim-SC_OLD-d15a3b10a3031b9552d67d2e2d435a689b448e2f.tar.xz |
When sending JSON-RPC calls (for UserProfile), use WebUtil instead of constructing the HTTP requests manually. This allows the calls to be logged when using "debug http all 6".
Diffstat (limited to 'OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs')
-rw-r--r-- | OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs | 175 |
1 files changed, 77 insertions, 98 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs index a44f471..ed6a14c 100644 --- a/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs +++ b/OpenSim/Framework/Servers/HttpServer/JsonRpcRequestManager.cs | |||
@@ -48,7 +48,6 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
48 | { | 48 | { |
49 | } | 49 | } |
50 | 50 | ||
51 | #region Web Util | ||
52 | /// <summary> | 51 | /// <summary> |
53 | /// Sends json-rpc request with a serializable type. | 52 | /// Sends json-rpc request with a serializable type. |
54 | /// </summary> | 53 | /// </summary> |
@@ -70,64 +69,62 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
70 | public bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId) | 69 | public bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId) |
71 | { | 70 | { |
72 | if (jsonId == null) | 71 | if (jsonId == null) |
73 | throw new ArgumentNullException ("jsonId"); | 72 | throw new ArgumentNullException("jsonId"); |
74 | if (uri == null) | 73 | if (uri == null) |
75 | throw new ArgumentNullException ("uri"); | 74 | throw new ArgumentNullException("uri"); |
76 | if (method == null) | 75 | if (method == null) |
77 | throw new ArgumentNullException ("method"); | 76 | throw new ArgumentNullException("method"); |
78 | if (parameters == null) | 77 | if (parameters == null) |
79 | throw new ArgumentNullException ("parameters"); | 78 | throw new ArgumentNullException("parameters"); |
80 | 79 | ||
81 | // Prep our payload | 80 | OSDMap request = new OSDMap(); |
82 | OSDMap json = new OSDMap(); | 81 | request.Add("jsonrpc", OSD.FromString("2.0")); |
83 | 82 | request.Add("id", OSD.FromString(jsonId)); | |
84 | json.Add("jsonrpc", OSD.FromString("2.0")); | 83 | request.Add("method", OSD.FromString(method)); |
85 | json.Add("id", OSD.FromString(jsonId)); | 84 | request.Add("params", OSD.SerializeMembers(parameters)); |
86 | json.Add("method", OSD.FromString(method)); | 85 | |
87 | 86 | OSDMap response; | |
88 | json.Add("params", OSD.SerializeMembers(parameters)); | ||
89 | |||
90 | string jsonRequestData = OSDParser.SerializeJsonString(json); | ||
91 | byte[] content = Encoding.UTF8.GetBytes(jsonRequestData); | ||
92 | |||
93 | HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); | ||
94 | |||
95 | webRequest.ContentType = "application/json-rpc"; | ||
96 | webRequest.Method = "POST"; | ||
97 | |||
98 | //Stream dataStream = webRequest.GetRequestStream(); | ||
99 | //dataStream.Write(content, 0, content.Length); | ||
100 | //dataStream.Close(); | ||
101 | |||
102 | using (Stream dataStream = webRequest.GetRequestStream()) | ||
103 | dataStream.Write(content, 0, content.Length); | ||
104 | |||
105 | WebResponse webResponse = null; | ||
106 | try | 87 | try |
107 | { | 88 | { |
108 | webResponse = webRequest.GetResponse(); | 89 | response = WebUtil.PostToService(uri, request, 10000, true); |
90 | } | ||
91 | catch (Exception e) | ||
92 | { | ||
93 | m_log.Debug(string.Format("JsonRpc request '{0}' failed", method), e); | ||
94 | return false; | ||
95 | } | ||
96 | |||
97 | if (!response.ContainsKey("_Result")) | ||
98 | { | ||
99 | m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}", | ||
100 | method, OSDParser.SerializeJsonString(response)); | ||
101 | return false; | ||
109 | } | 102 | } |
110 | catch (WebException e) | 103 | response = (OSDMap)response["_Result"]; |
104 | |||
105 | OSD data; | ||
106 | |||
107 | if (response.ContainsKey("error")) | ||
111 | { | 108 | { |
112 | Console.WriteLine("Web Error" + e.Message); | 109 | data = response["error"]; |
113 | Console.WriteLine ("Please check input"); | 110 | m_log.DebugFormat("JsonRpc request '{0}' returned an error: {1}", |
111 | method, OSDParser.SerializeJsonString(data)); | ||
114 | return false; | 112 | return false; |
115 | } | 113 | } |
116 | 114 | ||
117 | using (webResponse) | 115 | if (!response.ContainsKey("result")) |
118 | using (Stream rstream = webResponse.GetResponseStream()) | ||
119 | { | 116 | { |
120 | OSDMap mret = (OSDMap)OSDParser.DeserializeJson(rstream); | 117 | m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}", |
121 | 118 | method, OSDParser.SerializeJsonString(response)); | |
122 | if (mret.ContainsKey("error")) | 119 | return false; |
123 | return false; | ||
124 | |||
125 | // get params... | ||
126 | OSD.DeserializeMembers(ref parameters, (OSDMap)mret["result"]); | ||
127 | return true; | ||
128 | } | 120 | } |
121 | |||
122 | data = response["result"]; | ||
123 | OSD.DeserializeMembers(ref parameters, (OSDMap)data); | ||
124 | |||
125 | return true; | ||
129 | } | 126 | } |
130 | 127 | ||
131 | /// <summary> | 128 | /// <summary> |
132 | /// Sends json-rpc request with OSD parameter. | 129 | /// Sends json-rpc request with OSD parameter. |
133 | /// </summary> | 130 | /// </summary> |
@@ -135,7 +132,7 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
135 | /// The rpc request. | 132 | /// The rpc request. |
136 | /// </returns> | 133 | /// </returns> |
137 | /// <param name='data'> | 134 | /// <param name='data'> |
138 | /// data - incoming as parameters, outgong as result/error | 135 | /// data - incoming as parameters, outgoing as result/error |
139 | /// </param> | 136 | /// </param> |
140 | /// <param name='method'> | 137 | /// <param name='method'> |
141 | /// Json-rpc method to call. | 138 | /// Json-rpc method to call. |
@@ -148,64 +145,46 @@ namespace OpenSim.Framework.Servers.HttpServer | |||
148 | /// </param> | 145 | /// </param> |
149 | public bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId) | 146 | public bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId) |
150 | { | 147 | { |
151 | OSDMap map = new OSDMap(); | 148 | if (string.IsNullOrEmpty(jsonId)) |
152 | 149 | jsonId = UUID.Random().ToString(); | |
153 | map["jsonrpc"] = "2.0"; | 150 | |
154 | if(string.IsNullOrEmpty(jsonId)) | 151 | OSDMap request = new OSDMap(); |
155 | map["id"] = UUID.Random().ToString(); | 152 | request.Add("jsonrpc", OSD.FromString("2.0")); |
156 | else | 153 | request.Add("id", OSD.FromString(jsonId)); |
157 | map["id"] = jsonId; | 154 | request.Add("method", OSD.FromString(method)); |
158 | 155 | request.Add("params", data); | |
159 | map["method"] = method; | 156 | |
160 | map["params"] = data; | 157 | OSDMap response; |
161 | |||
162 | string jsonRequestData = OSDParser.SerializeJsonString(map); | ||
163 | byte[] content = Encoding.UTF8.GetBytes(jsonRequestData); | ||
164 | |||
165 | HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); | ||
166 | webRequest.ContentType = "application/json-rpc"; | ||
167 | webRequest.Method = "POST"; | ||
168 | |||
169 | using (Stream dataStream = webRequest.GetRequestStream()) | ||
170 | dataStream.Write(content, 0, content.Length); | ||
171 | |||
172 | WebResponse webResponse = null; | ||
173 | try | 158 | try |
174 | { | 159 | { |
175 | webResponse = webRequest.GetResponse(); | 160 | response = WebUtil.PostToService(uri, request, 10000, true); |
176 | } | 161 | } |
177 | catch (WebException e) | 162 | catch (Exception e) |
178 | { | 163 | { |
179 | Console.WriteLine("Web Error" + e.Message); | 164 | m_log.Debug(string.Format("JsonRpc request '{0}' failed", method), e); |
180 | Console.WriteLine ("Please check input"); | ||
181 | return false; | 165 | return false; |
182 | } | 166 | } |
183 | 167 | ||
184 | using (webResponse) | 168 | if (!response.ContainsKey("_Result")) |
185 | using (Stream rstream = webResponse.GetResponseStream()) | ||
186 | { | 169 | { |
187 | OSDMap response = new OSDMap(); | 170 | m_log.DebugFormat("JsonRpc request '{0}' returned an invalid response: {1}", |
188 | try | 171 | method, OSDParser.SerializeJsonString(response)); |
189 | { | 172 | return false; |
190 | response = (OSDMap)OSDParser.DeserializeJson(rstream); | ||
191 | } | ||
192 | catch (Exception e) | ||
193 | { | ||
194 | m_log.DebugFormat("[JSONRPC]: JsonRpcRequest Error {0}", e.Message); | ||
195 | return false; | ||
196 | } | ||
197 | |||
198 | if (response.ContainsKey("error")) | ||
199 | { | ||
200 | data = response["error"]; | ||
201 | return false; | ||
202 | } | ||
203 | |||
204 | data = response; | ||
205 | |||
206 | return true; | ||
207 | } | 173 | } |
174 | response = (OSDMap)response["_Result"]; | ||
175 | |||
176 | if (response.ContainsKey("error")) | ||
177 | { | ||
178 | data = response["error"]; | ||
179 | m_log.DebugFormat("JsonRpc request '{0}' returned an error: {1}", | ||
180 | method, OSDParser.SerializeJsonString(data)); | ||
181 | return false; | ||
182 | } | ||
183 | |||
184 | data = response; | ||
185 | |||
186 | return true; | ||
208 | } | 187 | } |
209 | #endregion Web Util | 188 | |
210 | } | 189 | } |
211 | } | 190 | } |