-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl_handler.c
94 lines (72 loc) · 2.06 KB
/
curl_handler.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <curl/curl.h>
#include "curl_handler.h"
#include "util.h"
static size_t curl_write(char *ptr, size_t size, size_t nmemb, void *data)
{
size_t real_size = size * nmemb;
struct XML *xml = (struct XML *) data;
xml->data = realloc(xml->data, xml->size + real_size);
if(xml->data == NULL)
{
/* out of memory */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(xml->data[xml->size]), ptr, real_size);
xml->size += real_size;
xml->data[xml->size] = 0;
return real_size;
}
struct XML get_mail_xml(char *username, char *password)
{
struct XML xml_data;
xml_data.data = malloc(1);
xml_data.size = 0;
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl)
{
char url[100];
snprintf(url, sizeof(url),
"https://%s:%s@mail.google.com/mail/feed/atom",
url_encode(username), url_encode(password));
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &xml_data);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "cURL: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
assert(xml_data.data);
return xml_data;
}
char *url_encode(char *string)
{
// TODO: handle more than @ if this ever becomes necessary
const char delim[] = "@";
int at_count = 0;
for(int i = 0; string[i]; ++i)
if(string[i] == '@')
++at_count;
// @ => %40, hence 1 character becomes 3 characters in the new buffer
const size_t encoded_size = strlen(string) + 2 * at_count;
char *encoded = malloc(encoded_size);
encoded[0] = '\0';
char *token = strtok(string, delim);
while(token != NULL)
{
strncat(encoded, token, encoded_size - strlen(encoded));
char *next = strtok(NULL, delim);
if(next)
strncat(encoded, "%40", sizeof("%40"));
token = next;
}
return encoded;
}