📄 xmlrpc.c
字号:
{ if (! xmlrpc_is_fault(faultresponse)) { error(0, "XMLRPC object is not fault response."); return -1; } return xmlrpc_response_get_faultcode(faultresponse->methodresponse);}Octstr *xmlrpc_get_faultstring(XMLRPCDocument *faultresponse){ if (! xmlrpc_is_fault(faultresponse)) { error(0, "XMLRPC object is not fault response."); return NULL; } return xmlrpc_response_get_faultstring(faultresponse->methodresponse);}/*** PARSE STATUS HANDLING***/int xmlrpc_parse_status(XMLRPCDocument *xrdoc){ if (xrdoc == NULL) return -1; return xrdoc->parse_status;}Octstr *xmlrpc_parse_error(XMLRPCDocument *xrdoc) { if (xrdoc == NULL) return NULL; return octstr_duplicate(xrdoc->parse_error);}/*------------------------------------------------- * Internal parser functions */static int parse_document(xmlDocPtr document, XMLRPCDocument *xrdoc){ xmlNodePtr node; Octstr *name; int n = 0, status = 0; node = xmlDocGetRootElement(document); /* * check if this is at least a valid root element */ if (node == NULL || node->name == NULL) { error(0, "XMLRPC: XML document - not valid root node!"); xrdoc->parse_status = XMLRPC_XMLPARSE_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: bad XML"); return -1; } name = octstr_create(node->name); if (octstr_len(name) == 0) { octstr_destroy(name); return -1; } if ((xrdoc->d_type == xr_methodcall || xrdoc->d_type == xr_undefined) && octstr_case_compare(name, octstr_imm("METHODCALL")) == 0) { xrdoc->d_type = xr_methodcall; xrdoc->methodcall = xmlrpc_call_create(NULL); octstr_destroy(name); status = parse_methodcall(document, node->xmlChildrenNode, xrdoc, xrdoc->methodcall); if (status < 0) { xmlrpc_call_destroy(xrdoc->methodcall); xrdoc->methodcall = NULL; if (xrdoc->parse_status == XMLRPC_COMPILE_OK) { xrdoc->parse_status = XMLRPC_XMLPARSE_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: bad XML"); } } else if ((xrdoc->methodcall->method_name) == NULL) { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: <methodName> tag expected!"); status = -1; } return status; } else if ((xrdoc->d_type == xr_methodresponse || xrdoc->d_type == xr_undefined) && octstr_case_compare(name, octstr_imm("METHODRESPONSE")) == 0) { xrdoc->d_type = xr_methodresponse; xrdoc->methodresponse = xmlrpc_response_create(); octstr_destroy(name); status = parse_methodresponse(document, node->xmlChildrenNode, xrdoc, xrdoc->methodresponse, &n); if (status < 0) { xmlrpc_response_destroy(xrdoc->methodresponse); xrdoc->methodresponse = NULL; } return status; } else { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: wrong root element <%s>, " "<%s> expected!", octstr_get_cstr(name), (xrdoc->d_type == xr_methodcall ? "methodCall" : "methodResponse")); octstr_destroy(name); return -1; }}static int parse_methodcall(xmlDocPtr doc, xmlNodePtr node, XMLRPCDocument *xrdoc, XMLRPCMethodCall *methodcall){ int status = 0; /* call for the parser function of the node type. */ switch (node->type) { case XML_ELEMENT_NODE: status = parse_methodcall_element(doc, node, xrdoc, methodcall); break; case XML_TEXT_NODE: case XML_COMMENT_NODE: case XML_PI_NODE: /* Text nodes, comments and PIs are ignored. */ break; /* * XML has also many other node types, these are not needed with * XML-RPC. Therefore they are assumed to be an error. */ default: xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: unknown XML node " "in the XML-RPC source."); return -1; break; } if (node->next != NULL) return parse_methodcall(doc, node->next, xrdoc, methodcall); return status;}static int parse_methodcall_element(xmlDocPtr doc, xmlNodePtr node, XMLRPCDocument *xrdoc, XMLRPCMethodCall *methodcall){ Octstr *name; xmlChar *content_buff; size_t i; /* * check if the element is allowed at this level */ if (node->name == NULL) { error(0, "XMLRPC: XML methodcall element nodes without name!"); xrdoc->parse_status = XMLRPC_XMLPARSE_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: bad XML"); return -1; } name = octstr_create(node->name); if (octstr_len(name) == 0) { octstr_destroy(name); return -1; } i = 0; while (i < NUMBER_OF_METHODCALL_ELEMENTS) { if (octstr_case_compare(name, octstr_imm(methodcall_elements[i].name)) == 0) break; ++i; } if (i == NUMBER_OF_METHODCALL_ELEMENTS) { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: unknown tag '%s' in XML source " "at level <methodCall>", octstr_get_cstr(name)); octstr_destroy(name); return -1; } octstr_destroy(name); /* * now check which type it is and process * * valid tags at this level are: * methodName [0] * params [1] */ if (i == 0) { /* this has been the <methodName> tag */ if (methodcall->method_name == NULL) { /*only one <methodName> tag allowed*/ content_buff = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); if (content_buff != NULL) { methodcall->method_name = octstr_create(content_buff); xmlFree(content_buff); } else { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: empty tag <methodName> in XML source " "at level <methodCall>"); return -1; } } else { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: duplicated tag <methodName> in XML source " "at level <methodCall>"); octstr_destroy(name); return -1; } } else { /* * ok, this has to be an <params> tag, otherwise we would * have returned previosly */ return parse_params(doc, node->xmlChildrenNode, xrdoc, methodcall->params); } return 0;} static int parse_methodresponse(xmlDocPtr doc, xmlNodePtr node, XMLRPCDocument *xrdoc, XMLRPCMethodResponse *methodresponse, int* n){ int status = 0; /* call for the parser function of the node type. */ switch (node->type) { case XML_ELEMENT_NODE: if (*n > 0) { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: unexpected XML node <%s> " "in the XML-RPC source.", node->name); return -1; } status = parse_methodresponse_element(doc, node, xrdoc, methodresponse); (*n)++; break; case XML_TEXT_NODE: case XML_COMMENT_NODE: case XML_PI_NODE: /* Text nodes, comments and PIs are ignored. */ break; /* * XML has also many other node types, these are not needed with * XML-RPC. Therefore they are assumed to be an error. */ default: xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: unknown XML node " "in the XML-RPC source."); return -1; break; } if (node->next != NULL) { if (parse_methodresponse(doc, node->next, xrdoc, methodresponse, n) == -1) { return -1; } } return status;}static int parse_methodresponse_element(xmlDocPtr doc, xmlNodePtr node, XMLRPCDocument *xrdoc, XMLRPCMethodResponse *methodresponse){ Octstr *name; size_t i; int status; /* * check if the element is allowed at this level */ if (node->name == NULL) { error(0, "XMLRPC: XML methodResponse element nodes without name!"); xrdoc->parse_status = XMLRPC_XMLPARSE_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: bad XML"); return -1; } name = octstr_create(node->name); if (octstr_len(name) == 0) { octstr_destroy(name); return -1; } i = 0; while (i < NUMBER_OF_METHODRESPONSE_ELEMENTS) { if (octstr_case_compare(name, octstr_imm(methodresponse_elements[i].name)) == 0) break; ++i; } if (i == NUMBER_OF_METHODRESPONSE_ELEMENTS) { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: unknown tag '%s' in XML source " "at level <methodResponse>", octstr_get_cstr(name)); octstr_destroy(name); return -1; } octstr_destroy(name); /* * now check which type it is and process * * valid tags at this level are: * fault [0] * params [1] */ if (i == 0) { /* this has been the <fault> tag */ methodresponse->fault = xmlrpc_fault_create(0, NULL); return parse_fault(doc, node->xmlChildrenNode, xrdoc, methodresponse->fault); } else { /* * ok, this has to be an <params> tag, otherwise we would * have returned previosly */ List *params = list_create();; status = parse_params(doc, node->xmlChildrenNode, xrdoc, params); if (status < 0) return -1; if (list_len(params) != 1) { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler:wrong number of params " "at level <methodResponse>"); list_destroy(params, xmlrpc_value_destroy_item); return -1; } methodresponse->param = list_consume(params); list_destroy(params, xmlrpc_value_destroy_item); return status; }}static int parse_params(xmlDocPtr doc, xmlNodePtr node, XMLRPCDocument *xrdoc, List *params){ int status = 0; /* call for the parser function of the node type. */ switch (node->type) { case XML_ELEMENT_NODE: status = parse_params_element(doc, node, xrdoc, params); break; case XML_TEXT_NODE: case XML_COMMENT_NODE: case XML_PI_NODE: /* Text nodes, comments and PIs are ignored. */ break; /* * XML has also many other node types, these are not needed with * XML-RPC. Therefore they are assumed to be an error. */ default: xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: unknown XML node in XML-RPC source."); return -1; break; } if (node->next != NULL) if (parse_params(doc, node->next, xrdoc, params) == -1) return -1; return status;}static int parse_params_element(xmlDocPtr doc, xmlNodePtr node, XMLRPCDocument *xrdoc, List *params){ Octstr *name; size_t i; int n = 0; /* * check if the element is allowed at this level * within <params> we only have one or more <param> */ if (node->name == NULL) { error(0, "XMLRPC: XML params element nodes without name!"); xrdoc->parse_status = XMLRPC_XMLPARSE_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: bad XML"); return -1; } name = octstr_create(node->name); if (octstr_len(name) == 0) { octstr_destroy(name); return -1; } i = 0; while (i < NUMBER_OF_PARAMS_ELEMENTS) { if (octstr_case_compare(name, octstr_imm(params_elements[i].name)) == 0) break; ++i; } if (i == NUMBER_OF_PARAMS_ELEMENTS) { xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: unknown tag '%s' " "in XML source at level <params>", octstr_get_cstr(name)); octstr_destroy(name); return -1; } octstr_destroy(name); /* * now check which type it is and process * * valid tags at this level are: * param [0] */ if (i == 0) { /* this has been a <param> tag */ if (parse_param(doc, node->xmlChildrenNode, xrdoc, params, &n) == -1) return -1; } else { /* we should never be here */ xrdoc->parse_status = XMLRPC_PARSING_FAILED; xrdoc->parse_error = octstr_format("XML-RPC compiler: bogus parsing exception in parse_params!"); return -1; } return 0;}static int parse_param(xmlDocPtr doc, xmlNodePtr node, XMLRPCDocument *xrdoc, List *params, int *n){ int status = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -