-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.cpp
More file actions
56 lines (46 loc) · 1.18 KB
/
fetch.cpp
File metadata and controls
56 lines (46 loc) · 1.18 KB
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
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <curl/curl.h>
#include <string>
#include "fetch.h"
static int set_uri(CURL *curl, const char *uri)
{
if (strncmp(uri, "ftpes://", 8) == 0) {
char *dup = strdup(uri);
/* Change ‘ftpes://’ to ‘ftp://’ */
memmove(dup + 3, dup + 5, strlen(dup) - 4);
curl_easy_setopt(curl, CURLOPT_URL, dup);
curl_easy_setopt(curl, CURLOPT_USE_SSL, 1);
free(dup);
return 0;
}
curl_easy_setopt(curl, CURLOPT_URL, uri);
return 0;
}
int fetch_uri(const std::string &uri, const std::string &filename)
{
CURL *curl;
FILE *fp;
int ret;
curl = curl_easy_init();
if (!curl)
return CURLE_FAILED_INIT;
fp = fopen(filename.c_str(), "w");
if (fp == NULL)
return -errno;
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
set_uri(curl, uri.c_str());
ret = curl_easy_perform(curl);
if (ret == CURLE_HTTP_RETURNED_ERROR) {
unsigned long code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
ret = code;
}
curl_easy_cleanup(curl);
fclose(fp);
return ret;
}