// Encode the request to call the specified method with the specified parameters into xml bool XmlRpcCurlClient::generateRequest(const char* methodName, XmlRpcValue const& params) { std::string body = REQUEST_BEGIN; body += methodName; body += REQUEST_END_METHODNAME; // If params is an array, each element is a separate parameter if (params.valid()) { body += PARAMS_TAG; if (params.getType() == XmlRpcValue::TypeArray) { for (int i=0; i<params.size(); ++i) { body += PARAM_TAG; body += params[i].toXml(); body += PARAM_ETAG; } } else { body += PARAM_TAG; body += params.toXml(); body += PARAM_ETAG; } body += PARAMS_ETAG; } body += REQUEST_END; _request = body; return true; }
// Encode the request to call the specified method with the specified parameters into xml bool XmlRpcClient::generateRequest(const char* methodName, XmlRpcValue const& params) { std::string body = REQUEST_BEGIN; body += methodName; body += REQUEST_END_METHODNAME; // If params is an array, each element is a separate parameter if (params.valid()) { body += PARAMS_TAG; if (params.getType() == XmlRpcValue::TypeArray) { for (int i=0; i<params.size(); ++i) { body += PARAM_TAG; body += params[i].toXml(); body += PARAM_ETAG; } } else { body += PARAM_TAG; body += params.toXml(); body += PARAM_ETAG; } body += PARAMS_ETAG; } body += REQUEST_END; std::string header = generateHeader(body); XmlRpcUtil::log(4, "XmlRpcClient::generateRequest: header is %d bytes, content-length is %d.", header.length(), body.length()); _request = header + body; return true; }
void XmlRpcServerConnection::generateFaultResponse(std::string const& errorMsg, int errorCode) { const char RESPONSE_1[] = "<?xml version=\"1.0\"?>\r\n" "<methodResponse><fault>\r\n\t"; const char RESPONSE_2[] = "\r\n</fault></methodResponse>\r\n"; XmlRpcValue faultStruct; faultStruct[FAULTCODE] = errorCode; faultStruct[FAULTSTRING] = errorMsg; std::string body = RESPONSE_1 + faultStruct.toXml() + RESPONSE_2; std::string header = generateHeader(body); _response = header + body; }
bool XmlRpcCarrier::write(Protocol& proto, SizedWriter& writer) { //XmlRpc::setVerbosity(10); StringOutputStream sos; StringInputStream sis; writer.write(sos); sis.reset(sos.toString()); String header; if (sender) { header = NetType::readLine(sis); } String body = NetType::readLine(sis); //printf("Asked to write: hdr %s body %s\n", // header.c_str(), body.c_str()); Value v; //printf("HEADER %s\n", header.c_str()); if (header[0]=='q') { body = "yarp.quit"; // XMLRPC does not need a quit message, this should get stripped return false; } Bottle *bot = v.asList(); //Bottle aux; bot->fromString(body.c_str()); ConstString methodName; if (sender) { methodName = bot->get(0).toString(); *bot = bot->tail(); } XmlRpcValue args; if (bot->size()==1) { toXmlRpcValue(bot->get(0),args); } else { toXmlRpcValue(v,args); } //printf("xmlrpc block to write is %s\n", args.toXml().c_str()); std::string req; if (sender) { const Address& addr = host.isValid()?host:proto.getStreams().getRemoteAddress(); XmlRpcClient c(addr.getName().c_str(),(addr.getPort()>0)?addr.getPort():80); c.generateRequest(methodName.c_str(),args); req = c.getRequest(); } else { XmlRpcServerConnection c(0,NULL); c.generateResponse(args.toXml()); req = c.getResponse(); } int start = 0; //printf("converts to %s\n", req.c_str()); if (sender) { if (req.length()<8) { fprintf(stderr, "XmlRpcCarrier fail, %s:%d\n", __FILE__, __LINE__); return false; } for (int i=0; i<(int)req.length(); i++) { if (req[i] == '\n') { start++; break; } start++; } if (!firstRound) { Bytes b((char*)http.c_str(),http.length()); proto.os().write(b); } firstRound = false; } Bytes b((char*)req.c_str()+start,req.length()-start); //printf("WRITING [%s]\n", req.c_str()+start); proto.os().write(b); return proto.os().isOk(); }
bool XmlRpcCarrier::write(ConnectionState& proto, SizedWriter& writer) { StringOutputStream sos; StringInputStream sis; writer.write(sos); sis.reset(sos.toString()); ConstString header; if (sender) { header = sis.readLine(); } ConstString body = sis.readLine(); Value v; if (header.length()>0 && header[0]=='q') { body = "yarp.quit"; // XMLRPC does not need a quit message, this should get stripped return false; } Bottle *bot = v.asList(); bot->fromString(body.c_str()); ConstString methodName; if (sender) { methodName = bot->get(0).toString(); *bot = bot->tail(); } XmlRpcValue args; if (bot->size()==1) { toXmlRpcValue(bot->get(0),args); } else { toXmlRpcValue(v,args); } std::string req; if (sender) { const Contact& addr = host.isValid()?host:proto.getStreams().getRemoteAddress(); XmlRpcClient c(addr.getHost().c_str(),(addr.getPort()>0)?addr.getPort():80); c.generateRequest(methodName.c_str(),args); req = c.getRequest(); } else { XmlRpcServerConnection c(0,NULL); c.generateResponse(args.toXml()); req = c.getResponse(); } int start = 0; if (sender) { if (req.length()<8) { fprintf(stderr, "XmlRpcCarrier fail, %s:%d\n", __FILE__, __LINE__); return false; } for (int i=0; i<(int)req.length(); i++) { if (req[i] == '\n') { start++; break; } start++; } if (!firstRound) { Bytes b((char*)http.c_str(),http.length()); proto.os().write(b); } firstRound = false; } Bytes b((char*)req.c_str()+start,req.length()-start); proto.os().write(b); return proto.os().isOk(); }