/*****************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * $Id: postit2.c,v 1.5 2007-07-12 21:11:10 danf Exp $
 *
 * Example code that uploads a file name 'foo' to a remote script that accepts
 * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
 *
 * The imaginary form we'll fill in looks like:
 *
 * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
 * Enter file: <input type="file" name="sendfile" size="40">
 * Enter file name: <input type="text" name="filename" size="30">
 * <input type="submit" value="send" name="submit">
 * </form>
 *
 * This exact source code has not been verified to work.
 */ 
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <malloc.h>

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>

#define PA "<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cwmp=\"urn:dslforum-org:cwmp-1-0\"><SOAP-ENV:header><cwmp:ID SOAP-ENV:mustUnderstand=\"1\">123456789</cwmp:ID><cwmp:HoldRequests SOAP-ENV:mustUnderstand=\"1\">0</cwmp:HoldRequests><cwmp:NoMoreRequests>0</cwmp:NoMoreRequests></SOAP-ENV:header><SOAP-ENV:Body><cwmp:Inform><DeviceId><Manufacturer>test</Manufacturer><OUI>000E1F</OUI><ProductClass>test</ProductClass><SerialNumber>000E1F-01000000000201101F000E1F6CD761000000000000000000000000000000D7</SerialNumber></DeviceId><Event soapenc:arrayType=\"cwmp:EventStruct[1]\"><EventStruct><EventCode>0 BOOTSTRAP</EventCode><CommandKey></CommandKey></EventStruct></Event><MaxEnvelopes>1</MaxEnvelopes><CurrentTime>2009-05-21T11:17:51</CurrentTime><RetryCount>0</RetryCount><ParameterList soapenc:arrayType=\"cwmp:ParameterValueStruct[2]\"><ParameterValueStruct><Name>Device.DeviceInfo.ProvisioningCode</Name><Value xsi:type=\"soapenc:string\">ProvisioningCode</Value></ParameterValueStruct><ParameterValueStruct><Name>Device.ManagementServer.PeriodicInformInterval</Name><Value xsi:type=\"soapenc:string\">60</Value></ParameterValueStruct></ParameterList></cwmp:Inform></SOAP-ENV:Body></SOAP-ENV:Envelope>"

#define OTHER "http://61.148.212.37:8189/videoServer/communication.do"


typedef struct
{
     char * memory;
     size_t size;
}memoryStruct;

void * REALLOC(void * ptr, int size)
{				
/**
 *realloc memory for not null pointer
 *malloc memory for null pointer
 */

     if(ptr)
     {
	  return (void *)realloc(ptr,size);
     }
     else
     {
	  return (void *)malloc(size);
     }
}

size_t writeMemory(void * ptr, size_t size, size_t nmemb, void * data)
{
     size_t realsize = size * nmemb; /* follow the way of
				      * curl libary to
				      * compute the memory
				      * block
				      */
     memoryStruct * mem = (memoryStruct * )data;

     mem->memory = (char *)REALLOC(mem->memory, mem->size + realsize + 1);
     if(mem->memory)
     {
	  memcpy(&(mem->memory[mem->size]), ptr, realsize);
	  mem->size += realsize;
	  mem->memory[mem->size] = '\0';
     }

     return realsize;
}

int main(int argc, char *argv[])
{
     CURL *curl;
     CURLcode res;
     memoryStruct chunk;

     chunk.memory = NULL;
     chunk.size = 0;

     struct curl_httppost *formpost=NULL;
     struct curl_httppost *lastptr=NULL;
     struct curl_slist *headerlist=NULL;
     static const char buf[] = "Expect:";
 
     curl_global_init(CURL_GLOBAL_ALL);
 
     curl = curl_easy_init();
     /* initalize custom header list (stating that Expect: 100-continue is not
	wanted */ 
     headerlist = curl_slist_append(headerlist, buf);
     headerlist = curl_slist_append(headerlist, "Accept:");
     headerlist = curl_slist_append(headerlist,"Content-Type: text/xml;");
     headerlist = curl_slist_append(headerlist,"Connection: Keep-Alive");
     if(curl) {
    
	  printf("start set parameters to package\n");
	  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
/* what URL that receives this POST */ 
	  curl_easy_setopt(curl,CURLOPT_HEADER,0);
	  curl_easy_setopt(curl,CURLOPT_NOBODY,0);
	  curl_easy_setopt(curl,CURLOPT_URL,OTHER);
	  curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"noname.txt");

	  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, PA);
	  curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,writeMemory);
	  curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&chunk);

	  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5);
	  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
	  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);

	  res = curl_easy_perform(curl);
	  
	  printf(chunk.memory);

	  free(chunk.memory);
	  chunk.size = 0;

	  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);

	  printf("test if stop\n");
	  res = curl_easy_perform(curl);
	  printf("test after stop\n");
	  printf(chunk.memory);

	  free(chunk.memory);
	  chunk.size = 0;

	  
	  /* always cleanup */ 
	  curl_easy_cleanup(curl);
 
	  /* then cleanup the formpost chain */ 
	  curl_formfree(formpost);
	  /* free slist */ 
	  curl_slist_free_all (headerlist);
     }
     return 0;
}

