bool XMLRPC::getCommand(t_int & _command) { doRewind = true; t_byte cmd; getByte(cmd); _command = cmd; if (XMLRPC_RequestGetRequestType(xmlrpc_request) == xmlrpc_request_call) { const char* str = XMLRPC_RequestGetMethodName(xmlrpc_request); if (str) { _command = getCommandID(std::string(str)); } } return true; }
xml_element* XMLRPC_REQUEST_to_xml_element(XMLRPC_REQUEST request) { xml_element* wrapper = NULL; if (request) { const char* pStr = NULL; XMLRPC_REQUEST_TYPE request_type = XMLRPC_RequestGetRequestType(request); XMLRPC_VALUE xParams = XMLRPC_RequestGetData(request); wrapper = xml_elem_new(); if (request_type == xmlrpc_request_call) { pStr = ELEM_METHODCALL; } else if (request_type == xmlrpc_request_response) { pStr = ELEM_METHODRESPONSE; } if (pStr) { wrapper->name = strdup(pStr); } if(request_type == xmlrpc_request_call) { pStr = XMLRPC_RequestGetMethodName(request); if (pStr) { xml_element* method = xml_elem_new(); method->name = strdup(ELEM_METHODNAME); simplestring_add(&method->text, pStr); Q_PushTail(&wrapper->children, method); } } if (xParams) { Q_PushTail(&wrapper->children, XMLRPC_to_xml_element_worker(NULL, XMLRPC_RequestGetData(request), XMLRPC_RequestGetRequestType(request), 0)); } else { /* Despite the spec, the xml-rpc list folk want me to send an empty params element */ xml_element* params = xml_elem_new(); params->name = strdup(ELEM_PARAMS); Q_PushTail(&wrapper->children, params); } } return wrapper; }
xml_element* DANDARPC_REQUEST_to_xml_element(XMLRPC_REQUEST request) { xml_element* wrapper = NULL; xml_element* root = NULL; if(request) { XMLRPC_REQUEST_TYPE request_type = XMLRPC_RequestGetRequestType(request); const char* pStr = NULL; xml_element_attr* version = emalloc(sizeof(xml_element_attr)); version->key = estrdup(ATTR_VERSION); version->val = estrdup(VAL_VERSION_0_9); wrapper = xml_elem_new(); if(request_type == xmlrpc_request_response) { pStr = ELEM_METHODRESPONSE; } else if(request_type == xmlrpc_request_call) { pStr = ELEM_METHODCALL; } if(pStr) { wrapper->name = estrdup(pStr); } root = xml_elem_new(); root->name = estrdup(ELEM_ROOT); Q_PushTail(&root->attrs, version); Q_PushTail(&root->children, wrapper); pStr = XMLRPC_RequestGetMethodName(request); if(pStr) { xml_element* method = xml_elem_new(); method->name = estrdup(ELEM_METHODNAME); simplestring_add(&method->text, pStr); Q_PushTail(&wrapper->children, method); } Q_PushTail(&wrapper->children, DANDARPC_to_xml_element_worker(request, XMLRPC_RequestGetData(request))); } return root; }
XMLRPC_VALUE xml_element_to_XMLRPC_REQUEST_worker(XMLRPC_REQUEST request, XMLRPC_VALUE parent_vector, XMLRPC_VALUE current_val, xml_element* el) { if (!current_val) { /* This should only be the case for the first element */ current_val = XMLRPC_CreateValueEmpty(); } if (el->name) { /* first, deal with the crazy/stupid fault format */ if (!strcmp(el->name, ELEM_FAULT)) { xml_element* fault_value = (xml_element*)Q_Head(&el->children); XMLRPC_SetIsVector(current_val, xmlrpc_vector_struct); if(fault_value) { xml_element* fault_struct = (xml_element*)Q_Head(&fault_value->children); if(fault_struct) { xml_element* iter = (xml_element*)Q_Head(&fault_struct->children); while (iter) { XMLRPC_VALUE xNextVal = XMLRPC_CreateValueEmpty(); xml_element_to_XMLRPC_REQUEST_worker(request, current_val, xNextVal, iter); XMLRPC_AddValueToVector(current_val, xNextVal); iter = (xml_element*)Q_Next(&fault_struct->children); } } } } else if (!strcmp(el->name, ELEM_DATA) /* should be ELEM_ARRAY, but there is an extra level. weird */ || (!strcmp(el->name, ELEM_PARAMS) && (XMLRPC_RequestGetRequestType(request) == xmlrpc_request_call)) ) { /* this "PARAMS" concept is silly. dave?! */ xml_element* iter = (xml_element*)Q_Head(&el->children); XMLRPC_SetIsVector(current_val, xmlrpc_vector_array); while (iter) { XMLRPC_VALUE xNextVal = XMLRPC_CreateValueEmpty(); xml_element_to_XMLRPC_REQUEST_worker(request, current_val, xNextVal, iter); XMLRPC_AddValueToVector(current_val, xNextVal); iter = (xml_element*)Q_Next(&el->children); } } else if (!strcmp(el->name, ELEM_STRUCT)) { xml_element* iter = (xml_element*)Q_Head(&el->children); XMLRPC_SetIsVector(current_val, xmlrpc_vector_struct); while ( iter ) { XMLRPC_VALUE xNextVal = XMLRPC_CreateValueEmpty(); xml_element_to_XMLRPC_REQUEST_worker(request, current_val, xNextVal, iter); XMLRPC_AddValueToVector(current_val, xNextVal); iter = (xml_element*)Q_Next(&el->children); } } else if (!strcmp(el->name, ELEM_STRING) || (!strcmp(el->name, ELEM_VALUE) && Q_Size(&el->children) == 0)) { XMLRPC_SetValueString(current_val, el->text.str, el->text.len); } else if (!strcmp(el->name, ELEM_NAME)) { XMLRPC_SetValueID_Case(current_val, el->text.str, 0, xmlrpc_case_exact); } else if (!strcmp(el->name, ELEM_INT) || !strcmp(el->name, ELEM_I4)) { XMLRPC_SetValueInt(current_val, atoi(el->text.str)); } else if (!strcmp(el->name, ELEM_BOOLEAN)) { XMLRPC_SetValueBoolean(current_val, atoi(el->text.str)); } else if (!strcmp(el->name, ELEM_DOUBLE)) { XMLRPC_SetValueDouble(current_val, atof(el->text.str)); } else if (!strcmp(el->name, ELEM_DATETIME)) { XMLRPC_SetValueDateTime_ISO8601(current_val, el->text.str); } else if (!strcmp(el->name, ELEM_BASE64)) { struct buffer_st buf; base64_decode(&buf, el->text.str, el->text.len); XMLRPC_SetValueBase64(current_val, buf.data, buf.offset); buffer_delete(&buf); } else { xml_element* iter; if (!strcmp(el->name, ELEM_METHODCALL)) { if (request) { XMLRPC_RequestSetRequestType(request, xmlrpc_request_call); } } else if (!strcmp(el->name, ELEM_METHODRESPONSE)) { if (request) { XMLRPC_RequestSetRequestType(request, xmlrpc_request_response); } } else if (!strcmp(el->name, ELEM_METHODNAME)) { if (request) { XMLRPC_RequestSetMethodName(request, el->text.str); } } iter = (xml_element*)Q_Head(&el->children); while ( iter ) { xml_element_to_XMLRPC_REQUEST_worker(request, parent_vector, current_val, iter); iter = (xml_element*)Q_Next(&el->children); } } } return current_val; }
/* convert XMLRPC_REQUEST to soap xml dom. */ xml_element* SOAP_REQUEST_to_xml_element(XMLRPC_REQUEST request) { xml_element* root = xml_elem_new(); /* safety first. */ if (root) { xml_element* body = xml_elem_new(); root->name = strdup("SOAP-ENV:Envelope"); /* silly namespace stuff */ Q_PushTail(&root->attrs, new_attr("xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")); Q_PushTail(&root->attrs, new_attr("xmlns:xsi", "http://www.w3.org/1999/XMLSchema-instance")); Q_PushTail(&root->attrs, new_attr("xmlns:xsd", "http://www.w3.org/1999/XMLSchema")); Q_PushTail(&root->attrs, new_attr("xmlns:SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/")); Q_PushTail(&root->attrs, new_attr("xmlns:si", "http://soapinterop.org/xsd")); Q_PushTail(&root->attrs, new_attr("xmlns:ns6", "http://testuri.org")); Q_PushTail(&root->attrs, new_attr("SOAP-ENV:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/")); /* Q_PushHead(&root->attrs, new_attr("xmlns:ks", "http://kitchen.sink.org/soap/everything/under/sun")); JUST KIDDING!! :-) ----> ------------------------------------------------- */ if (body) { /* go ahead and serialize first... */ xml_element* el_serialized = SOAP_to_xml_element_worker(request, XMLRPC_RequestGetData(request)); /* check for fault, in which case, there is no intermediate element */ if (el_serialized && !strcmp(el_serialized->name, TOKEN_FAULT)) { Q_PushTail(&body->children, el_serialized); } /* usual case: not a fault. Add Response element in between. */ else { xml_element* rpc = xml_elem_new(); if (rpc) { const char* methodname = XMLRPC_RequestGetMethodName(request); XMLRPC_REQUEST_TYPE rtype = XMLRPC_RequestGetRequestType(request); /* if we are making a request, we want to use the methodname as is. */ if (rtype == xmlrpc_request_call) { if (methodname) { rpc->name = strdup(methodname); } } /* if it's a response, we append "Response". Also, given xmlrpc-epi API/architecture, it's likely that we don't have a methodname for the response, so we have to check that. */ else { char buf[128]; snprintf(buf, sizeof(buf), "%s%s", methodname ? methodname : "", "Response"); rpc->name = strdup(buf); } /* add serialized data to method call/response. add method call/response to body element */ if (rpc->name) { if(el_serialized) { if(Q_Size(&el_serialized->children) && rtype == xmlrpc_request_call) { xml_element* iter = (xml_element*)Q_Head(&el_serialized->children); while(iter) { Q_PushTail(&rpc->children, iter); iter = (xml_element*)Q_Next(&el_serialized->children); } xml_elem_free_non_recurse(el_serialized); } else { Q_PushTail(&rpc->children, el_serialized); } } Q_PushTail(&body->children, rpc); } else { /* no method name?! TODO: fault here...? */ } } } body->name = strdup("SOAP-ENV:Body"); Q_PushTail(&root->children, body); } } return root; }