Newer
Older
/***************************************************************************
*
* Project: dongle
*
*
***************************************************************************/
//global varibales
char *home_url = "http://192.168.8.1/html/index.html";
char *sess_tok_url = "http://192.168.8.1/api/webserver/SesTokInfo";
//free mem.
char *parser(char *data, char *keyword)
{
char *session_token;
char *ptr = NULL;
session_token = (char *)calloc(1024, sizeof(*session_token));
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
if (!session_token)
goto leave_token;
//parse keyword..
ptr = strstr(data, keyword);
if (ptr == NULL)
{
goto leave_not_found;
}
int strl = strlen(keyword);
ptr += strl;
sscanf(ptr, "%[^<]", session_token);
return session_token;
leave_not_found:
free(session_token);
leave_token:
return NULL;
}
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t total_size = size * nmemb;
struct write_result *result = (struct write_result *)stream;
if (result->pos + total_size >= BUFFER_SIZE - 1) {
fprintf(stderr, "error: too small buffer\n");
return 0;
}
memcpy(result->data + result->pos, ptr, total_size);
result->pos += total_size;
return total_size;
}
struct write_result *memalloc_write_result(void)
{
struct write_result *wr = (struct write_result *) calloc(1, sizeof(*wr));
wr->data = calloc(BUFFER_SIZE, sizeof(wr->data));
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
if (!wr->data)
goto leave;
wr->pos = 0;
return wr;
leave:
if (wr)
free(wr);
if (wr->data)
free(wr->data);
return NULL;
}
void free_write_result(struct write_result *result)
{
if (result) {
free(result);
}
}
int check_cookie_status(CURLcode status, CURL *curl)
{
long code;
if (status != 0) {
if (DEBUG)
printf(": %s\n", curl_easy_strerror(status));
return 1;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if (code != 200) {
if (DEBUG)
printf("error: server responded with code %ld\n", code);
return 1;
}
return 0;
}
void prepare_curl_write_callback(CURL *curl, const char *url, struct write_result *result)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, result);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
}
//free mem
static struct write_result *get_request(const char *url, const char *api_url)
{
CURL *curl = NULL;
CURLcode status;
struct write_result *result;
curl = curl_easy_init();
if (!curl)
goto leave;
result = memalloc_write_result();
if (!result)
goto leave;
prepare_curl_write_callback(curl, url, result);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); //start the cookie engine ..
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt"); //Save the cookie from the server to cookie.txt
status = curl_easy_perform(curl);
if (check_cookie_status(status, curl))
goto leave;
//Prepare result struct to get next http response.
memset(result->data, 0, BUFFER_SIZE);
result->pos = 0;
prepare_curl_write_callback(curl, api_url, result);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt"); //Read locally stored cookies
status = curl_easy_perform(curl);
if (check_cookie_status(status, curl))
goto leave;
result->data[result->pos] = '\0';
if (DEBUG_RESULT)
printf("Http get request data is: %s\n", result->data);
curl_easy_cleanup(curl);
return result;
leave:
if (curl) {
printf("error occured: easy cleanup curl");
curl_easy_cleanup(curl);
}
if (result)
free_write_result(result);
return NULL;
}
//free mem
static struct write_result *post_request(const char *url, const char *api_url, char *post_query)
{
char token[1024] = {0};
char session[1024] = {0};
char *token_text = "__RequestVerificationToken:";
char *session_text = "Cookie:SessionID=";
struct curl_slist *headers = NULL;
struct write_result *result;
CURL *curl = NULL;
CURLcode status;
curl = curl_easy_init();
if (!curl)
goto leave;
result = memalloc_write_result();
Loading
Loading full blame...