2018-12-07 06:47:47 +08:00
|
|
|
/* Copyright 2018, UCAR/Unidata and OPeNDAP, Inc.
|
2012-08-01 04:34:13 +08:00
|
|
|
See the COPYRIGHT file for more information. */
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include "ocinternal.h"
|
|
|
|
#include "ocdebug.h"
|
|
|
|
#include "ochttp.h"
|
|
|
|
|
|
|
|
static size_t WriteFileCallback(void*, size_t, size_t, void*);
|
|
|
|
static size_t WriteMemoryCallback(void*, size_t, size_t, void*);
|
|
|
|
|
|
|
|
struct Fetchdata {
|
|
|
|
FILE* stream;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
long
|
|
|
|
ocfetchhttpcode(CURL* curl)
|
|
|
|
{
|
2014-03-15 04:07:35 +08:00
|
|
|
long httpcode = 200;
|
2012-08-01 04:34:13 +08:00
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
/* Extract the http code */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&httpcode));
|
2012-08-01 04:34:13 +08:00
|
|
|
if(cstat != CURLE_OK) httpcode = 0;
|
|
|
|
return httpcode;
|
|
|
|
}
|
|
|
|
|
2014-05-09 04:13:51 +08:00
|
|
|
OCerror
|
2012-08-01 04:34:13 +08:00
|
|
|
ocfetchurl_file(CURL* curl, const char* url, FILE* stream,
|
|
|
|
off_t* sizep, long* filetime)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
struct Fetchdata fetchdata;
|
|
|
|
|
|
|
|
/* Set the URL */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_URL, (void*)url));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* send all data to this function */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteFileCallback));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* we pass our file to the callback function */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&fetchdata));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* One last thing; always try to get the last modified time */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
fetchdata.stream = stream;
|
|
|
|
fetchdata.size = 0;
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_perform(curl));
|
2014-03-12 01:58:22 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (stat == OC_NOERR) {
|
|
|
|
/* return the file size*/
|
|
|
|
#ifdef OCDEBUG
|
2012-08-09 07:15:18 +08:00
|
|
|
oclog(OCLOGNOTE,"filesize: %lu bytes",fetchdata.size);
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
|
|
|
if (sizep != NULL)
|
2024-03-16 01:25:09 +08:00
|
|
|
*sizep = (off_t)fetchdata.size;
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Get the last modified time */
|
|
|
|
if(filetime != NULL)
|
|
|
|
cstat = curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime);
|
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
}
|
|
|
|
return OCTHROW(stat);
|
|
|
|
|
|
|
|
fail:
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(OC_ECURL);
|
|
|
|
}
|
|
|
|
|
2015-11-10 07:19:33 +08:00
|
|
|
OCerror
|
2017-09-01 04:19:56 +08:00
|
|
|
ocfetchurl(CURL* curl, const char* url, NCbytes* buf, long* filetime)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2015-11-10 07:19:33 +08:00
|
|
|
OCerror stat = OC_NOERR;
|
2012-08-01 04:34:13 +08:00
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
size_t len;
|
2014-04-17 04:08:12 +08:00
|
|
|
long httpcode = 0;
|
2014-06-27 06:40:08 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Set the URL */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(CURLERR(curl_easy_setopt(curl, CURLOPT_URL, (void*)url)));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
2014-06-27 06:40:08 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
/* send all data to this function */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* we pass our file to the callback function */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)buf));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* One last thing; always try to get the last modified time */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1));
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_perform(curl));
|
2014-03-15 04:07:35 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
if(cstat == CURLE_PARTIAL_FILE) {
|
|
|
|
/* Log it but otherwise ignore */
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGWARN, "curl error: %s; ignored",
|
2012-08-01 04:34:13 +08:00
|
|
|
curl_easy_strerror(cstat));
|
|
|
|
cstat = CURLE_OK;
|
|
|
|
}
|
2014-03-15 04:07:35 +08:00
|
|
|
httpcode = ocfetchhttpcode(curl);
|
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
/* Get the last modified time */
|
|
|
|
if(filetime != NULL)
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime));
|
2012-08-01 04:34:13 +08:00
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
/* Null terminate the buffer*/
|
2017-03-09 08:01:10 +08:00
|
|
|
len = ncbyteslength(buf);
|
2023-04-26 07:15:06 +08:00
|
|
|
ncbytesnull(buf);
|
2018-03-16 22:38:40 +08:00
|
|
|
ncbytessetlength(buf, len); /* don't count null in buffer size*/
|
2012-08-01 04:34:13 +08:00
|
|
|
#ifdef OCDEBUG
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGNOTE,"buffersize: %lu bytes",(off_t)ncbyteslength(buf));
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return OCTHROW(stat);
|
|
|
|
|
|
|
|
fail:
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
2014-03-15 04:07:35 +08:00
|
|
|
switch (httpcode) {
|
Add hack to deal with DAP2 signed byte hack.
re: issue https://github.com/Unidata/netcdf-c/issues/1316
The DAP2 data model does not have a signed byte type,
but netcdf-3 does have (only) a signed byte type.
So, when converting a netcdf-3 signed byte typed variable to
a DAP2 unsigned byte, the following actions are taken by thredds:
1. The variable type is marked as DAP2 (unsigned) byte.
2. A special attribute, "_Unsigned=false" is set for the variable
3. The corresponding "_FillValue" attribute, if any, is up-converted
to the DAP2 Int16 type in order to hold, with sign, any signed byte
fill value.
On the netcdf-c side, this looks like a fillvalue type mismatch and causes
an error. Instead, the netcdf-c dap2 conversion code needs to recognize
this hack and undo it locally.
So this change looks for the above case, and if found, then it properly
converts the _FillValue type to netcdf-3 signed byte.
Since DAP2 supports both signed and unsigned integers of sizes 16 and 32 bits,
this should be the only hack needed (famous last words).
It may later be desirable for the thredds DAP2 converter to modify its
behavior as well.
2019-02-14 05:36:14 +08:00
|
|
|
case 400: stat = OC_EBADURL; break;
|
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: stat = OC_EACCESS; break;
|
|
|
|
case 403: stat = OC_EAUTH; break;
|
2014-03-15 04:07:35 +08:00
|
|
|
case 404: stat = OC_ENOFILE; break;
|
|
|
|
case 500: stat = OC_EDAPSVC; break;
|
|
|
|
case 200: break;
|
|
|
|
default: stat = OC_ECURL; break;
|
|
|
|
}
|
|
|
|
return OCTHROW(stat);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGWARN,"WriteFileCallback: zero sized chunk");
|
2012-08-01 04:34:13 +08:00
|
|
|
count = fwrite(ptr, size, nmemb, fetchdata->stream);
|
|
|
|
if (count > 0) {
|
|
|
|
fetchdata->size += (count * size);
|
|
|
|
} else {
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGWARN,"WriteFileCallback: zero sized write");
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
#ifdef OCPROGRESS
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGNOTE,"callback: %lu bytes",(off_t)realsize);
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
|
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
2017-03-09 08:01:10 +08:00
|
|
|
NCbytes* buf = (NCbytes*) data;
|
2012-08-01 04:34:13 +08:00
|
|
|
if(realsize == 0)
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGWARN,"WriteMemoryCallback: zero sized chunk");
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Optimize for reading potentially large dods datasets */
|
2017-03-09 08:01:10 +08:00
|
|
|
if(!ncbytesavail(buf,realsize)) {
|
2012-08-01 04:34:13 +08:00
|
|
|
/* double the size of the packet */
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytessetalloc(buf,2*ncbytesalloc(buf));
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytesappendn(buf, ptr, realsize);
|
2012-08-01 04:34:13 +08:00
|
|
|
#ifdef OCPROGRESS
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGNOTE,"callback: %lu bytes",(off_t)realsize);
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void
|
2017-03-09 08:01:10 +08:00
|
|
|
assembleurl(DAPURL* durl, NCbytes* buf, int what)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
encodeurltext(durl->url,buf);
|
|
|
|
if(what & WITHPROJ) {
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buf,"?");
|
2012-08-01 04:34:13 +08:00
|
|
|
encodeurltext(durl->projection,buf);
|
|
|
|
}
|
|
|
|
if(what & WITHSEL) encodeurltext(durl->selection,buf);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static char mustencode="";
|
|
|
|
static char hexchars[16] = {
|
|
|
|
'0', '1', '2', '3',
|
|
|
|
'4', '5', '6', '7',
|
|
|
|
'8', '9', 'a', 'b',
|
|
|
|
'c', 'd', 'e', 'f',
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2017-03-09 08:01:10 +08:00
|
|
|
encodeurltext(char* text, NCbytes* buf)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
/* Encode the URL to handle illegal characters */
|
|
|
|
len = strlen(url);
|
|
|
|
encoded = ocmalloc(len*4+1); /* should never be larger than this*/
|
|
|
|
if(encoded==NULL) return;
|
|
|
|
p = url; q = encoded;
|
|
|
|
while((c=*p++)) {
|
|
|
|
if(strchr(mustencode,c) != NULL) {
|
|
|
|
char tmp[8];
|
|
|
|
int hex1, hex2;
|
|
|
|
hex1 = (c & 0x0F);
|
|
|
|
hex2 = (c & 0xF0) >> 4;
|
|
|
|
tmp[0] = '0'; tmp[1] = 'x';
|
|
|
|
tmp[2] = hexchars[hex2]; tmp[3] = hexchars[hex1];
|
|
|
|
tmp[4] = '\0';
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytescat(buf,tmp);
|
2012-08-01 04:34:13 +08:00
|
|
|
} else *q++ = (char)c;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-05-09 04:13:51 +08:00
|
|
|
OCerror
|
2012-08-01 04:34:13 +08:00
|
|
|
occurlopen(CURL** curlp)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
CURL* curl;
|
|
|
|
/* initialize curl*/
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if (curl == NULL)
|
|
|
|
stat = OC_ECURL;
|
|
|
|
else {
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
stat = OC_ECURL;
|
|
|
|
}
|
|
|
|
if (curlp)
|
|
|
|
*curlp = curl;
|
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
occurlclose(CURL* curl)
|
|
|
|
{
|
|
|
|
if (curl != NULL)
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
}
|
|
|
|
|
2014-05-09 04:13:51 +08:00
|
|
|
OCerror
|
2012-08-01 04:34:13 +08:00
|
|
|
ocfetchlastmodified(CURL* curl, char* url, long* filetime)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
|
|
|
|
/* Set the URL */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_URL, (void*)url));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Ask for head */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30)); /* 30sec timeout*/
|
2020-01-10 02:48:04 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5));
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_HEADER, 1));
|
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_NOBODY, 1));
|
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1));
|
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_FILETIME, (long)1));
|
|
|
|
|
|
|
|
cstat = CURLERR(curl_easy_perform(curl));
|
2012-08-01 04:34:13 +08:00
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
if(filetime != NULL)
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_getinfo(curl,CURLINFO_FILETIME,filetime));
|
2012-08-01 04:34:13 +08:00
|
|
|
if(cstat != CURLE_OK) goto fail;
|
|
|
|
|
|
|
|
return OCTHROW(stat);
|
|
|
|
|
|
|
|
fail:
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(OC_ECURL);
|
|
|
|
}
|
|
|
|
|
2014-05-09 04:13:51 +08:00
|
|
|
OCerror
|
2012-08-01 04:34:13 +08:00
|
|
|
ocping(const char* url)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
CURLcode cstat = CURLE_OK;
|
|
|
|
CURL* curl = NULL;
|
2017-03-09 08:01:10 +08:00
|
|
|
NCbytes* buf = NULL;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Create a CURL instance */
|
|
|
|
stat = occurlopen(&curl);
|
|
|
|
if(stat != OC_NOERR) return stat;
|
|
|
|
|
2014-05-07 03:10:59 +08:00
|
|
|
/* Use redirects */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10L));
|
2014-05-07 03:10:59 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
2020-01-10 02:48:04 +08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* use a very short conn timeout: 10 seconds */
|
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, (long)10));
|
|
|
|
if (cstat != CURLE_OK)
|
2012-08-01 04:34:13 +08:00
|
|
|
goto done;
|
|
|
|
|
2018-09-05 01:27:47 +08:00
|
|
|
/* use a very short timeout: 5 seconds */
|
2017-03-09 08:01:10 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)5));
|
2014-06-27 06:40:08 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
/* fail on HTTP 400 code errors */
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_setopt(curl, CURLOPT_FAILONERROR, (long)1));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Try to get the file */
|
2017-03-09 08:01:10 +08:00
|
|
|
buf = ncbytesnew();
|
2017-09-01 04:19:56 +08:00
|
|
|
stat = ocfetchurl(curl,url,buf,NULL);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(stat == OC_NOERR) {
|
|
|
|
/* Don't trust curl to return an error when request gets 404 */
|
|
|
|
long http_code = 0;
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = CURLERR(curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &http_code));
|
2012-08-01 04:34:13 +08:00
|
|
|
if (cstat != CURLE_OK)
|
|
|
|
goto done;
|
|
|
|
if(http_code >= 400) {
|
|
|
|
cstat = CURLE_HTTP_RETURNED_ERROR;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
done:
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytesfree(buf);
|
2012-08-01 04:34:13 +08:00
|
|
|
occurlclose(curl);
|
|
|
|
if(cstat != CURLE_OK) {
|
2017-03-09 08:01:10 +08:00
|
|
|
nclog(NCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
|
2012-08-01 04:34:13 +08:00
|
|
|
stat = OC_EDAPSVC;
|
|
|
|
}
|
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|