Skip to content

Instantly share code, notes, and snippets.

@jige003
Created May 15, 2019 08:42
Show Gist options
  • Save jige003/5901f9a5ff2bfc1648dbdd0aa92f8ef7 to your computer and use it in GitHub Desktop.
Save jige003/5901f9a5ff2bfc1648dbdd0aa92f8ef7 to your computer and use it in GitHub Desktop.
down file snippet
/*************************************************************************
> File Name: d.c
> Author: jige003
> Created Time: Tue 14 May 2019 07:21:06 PM CST
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <stdbool.h>
#include <libgen.h>
#include <sys/stat.h>
#include <curl/curl.h>
#include <event2/http.h>
#include <event2/util.h>
size_t getcontentlengthfunc(void *ptr, size_t size, size_t nmemb, void *stream) {
int r;
long len = 0;
r = sscanf(ptr, "Content-Length: %ld\n", &len);
if (r)
*((long *) stream) = len;
return (size * nmemb);
}
size_t wirtefunc(void *ptr, size_t size, size_t nmemb, void *stream){
return fwrite(ptr, size, nmemb, stream);
}
int download(CURL *curlhandle, const char * remotepath, const char * localpath, long tries, bool ver){
FILE *f;
curl_off_t local_file_len = -1 ;
long filesize =0 ;
CURLcode r = CURLE_GOT_NOTHING;
int c;
struct stat file_info;
int use_resume = 0;
if(stat(localpath, &file_info) == 0) {
local_file_len = file_info.st_size;
use_resume = 1;
}
f = fopen(localpath, "ab+");
if (f == NULL) {
perror(NULL);
return 0;
}
//curl_easy_setopt(curlhandle, CURLOPT_PROXY, "proxyip");
curl_easy_setopt(curlhandle, CURLOPT_URL, remotepath);
//curl_easy_setopt(curlhandle, CURLOPT_CONNECTTIMEOUT, timeout);
curl_easy_setopt(curlhandle, CURLOPT_HEADERFUNCTION, getcontentlengthfunc);
curl_easy_setopt(curlhandle, CURLOPT_HEADERDATA, &filesize);
curl_easy_setopt(curlhandle, CURLOPT_RESUME_FROM_LARGE, use_resume?local_file_len:0);
curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, f);
curl_easy_setopt(curlhandle, CURLOPT_WRITEFUNCTION, wirtefunc);
curl_easy_setopt(curlhandle, CURLOPT_NOPROGRESS, 1L);
if (ver == true )
curl_easy_setopt(curlhandle, CURLOPT_VERBOSE, 1L);
r = curl_easy_perform(curlhandle);
fclose(f);
if (r == CURLE_OK)
return 1;
else {
fprintf(stderr, "%s\n", curl_easy_strerror(r));
return 0;
}
}
void xfree(void *ptr){
if (ptr != ((void*)0)) free(ptr);
}
int main(int c, char **v) {
struct options {
char *url;
char *filepath;
bool verbose;
} o;
memset(&o, 0, sizeof(o));
int opt;
#define ff fprintf
if (c < 3){
ff(stderr, "usage:\n");
ff(stderr, "%s -u url [ -f filepath ]\n", v[0]);
return (1);
}
while ((opt = getopt(c, v, "u:f:v")) != -1) {
switch(opt){
case 'u': o.url = optarg; break;
case 'f': o.filepath = strdup(optarg); break;
case 'v': o.verbose = true; break;
default : ff(stderr, "Unknown option %c\n", opt); break;
}
}
struct evhttp_uri *uri;
uri = evhttp_uri_parse(o.url);
const char *path = evhttp_uri_get_path(uri);
ff(stdout, "path:%s\n", path);
char *filename = basename((char*)path);
ff(stdout, "filename:%s\n", filename);
if (o.filepath == NULL){
o.filepath = strdup(filename);
}
CURL *curlhandle = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curlhandle = curl_easy_init();
download(curlhandle, o.url, o.filepath, 3, o.verbose);
curl_easy_cleanup(curlhandle);
curl_global_cleanup();
xfree(o.filepath);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment