Browse Source

Implement support for http(s) schemes.

Allow to fetch a share from a remote location via a http(s) URI.
master
Damien Goutte-Gattat 3 months ago
parent
commit
4843ce672d
  1. 69
      src/scheme-libcurl.c
  2. 14
      src/scheme-module.c

69
src/scheme-libcurl.c

@ -22,8 +22,39 @@
#include "scheme-libcurl.h"
#include <string.h>
#include <xmem.h>
#include "scheme-module.h"
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
struct getfile_cb_data {
unsigned char *buffer;
size_t written;
size_t allocated;
};
static size_t
write_cb(void *buffer, size_t size, size_t nmemb, void *userp)
{
struct getfile_cb_data *data = (struct getfile_cb_data *)userp;
if ( data->written + nmemb >= data->allocated ) {
size_t needed = data->written + nmemb;
data->buffer = xrealloc(data->buffer, needed);
data->allocated = needed;
}
memcpy(data->buffer + data->written, buffer, nmemb);
data->written += nmemb;
return nmemb;
}
int
gfsec_scheme_libcurl_get_file(gfsec_scheme_t scheme,
const char *authority,
@ -31,10 +62,42 @@ gfsec_scheme_libcurl_get_file(gfsec_scheme_t scheme,
unsigned char **buffer,
size_t *len)
{
if ( scheme != GFSEC_SCHEME_HTTP )
int ret = GFSEC_SCHEME_STATUS_SUCCESS;
char *url = NULL;
CURL *curl;
struct getfile_cb_data data = { 0 };
int cret;
if ( scheme != GFSEC_SCHEME_HTTP && scheme != GFSEC_SCHEME_HTTPS )
return GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME;
return GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME;
if ( ! (curl = curl_easy_init()) )
return GFSEC_SCHEME_STATUS_ERROR;
xasprintf(&url, "http%s://%s%s",
scheme == GFSEC_SCHEME_HTTP ? "" : "s",
authority, path);
if ( curl_easy_setopt(curl, CURLOPT_URL, url) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
if ( ret == 0 && curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
if ( ret == 0 && curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
if ( ret == 0 && (cret = curl_easy_perform(curl)) != 0 )
ret = GFSEC_SCHEME_STATUS_ERROR;
if ( ret == 0 ) {
*buffer = data.buffer;
*len = data.written;
}
curl_easy_cleanup(curl);
return ret;
}
int
@ -60,3 +123,5 @@ gfsec_scheme_libcurl_get_supports(gfsec_supports_list_t *list)
return 0;
}
#endif /* HAVE_LIBCURL */

14
src/scheme-module.c

@ -30,6 +30,10 @@
#include <libmtp.h>
#endif
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
typedef int (*fn_get_file)(gfsec_scheme_t,
const char *,
const char *,
@ -85,10 +89,12 @@ gfsec_scheme_module_init(void)
#endif
#ifdef HAVE_LIBCURL
mods[n].get_file = gfsec_scheme_libcurl_get_file;
mods[n].put_file = gfsec_scheme_libcurl_put_file;
mods[n].get_supp = gfsec_scheme_libcurl_get_supports;
mods[n++].present = 1;
if ( curl_global_init(CURL_GLOBAL_DEFAULT) == 0 ) {
mods[n].get_file = gfsec_scheme_libcurl_get_file;
mods[n].put_file = gfsec_scheme_libcurl_put_file;
mods[n].get_supp = gfsec_scheme_libcurl_get_supports;
mods[n++].present = 1;
}
#endif
mods[n++].present = 0;

Loading…
Cancel
Save