-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
45 lines (37 loc) · 1.13 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "util.h"
int main(int argc, char *argv[0]) {
if(argc != 2) {
printf("usage: ./curl <url>\n");
return 1;
}
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
curl_handle = curl_easy_init();
if (curl_handle) {
curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl_handle);
if (res != CURLE_OK) {
fprintf(stderr, "error: %s\n", curl_easy_strerror(res));
} else {
long http_code;
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_code);
printf("Status: %lu\n", (unsigned long)http_code);
printf("Size: %lu\n", (unsigned long)chunk.size);
printf("Data: %s\n", chunk.memory);
}
curl_easy_cleanup(curl_handle);
free(chunk.memory);
}
return 0;
}