xmlrpcHandler()

CtrlXmlRpc is a CTRL extension including XML-RPC functions. XML-RPC is a protocol for web services, similar to SOAP but much simpler.

In order to run an XMLRPC server, you have to start the HTTP server and install a handler. "/RPC2" is the default URL for XMLRPC. See also httpConnect().

See also chapter XmlRpc, basics.

Synopsis

mixed xmlrpcHandler(string content);

Parameters

Parameter Description
content The content

Return value

Result of the function call like it is sent from the HTTP server to the client. This can be a string (XML coded result) or a dyn_string (result + additional header for HTTP) or a dyn_mixed (first entry is the result either as a string or a compressed blob. The further rows are additional header. Since a "Content-Type" header is required for a gzip'ed result, the gziped result cannot be returned as blob.

Description

CtrlXmlRpc is a CTRL extension with XmlRpc functions. XmlRpc is a protocol for web services, similar to SOAP but much simplier.

In order to run an XmlRpc server, you have to start the HTTP server and install a handler. "/RPC2" is the default URL for XmlRpc.

Example

#uses "CtrlXmlRpc"
main()
{
                                 httpServer(false, 80);
                                 httpConnect("xmlrpcHandler", "/RPC2");
}
mixed xmlrpcHandler(mixed content)
{
  // content is the content
  // decode content
  string function;
  dyn_mixed args;
  xmlrpcDecodeRequest(content, function, args);
  // Execute a little bit different according to the function
  mixed result;
  switch (function)
  {
    case "test" :
    result = test(args[1]);
    break;
  }
  // Encode the result again and return to the server
  string ret;
  xmlrpcEncodeResponse(result, ret);
  return ret;
}

Example

This is an example of the xmlrpcHandler when the function gzip() is used tocompressthe response.

# uses "CtrlXmlRpc"
main()
{
  httpServer(false, 80, 0); // Starts the HTTP server
  httpConnect("xmlrpcHandler", "/RPC2"); /* Registers the handler
  for XmlRpc
  calls */
}
// If you want to compress (gzip) the response:
mixed xmlrpcHandler(mixed content, string user, string ip,
dyn_string ds1, dyn_string ds2, int connIdx)
{
  string method; // Method name
  dyn_mixed args; // Method arguments
  mixed result; // Return value from method call
  string ret; // Encoded response
  xmlrpcDecodeRequest(content, method, args); // Decodes the
  XmlRpc request
  switch (method)
  {
    case "system.listMethods" :
    // Introspection: Return a dyn_string with all defined
    methods
    result = makeDynString();
    break;
    case "test.add" :
    // Add 2 values
    if (dynlen(args) < 2)
    return xmlrpcReturnFault(2, "Arguments missing in
    \"test.add\"");
    result = args[1] + args[2];
    break;
    default :
    // Wrong method name
    return xmlrpcReturnFault(1, "No such method");
  }
  xmlrpcEncodeResponse(result, ret);
  /* If you want to compress the result,the prerequisites are:
  The result is long enough (bigger than a TCP package)
  The server accepts gzip */
  if (strlen(ret) > 1024) &&
  strpos(httpGetHeader("Accept-Encoding"), "gzip")>= 0)
  {
    blob b;
    gzip(ret, b);
    return makeDynMixed(b, "Content-Type: text/html", "Content- 
    Encoding:gzip");
  }
  return ret;
}
string xmlrpcReturnFault(int faultCode, string faultString)
{
  string ret = "<?xml version=\"1.0\"
  encoding=\"ISO-8859-1\"?>\r\n"
  "<methodResponse>\r\n"
  " <fault>\r\n"
  " <value>\r\n"
  " <struct>\r\n"
  " <member>\r\n"
  " <name>faultCode</name>\r\n"
  "
  <value><i4>"+faultCode+"</i4></value>\r\n"
  " </member>\r\n"
  " <member>\r\n"
  " <name>faultString</name>\r\n"
  "
  <value><string>"+faultString+"</string></value>\r\n"
  " </member>\r\n"
  " </struct>\r\n"
  " </value>\r\n"
  " </fault>\r\n"
  "</methodResponse>\r\n";
  return ret; 
}

Assignment

XML RPC

Availability

CTRL