summaryrefslogtreecommitdiff
path: root/libs/curl.c
blob: bf6cb9377841213df9a52282709ac9df406b59c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include "curl.h"
#include "string.h"

size_t downloadCallback(char *ptr, size_t size, size_t nmemb, String *output)
{
	output->length += nmemb;
	output->string = realloc(output->string, output->length + 1);
	strcat(output->string, ptr);
	return size*nmemb;
}

char* downloadPage(char *page)
{
	char *str = NULL;

	if ( page != NULL && strlen(page) > 0 )
	{
		// Initializing cURL and Temporary String Structure
		CURL *curl;
		String s = newString("");
		curl = curl_easy_init();

		// Saving cURL'ed webpage into String Structure
		curl_easy_setopt(curl, CURLOPT_URL, page);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
		curl_easy_perform(curl);

		str = (char *) calloc(s.length + 1, sizeof(char));
		strcpy(str, s.string);

		// Cleanup
		curl_easy_cleanup(curl);
		freeString(&s);
	}

	return str;
}