diff --git a/src/server/slavefuncs.c b/src/server/slavefuncs.c --- a/src/server/slavefuncs.c +++ b/src/server/slavefuncs.c @@ -26,6 +26,10 @@ #include "blendjob.h" #include "execio.h" +#include +#include +#include + #include #include #include @@ -33,6 +37,78 @@ #include #include +/** Function referenced by curlget() to write data to disk. */ +size_t curl_writetodisk(void *ptr, size_t size, size_t nmemb, FILE *stream) + { + return fwrite(ptr, size, nmemb, stream); + } + +/** Gets a URL with cURL and saves it to disk */ +int curlget(char *url, char *out){ + CURL *curl; + CURLcode res; + FILE *outfile; + + curl = curl_easy_init(); + if(curl) { + outfile = fopen(out, "w"); // Open where we're writing to + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_writetodisk); // this MUST be set for win32 compat. + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + } + return 0; +} + +/** Posts a file to a url with cUrl */ +int curlpost(char *filename, char *url){ + char *targetname = "uploadedfile"; // Name of the target in the php file + CURL *curl; + CURLcode res; + 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); + + /* upload field... */ + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, targetname, + CURLFORM_FILE, filename, + CURLFORM_END); + /* filename field... */ + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "filename", + CURLFORM_COPYCONTENTS, filename, + CURLFORM_END); + /* submit field, not usually needed, just in case */ + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "submit", + CURLFORM_COPYCONTENTS, "send", + CURLFORM_END); + + curl = curl_easy_init(); + headerlist = curl_slist_append(headerlist, buf); + if(curl) { + /* Setting the URL to get the post, and the contents of the post */ + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + res = curl_easy_perform(curl); + + curl_easy_cleanup(curl); + /* cleanup the formpost junk */ + curl_formfree(formpost); + curl_slist_free_all (headerlist); + } + return 0; +} + /** Converts an integer to a string, currently a stub. */ char *int_to_str(int nbr) {