mUpnpSoapResponse *mupnp_soap_request_post(mUpnpSoapRequest *soapReq, const char *ipaddr, int port) { mUpnpHttpResponse *httpRes; char *content; size_t contentLen; mUpnpXmlParser *xmlParser; mUpnpHttpHeader *header = NULL; mupnp_log_debug_l4("Entering...\n"); httpRes = mupnp_http_request_post(soapReq->httpReq, ipaddr, port); /* Check for HTTP response 405 Method Not Allowed */ if (mupnp_http_response_getstatuscode(httpRes) == MUPNP_HTTP_STATUS_METHOD_NOT_ALLOWED) { /* Status code implies that we need to use M-POST */ mupnp_http_request_setmethod(soapReq->httpReq, MUPNP_HTTP_MPOST); /* Add MAN header */ header = mupnp_http_header_new(); mupnp_http_header_setname(header, MUPNP_HTTP_MAN); mupnp_http_header_setvalue(header, MUPNP_HTTP_SOAP_MAN_VALUE); mupnp_http_packet_addheader((mUpnpHttpPacket*)soapReq->httpReq, header); /* Change soapaction header name to include namespace */ header = mupnp_http_packet_getheader((mUpnpHttpPacket*)soapReq->httpReq, MUPNP_HTTP_SOAP_ACTION); if (header != NULL) { mupnp_http_header_setname(header, MUPNP_HTTP_SOAP_ACTION_WITH_NS); } /* New attempt */ httpRes = mupnp_http_request_post(soapReq->httpReq, ipaddr, port); } mupnp_soap_response_sethttpresponse(soapReq->soapRes,httpRes); content = mupnp_http_response_getcontent(httpRes); contentLen = mupnp_http_response_getcontentlength(httpRes); if (content == NULL || contentLen <= 0) return soapReq->soapRes; xmlParser = mupnp_xml_parser_new(); mupnp_xml_parse(xmlParser, soapReq->soapRes->rootNodeList, content, contentLen); mupnp_xml_parser_delete(xmlParser); mupnp_log_debug_l4("Leaving...\n"); return soapReq->soapRes; }
bool round_remote_node_posthttpjsonrequest(RoundRemoteNode* node, const char* reqContent, RoundJSONObject** resObj, RoundError* err) { const char* remoteAddr = NULL; int remotePort = 0; if (!round_remote_node_getaddress(node, &remoteAddr) || !round_remote_node_getport(node, &remotePort)) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); return false; } mUpnpHttpRequest* httpReq = mupnp_http_request_new(); if (!httpReq) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); return false; } mupnp_http_request_setmethod(httpReq, MUPNP_HTTP_POST); mupnp_http_request_seturi(httpReq, ROUND_RPC_HTTP_ENDPOINT); mupnp_http_request_setcontent(httpReq, reqContent); mupnp_http_request_setcontentlength(httpReq, mupnp_strlen(reqContent)); mUpnpHttpResponse* httpRes = mupnp_http_request_post(httpReq, remoteAddr, remotePort); if (!httpRes) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_BAD_DESTINATION, err); mupnp_http_request_delete(httpReq); return false; } const char* resContent = mupnp_http_response_getcontent(httpRes); if (round_strlen(resContent) <= 0) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_BAD_DESTINATION, err); mupnp_http_request_delete(httpReq); return false; } // Set JSON Response RoundJSON* json = round_json_new(); if (!json) { round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); mupnp_http_request_delete(httpReq); } *resObj = NULL; if (round_json_parse(json, resContent, err)) { *resObj = round_json_poprootobject(json); } round_json_delete(json); // Set Error Response if (!mupnp_http_response_issuccessful(httpRes)) { if (*resObj) { RoundJSONObject* errObj; if (round_json_rpc_geterror(*resObj, &errObj)) { round_error_setjsonrpcerror(err, errObj); } } } return true; }