
#include <stdio.h>

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

struct MemoryStruct {
  char *memory;
  size_t size;
};

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);
  if (mem->memory) {
    memcpy(&(mem->memory[mem->size]), ptr, realsize);
    mem->size += realsize;
    mem->memory[mem->size] = 0;
  }
  return realsize;
}

int debug_callback (CURL *handle, curl_infotype type, char *message, size_t size, void *data) {
 
    char buffer[8192];
    
    if(type == CURLINFO_HEADER_OUT || type == CURLINFO_TEXT) {
        if(size > 8191) {
            strncpy(buffer, message, 8191);
            buffer[8192] = '\0';
        } else {
            strncpy(buffer, message, size);
            buffer[size] = '\0';
        }
        printf("debug_callback: %s", buffer);
    }
    return 0;   
}

int main(int argc, char **argv)
{
  CURL *curl_handle;
  int i;
  
  struct MemoryStruct header;
  struct MemoryStruct data;
  struct curl_slist *curl_list = NULL;
  char *value;
  char *url;
  long buff_size = 8192;
  long timeout = 3;
  header.memory=NULL; /* we expect realloc(NULL, size) to work */
  header.size = 0;    /* no data at this point */
  data.memory=NULL; /* we expect realloc(NULL, size) to work */
  data.size = 0;    /* no data at this point */

  curl_global_init(CURL_GLOBAL_ALL);

  curl_handle = curl_easy_init();
  
  curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);
  curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT, timeout);
  curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);
  curl_easy_setopt(curl_handle, CURLOPT_BUFFERSIZE, &buff_size);
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, WriteMemoryCallback);
  curl_easy_setopt(curl_handle, CURLOPT_DEBUGFUNCTION , debug_callback);
  curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);

  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&data);
  curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, (void *)&header);
      
  for(i = 0; i < 15; i++) {
      printf("----------------------------------------------------------------------\n");
      printf("i = %d\n", i);

      if(i == 0) {
          /*this simulates a "healthcheck*/
          curl_easy_setopt(curl_handle, CURLOPT_URL, "http://cnnsquid6/pixel.gif");
      } else {
          /*the connection is up, so make a normal request*/
          curl_easy_setopt(curl_handle, CURLOPT_URL, "http://cnnsquid6/euro?this=that");
          curl_list = curl_slist_append(curl_list, "Host: www.cnn.com");
      }
 
      curl_list = curl_slist_append(curl_list, "User-Agent: Mozilla Something");
      
      curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, curl_list);

      /* get it! */
      curl_easy_perform(curl_handle);
      free(header.memory);
      free(data.memory);
      
      header.memory=NULL; /* we expect realloc(NULL, size) to work */
      header.size = 0;    /* no data at this point */
      data.memory=NULL; /* we expect realloc(NULL, size) to work */
      data.size = 0;    /* no data at this point */
      
      curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, NULL); 
      curl_slist_free_all(curl_list);
      curl_list = NULL;

      sleep(10);
  }
  curl_easy_cleanup(curl_handle);
  return 0;
}

