-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
120 lines (97 loc) · 3.21 KB
/
http.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
#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
#include "helpers.h"
#include "main.h"
int http_get(const char *url, int urllength, struct string *s) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
char *encodedURL = curl_easy_unescape(curl, url, urllength, NULL);
printf("url: %s, length: %i, encodedURL: %s \n", url, urllength, encodedURL);
init_string(s);
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, encodedURL);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc_get);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, s);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed KEK: %s\n",
curl_easy_strerror(res));
if (res == CURLE_UNSUPPORTED_PROTOCOL)
s->error = -2;
else if (res == CURLE_URL_MALFORMAT)
s->error = -3;
else if (res == CURLE_COULDNT_CONNECT)
s->error = -4;
else if (res == CURLE_COULDNT_RESOLVE_HOST ||
res == CURLE_COULDNT_RESOLVE_PROXY)
s->error = -5;
else
s->error = -1;
}
}
curl_free(encodedURL);
curl_easy_cleanup(curl);
return 0;
}
int http_post(struct open_post_req *req) {
printf("[http_post] called\n\tname: %s content: %s\n", req->name, req->content);
CURL *curl;
CURLcode res;
curl = curl_easy_init();
init_string(req->answ);
char *encodedURL = curl_easy_unescape(curl, req->name, req->name_len, NULL);
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, encodedURL);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc_post);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, req->answ);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, req->content);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_free(encodedURL);
curl_global_cleanup();
return 0;
}
int http_head(const char *url, int urllength, struct string *s) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
char *encodedURL = curl_easy_unescape(curl, url, urllength, NULL);
printf("url: %s, length: %i, encodedURL: %s \n", url, urllength, encodedURL);
init_string(s);
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, encodedURL);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc_get);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, s);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed KEK: %s\n",
curl_easy_strerror(res));
if (res == CURLE_UNSUPPORTED_PROTOCOL)
s->error = -2;
else if (res == CURLE_URL_MALFORMAT)
s->error = -3;
else if (res == CURLE_COULDNT_CONNECT)
s->error = -4;
else if (res == CURLE_COULDNT_RESOLVE_HOST ||
res == CURLE_COULDNT_RESOLVE_PROXY)
s->error = -5;
else
s->error = -1;
}
}
curl_free(encodedURL);
curl_easy_cleanup(curl);
return 0;
}