-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl.c
125 lines (100 loc) · 3.13 KB
/
curl.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "curl.h"
#include <syslog.h>
CURLcode
twid_curl_perform(const char *url, const char *post_body, int auth,
PSTREAM pstream_header, PSTREAM pstream_body){
CURL *curl;
CURLcode code;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
if (post_body && *post_body){
/* POST body found */
curl_easy_setopt(curl, CURLOPT_POST, 1); /* use POST method */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_body);
}
if (auth){
/* HTTP Basic Authentication */
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
/* POST body */
curl_easy_setopt(curl, CURLOPT_USERPWD, twid_twitter_user->userpwd);
}
if (twid_twitter_user->sessionid){
/* Cookie / Session holds by twid */
curl_easy_setopt(curl, CURLOPT_COOKIE,
twid_twitter_user->sessionid);
}
if (pstream_body){
curl_easy_setopt(curl, CURLOPT_WRITEDATA, pstream_body);
/* register callback entry */
curl_easy_setopt(
curl,
CURLOPT_WRITEFUNCTION,
twid_write_stream_callback
);
}
if (pstream_header){
curl_easy_setopt(curl, CURLOPT_HEADERDATA, pstream_header);
/* register callback entry */
curl_easy_setopt(
curl,
CURLOPT_HEADERFUNCTION,
twid_write_stream_callback
);
}
code = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return code;
}
/* normalize the tweet to be escaped, truncated string in post format with
prefix which is ready to submit to the Twitter API to publish */
char *
twid_normalize_tweet(char *tweet_raw_bytes){
CURL *curl;
curl = curl_easy_init();
char *tweet_post_body = NULL;
/* considering the utf-8 encoding where one character takes 3 bytes */
/* the memory needed should be 140 * 3 = 420 bytes */
char *tweet_truncated = (char *)malloc(TWEET_MAX_LEN * 3 + 1);
strncpy(tweet_truncated, tweet_raw_bytes, TWEET_MAX_LEN * 3);
/* escape the post body */
char *tweet_escaped = curl_easy_escape(curl, tweet_truncated, 0);
if (tweet_escaped){
int tweet_length = strlen(tweet_escaped);
/* allocate memory for store the whole POST body */
tweet_post_body = (char *)malloc(tweet_length + 7);
memset(tweet_post_body, 0, tweet_length + 7);
/* a safe cast of string duplication */
strncpy(tweet_post_body, "status=", 7);
strncat(tweet_post_body, tweet_escaped, tweet_length);
/* concat the two strings */
}
curl_easy_cleanup(curl);
return tweet_post_body;
}
PSTREAM
twid_init_stream_chunk(){
PSTREAM chunk = (PSTREAM)malloc(sizeof(struct twid_stream));
chunk->text = NULL;
chunk->size = 0;
return chunk;
}
static size_t
twid_write_stream_callback(void *ptr, size_t size, size_t nmemb, void *data){
PSTREAM chunk = (PSTREAM) data;
size_t full_size = size * nmemb;
/* reallocate memory */
if (chunk->text == NULL){
/* 1 char for '\0' */
chunk->text = (char *)malloc(chunk->size + full_size + 1);
}
else {
chunk->text = (char *)realloc(chunk->text, chunk->size + full_size + 1);
}
if (chunk->text){
/* concatenate two strings */
memcpy(&(chunk->text[chunk->size]), ptr, full_size);
chunk->size += full_size;
chunk->text[chunk->size] = 0; /* string terminator */
}
return full_size;
}