// Parse the method name and the argument values from the request. private string ParseRequest(XmlRpcValue parms, string request) { string methodName = "unknown"; var requestDocument = XDocument.Parse(request); var methodCallElement = requestDocument.Element("methodCall"); if (methodCallElement == null) { throw new XmlRpcException("Expected <methodCall> element of XML-RPC is missing."); } var methodNameElement = methodCallElement.Element("methodName"); if (methodNameElement != null) { methodName = methodNameElement.Value; } var xmlParameters = methodCallElement.Element("params").Elements("param").ToList(); if (xmlParameters.Count > 0) { parms.SetArray(xmlParameters.Count); for (int i = 0; i < xmlParameters.Count; i++) { var value = new XmlRpcValue(); value.FromXElement(xmlParameters[i].Element("value")); parms.Set(i, value); } } return(methodName); }
/// <summary> // Parse the server response XML into an XmlRpcCallResult. /// </summary> /// <param name="responseText">A string that contains the response receiveld from the server.</param> /// <returns>An XmlRpcCallResult holding the response status and the returned XmlRpcValue.</returns> private XmlRpcCallResult ParseResponse(string responseText) { var responseDocument = XDocument.Parse(responseText); var methodResponseElement = responseDocument.Element("methodResponse"); if (methodResponseElement == null) { throw new XmlRpcException("Expected <methodResponse> element is missing."); } var paramsElement = methodResponseElement.Element("params"); var faultElement = methodResponseElement.Element("fault"); var result = new XmlRpcValue(); if (paramsElement != null) { var selection = paramsElement.Elements("param").ToList(); if (selection.Count > 1) { result.SetArray(selection.Count); for (int i = 0; i < selection.Count; i++) { var value = new XmlRpcValue(); value.FromXElement(selection[i].Element("value")); result.Set(i, value); } } else if (selection.Count == 1) { result.FromXElement(selection[0].Element("value")); } else { return(new XmlRpcCallResult { Value = result, Success = false }); } } else if (faultElement != null) { result.FromXElement(faultElement.Element("value")); return(new XmlRpcCallResult { Value = result, Success = false }); } else { throw new XmlRpcException("Invalid response - no param or fault tag found."); } return(new XmlRpcCallResult { Value = result, Success = true }); }
// Execute multiple calls and return the results in an XML RPC array. public bool ExecuteMulticall(string methodNameRoot, XmlRpcValue parms, XmlRpcValue result) { if (methodNameRoot != SYSTEM_MULTICALL) { return(false); } // There ought to be 1 parameter, an array of structs if (parms.Count != 1 || parms[0].Type != XmlRpcType.Array) { throw new XmlRpcException(SYSTEM_MULTICALL + ": Invalid argument (expected an array)"); } int nc = parms[0].Count; result.SetArray(nc); for (int i = 0; i < nc; ++i) { if (!parms[0][i].HasMember(METHODNAME) || !parms[0][i].HasMember(PARAMS)) { result[i].Set(FAULTCODE, -1); result[i].Set(FAULTSTRING, SYSTEM_MULTICALL + ": Invalid argument (expected a struct with members methodName and params)"); continue; } string methodName = parms[0][i][METHODNAME].GetString(); XmlRpcValue methodParams = parms[0][i][PARAMS]; XmlRpcValue resultValue = new XmlRpcValue(); resultValue.SetArray(1); try { if (!ExecuteMethod(methodName, methodParams, resultValue[0]) && !ExecuteMulticall(methodName, parms, resultValue[0])) { result[i].Set(FAULTCODE, -1); result[i].Set(FAULTSTRING, methodName + ": unknown method name"); } else { result[i] = resultValue; } } catch (XmlRpcException fault) { result[i].Set(FAULTCODE, 0); result[i].Set(FAULTSTRING, fault.Message); } } return(true); }
private void ListMethods(XmlRpcValue result) { result.SetArray(methods.Count + 1); int i = 0; foreach (var rec in methods) { result.Set(i++, rec.Key); } // Multicall support is built into XmlRpcServerConnection result.Set(i, MULTICALL); }