mirror of
https://github.com/fangfufu/httpdirfs.git
synced 2024-09-27 20:51:55 +02:00
added partial download script
This commit is contained in:
parent
98ac7d19e8
commit
14b2038f09
5
data.h
5
data.h
@ -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 */
|
||||||
|
24
link.c
24
link.c
@ -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,6 +129,9 @@ 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);
|
||||||
|
long http_resp;
|
||||||
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
|
||||||
|
if (http_resp == HTTP_OK) {
|
||||||
double cl;
|
double cl;
|
||||||
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
|
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &cl);
|
||||||
if (cl == -1) {
|
if (cl == -1) {
|
||||||
@ -120,6 +141,9 @@ void LinkTable_fill(LinkTable *linktbl)
|
|||||||
this_link->content_length = cl;
|
this_link->content_length = cl;
|
||||||
this_link->type = LINK_FILE;
|
this_link->type = LINK_FILE;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this_link->type = LINK_INVALID;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
link.h
4
link.h
@ -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
3
main.c
@ -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
21
test.c
@ -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");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user