📄 soap.c
字号:
} } printf( "=========================================\n"); printf(" GetActionName -> Name : %s \n", Name); printf( "=========================================\n"); return 1;} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : int GetNodeValue(Upnp_Document XmlDoc, char * NodeName, char * NodeVal) // Description : Returns the node string based on the node tag name. // Parameters : XmlDoc : The root DOM node. // NodeName: Node name to be searched. // NodeVal : Output node val. // Return value: 1 if successfull -1 otherwise. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int GetNodeValue(Upnp_Document XmlDoc, char * NodeName, char ** VarVal){ int RetVal=-1; Upnp_NodeList NodeList; Upnp_Node RespNode=NULL; Upnp_DOMException Err; Upnp_Node temp=NULL; *VarVal = NULL; NodeList = UpnpDocument_getElementsByTagName(XmlDoc,NodeName); if (NodeList != NULL) { RespNode = UpnpNodeList_item(NodeList,0); if(RespNode != NULL) { temp=UpnpNode_getFirstChild(RespNode); *VarVal = UpnpNode_getNodeValue(temp,&Err); UpnpNode_free(temp); DBGONLY(UpnpPrintf(UPNP_INFO,SOAP,__FILE__,__LINE__,"Fn :GetVarValue : Return value is = %s\n", *VarVal);) RetVal =1; } else { UpnpNodeList_free(NodeList); return -1; } } UpnpNode_free(RespNode); UpnpNodeList_free(NodeList); return RetVal;} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : void CreateControlRequest(char * OutBuf, char * ActBuf) // Description : This function creates the complete Action request by adding the HTTP header to the input Action node // buffer. // Parameters : OutBuf : The complete action o/p request packet. // ActBuf : Action buffer(In XML format). // Return value: None //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void CreateControlRequest(char * OutBuf, char * ActBuf){ char S[]="<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding\"><s:Body>"; char E[]="</s:Body></s:Envelope>"; strcpy(OutBuf,S); strcat(OutBuf,ActBuf); strcat(OutBuf,E);} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : int GetHostHeader(char *CtrlUrl,char *Host, char *Path) // Description : This function parses the control URL to Host and path format. // // Parameters : CtrlUrl : The complete control URL path // Host : Output Host portion. // Path : Output Path portion. // Return value: None //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int GetHostHeader(char *CtrlUrl,char *Host, char *Path){ uri_type parsed_url; int return_code=-1; if ( (return_code=parse_uri(CtrlUrl,strlen(CtrlUrl),&parsed_url))==HTTP_SUCCESS ) { strncpy(Host,parsed_url.hostport.text.buff,parsed_url.hostport.text.size); Host[parsed_url.hostport.text.size]='\0'; strncpy(Path,parsed_url.pathquery.buff,parsed_url.pathquery.size); Path[parsed_url.pathquery.size]='\0'; } return return_code;}#ifdef INCLUDE_CLIENT_APIS ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : int SoapSendAction( char * ActionURL, char *ServiceType, char *ServiceVer, Upnp_Document ActNode , Upnp_Document * RespNode) //From SOAP module // Description : Creates the soap action packet, send it to the loaction specified in the control URL, receives the reply and // pass it back to the caller. // // Parameters : ActionURL : Address to send this action packet. // ServiceType : Service Type // ServiceVer : Service Version // ActNode : Input Action node // RespNode: Output response node. // Return value: UPNP_E_SUCCESS if action successfull < 0 otherwise. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int SoapSendAction( char * ActionURL, char *ServiceType, Upnp_Document ActNode , Upnp_Document * RespNode) //From SOAP module{ char *RqstBuff,*RecvBuff=NULL,ActName[NAME_SIZE]="",*XmlPtr, *Xml, Path[NAME_SIZE], Host[NAME_SIZE], *NodeVal; int retCode=UPNP_E_INVALID_ACTION,Buf_Len; Upnp_Document XmlDoc=NULL; Upnp_DOMString ActBuf=NULL; DBGONLY(UpnpPrintf(UPNP_INFO,SOAP,__FILE__,__LINE__,"Inside function SoapSendAction \n");) ActBuf = UpnpNewPrintDocument(ActNode); if( ActBuf == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"SoapSendAction:Error in getting the action node\n");) return UPNP_E_INVALID_ACTION; } if(GetActionName(ActBuf,ActName) < 0 ) { free(ActBuf); DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"SoapSendAction:Error in getting the action name\n");) return UPNP_E_INVALID_ACTION; } if(strlen(ActName) < 1 ) { free(ActBuf); return UPNP_E_INVALID_ACTION; } DBGONLY(UpnpPrintf(UPNP_INFO,SOAP,__FILE__,__LINE__,"SoapSendAction : Action name is =%s\n",ActName);) XmlPtr = (char *) malloc(strlen(ActBuf)+ XML_HEADER); if(XmlPtr == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"Error in memory allocation!!!!!!!!!!!\n");) return UPNP_E_OUTOF_MEMORY; } CreateControlRequest(XmlPtr,ActBuf); free(ActBuf); Buf_Len = HEADER_LENGTH+strlen(XmlPtr) +1; RqstBuff =(char *) malloc(Buf_Len); if(RqstBuff == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"Error in memory allocation!!!!!!!!!!!\n");)#if 1 // WONSOO , 2005. 2. 25 free(XmlPtr);#endif return UPNP_E_OUTOF_MEMORY; } if(GetHostHeader(ActionURL,Host,Path) != HTTP_SUCCESS) return UPNP_E_INVALID_URL; // Modified By Jcahn. sprintf(RqstBuff,"POST %s HTTP/1.0\r\nContent-Type: text/xml\r\nSOAPACTION:\"%s#%s\"\r\nContent-Length: %d\r\nHost: %s\r\n\r\n%s\r\n\r\n",Path,ServiceType,ActName,strlen(XmlPtr)+4,Host,XmlPtr); // End of. DBGONLY(UpnpPrintf(UPNP_PACKET,SOAP,__FILE__,__LINE__,"SoapSendAction sending buffer = \n%s\n",RqstBuff);) free(XmlPtr); // Modified By Jcahn. transferHTTPRaw(RqstBuff,strlen(RqstBuff),&RecvBuff,ActionURL); // End Of. free(RqstBuff); DBGONLY(UpnpPrintf(UPNP_PACKET,SOAP,__FILE__,__LINE__,"SoapSendAction Receved buffer\n %s\n",RecvBuff);) if( RecvBuff != NULL && strlen(RecvBuff) > MIN_LEN) { Xml= strstr(RecvBuff,"\r\n\r\n"); if(Xml == NULL) { free(RecvBuff); return UPNP_E_INVALID_ACTION; } else Xml = Xml+4; XmlDoc=UpnpParse_Buffer(Xml); if(XmlDoc == NULL) { free(RecvBuff); return UPNP_E_INVALID_ACTION; } if ((retCode=GetBufferErrorCode(RecvBuff)) > 0) { if( GetActionNode(XmlDoc, ActName, RespNode) < 0) { retCode = UPNP_E_INVALID_ACTION; } else if ( *RespNode != NULL) { retCode = UPNP_E_SUCCESS ; } else { retCode = UPNP_E_INVALID_ACTION; } } else if(GetNode(XmlDoc, "UPnPError", RespNode) > 0) { GetNodeValue(XmlDoc,"errorCode",&NodeVal); retCode = atoi(NodeVal); Upnpfree(NodeVal); } UpnpDocument_free(XmlDoc); free(RecvBuff); } else { DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"SoapSendAction: Received NULL buffer as response\n");) retCode = UPNP_E_INVALID_URL; *RespNode = NULL;#if 1 // WONSOO, 2005. 2. 25 if(RecvBuff) free(RecvBuff);#endif } return retCode;}#endif ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : int CreateControlResponse(char * OutBuf, char * ActBuf) // Description : It adds the required HTTP header and XML buffer to create the complete soap action packet. // // Parameters : OutBuf : The complete SOAP action request packet including HTTP header and XML. // ActBuf : Action buffer. // Return value: 1 if successfull. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int CreateControlResponse(char * OutBuf, char * ActBuf){ char *XmlBuf; char S[]="<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body>\n"; char E[]="</s:Body> </s:Envelope>"; XmlBuf =(char *) malloc(HEADER_LENGTH+strlen(ActBuf)); if(XmlBuf == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"Error in memory allocation!!!!!!!!!!!\n");) return UPNP_E_OUTOF_MEMORY; } strcpy(XmlBuf,S); strcat(XmlBuf,ActBuf); strcat(XmlBuf,E); AddResponseHeader("HTTP/1.1 200 OK\r\n",OutBuf,XmlBuf); free(XmlBuf); return 1;} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : int CreateControlFailure(char *OutBuf,int ErrCode, char * ErrStr) // Description : This function creates a error response packet by adding the error code and error string in the input // parameter. // // Parameters : OutBuf : The complete SOAP request packet including HTTP header and XML. // ErrCode : Error code to be included in the reply message. // ErrSt : Error string to be included in the reply message. // Return value: 1 if successfull. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int CreateControlFailure(char *OutBuf,int ErrCode, char * ErrStr){ char *XmlBuf; char S[]="<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><s:Fault><faultcode>s:Client</faultcode><faultstring>UPnPError</faultstring><detail><UPnPError xmlns=\"urn:schemas-upnp-org:control-1-0\"><errorCode>"; char E[]="</UPnPError></detail></s:Fault></s:Body></s:Envelope>"; char T[]="Unknown Error !!!!"; XmlBuf = (char *) malloc(HEADER_LENGTH+strlen(ErrStr)); if(XmlBuf == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"Error in memory allocation!!!!!!!!!!!\n");) return UPNP_E_OUTOF_MEMORY; } if (ErrStr != NULL) sprintf(XmlBuf,"%s%d</errorCode><errorDescription>%s</errorDescription>%s",S,ErrCode,ErrStr,E); else sprintf(XmlBuf,"%s%d</errorCode><errorDescription>%s</errorDescription>%s",S,ErrCode,T,E); AddResponseHeader("HTTP/1.1 500 Internal Server Error\r\n",OutBuf,XmlBuf); DBGONLY(UpnpPrintf(UPNP_PACKET,SOAP,__FILE__,__LINE__,"CreateControlFailure: Sending \n %s\n",OutBuf);)#if 1 // WONSOO add, 2005. 2. 24 free(XmlBuf);#endif return 1;} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : void CreateControlQueryMsg(char * OutBuf, char *VarName) // Description : This function creates a status variable query message. // // Parameters : OutBuf : The complete SOAP request packet including HTTP header and XML. // // Return value: 1 if successfull. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void CreateControlQueryMsg(char * OutBuf, char *VarName){ char S[]="<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding\"><s:Body> <u:QueryStateVariable xmlns:u=\"urn:schemas-upnp-org:control-1-0\"> <u:varName>"; char E[]="</u:varName> </u:QueryStateVariable> </s:Body> </s:Envelope>"; strcpy(OutBuf,S); strcat(OutBuf,VarName); strcat(OutBuf,E);} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : int CreateControlQueryResponse(char * OutBuf, char *Var) // Description : This function creates the response or reply packet for the devices. // // Parameters : OutBuf : The complete SOAP response packet including HTTP header and XML. // Var : The value of the variable. // // Return value: 1 if successfull. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int CreateControlQueryResponse(char * OutBuf, char *Var){ char * XmlBuf; char S[]="<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><s:Body><u:QueryStateVariableResponse xmlns:u=\"urn:schemas-upnp-org:control-1-0\"><return>"; char E[]="</return> </u:QueryStateVariableResponse> </s:Body> </s:Envelope>"; XmlBuf =(char *) malloc(HEADER_LENGTH+strlen(Var)); if(XmlBuf == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"Error in memory allocation!!!!!!!!!!!\n");) return UPNP_E_OUTOF_MEMORY; } strcpy(XmlBuf,S); strcat(XmlBuf,Var); strcat(XmlBuf,E); AddResponseHeader("HTTP/1.1 200 OK\r\n",OutBuf,XmlBuf); free(XmlBuf); return 1;} ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Function : int SoapGetServiceVarStatus( char * ActionURL, char *VarName, char **VarVal) //From SOAP module // Description : This function creates a status variable query message send it to the specified URL. It also // collect the response. // // Parameters : ActionURL : Address to send this variable query message. // VarName : Name of the variable. // VarVal : Output value. // Return value: None //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////#ifdef INCLUDE_CLIENT_APISint SoapGetServiceVarStatus( char * ActionURL, char *VarName, char **VarVal) //From SOAP module{ char *InBuff, LenBuf[7],*Xml,*RecvBuff,*XmlPtr,*Val; int RetVal,Buf_Len; Upnp_Document XmlDoc; Buf_Len = HEADER_LENGTH+strlen(VarName); RecvBuff = NULL; InBuff =(char *) malloc(Buf_Len); if(InBuff == NULL) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,SOAP,__FILE__,__LINE__,"Error in memory allocation!!!!!!!!!!!\n");) return UPNP_E_OUTOF_MEMORY;; } // Creating request packet strcpy(InBuff,"CONTENT-LENGTH: \r\n"); sprintf(InBuff+strlen(InBuff),"CONTENT-TYPE:text/xml\r\nSOAPACTION:urn:schemas-upnp-org:control-1-0#QueryStateVariable\r\n\r\n"); XmlPtr = InBuff+strlen(InBuff); CreateControlQueryMsg(XmlPtr, VarName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -