
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <assert.h>
#include "base64.h"
#define file_location "/home/ravi/Desktop/single_data.xml"


struct WriteThis {
  const char *readptr;
  long sizeleft;
};

static size_t header_callback(void *ptr, size_t size,
                              size_t nmemb, void *userdata)
{
		

  struct WriteThis *pooh = (struct WriteThis *)userdata;
 //printf("data=%s", pooh->readptr);
// printf("data_len=%ld",pooh->sizeleft);
	
  if(size*nmemb < 1)
    return 0;
 
  if(pooh->sizeleft) {
    *(char *)ptr = pooh->readptr[0]; // copy one single byte  
    pooh->readptr++;                 //advance pointer  
    pooh->sizeleft--;                // less data left / 
              //we return 1 byte at a time! 
  }
  //printf("data= %s",(char *)ptr);
  return size * nmemb;
}

int main(int argc, char *argv[])
{
FILE *f;
	size_t len, elen;
	unsigned char *buf, *e;
	//char *e;	
	CURL *curl;
    CURLcode res;
     struct WriteThis pooh;
    char *url = "https://ice.connect2.me/Ice.svc/PostFeedData?apikey=z7s1iZ1014kWrrVfukW1RolO39cEwg&feedid=4ZbVetqqRHhCQXLsXR08OKg@ZSE5qT4pc3Xz3uNgURs=";
   
	curl = curl_easy_init();
	curl_global_init(CURL_GLOBAL_ALL);
	
	
	f = fopen(file_location, "r");
	if (f == NULL)
		return -1;
	fseek(f, 0, SEEK_END);
	len = ftell(f);
	fseek(f, 0, SEEK_SET);
	buf = malloc(len);
	if (buf == NULL) {
		fclose(f);
		return -1;
	}
	fread(buf, 1, len, f);
	fclose(f);

	e = base64_encode(buf, len, &elen);		
	if (e == NULL)
		return -2;
		
	//printf("encoded data= %s",e);
	pooh.readptr = e;
	pooh.sizeleft = (long)strlen(e);
	//printf("len= %ld",pooh.sizeleft);
	if(curl)
	  {		
		   struct curl_slist *slist = NULL;
		//slist = curl_slist_append(slist, "Content-Type: application/xml");
	//	curl_easy_setopt(curl, CURLOPT_HTTPHEADER,slist );
	
		curl_easy_setopt(curl, CURLOPT_HEADER, 1L);	
		curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
		curl_easy_setopt(curl, CURLOPT_HEADERDATA,&pooh );
		
		
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
		curl_easy_setopt(curl, CURLOPT_URL, url);
		//curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS,"");
		//curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,1024);
		// curl_easy_setopt(curl, CURLOPT_POST, 1L);
	//curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &pooh);
	
	   res = curl_easy_perform(curl);
	    if(res != CURLE_OK)
				fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
				
	    curl_easy_cleanup(curl);
	     curl_global_cleanup();
	    curl_slist_free_all(slist);
	}


	return 0;
}


