2017-03-09 08:01:10 +08:00
|
|
|
/*********************************************************************
|
2018-12-07 05:24:28 +08:00
|
|
|
* Copyright 2018, UCAR/Unidata
|
2017-03-09 08:01:10 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "d4includes.h"
|
|
|
|
#include "d4curlfunctions.h"
|
|
|
|
|
|
|
|
static size_t WriteFileCallback(void*, size_t, size_t, void*);
|
|
|
|
static size_t WriteMemoryCallback(void*, size_t, size_t, void*);
|
|
|
|
static int curlerrtoncerr(CURLcode cstat);
|
|
|
|
|
|
|
|
struct Fetchdata {
|
|
|
|
FILE* stream;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
long
|
|
|
|
NCD4_fetchhttpcode(CURL* curl)
|
|
|
|
{
|
|
|
|
long httpcode = 200;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
/* Extract the http code */
|
|
|
|
#ifdef HAVE_CURLINFO_RESPONSE_CODE
|
|
|
|
cstat = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&httpcode);
|
|
|
|
#else
|
2017-09-26 07:38:48 +08:00
|
|
|
cstat = curl_easy_getinfo(curl,CURLINFO_HTTP_CONNECTCODE,&httpcode);
|
2017-03-09 08:01:10 +08:00
|
|
|
#endif
|
|
|
|
if(cstat != CURLE_OK) {
|
|
|
|
httpcode = 0;
|
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
|
|
|
}
|
|
|
|
return httpcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
NCD4_fetchurl_file(CURL* curl, const char* url, FILE* stream,
|
|
|
|
d4size_t* sizep, long* filetime)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
struct Fetchdata fetchdata;
|
|
|
|
|
|
|
|
/* Set the URL */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
|
|
|
|
if (cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
/* send all data to this function */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteFileCallback);
|
|
|
|
if (cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
/* we pass our file to the callback function */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&fetchdata);
|
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
/* One last thing; always try to get the last modified time */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);
|
|
|
|
if (cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
fetchdata.stream = stream;
|
|
|
|
fetchdata.size = 0;
|
|
|
|
cstat = curl_easy_perform(curl);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
{ret = NC_EDAPSVC; goto fail;}
|
|
|
|
|
|
|
|
if (ret == NC_NOERR) {
|
|
|
|
/* return the file size*/
|
|
|
|
#ifdef D4DEBUG
|
|
|
|
nclog(NCLOGNOTE,"filesize: %lu bytes",fetchdata.size);
|
|
|
|
#endif
|
|
|
|
if (sizep != NULL)
|
|
|
|
*sizep = fetchdata.size;
|
|
|
|
/* Get the last modified time */
|
|
|
|
if(filetime != NULL)
|
|
|
|
cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
|
|
|
|
if(cstat != CURLE_OK)
|
|
|
|
{ret = NC_ECURL; goto fail;}
|
|
|
|
}
|
|
|
|
return THROW(ret);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if(cstat != CURLE_OK) {
|
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
|
|
|
ret = curlerrtoncerr(cstat);
|
|
|
|
}
|
|
|
|
return THROW(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-09-01 04:19:56 +08:00
|
|
|
NCD4_fetchurl(CURL* curl, const char* url, NCbytes* buf, long* filetime)
|
2017-03-09 08:01:10 +08:00
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
size_t len;
|
|
|
|
long httpcode = 0;
|
|
|
|
|
|
|
|
/* send all data to this function */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* we pass our file to the callback function */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buf);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* One last thing; always try to get the last modified time */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);
|
|
|
|
|
|
|
|
/* Set the URL */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)"");
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
cstat = curl_easy_perform(curl);
|
|
|
|
|
|
|
|
if(cstat == CURLE_PARTIAL_FILE) {
|
|
|
|
/* Log it but otherwise ignore */
|
|
|
|
nclog(NCLOGWARN, "curl error: %s; ignored",
|
|
|
|
curl_easy_strerror(cstat));
|
|
|
|
cstat = CURLE_OK;
|
|
|
|
}
|
|
|
|
httpcode = NCD4_fetchhttpcode(curl);
|
|
|
|
|
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
/* Get the last modified time */
|
|
|
|
if(filetime != NULL)
|
|
|
|
cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
|
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
/* Null terminate the buffer*/
|
|
|
|
len = ncbyteslength(buf);
|
|
|
|
ncbytesappend(buf, '\0');
|
2018-04-24 06:38:08 +08:00
|
|
|
ncbytessetlength(buf, len); /* don't count null in buffer size*/
|
2017-03-09 08:01:10 +08:00
|
|
|
#ifdef D4DEBUG
|
|
|
|
nclog(NCLOGNOTE,"buffersize: %lu bytes",(d4size_t)ncbyteslength(buf));
|
|
|
|
#endif
|
|
|
|
return THROW(ret);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if(cstat != CURLE_OK) {
|
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
|
|
|
ret = curlerrtoncerr(cstat);
|
|
|
|
} else switch (httpcode) {
|
This PR adds EXPERIMENTAL support for accessing data in the
cloud using a variant of the Zarr protocol and storage
format. This enhancement is generically referred to as "NCZarr".
The data model supported by NCZarr is netcdf-4 minus the user-defined
types and the String type. In this sense it is similar to the CDF-5
data model.
More detailed information about enabling and using NCZarr is
described in the document NUG/nczarr.md and in a
[Unidata Developer's blog entry](https://www.unidata.ucar.edu/blogs/developer/en/entry/overview-of-zarr-support-in).
WARNING: this code has had limited testing, so do use this version
for production work. Also, performance improvements are ongoing.
Note especially the following platform matrix of successful tests:
Platform | Build System | S3 support
------------------------------------
Linux+gcc | Automake | yes
Linux+gcc | CMake | yes
Visual Studio | CMake | no
Additionally, and as a consequence of the addition of NCZarr,
major changes have been made to the Filter API. NOTE: NCZarr
does not yet support filters, but these changes are enablers for
that support in the future. Note that it is possible
(probable?) that there will be some accidental reversions if the
changes here did not correctly mimic the existing filter testing.
In any case, previously filter ids and parameters were of type
unsigned int. In order to support the more general zarr filter
model, this was all converted to char*. The old HDF5-specific,
unsigned int operations are still supported but they are
wrappers around the new, char* based nc_filterx_XXX functions.
This entailed at least the following changes:
1. Added the files libdispatch/dfilterx.c and include/ncfilter.h
2. Some filterx utilities have been moved to libdispatch/daux.c
3. A new entry, "filter_actions" was added to the NCDispatch table
and the version bumped.
4. An overly complex set of structs was created to support funnelling
all of the filterx operations thru a single dispatch
"filter_actions" entry.
5. Move common code to from libhdf5 to libsrc4 so that it is accessible
to nczarr.
Changes directly related to Zarr:
1. Modified CMakeList.txt and configure.ac to support both C and C++
-- this is in support of S3 support via the awd-sdk libraries.
2. Define a size64_t type to support nczarr.
3. More reworking of libdispatch/dinfermodel.c to
support zarr and to regularize the structure of the fragments
section of a URL.
Changes not directly related to Zarr:
1. Make client-side filter registration be conditional, with default off.
2. Hack include/nc4internal.h to make some flags added by Ed be unique:
e.g. NC_CREAT, NC_INDEF, etc.
3. cleanup include/nchttp.h and libdispatch/dhttp.c.
4. Misc. changes to support compiling under Visual Studio including:
* Better testing under windows for dirent.h and opendir and closedir.
5. Misc. changes to the oc2 code to support various libcurl CURLOPT flags
and to centralize error reporting.
6. By default, suppress the vlen tests that have unfixed memory leaks; add option to enable them.
7. Make part of the nc_test/test_byterange.sh test be contingent on remotetest.unidata.ucar.edu being accessible.
Changes Left TO-DO:
1. fix provenance code, it is too HDF5 specific.
2020-06-29 08:02:47 +08:00
|
|
|
case 401: ret = NC_EACCESS; break;
|
|
|
|
case 403: ret = NC_EAUTH; break;
|
2017-03-09 08:01:10 +08:00
|
|
|
case 404: ret = ENOENT; break;
|
|
|
|
case 500: ret = NC_EDAPSVC; break;
|
|
|
|
case 200: break;
|
|
|
|
default: ret = NC_ECURL; break;
|
|
|
|
}
|
|
|
|
return THROW(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
WriteFileCallback(void* ptr, size_t size, size_t nmemb, void* data)
|
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
size_t count;
|
|
|
|
struct Fetchdata* fetchdata;
|
|
|
|
fetchdata = (struct Fetchdata*) data;
|
|
|
|
if(realsize == 0)
|
|
|
|
nclog(NCLOGWARN,"WriteFileCallback: zero sized chunk");
|
|
|
|
count = fwrite(ptr, size, nmemb, fetchdata->stream);
|
|
|
|
if (count > 0) {
|
|
|
|
fetchdata->size += (count * size);
|
|
|
|
} else {
|
|
|
|
nclog(NCLOGWARN,"WriteFileCallback: zero sized write");
|
|
|
|
}
|
|
|
|
#ifdef PROGRESS
|
|
|
|
nclog(NCLOGNOTE,"callback: %lu bytes",(d4size_t)realsize);
|
|
|
|
#endif
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
|
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
NCbytes* buf = (NCbytes*) data;
|
|
|
|
if(realsize == 0)
|
|
|
|
nclog(NCLOGWARN,"WriteMemoryCallback: zero sized chunk");
|
|
|
|
/* Optimize for reading potentially large dods datasets */
|
|
|
|
if(!ncbytesavail(buf,realsize)) {
|
|
|
|
/* double the size of the packet */
|
|
|
|
ncbytessetalloc(buf,2*ncbytesalloc(buf));
|
|
|
|
}
|
|
|
|
ncbytesappendn(buf, ptr, realsize);
|
|
|
|
#ifdef PROGRESS
|
|
|
|
nclog(NCLOGNOTE,"callback: %lu bytes",(d4size_t)realsize);
|
|
|
|
#endif
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
NCD4_curlopen(CURL** curlp)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
CURL* curl;
|
|
|
|
/* initialize curl*/
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if (curl == NULL)
|
|
|
|
ret = NC_ECURL;
|
|
|
|
else {
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
ret = NC_ECURL;
|
|
|
|
}
|
|
|
|
if (curlp)
|
|
|
|
*curlp = curl;
|
|
|
|
if(cstat != CURLE_OK) {
|
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
|
|
|
ret = curlerrtoncerr(cstat);
|
|
|
|
}
|
|
|
|
return THROW(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NCD4_curlclose(CURL* curl)
|
|
|
|
{
|
|
|
|
if (curl != NULL)
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
NCD4_fetchlastmodified(CURL* curl, char* url, long* filetime)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
|
|
|
|
/* Set the URL */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Ask for head */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30); /* 30sec timeout*/
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_HEADER, 1);
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1);
|
|
|
|
|
|
|
|
cstat = curl_easy_perform(curl);
|
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
if(filetime != NULL)
|
|
|
|
cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
|
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
return THROW(ret);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if(cstat != CURLE_OK) {
|
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
|
|
|
ret = curlerrtoncerr(cstat);
|
|
|
|
}
|
|
|
|
return THROW(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
NCD4_ping(const char* url)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
CURL* curl = NULL;
|
|
|
|
NCbytes* buf = NULL;
|
|
|
|
|
|
|
|
/* Create a CURL instance */
|
|
|
|
ret = NCD4_curlopen(&curl);
|
|
|
|
if(ret != NC_NOERR) return THROW(ret);
|
|
|
|
|
|
|
|
/* Use redirects */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10L);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* use a very short timeout: 10 seconds */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)5);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* fail on HTTP 400 code errors */
|
|
|
|
cstat = curl_easy_setopt(curl, CURLOPT_FAILONERROR, (long)1);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Try to get the file */
|
|
|
|
buf = ncbytesnew();
|
2017-09-01 04:19:56 +08:00
|
|
|
ret = NCD4_fetchurl(curl,url,buf,NULL);
|
2017-03-09 08:01:10 +08:00
|
|
|
if(ret == NC_NOERR) {
|
|
|
|
/* Don't trust curl to return an error when request gets 404 */
|
|
|
|
long http_code = 0;
|
|
|
|
cstat = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &http_code);
|
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
|
|
|
if(http_code >= 400) {
|
|
|
|
cstat = CURLE_HTTP_RETURNED_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
done:
|
|
|
|
ncbytesfree(buf);
|
|
|
|
NCD4_curlclose(curl);
|
|
|
|
if(cstat != CURLE_OK) {
|
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
|
|
|
ret = curlerrtoncerr(cstat);
|
|
|
|
}
|
|
|
|
return THROW(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
curlerrtoncerr(CURLcode cstat)
|
|
|
|
{
|
|
|
|
switch (cstat) {
|
|
|
|
case CURLE_OK: return THROW(NC_NOERR);
|
|
|
|
case CURLE_URL_MALFORMAT:
|
2017-09-26 07:38:48 +08:00
|
|
|
return THROW(NC_EURL);
|
2017-03-09 08:01:10 +08:00
|
|
|
case CURLE_COULDNT_RESOLVE_HOST:
|
|
|
|
case CURLE_COULDNT_CONNECT:
|
|
|
|
case CURLE_REMOTE_ACCESS_DENIED:
|
|
|
|
case CURLE_TOO_MANY_REDIRECTS:
|
|
|
|
return THROW(NC_EDAPSVC);
|
|
|
|
case CURLE_OUT_OF_MEMORY:
|
|
|
|
return THROW(NC_ENOMEM);
|
|
|
|
/* Eventually would be nice to convert these */
|
|
|
|
case CURLE_SSL_CONNECT_ERROR:
|
|
|
|
case CURLE_READ_ERROR:
|
|
|
|
case CURLE_OPERATION_TIMEDOUT:
|
|
|
|
case CURLE_SSL_CERTPROBLEM:
|
|
|
|
case CURLE_FILESIZE_EXCEEDED:
|
|
|
|
case CURLE_SSL_CACERT_BADFILE:
|
|
|
|
default: break;
|
|
|
|
}
|
2017-09-26 07:38:48 +08:00
|
|
|
return THROW(NC_ECURL);
|
2017-03-09 08:01:10 +08:00
|
|
|
}
|