removed '/' from linktable, configured curl to follow redirection

This commit is contained in:
Fufu Fang 2018-07-22 04:26:22 +01:00
parent 8926119400
commit 06be26afd0
9 changed files with 72 additions and 53 deletions

View File

@ -1,6 +1,6 @@
CC=gcc
CFLAGS= -Wall -Wextra -lgumbo -lcurl -g
OBJ = main.o network.o test.o
OBJ = main.o network.o
%.o: %.c
$(CC) -c -o $@ $< $(CFLAGS)

View File

@ -1,3 +0,0 @@
- implement fseek
- work on linklist again - append it so it includes curl data structure
- implement function which generates the initial file list.

24
data.h
View File

@ -20,23 +20,31 @@ typedef enum {
LINK_INVALID = 'I'
} LinkType;
/**
* \brief link table type
* \details index 0 contains the Link for the base URL
*/
typedef struct LinkTable LinkTable;
/** \brief link data type */
typedef struct {
typedef struct Link Link;
struct Link {
char p_url[255];
LinkType type;
CURL *curl;
char *body;
size_t body_sz;
size_t content_length;
} Link;
LinkTable *nextTable;
};
/**
* \brief link table type
* \details index 0 contains the Link for the base URL
*/
typedef struct {
struct LinkTable {
int num;
Link **links;
} LinkTable;
};
#endif

View File

@ -1,19 +1,32 @@
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include "network.h"
#include "fuse_local.h"
static struct fuse_lowlevel_ops fs_ops = {
.lookup = fs_lookup,
static struct fuse_operations fs_oper = {
.getattr = fs_getattr,
.readdir = fs_readdir,
.open = fs_open,
.read = fs_read,
};
static int fs_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else if (strcmp(path, hello_path) == 0) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(hello_str);
} else
res = -ENOENT;
return res;
}

View File

@ -1,4 +1,9 @@
#ifndef FUSE_H
#define FUSE_H
#ifndef FUSE_LOCAL_H
#define FUSE_LOCAL_H
/* must be included before including <fuse.h> */
#define FUSE_USE_VERSION 26
#include <fuse.h>
#endif

14
main.c
View File

@ -4,7 +4,19 @@
#include <string.h>
#include "network.h"
#include "test.h"
void link_test()
{
printf("--- start of link_test ---\n");
LinkTable *linktbl = LinkTable_new(
"http://127.0.0.1/~fangfufu/");
LinkTable_print(linktbl);
Link_download(linktbl->links[1], 1, 20);
printf(linktbl->links[1]->body);
LinkTable_free(linktbl);
printf("\n--- end of link_test ---\n\n");
}
void init()
{

View File

@ -110,6 +110,12 @@ Link *Link_new(const char *p_url)
strncpy(link->p_url, p_url, LINK_LEN_MAX);
/* remove the '/' */
char *c = &(link->p_url[strnlen(link->p_url, LINK_LEN_MAX) - 1]);
if ( *c == '/') {
*c = '\0';
}
link->type = LINK_UNKNOWN;
link->curl = curl_easy_init();
@ -119,6 +125,12 @@ Link *Link_new(const char *p_url)
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_VERBOSE, 0);
curl_easy_setopt(link->curl, CURLOPT_FOLLOWLOCATION, 1);
/*
* only 1 redirection is really needed
* - for following directories without the '/'
*/
curl_easy_setopt(link->curl, CURLOPT_MAXREDIRS, 3);
return link;
}
@ -151,7 +163,7 @@ LinkTable *LinkTable_new(const char *url)
LinkTable *linktbl = calloc(1, sizeof(LinkTable));
/* populate the base URL */
LinkTable_add(linktbl, Link_new(url));
LinkTable_add(linktbl, Link_new("/"));
Link *head_link = linktbl->links[0];
head_link->type = LINK_HEAD;
curl_easy_setopt(head_link->curl, CURLOPT_URL, url);
@ -200,13 +212,14 @@ void LinkTable_add(LinkTable *linktbl, Link *link)
void LinkTable_fill(LinkTable *linktbl)
{
Link *head_link = linktbl->links[0];
for (int i = 0; i < linktbl->num; i++) {
Link *this_link = linktbl->links[i];
if (this_link->type == LINK_UNKNOWN) {
CURL *curl = this_link->curl;
char *url;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
url = url_append(linktbl->links[0]->p_url, this_link->p_url);
curl_easy_getinfo(head_link->curl, CURLINFO_EFFECTIVE_URL, &url);
url = url_append(url, this_link->p_url);
curl_easy_setopt(curl, CURLOPT_URL, url);
free(url);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);

23
test.c
View File

@ -1,23 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include "network.h"
#include "test.h"
void link_test()
{
printf("--- start of link_test ---\n");
LinkTable *linktbl = LinkTable_new(
"http://127.0.0.1/~fangfufu/");
LinkTable_print(linktbl);
Link_download(linktbl->links[1], 1, 20);
printf(linktbl->links[1]->body);
LinkTable_free(linktbl);
printf("\n--- end of link_test ---\n\n");
}

6
test.h
View File

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