added partial download script

This commit is contained in:
Fufu Fang 2018-07-20 15:38:44 +01:00
parent 98ac7d19e8
commit 14b2038f09
6 changed files with 47 additions and 27 deletions

5
data.h
View File

@ -9,12 +9,15 @@
#define URL_LEN_MAX 2048 #define URL_LEN_MAX 2048
#define LINK_LEN_MAX 255 #define LINK_LEN_MAX 255
#define HTTP_OK 200
/** \brief the link type */ /** \brief the link type */
typedef enum { typedef enum {
LINK_HEAD = 'H',
LINK_DIR = 'D', LINK_DIR = 'D',
LINK_FILE = 'F', LINK_FILE = 'F',
LINK_UNKNOWN = 'U' LINK_UNKNOWN = 'U',
LINK_INVALID = 'I'
} LinkType; } LinkType;
/** \brief link data type */ /** \brief link data type */

38
link.c
View File

@ -37,6 +37,7 @@ Link *Link_new(const char *p_url)
curl_easy_setopt(link->curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); curl_easy_setopt(link->curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(link->curl, CURLOPT_WRITEDATA, (void *)link); curl_easy_setopt(link->curl, CURLOPT_WRITEDATA, (void *)link);
curl_easy_setopt(link->curl, CURLOPT_USERAGENT, "mount-http-dir/libcurl"); curl_easy_setopt(link->curl, CURLOPT_USERAGENT, "mount-http-dir/libcurl");
curl_easy_setopt(link->curl, CURLOPT_VERBOSE, 1);
return link; return link;
} }
@ -49,6 +50,22 @@ void Link_free(Link *link)
link = NULL; link = NULL;
} }
int Link_download(Link *link, size_t start, size_t end)
{
CURL *curl = link->curl;
char range_str[64];
snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end);
curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
curl_easy_setopt(curl, CURLOPT_RANGE, range_str);
curl_easy_perform(link->curl);
long http_resp;
curl_easy_getinfo(link->curl, CURLINFO_RESPONSE_CODE, &http_resp);
return http_resp;
}
LinkTable *LinkTable_new(const char *url) LinkTable *LinkTable_new(const char *url)
{ {
LinkTable *linktbl = calloc(1, sizeof(LinkTable)); LinkTable *linktbl = calloc(1, sizeof(LinkTable));
@ -56,6 +73,7 @@ LinkTable *LinkTable_new(const char *url)
/* populate the base URL */ /* populate the base URL */
LinkTable_add(linktbl, Link_new(url)); LinkTable_add(linktbl, Link_new(url));
Link *head_link = linktbl->links[0]; Link *head_link = linktbl->links[0];
head_link->type = LINK_HEAD;
curl_easy_setopt(head_link->curl, CURLOPT_URL, url); curl_easy_setopt(head_link->curl, CURLOPT_URL, url);
/* start downloading the base URL */ /* start downloading the base URL */
@ -111,14 +129,20 @@ void LinkTable_fill(LinkTable *linktbl)
free(url); free(url);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1); curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
curl_easy_perform(curl); curl_easy_perform(curl);
double cl; long http_resp;
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (cl == -1) { if (http_resp == HTTP_OK) {
this_link->content_length = 0; double cl;
this_link->type = LINK_DIR; curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
if (cl == -1) {
this_link->content_length = 0;
this_link->type = LINK_DIR;
} else {
this_link->content_length = cl;
this_link->type = LINK_FILE;
}
} else { } else {
this_link->content_length = cl; this_link->type = LINK_INVALID;
this_link->type = LINK_FILE;
} }
} }
} }

4
link.h
View File

@ -13,6 +13,10 @@ Link *Link_new();
/** \brief free a Link */ /** \brief free a Link */
void Link_free(Link *link); void Link_free(Link *link);
/** \brief download a link */
int Link_download(Link *link, size_t start, size_t end);
/** \brief make a new LinkTable */ /** \brief make a new LinkTable */
LinkTable *LinkTable_new(const char *url); LinkTable *LinkTable_new(const char *url);

3
main.c
View File

@ -19,8 +19,7 @@ int main(int argc, char** argv)
init(); init();
gumbo_test(); link_test();
url_test();
return 0; return 0;
} }

21
test.c
View File

@ -7,26 +7,17 @@
#include "link.h" #include "link.h"
#include "test.h" #include "test.h"
void url_test()
{
printf("--- start of url_test ---\n");
char *url1 = "http://www.google.com/";
char *url2 = "http://www.google.com";
char *cat_url1 = url_append(url1, "fangfufu");
char *cat_url2 = url_append(url2, "fangfufu");
printf("%d %s\n", (int) strlen(cat_url1), cat_url1);
printf("%d %s\n", (int) strlen(cat_url2), cat_url2);
printf("--- end of url_test ---\n\n");
}
void gumbo_test() void link_test()
{ {
printf("--- start of gumbo_test ---\n"); printf("--- start of link_test ---\n");
LinkTable *linktbl = LinkTable_new( LinkTable *linktbl = LinkTable_new(
"https://www.fangfufu.co.uk/~fangfufu/test/"); "http://127.0.0.1/~fangfufu/");
LinkTable_print(linktbl); LinkTable_print(linktbl);
Link_download(linktbl->links[1], 1, 20);
printf(linktbl->links[1]->body);
LinkTable_free(linktbl); LinkTable_free(linktbl);
printf("--- end of gumbo_test ---\n\n"); printf("\n--- end of link_test ---\n\n");
} }

3
test.h
View File

@ -1,7 +1,6 @@
#ifndef TEST_H #ifndef TEST_H
#define TEST_H #define TEST_H
void url_test();
void gumbo_test(); void link_test();
#endif #endif