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"
|
2017-08-31 07:44:57 +08:00
|
|
|
#include "ncrc.h"
|
2012-08-01 04:34:13 +08:00
|
|
|
#include "ocinternal.h"
|
|
|
|
#include "ocdebug.h"
|
2014-12-25 01:22:47 +08:00
|
|
|
#include "occurlfunctions.h"
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
#define OC_MAX_REDIRECTS 20L
|
2014-03-15 04:07:35 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Mnemonic */
|
|
|
|
#define OPTARG void*
|
2014-03-15 04:07:35 +08:00
|
|
|
|
2018-08-27 07:04:46 +08:00
|
|
|
/* Define some .rc file entries of interest*/
|
2014-12-25 01:22:47 +08:00
|
|
|
#define NETRCFILETAG "HTTP.NETRC"
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2018-08-27 07:04:46 +08:00
|
|
|
/* Check return value */
|
2014-12-25 01:22:47 +08:00
|
|
|
#define CHECK(state,flag,value) {if(check(state,flag,(void*)value) != OC_NOERR) {goto done;}}
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
static OCerror
|
|
|
|
check(OCstate* state, int flag, void* value)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2014-12-25 01:22:47 +08:00
|
|
|
OCerror stat = ocset_curlopt(state,flag,value);
|
|
|
|
return stat;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/*
|
|
|
|
Set a specific curl flag; primary wrapper for curl_easy_setopt
|
2012-08-01 04:34:13 +08:00
|
|
|
*/
|
2014-12-25 01:22:47 +08:00
|
|
|
OCerror
|
|
|
|
ocset_curlopt(OCstate* state, int flag, void* value)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2014-12-25 01:22:47 +08:00
|
|
|
OCerror stat = OC_NOERR;
|
2015-01-21 02:09:03 +08:00
|
|
|
CURLcode cstat = CURLE_OK;
|
2014-12-25 01:22:47 +08:00
|
|
|
cstat = OCCURLERR(state,curl_easy_setopt(state->curl,flag,value));
|
|
|
|
if(cstat != CURLE_OK)
|
|
|
|
stat = OC_ECURL;
|
|
|
|
return stat;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/*
|
|
|
|
Update a specific flag from state
|
|
|
|
*/
|
|
|
|
OCerror
|
|
|
|
ocset_curlflag(OCstate* state, int flag)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2014-12-25 01:22:47 +08:00
|
|
|
OCerror stat = OC_NOERR;
|
|
|
|
|
|
|
|
switch (flag) {
|
|
|
|
|
2017-08-30 04:11:15 +08:00
|
|
|
case CURLOPT_USERPWD: /* Does both user and pwd */
|
2017-09-01 04:19:56 +08:00
|
|
|
if(state->auth.creds.user != NULL && state->auth.creds.pwd != NULL) {
|
|
|
|
CHECK(state, CURLOPT_USERNAME, state->auth.creds.user);
|
|
|
|
CHECK(state, CURLOPT_PASSWORD, state->auth.creds.pwd);
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_HTTPAUTH, (OPTARG)CURLAUTH_ANY);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLOPT_COOKIEJAR: case CURLOPT_COOKIEFILE:
|
2017-09-01 04:19:56 +08:00
|
|
|
if(state->auth.curlflags.cookiejar) {
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Assume we will read and write cookies to same place */
|
2017-09-01 04:19:56 +08:00
|
|
|
CHECK(state, CURLOPT_COOKIEJAR, state->auth.curlflags.cookiejar);
|
|
|
|
CHECK(state, CURLOPT_COOKIEFILE, state->auth.curlflags.cookiejar);
|
2014-12-25 01:22:47 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
case CURLOPT_NETRC: case CURLOPT_NETRC_FILE:
|
2017-09-01 04:19:56 +08:00
|
|
|
if(state->auth.curlflags.netrc) {
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_NETRC, (OPTARG)CURL_NETRC_REQUIRED);
|
2017-09-01 04:19:56 +08:00
|
|
|
CHECK(state, CURLOPT_NETRC_FILE, state->auth.curlflags.netrc);
|
2014-12-25 01:22:47 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLOPT_VERBOSE:
|
2017-09-01 04:19:56 +08:00
|
|
|
if(state->auth.curlflags.verbose)
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_VERBOSE, (OPTARG)1L);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLOPT_TIMEOUT:
|
2017-09-01 04:19:56 +08:00
|
|
|
if(state->auth.curlflags.timeout)
|
|
|
|
CHECK(state, CURLOPT_TIMEOUT, (OPTARG)((long)state->auth.curlflags.timeout));
|
2014-12-25 01:22:47 +08:00
|
|
|
break;
|
|
|
|
|
2020-01-10 02:48:04 +08:00
|
|
|
case CURLOPT_CONNECTTIMEOUT:
|
|
|
|
if(state->auth.curlflags.connecttimeout)
|
|
|
|
CHECK(state, CURLOPT_CONNECTTIMEOUT, (OPTARG)((long)state->auth.curlflags.connecttimeout));
|
|
|
|
break;
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
case CURLOPT_USERAGENT:
|
2017-09-01 04:19:56 +08:00
|
|
|
if(state->auth.curlflags.useragent)
|
|
|
|
CHECK(state, CURLOPT_USERAGENT, state->auth.curlflags.useragent);
|
2014-12-25 01:22:47 +08:00
|
|
|
break;
|
2013-11-15 06:13:20 +08:00
|
|
|
|
2015-01-21 02:09:03 +08:00
|
|
|
case CURLOPT_FOLLOWLOCATION:
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_FOLLOWLOCATION, (OPTARG)1L);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLOPT_MAXREDIRS:
|
|
|
|
CHECK(state, CURLOPT_MAXREDIRS, (OPTARG)OC_MAX_REDIRECTS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLOPT_ERRORBUFFER:
|
|
|
|
CHECK(state, CURLOPT_ERRORBUFFER, state->error.curlerrorbuf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLOPT_ENCODING:
|
2012-08-01 04:34:13 +08:00
|
|
|
#ifdef CURLOPT_ENCODING
|
2017-09-01 04:19:56 +08:00
|
|
|
if(state->auth.curlflags.compress) {
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_ENCODING,"deflate, gzip");
|
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
2014-12-25 01:22:47 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CURLOPT_PROXY:
|
2017-09-01 04:19:56 +08:00
|
|
|
if(state->auth.proxy.host != NULL) {
|
|
|
|
CHECK(state, CURLOPT_PROXY, state->auth.proxy.host);
|
|
|
|
CHECK(state, CURLOPT_PROXYPORT, (OPTARG)(long)state->auth.proxy.port);
|
|
|
|
if(state->auth.proxy.user != NULL && state->auth.proxy.pwd != NULL) {
|
|
|
|
CHECK(state, CURLOPT_PROXYUSERNAME, state->auth.proxy.user);
|
|
|
|
CHECK(state, CURLOPT_PROXYPASSWORD, state->auth.proxy.pwd);
|
2014-12-25 01:22:47 +08:00
|
|
|
#ifdef CURLOPT_PROXYAUTH
|
|
|
|
CHECK(state, CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
|
2013-11-15 06:13:20 +08:00
|
|
|
#endif
|
2014-12-25 01:22:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
case CURLOPT_USE_SSL:
|
|
|
|
case CURLOPT_SSLCERT: case CURLOPT_SSLKEY:
|
|
|
|
case CURLOPT_SSL_VERIFYPEER: case CURLOPT_SSL_VERIFYHOST:
|
|
|
|
{
|
2017-09-01 04:19:56 +08:00
|
|
|
struct ssl* ssl = &state->auth.ssl;
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_SSL_VERIFYPEER, (OPTARG)(ssl->verifypeer?1L:0L));
|
2015-05-25 07:31:39 +08:00
|
|
|
CHECK(state, CURLOPT_SSL_VERIFYHOST, (OPTARG)(ssl->verifyhost?1L:0L));
|
2014-12-25 01:22:47 +08:00
|
|
|
if(ssl->certificate)
|
|
|
|
CHECK(state, CURLOPT_SSLCERT, ssl->certificate);
|
2015-01-21 02:09:03 +08:00
|
|
|
if(ssl->key)
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_SSLKEY, ssl->key);
|
|
|
|
if(ssl->keypasswd)
|
|
|
|
/* libcurl prior to 7.16.4 used 'CURLOPT_SSLKEYPASSWD' */
|
|
|
|
CHECK(state, CURLOPT_KEYPASSWD, ssl->keypasswd);
|
2015-01-21 02:09:03 +08:00
|
|
|
if(ssl->cainfo)
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_CAINFO, ssl->cainfo);
|
2015-01-21 02:09:03 +08:00
|
|
|
if(ssl->capath)
|
2014-12-25 01:22:47 +08:00
|
|
|
CHECK(state, CURLOPT_CAPATH, ssl->capath);
|
2015-01-21 02:09:03 +08:00
|
|
|
}
|
2014-12-25 01:22:47 +08:00
|
|
|
break;
|
|
|
|
|
2018-08-27 07:04:46 +08:00
|
|
|
#ifdef HAVE_CURLOPT_BUFFERSIZE
|
|
|
|
case CURLOPT_BUFFERSIZE:
|
|
|
|
CHECK(state, CURLOPT_BUFFERSIZE, (OPTARG)state->curlbuffersize);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CURLOPT_KEEPALIVE
|
|
|
|
case CURLOPT_TCP_KEEPALIVE:
|
|
|
|
if(state->curlkeepalive.active != 0)
|
|
|
|
CHECK(state, CURLOPT_TCP_KEEPALIVE, (OPTARG)1L);
|
|
|
|
if(state->curlkeepalive.idle > 0)
|
|
|
|
CHECK(state, CURLOPT_TCP_KEEPIDLE, (OPTARG)state->curlkeepalive.idle);
|
|
|
|
if(state->curlkeepalive.interval > 0)
|
|
|
|
CHECK(state, CURLOPT_TCP_KEEPINTVL, (OPTARG)state->curlkeepalive.interval);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
nclog(NCLOGWARN,"Attempt to update unexpected curl flag: %d",flag);
|
|
|
|
break;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2014-12-25 01:22:47 +08:00
|
|
|
done:
|
2015-01-21 02:09:03 +08:00
|
|
|
return stat;
|
2014-12-25 01:22:47 +08:00
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2012-12-04 11:32:41 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Set various general curl flags per fetch */
|
|
|
|
OCerror
|
|
|
|
ocset_flags_perfetch(OCstate* state)
|
|
|
|
{
|
|
|
|
OCerror stat = OC_NOERR;
|
|
|
|
/* currently none */
|
|
|
|
return stat;
|
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Set various general curl flags per link */
|
2014-11-22 07:20:44 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
OCerror
|
|
|
|
ocset_flags_perlink(OCstate* state)
|
|
|
|
{
|
|
|
|
OCerror stat = OC_NOERR;
|
|
|
|
|
|
|
|
/* Following are always set */
|
2015-10-22 04:53:30 +08:00
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_ENCODING);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_NETRC);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_VERBOSE);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_TIMEOUT);
|
2020-01-10 02:48:04 +08:00
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_CONNECTTIMEOUT);
|
2015-10-22 04:53:30 +08:00
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_USERAGENT);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_COOKIEJAR);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_USERPWD);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_PROXY);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state,CURLOPT_USE_SSL);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state, CURLOPT_FOLLOWLOCATION);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state, CURLOPT_MAXREDIRS);
|
|
|
|
if(stat == OC_NOERR) stat = ocset_curlflag(state, CURLOPT_ERRORBUFFER);
|
2018-08-27 07:04:46 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CURLOPT_BUFFERSIZE
|
|
|
|
/* Optional */
|
|
|
|
if(stat == OC_NOERR && state->curlbuffersize > 0)
|
|
|
|
stat = ocset_curlflag(state, CURLOPT_BUFFERSIZE);
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_CURLOPT_KEEPALIVE
|
|
|
|
if(stat == NC_NOERR && state->curlkeepalive.active != 0)
|
|
|
|
stat = ocset_curlflag(state, CURLOPT_TCP_KEEPALIVE);
|
|
|
|
#endif
|
2014-12-25 01:22:47 +08:00
|
|
|
return stat;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
void
|
|
|
|
oc_curl_debug(OCstate* state)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2017-09-01 04:19:56 +08:00
|
|
|
state->auth.curlflags.verbose = 1;
|
2014-12-25 01:22:47 +08:00
|
|
|
ocset_curlflag(state,CURLOPT_VERBOSE);
|
|
|
|
ocset_curlflag(state,CURLOPT_ERRORBUFFER);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Misc. */
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
int
|
|
|
|
ocrc_netrc_required(OCstate* state)
|
|
|
|
{
|
2017-09-01 04:19:56 +08:00
|
|
|
char* netrcfile = NC_rclookup(NETRCFILETAG,state->uri->uri);
|
|
|
|
return (netrcfile != NULL || state->auth.curlflags.netrc != NULL ? 0 : 1);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
void
|
|
|
|
oc_curl_printerror(OCstate* state)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2014-12-25 01:22:47 +08:00
|
|
|
fprintf(stderr,"curl error details: %s\n",state->curlerror);
|
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2017-09-03 08:09:36 +08:00
|
|
|
/* See if http: protocol is supported */
|
2014-12-25 01:22:47 +08:00
|
|
|
void
|
2017-09-01 04:19:56 +08:00
|
|
|
oc_curl_protocols(OCstate* state)
|
2014-12-25 01:22:47 +08:00
|
|
|
{
|
|
|
|
const char* const* proto; /*weird*/
|
|
|
|
curl_version_info_data* curldata;
|
|
|
|
curldata = curl_version_info(CURLVERSION_NOW);
|
|
|
|
for(proto=curldata->protocols;*proto;proto++) {
|
2017-09-03 08:09:36 +08:00
|
|
|
if(strcmp("http",*proto)==0)
|
|
|
|
state->auth.curlflags.proto_https=1;
|
2014-12-25 01:22:47 +08:00
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|