#include "easycurl.h"
#include <iostream>

using namespace std;

/* SIMPLE GET METHOD */
int main() {
	
	EasyCurl curl("www.codeproject.com");
	if (!curl.Perform()) cout << curl.GetLastError();
		
	return 1;
}

/* POST METHOD */


int main() {
	EasyCurl curl("192.168.0.1/post.php");
	curl.SetParms("name=charles&password=for_shizzle_my_nizzle");
	
	string html = curl.StringPerform();
	// DoSomethingWithHtml(html);
	cout << "HTML for URL : " << curl << "\n" << html;
}

/* MultiPart File Upload */

int main() {

	EasyCurl x;
	x.SetUrl("192.168.0.1/test.php");
	/* test.php
	   <?php

	   $uploaddir = '/www/htdocs/';

	   print "<pre>";
	   if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {
	      print "File is valid, and was successfully uploaded.  Here's some more debugging info:\n";
	      print_r($_FILES);
	   } 

	   else {
	      print "Possible file upload attack!  Here's some debugging info:\n";
	      print_r($_FILES);
	   }

	   ?>
	   
	*/

	  
	struct HttpPost* post = NULL;
	struct HttpPost* last = NULL;

	curl_formadd(&post, &last, CURLFORM_COPYNAME, "userfile",
		     CURLFORM_FILE, "easy_curl_examples.cpp", CURLFORM_END);

	
	if (!x.SetMultipartForm(post)) cout << x.GetLastError();
	if (!x.Perform()) cout << x.GetLastError();
	
	
}	

/* Cookie tests */
int main() {
	EasyCurl curl;

	curl.UseCookieEngine();

	curl.SetUrl("192.168.0.1/cookietest.php");
	
	/* cookietest.php
		
		<?php
			setcookie ("TestCookie", "testaroo", time() + 3600) or die("Cant set cookie");
		?>
	*/
	if (!curl.Perform()) cout << curl.GetLastError();
	
	curl.SetUrl("192.168.0.1/testcookie.php");
	
	/* testcookie.php
		<?=$_COOKIE['TestCookie'] ?>
	*/
	
	if (curl.StringPerform() != "testaroo") cout << "Error with setting cookie";
	else cout << "success";
	
}

/* Functinos not handle by easy wrappers */


string g_sBuffer;

size_t write_function(void  *ptr,  size_t  size,
		      size_t nmemb, void *stream) {
	g_sBuffer += (char*)ptr;
	return size*nmemb;
}
		
int main() {
	CURL* x;
	x = curl_easy_init();
	curl_easy_setopt(x,CURLOPT_WRITEFUNCTION,write_function);
	EasyCurl curl(x);
	curl.SetUrl("www.google.com");
	curl.Perform();
	cout << "\n START BUFFER" << g_sBuffer << "\n END BUFFER";
	g_sBuffer.clear();

	/* OR */
	
	EasyCurl _curl("www.google.com");
	
	curl_easy_setopt(_curl.GetHandle(),CURLOPT_WRITEFUNCTION,write_function);
	_curl.Perform(); 
	cout << "\n START BUFFER" << g_sBuffer << "\n END BUFFER";
	return 1;
}

/* ONE Big Example , dont compile this*/

int main() {
	
	EasyCurl curl;
	
	curl.SetUrl("www.foo.com/redirect.php");
	curl.SetRedirect(true);
	curl.SetProxy("192.168.0.1:8080");
	curl.ForceGet();

	if (!curl.Perform()) 
		cout << curl.GetLastError();
	
	if (curl.GetCode() != 200) 
		cout << "error";

	curl.Reset(); //clears out all the data

	curl.SetUrl("192.168.0.1/testcookie.php");
	curl.SetCookie("name=triplefoo&password=crap");
	curl.SetCookieJar("-"); //outputs to STDOUT
	curl.SetTimeout(120); //seconds
	string timeout = GetTimeoutFromUser();
	curl.SetTimeout(timeout); //same
	
	curl.SetFile("my_html.html"); //all html will co to this file
	//alternatively
	FILE* file = fopen("Test.html","w");
	curl.SetFile(file);
	
	curl.SetReferer("www.referer.com");
	curl.SetUserAgent("CURL BROWSER");
	curl.SetUserPassword("Charles:Something");
	curl.SetOpt(CURLOPT_INTERFACE,"eth0");
	curl.SetOpt(CURLOPT_CLOSEPOLICY,1);

	if (!curl.Perform()) 
		cout << curl.GetLastError();

	cout << curl.GetLastEffectiveUrl();

	return 1;
}


