cURL / Mailing Lists / curl-library / Single Mail

curl-library

Sending SOAP query using CURLOPT_POST not working for me

From: <nitin.mittal_at_rsa.com>
Date: Mon, 4 Jan 2010 04:43:36 -0500

Hi,

I am working on a project in which I am supposed to send a SOAP query
over http.

While surfing on net I found the following link

http://curl.haxx.se/mail/archive-2002-07/0036.html

By taking inputs from the above link, I wrote the following sample code
to collect application/system/security events from windows server:

************************************************************************
************************************************************************
**************************
CURL *ch;
struct curl_slist *headerlist=NULL;
struct MemoryStruct *bodyStruct=NULL;
char SoapReq[2000]="";

/* imaginary callback function */
size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void
*data)
{
        register int realsize = size * nmemb;
        struct MemoryStruct *mem = (struct MemoryStruct *)data;

        mem->memory = (char *)realloc(mem->memory, mem->size + realsize
+ 1); // Application crashes here.
        if (mem->memory)
        {
                memcpy(&(mem->memory[mem->size]), ptr, realsize);
                mem->size += realsize;
                mem->memory[mem->size] = 0;
        }
        return realsize;
}

void Init()
{
        ch = curl_easy_init();
        curl_easy_setopt(ch, CURLOPT_URL,
"http://10.31.251.161:5985/wsman");
        curl_easy_setopt(ch, CURLOPT_POST, 1);
        curl_easy_setopt(ch, CURLOPT_HEADER, 1);
        curl_easy_setopt(ch, CURLOPT_VERBOSE, 1);
        curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);
        if ((bodyStruct = (struct MemoryStruct *) malloc(sizeof(struct
MemoryStruct))) == NULL) exit(1);
        curl_easy_setopt(ch, CURLOPT_FILE, bodyStruct);
}

void sendQuery(const char *arg1)
{
  // I make sure that headerlist is erased before rebuilding it with the
soap function
  curl_slist_free_all(headerlist);
  headerlist = curl_slist_append(headerlist, "Content-Type: text/xml");
  headerlist = curl_slist_append(headerlist, "SOAPAction: \"\"");
  sprintf(SoapReq, "<s:Envelope
xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"
xmlns:a=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\"
xmlns:e=\"http://schemas.xmlsoap.org/ws/2004/08/eventing\"
xmlns:n=\"http://schemas.xmlsoap.org/ws/2004/09/enumeration\"
xmlns:w=\"http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd\"><s:Header><a:
To>http://10.31.251.161:5985/wsman</a:To><w:ResourceURI
s:mustUnderstand=\"true\">http://schemas.microsoft.com/wbem/wsman/1/wind
ows/EventLog</w:ResourceURI><a:ReplyTo><a:Address
s:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/08/addressi
ng/role/anonymous</a:Address></a:ReplyTo><a:Action
s:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2004/08/eventing
/Subscribe</a:Action><w:MaxEnvelopeSize
s:mustUnderstand=\"true\">153600</w:MaxEnvelopeSize><a:MessageID>%s</a:M
essageID><w:Locale xml:lang=\"en-US\" s:mustUnderstand=\"false\"
/><w:OptionSet
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><w:Option
Name=\"SubscriptionName\">RSAenVision</w:Option><w:Option
Name=\"ContentFormat\">RenderedText</w:Option><w:Option
Name=\"ReadExistingEvents\" xsi:nil=\"true\"/><w:Option
Name=\"IgnoreChannelError\"
xsi:nil=\"true\"/></w:OptionSet><w:OperationTimeout>PT60.000S</w:Operati
onTimeout></s:Header><s:Body><e:Subscribe><e:Delivery
Mode=\"http://schemas.dmtf.org/wbem/wsman/1/wsman/Pull\"><w:Heartbeats>P
T2000.000S</w:Heartbeats><w:Locale
xml:lang=\"en-US\"/><w:ContentEncoding>UTF-8</w:ContentEncoding></e:Deli
very><w:Filter
Dialect=\"http://schemas.microsoft.com/win/2004/08/events/eventquery\"><
QueryList><Query Id=\"0\"><Select Path=\"Application\">*</Select><Select
Path=\"System\">*</Select><Select
Path=\"Security\">*</Select></Query></QueryList></w:Filter><w:SendBookma
rks/></e:Subscribe></s:Body></s:Envelope>", arg1);
  curl_easy_setopt(ch, CURLOPT_POSTFIELDS, SoapReq);
  curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headerlist);
  curl_easy_perform(ch);
}

void EndReq()
{
        free(bodyStruct);
        curl_slist_free_all(headerlist);
        curl_easy_cleanup(ch);
}

////////////////////////
void LibcurlImp::GetEventsFromSource1()
{
  Init();

  //Generate a UUID to be sent with SOAP query.
        UUID uuid;
        ::ZeroMemory(&uuid, sizeof(UUID));
        ::UuidCreate(&uuid);
        UCHAR *pszUuid = NULL;
        ::UuidToString(&uuid, &pszUuid);

        char szFormatName[54] = {0};
        sprintf(szFormatName, "%s:%s", "uuid", pszUuid);
        std::string strFormatName(szFormatName);
  
   sendQuery(strFormatName.c_str());

        printf("Retour: %s\n", bodyStruct->memory);
  
   EndReq();
}
************************************************************************
************************************************************************
**************************

Could you please have a look into this & let me know if I am missing
anything. The application crashes in "WriteMemoryCallback" function at
the line marked in red.

Also, I have kept the lines, I am not sure about, in bold.

* Is there anything anything which I am missing here, that might
be the the reason for crash?

* Also, is this fine to send a SOAP query, this way?

Any help on this is really appreciated.

Thanks & Regards,
Nitin

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-01-04