2012-08-01 04:34:13 +08:00
|
|
|
/* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
|
|
|
|
See the COPYRIGHT file for more information. */
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2015-05-29 00:14:45 +08:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
typedef int pid_t;
|
|
|
|
#endif
|
|
|
|
|
2013-11-15 06:13:20 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
#include "ocinternal.h"
|
|
|
|
#include "ocdebug.h"
|
|
|
|
#include "occlientparams.h"
|
|
|
|
#include "occurlfunctions.h"
|
|
|
|
#include "ochttp.h"
|
|
|
|
#include "ocread.h"
|
|
|
|
#include "dapparselex.h"
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
#define DATADDSFILE "datadds"
|
|
|
|
|
|
|
|
#if 0
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Note: TMPPATH must end in '/' */
|
|
|
|
#ifdef __CYGWIN__
|
2013-11-15 06:13:20 +08:00
|
|
|
#define TMPPATH1 "/cygdrive/c/temp/datadds"
|
|
|
|
#define TMPPATH2 "./datadds"
|
2013-11-16 06:41:17 +08:00
|
|
|
#elif defined(_WIN32) || defined(_WIN64)
|
2013-11-15 06:13:20 +08:00
|
|
|
#define TMPPATH1 "c:\\temp\\datadds"
|
|
|
|
#define TMPPATH2 ".\\datadds"
|
2012-08-01 04:34:13 +08:00
|
|
|
#else
|
2013-11-15 06:13:20 +08:00
|
|
|
#define TMPPATH1 "/tmp/datadds"
|
|
|
|
#define TMPPATH2 "./datadds"
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
2014-12-25 01:22:47 +08:00
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2013-02-08 06:14:57 +08:00
|
|
|
#define CLBRACE '{'
|
|
|
|
#define CRBRACE '}'
|
|
|
|
|
2014-05-07 03:10:59 +08:00
|
|
|
static OCerror ocextractddsinmemory(OCstate*,OCtree*,int);
|
|
|
|
static OCerror ocextractddsinfile(OCstate*,OCtree*,int);
|
2012-08-01 04:34:13 +08:00
|
|
|
static char* constraintescape(const char* url);
|
|
|
|
static OCerror createtempfile(OCstate*,OCtree*);
|
2013-02-08 06:14:57 +08:00
|
|
|
static int dataError(XXDR* xdrs, OCstate*);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
static OCerror ocset_curlproperties(OCstate*);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
extern OCnode* makeunlimiteddimension(void);
|
|
|
|
|
2013-11-16 06:41:17 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2012-12-19 05:08:23 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
2012-08-01 04:34:13 +08:00
|
|
|
#include <fcntl.h>
|
2012-12-19 05:08:23 +08:00
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
#define _S_IREAD 256
|
|
|
|
#define _S_IWRITE 128
|
|
|
|
#else
|
2012-12-19 05:08:23 +08:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
2012-08-01 04:34:13 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
2012-12-19 05:08:23 +08:00
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/* Collect global state info in one place */
|
|
|
|
struct OCGLOBALSTATE ocglobalstate;
|
|
|
|
|
2014-05-09 04:13:51 +08:00
|
|
|
OCerror
|
2012-08-01 04:34:13 +08:00
|
|
|
ocinternalinitialize(void)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
2014-12-25 01:22:47 +08:00
|
|
|
|
2015-03-03 11:26:39 +08:00
|
|
|
#if 0
|
2015-01-16 05:19:51 +08:00
|
|
|
if(sizeof(off_t) != sizeof(void*)) {
|
2015-01-17 00:18:39 +08:00
|
|
|
fprintf(stderr,"OC xxdr depends on the assumption that sizeof(off_t) == sizeof(void*)\n");
|
|
|
|
//Commenting out for now, as this does not hold true on 32-bit
|
|
|
|
//linux systems.
|
|
|
|
//OCASSERT(sizeof(off_t) == sizeof(void*));
|
2014-12-25 01:22:47 +08:00
|
|
|
}
|
2015-03-03 11:26:39 +08:00
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
if(!ocglobalstate.initialized) {
|
2015-01-17 00:18:39 +08:00
|
|
|
memset((void*)&ocglobalstate,0,sizeof(ocglobalstate));
|
|
|
|
ocglobalstate.initialized = 1;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
/* Capture temp dir*/
|
2013-11-15 06:13:20 +08:00
|
|
|
{
|
2014-12-01 11:30:23 +08:00
|
|
|
char* tempdir;
|
2013-11-15 06:13:20 +08:00
|
|
|
char* p;
|
|
|
|
char* q;
|
|
|
|
char cwd[4096];
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2014-12-01 11:30:23 +08:00
|
|
|
tempdir = getenv("TEMP");
|
2013-11-15 06:13:20 +08:00
|
|
|
#else
|
2014-12-01 11:30:23 +08:00
|
|
|
tempdir = "/tmp";
|
2013-11-15 06:13:20 +08:00
|
|
|
#endif
|
2014-12-01 11:30:23 +08:00
|
|
|
if(tempdir == NULL) {
|
|
|
|
fprintf(stderr,"Cannot find a temp dir; using ./\n");
|
|
|
|
tempdir = getcwd(cwd,sizeof(cwd));
|
|
|
|
if(tempdir == NULL || *tempdir == '\0') tempdir = ".";
|
2013-11-15 06:13:20 +08:00
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
ocglobalstate.tempdir= (char*)malloc(strlen(tempdir) + 1);
|
|
|
|
for(p=tempdir,q=ocglobalstate.tempdir;*p;p++,q++) {
|
|
|
|
if((*p == '/' && *(p+1) == '/')
|
|
|
|
|| (*p == '\\' && *(p+1) == '\\')) {p++;}
|
|
|
|
*q = *p;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#else
|
|
|
|
/* Canonicalize */
|
|
|
|
for(p=ocglobalstate.tempdir;*p;p++) {
|
|
|
|
if(*p == '\\') {*p = '/'; };
|
2013-11-15 06:13:20 +08:00
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
*q = '\0';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Capture $HOME */
|
|
|
|
{
|
|
|
|
char* p;
|
|
|
|
char* q;
|
|
|
|
char* home = getenv("HOME");
|
2013-11-15 06:13:20 +08:00
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
if(home == NULL) {
|
|
|
|
/* use tempdir */
|
|
|
|
home = ocglobalstate.tempdir;
|
|
|
|
}
|
2013-11-15 06:13:20 +08:00
|
|
|
ocglobalstate.home = (char*)malloc(strlen(home) + 1);
|
|
|
|
for(p=home,q=ocglobalstate.home;*p;p++,q++) {
|
2014-12-01 11:30:23 +08:00
|
|
|
if((*p == '/' && *(p+1) == '/')
|
|
|
|
|| (*p == '\\' && *(p+1) == '\\')) {p++;}
|
|
|
|
*q = *p;
|
2013-11-15 06:13:20 +08:00
|
|
|
}
|
|
|
|
*q = '\0';
|
2014-12-01 11:30:23 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#else
|
|
|
|
/* Canonicalize */
|
|
|
|
for(p=home;*p;p++) {
|
|
|
|
if(*p == '\\') {*p = '/'; };
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-15 06:13:20 +08:00
|
|
|
}
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Compute some xdr related flags */
|
|
|
|
xxdr_init();
|
|
|
|
|
|
|
|
ocloginit();
|
|
|
|
|
|
|
|
oc_curl_protocols(&ocglobalstate); /* see what protocols are supported */
|
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
/**************************************************/
|
|
|
|
OCerror
|
|
|
|
ocopen(OCstate** statep, const char* url)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
OCstate * state = NULL;
|
|
|
|
OCURI* tmpurl = NULL;
|
|
|
|
CURL* curl = NULL; /* curl handle*/
|
|
|
|
|
|
|
|
if(!ocuriparse(url,&tmpurl)) {OCTHROWCHK(stat=OC_EBADURL); goto fail;}
|
2015-01-13 03:09:34 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
stat = occurlopen(&curl);
|
|
|
|
if(stat != OC_NOERR) {OCTHROWCHK(stat); goto fail;}
|
|
|
|
|
|
|
|
state = (OCstate*)ocmalloc(sizeof(OCstate)); /* ocmalloc zeros memory*/
|
|
|
|
if(state == NULL) {OCTHROWCHK(stat=OC_ENOMEM); goto fail;}
|
|
|
|
|
|
|
|
/* Setup DAP state*/
|
|
|
|
state->header.magic = OCMAGIC;
|
|
|
|
state->header.occlass = OC_State;
|
|
|
|
state->curl = curl;
|
|
|
|
state->trees = oclistnew();
|
|
|
|
state->uri = tmpurl;
|
|
|
|
if(!ocuridecodeparams(state->uri)) {
|
2012-08-09 07:15:18 +08:00
|
|
|
oclog(OCLOGWARN,"Could not parse client parameters");
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
state->packet = ocbytesnew();
|
|
|
|
ocbytessetalloc(state->packet,DFALTPACKETSIZE); /*initial reasonable size*/
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/* capture curl properties for this link from rc file1*/
|
|
|
|
stat = ocset_curlproperties(state);
|
|
|
|
if(stat != OC_NOERR) goto fail;
|
|
|
|
|
|
|
|
/* Set the one-time curl flags */
|
|
|
|
if((stat=ocset_flags_perlink(state))!= OC_NOERR) goto fail;
|
|
|
|
#if 1 /* temporarily make per-link */
|
|
|
|
if((stat=ocset_flags_perfetch(state))!= OC_NOERR) goto fail;
|
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
if(statep) *statep = state;
|
2015-01-21 01:53:42 +08:00
|
|
|
else {
|
|
|
|
if(state != NULL) ocfree(state);
|
|
|
|
}
|
2015-01-13 03:09:34 +08:00
|
|
|
return OCTHROW(stat);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
fail:
|
|
|
|
ocurifree(tmpurl);
|
|
|
|
if(state != NULL) ocfree(state);
|
|
|
|
if(curl != NULL) occurlclose(curl);
|
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
OCerror
|
|
|
|
ocfetch(OCstate* state, const char* constraint, OCdxd kind, OCflags flags,
|
|
|
|
OCnode** rootp)
|
|
|
|
{
|
|
|
|
OCtree* tree = NULL;
|
|
|
|
OCnode* root = NULL;
|
|
|
|
OCerror stat = OC_NOERR;
|
2015-01-13 03:09:34 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
tree = (OCtree*)ocmalloc(sizeof(OCtree));
|
|
|
|
MEMCHECK(tree,OC_ENOMEM);
|
|
|
|
memset((void*)tree,0,sizeof(OCtree));
|
|
|
|
tree->dxdclass = kind;
|
|
|
|
tree->state = state;
|
|
|
|
tree->constraint = constraintescape(constraint);
|
|
|
|
if(tree->constraint == NULL)
|
|
|
|
tree->constraint = nulldup(constraint);
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Set per-fetch curl properties */
|
|
|
|
#if 0 /* temporarily make per-link */
|
|
|
|
if((stat=ocset_flags_perfetch(state))!= OC_NOERR) goto fail;
|
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
ocbytesclear(state->packet);
|
|
|
|
|
|
|
|
switch (kind) {
|
|
|
|
case OCDAS:
|
|
|
|
stat = readDAS(state,tree);
|
|
|
|
if(stat == OC_NOERR) {
|
|
|
|
tree->text = ocbytesdup(state->packet);
|
|
|
|
if(tree->text == NULL) stat = OC_EDAS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OCDDS:
|
|
|
|
stat = readDDS(state,tree);
|
|
|
|
if(stat == OC_NOERR) {
|
|
|
|
tree->text = ocbytesdup(state->packet);
|
|
|
|
if(tree->text == NULL) stat = OC_EDDS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OCDATADDS:
|
|
|
|
if((flags & OCONDISK) != 0) {/* store in file */
|
|
|
|
/* Create the datadds file immediately
|
|
|
|
so that DRNO can reference it*/
|
|
|
|
/* Make the tmp file*/
|
|
|
|
stat = createtempfile(state,tree);
|
2012-08-03 02:43:21 +08:00
|
|
|
if(stat) {OCTHROWCHK(stat); goto fail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
stat = readDATADDS(state,tree,flags);
|
|
|
|
if(stat == OC_NOERR) {
|
|
|
|
/* Separate the DDS from data and return the dds;
|
|
|
|
will modify packet */
|
|
|
|
stat = ocextractddsinfile(state,tree,flags);
|
|
|
|
}
|
|
|
|
} else { /*inmemory*/
|
|
|
|
stat = readDATADDS(state,tree,flags);
|
|
|
|
if(stat == OC_NOERR) {
|
|
|
|
/* Separate the DDS from data and return the dds;
|
|
|
|
will modify packet */
|
|
|
|
stat = ocextractddsinmemory(state,tree,flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}/*switch*/
|
2014-03-12 01:58:22 +08:00
|
|
|
/* Obtain any http code */
|
|
|
|
state->error.httpcode = ocfetchhttpcode(state->curl);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(stat != OC_NOERR) {
|
|
|
|
if(state->error.httpcode >= 400) {
|
2012-08-09 07:15:18 +08:00
|
|
|
oclog(OCLOGWARN,"oc_open: Could not read url; http error = %l",state->error.httpcode);
|
2012-08-01 04:34:13 +08:00
|
|
|
} else {
|
2012-08-09 07:15:18 +08:00
|
|
|
oclog(OCLOGWARN,"oc_open: Could not read url");
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
2012-08-03 02:43:21 +08:00
|
|
|
goto fail;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tree->nodes = NULL;
|
|
|
|
stat = DAPparse(state,tree,tree->text);
|
|
|
|
/* Check and report on an error return from the server */
|
|
|
|
if(stat == OC_EDAPSVC && state->error.code != NULL) {
|
2012-08-09 07:15:18 +08:00
|
|
|
oclog(OCLOGERR,"oc_open: server error retrieving url: code=%s message=\"%s\"",
|
2015-01-13 03:09:34 +08:00
|
|
|
state->error.code,
|
2012-08-01 04:34:13 +08:00
|
|
|
(state->error.message?state->error.message:""));
|
|
|
|
}
|
2012-08-03 02:43:21 +08:00
|
|
|
if(stat) {OCTHROWCHK(stat); goto fail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
root = tree->root;
|
|
|
|
/* make sure */
|
|
|
|
tree->root = root;
|
|
|
|
root->tree = tree;
|
|
|
|
|
|
|
|
/* Verify the parse */
|
|
|
|
switch (kind) {
|
|
|
|
case OCDAS:
|
|
|
|
if(root->octype != OC_Attributeset)
|
2012-08-03 02:43:21 +08:00
|
|
|
{OCTHROWCHK(stat=OC_EDAS); goto fail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
break;
|
|
|
|
case OCDDS:
|
|
|
|
if(root->octype != OC_Dataset)
|
2012-08-03 02:43:21 +08:00
|
|
|
{OCTHROWCHK(stat=OC_EDDS); goto fail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
break;
|
|
|
|
case OCDATADDS:
|
|
|
|
if(root->octype != OC_Dataset)
|
2012-08-03 02:43:21 +08:00
|
|
|
{OCTHROWCHK(stat=OC_EDATADDS); goto fail;}
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Modify the tree kind */
|
|
|
|
tree->dxdclass = OCDATADDS;
|
|
|
|
break;
|
|
|
|
default: return OC_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(kind != OCDAS) {
|
|
|
|
/* Process ocnodes to mark those that are cacheable */
|
|
|
|
ocmarkcacheable(state,root);
|
|
|
|
/* Process ocnodes to handle various semantic issues*/
|
|
|
|
occomputesemantics(tree->nodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process ocnodes to compute name info*/
|
|
|
|
occomputefullnames(tree->root);
|
|
|
|
|
|
|
|
if(kind == OCDATADDS) {
|
|
|
|
if((flags & OCONDISK) != 0) {
|
|
|
|
tree->data.xdrs = xxdr_filecreate(tree->data.file,tree->data.bod);
|
|
|
|
} else {
|
2012-08-22 00:46:10 +08:00
|
|
|
#ifdef OCDEBUG
|
|
|
|
fprintf(stderr,"ocfetch.datadds.memory: datasize=%lu bod=%lu\n",
|
|
|
|
(unsigned long)tree->data.datasize,(unsigned long)tree->data.bod);
|
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Switch to zero based memory */
|
|
|
|
tree->data.xdrs
|
|
|
|
= xxdr_memcreate(tree->data.memory,tree->data.datasize,tree->data.bod);
|
|
|
|
}
|
|
|
|
MEMCHECK(tree->data.xdrs,OC_ENOMEM);
|
2013-02-08 06:14:57 +08:00
|
|
|
/* Do a quick check to see if server returned an ERROR {}
|
|
|
|
at the beginning of the data
|
|
|
|
*/
|
|
|
|
if(dataError(tree->data.xdrs,state)) {
|
|
|
|
stat = OC_EDATADDS;
|
|
|
|
oclog(OCLOGERR,"oc_open: server error retrieving url: code=%s message=\"%s\"",
|
2015-01-13 03:09:34 +08:00
|
|
|
state->error.code,
|
2013-02-08 06:14:57 +08:00
|
|
|
(state->error.message?state->error.message:""));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
/* Compile the data into a more accessible format */
|
|
|
|
stat = occompile(state,tree->root);
|
|
|
|
if(stat != OC_NOERR)
|
2012-08-03 02:43:21 +08:00
|
|
|
goto fail;
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put root into the state->trees list */
|
2012-08-20 05:12:35 +08:00
|
|
|
oclistpush(state->trees,(void*)root);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
if(rootp) *rootp = root;
|
|
|
|
return stat;
|
|
|
|
|
|
|
|
fail:
|
2012-08-03 02:43:21 +08:00
|
|
|
if(root != NULL)
|
|
|
|
ocroot_free(root);
|
|
|
|
else if(tree != NULL)
|
|
|
|
octree_free(tree);
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OCerror
|
|
|
|
createtempfile(OCstate* state, OCtree* tree)
|
|
|
|
{
|
2013-11-15 06:13:20 +08:00
|
|
|
int stat = OC_NOERR;
|
2014-12-25 01:22:47 +08:00
|
|
|
char* path = NULL;
|
2012-08-01 04:34:13 +08:00
|
|
|
char* name = NULL;
|
2014-12-25 01:22:47 +08:00
|
|
|
int len;
|
|
|
|
|
2015-01-13 03:09:34 +08:00
|
|
|
len =
|
2014-12-25 01:22:47 +08:00
|
|
|
strlen(ocglobalstate.tempdir)
|
|
|
|
+ 1 /* '/' */
|
|
|
|
+ strlen(DATADDSFILE);
|
|
|
|
path = (char*)malloc(len+1);
|
|
|
|
if(path == NULL) return OC_ENOMEM;
|
|
|
|
occopycat(path,len,3,ocglobalstate.tempdir,"/",DATADDSFILE);
|
|
|
|
stat = ocmktmp(path,&name);
|
|
|
|
free(path);
|
2013-11-15 06:13:20 +08:00
|
|
|
if(stat != OC_NOERR) goto fail;
|
2012-08-01 04:34:13 +08:00
|
|
|
#ifdef OCDEBUG
|
2014-12-25 01:22:47 +08:00
|
|
|
oclog(OCLOGNOTE,"oc_open: creating tmp file: %s",name);
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
|
|
|
tree->data.filename = name; /* remember our tmp file name */
|
2014-12-25 01:22:47 +08:00
|
|
|
name = NULL;
|
2015-01-21 03:44:28 +08:00
|
|
|
tree->data.file = fopen(tree->data.filename,"w+");
|
2012-08-01 04:34:13 +08:00
|
|
|
if(tree->data.file == NULL) return OC_EOPEN;
|
|
|
|
/* unlink the temp file so it will automatically be reclaimed */
|
|
|
|
if(ocdebug == 0) unlink(tree->data.filename);
|
2013-11-15 06:13:20 +08:00
|
|
|
return stat;
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2013-11-15 06:13:20 +08:00
|
|
|
fail:
|
2014-03-29 07:11:26 +08:00
|
|
|
if(name != NULL) {
|
2014-06-27 06:40:08 +08:00
|
|
|
oclog(OCLOGERR,"oc_open: attempt to create tmp file failed: %s",name);
|
|
|
|
free(name);
|
2014-03-29 07:11:26 +08:00
|
|
|
} else {
|
2014-06-27 06:40:08 +08:00
|
|
|
oclog(OCLOGERR,"oc_open: attempt to create tmp file failed: NULL");
|
2014-03-29 07:11:26 +08:00
|
|
|
}
|
2014-11-22 07:20:44 +08:00
|
|
|
return OCTHROW(stat);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
occlose(OCstate* state)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
if(state == NULL) return;
|
|
|
|
|
|
|
|
/* Warning: ocfreeroot will attempt to remove the root from state->trees */
|
|
|
|
/* Ok in this case because we are popping the root out of state->trees */
|
|
|
|
for(i=0;i<oclistlength(state->trees);i++) {
|
|
|
|
OCnode* root = (OCnode*)oclistpop(state->trees);
|
|
|
|
ocroot_free(root);
|
|
|
|
}
|
|
|
|
oclistfree(state->trees);
|
|
|
|
ocurifree(state->uri);
|
|
|
|
ocbytesfree(state->packet);
|
|
|
|
ocfree(state->error.code);
|
|
|
|
ocfree(state->error.message);
|
|
|
|
ocfree(state->curlflags.useragent);
|
2013-11-16 02:38:54 +08:00
|
|
|
if(state->curlflags.cookiejar) {
|
2014-11-22 07:20:44 +08:00
|
|
|
#if 0
|
2014-12-25 01:22:47 +08:00
|
|
|
if(state->curlflags.createdflags & COOKIECREATED)
|
|
|
|
unlink(state->curlflags.cookiejar);
|
2014-11-22 07:20:44 +08:00
|
|
|
#endif
|
2013-11-16 02:38:54 +08:00
|
|
|
ocfree(state->curlflags.cookiejar);
|
|
|
|
}
|
2014-12-25 01:22:47 +08:00
|
|
|
if(state->curlflags.netrc != NULL) {
|
|
|
|
#if 0
|
|
|
|
if(state->curlflags.createdflags & NETRCCREATED)
|
|
|
|
unlink(state->curlflags.netrc);
|
|
|
|
#endif
|
|
|
|
ocfree(state->curlflags.netrc);
|
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
ocfree(state->ssl.certificate);
|
|
|
|
ocfree(state->ssl.key);
|
|
|
|
ocfree(state->ssl.keypasswd);
|
|
|
|
ocfree(state->ssl.cainfo);
|
2015-01-13 03:09:34 +08:00
|
|
|
ocfree(state->ssl.capath);
|
2012-08-01 04:34:13 +08:00
|
|
|
ocfree(state->proxy.host);
|
2014-12-25 01:22:47 +08:00
|
|
|
ocfree(state->proxy.userpwd);
|
|
|
|
ocfree(state->creds.userpwd);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(state->curl != NULL) occurlclose(state->curl);
|
|
|
|
ocfree(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OCerror
|
|
|
|
ocextractddsinmemory(OCstate* state, OCtree* tree, OCflags flags)
|
|
|
|
{
|
|
|
|
OCerror stat = OC_NOERR;
|
|
|
|
size_t ddslen, bod, bodfound;
|
|
|
|
/* Read until we find the separator (or EOF)*/
|
|
|
|
bodfound = ocfindbod(state->packet,&bod,&ddslen);
|
|
|
|
if(!bodfound) {/* No BOD; pretend */
|
|
|
|
bod = tree->data.bod;
|
|
|
|
ddslen = tree->data.datasize;
|
|
|
|
}
|
|
|
|
tree->data.bod = bod;
|
|
|
|
tree->data.ddslen = ddslen;
|
|
|
|
/* copy out the dds */
|
|
|
|
if(ddslen > 0) {
|
|
|
|
tree->text = (char*)ocmalloc(ddslen+1);
|
|
|
|
memcpy((void*)tree->text,(void*)ocbytescontents(state->packet),ddslen);
|
|
|
|
tree->text[ddslen] = '\0';
|
|
|
|
} else
|
|
|
|
tree->text = NULL;
|
|
|
|
/* Extract the inmemory contents */
|
|
|
|
tree->data.memory = ocbytesextract(state->packet);
|
|
|
|
#ifdef OCIGNORE
|
|
|
|
/* guarantee the data part is on an 8 byte boundary */
|
|
|
|
if(tree->data.bod % 8 != 0) {
|
|
|
|
unsigned long count = tree->data.datasize - tree->data.bod;
|
|
|
|
memcpy(tree->xdrmemory,tree->xdrmemory+tree->data.bod,count);
|
|
|
|
tree->data.datasize = count;
|
|
|
|
tree->data.bod = 0;
|
|
|
|
tree->data.ddslen = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if(tree->text == NULL) stat = OC_EDATADDS;
|
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
static OCerror
|
|
|
|
ocextractddsinfile(OCstate* state, OCtree* tree, OCflags flags)
|
|
|
|
{
|
|
|
|
OCerror stat = OC_NOERR;
|
|
|
|
size_t ddslen, bod, bodfound;
|
|
|
|
|
|
|
|
/* Read until we find the separator (or EOF)*/
|
|
|
|
ocbytesclear(state->packet);
|
|
|
|
rewind(tree->data.file);
|
|
|
|
bodfound = 0;
|
|
|
|
do {
|
|
|
|
char chunk[1024];
|
|
|
|
size_t count;
|
|
|
|
/* read chunks of the file until we find the separator*/
|
|
|
|
count = fread(chunk,1,sizeof(chunk),tree->data.file);
|
|
|
|
if(count <= 0) break; /* EOF;*/
|
|
|
|
ocbytesappendn(state->packet,chunk,count);
|
|
|
|
bodfound = ocfindbod(state->packet,&bod,&ddslen);
|
|
|
|
} while(!bodfound);
|
|
|
|
if(!bodfound) {/* No BOD; pretend */
|
|
|
|
bod = tree->data.bod;
|
|
|
|
ddslen = tree->data.datasize;
|
2012-08-18 04:51:03 +08:00
|
|
|
#ifdef OCDEBUG
|
2012-08-22 00:46:10 +08:00
|
|
|
fprintf(stderr,"missing bod: ddslen=%lu bod=%lu\n",
|
2012-08-20 05:12:35 +08:00
|
|
|
(unsigned long)ddslen,(unsigned long)bod);
|
2012-08-18 04:51:03 +08:00
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
tree->data.bod = bod;
|
|
|
|
tree->data.ddslen = ddslen;
|
|
|
|
/* copy out the dds */
|
|
|
|
if(ddslen > 0) {
|
|
|
|
tree->text = (char*)ocmalloc(ddslen+1);
|
|
|
|
memcpy((void*)tree->text,(void*)ocbytescontents(state->packet),ddslen);
|
|
|
|
tree->text[ddslen] = '\0';
|
|
|
|
} else
|
|
|
|
tree->text = NULL;
|
|
|
|
/* reset the position of the tmp file*/
|
2014-11-22 07:20:44 +08:00
|
|
|
if(fseek(tree->data.file,(long)tree->data.bod,SEEK_SET) < 0
|
|
|
|
|| tree->text == NULL)
|
|
|
|
stat = OC_EDATADDS;
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow these (non-alpha-numerics) to pass thru */
|
|
|
|
static char okchars[] = "&/:;,.=?@'\"<>{}!|\\^[]`~";
|
|
|
|
static char hexdigits[] = "0123456789abcdef";
|
|
|
|
|
|
|
|
/* Modify constraint to use %XX escapes */
|
|
|
|
static char*
|
|
|
|
constraintescape(const char* url)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char* p;
|
|
|
|
int c;
|
|
|
|
char* eurl;
|
|
|
|
|
|
|
|
if(url == NULL) return NULL;
|
|
|
|
len = strlen(url);
|
|
|
|
eurl = ocmalloc(1+3*len); /* worst case: c -> %xx */
|
|
|
|
MEMCHECK(eurl,NULL);
|
|
|
|
p = eurl;
|
|
|
|
*p = '\0';
|
|
|
|
while((c=*url++)) {
|
|
|
|
if(c >= '0' && c <= '9') {*p++ = c;}
|
|
|
|
else if(c >= 'a' && c <= 'z') {*p++ = c;}
|
|
|
|
else if(c >= 'A' && c <= 'Z') {*p++ = c;}
|
|
|
|
else if(strchr(okchars,c) != NULL) {*p++ = c;}
|
|
|
|
else {
|
|
|
|
*p++ = '%';
|
|
|
|
*p++ = hexdigits[(c & 0xf0)>>4];
|
|
|
|
*p++ = hexdigits[(c & 0xf)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return eurl;
|
|
|
|
}
|
|
|
|
|
|
|
|
OCerror
|
|
|
|
ocupdatelastmodifieddata(OCstate* state)
|
|
|
|
{
|
|
|
|
OCerror status = OC_NOERR;
|
|
|
|
long lastmodified;
|
|
|
|
char* base = NULL;
|
|
|
|
base = ocuribuild(state->uri,NULL,NULL,OCURIENCODE);
|
|
|
|
status = ocfetchlastmodified(state->curl, base, &lastmodified);
|
|
|
|
free(base);
|
|
|
|
if(status == OC_NOERR) {
|
|
|
|
state->datalastmodified = lastmodified;
|
|
|
|
}
|
2014-11-22 07:20:44 +08:00
|
|
|
return OCTHROW(status);
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-15 06:13:20 +08:00
|
|
|
Set curl properties for link based on rc files etc.
|
2012-08-01 04:34:13 +08:00
|
|
|
*/
|
2014-05-09 04:13:51 +08:00
|
|
|
static OCerror
|
2014-12-25 01:22:47 +08:00
|
|
|
ocset_curlproperties(OCstate* state)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
2014-12-25 01:22:47 +08:00
|
|
|
OCerror stat = OC_NOERR;
|
|
|
|
|
|
|
|
/* extract the relevant triples int state */
|
|
|
|
ocrc_process(state);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2012-12-05 07:32:32 +08:00
|
|
|
if(state->curlflags.useragent == NULL) {
|
2012-12-19 05:08:23 +08:00
|
|
|
size_t len = strlen(DFALTUSERAGENT) + strlen(VERSION) + 1;
|
2012-12-07 05:17:36 +08:00
|
|
|
char* agent = (char*)malloc(len+1);
|
2013-05-12 03:36:31 +08:00
|
|
|
if(occopycat(agent,len,2,DFALTUSERAGENT,VERSION))
|
2013-11-15 06:13:20 +08:00
|
|
|
state->curlflags.useragent = agent;
|
2013-08-06 04:36:33 +08:00
|
|
|
else
|
2013-11-15 06:13:20 +08:00
|
|
|
free(agent);
|
2012-12-04 11:32:41 +08:00
|
|
|
}
|
2013-11-15 06:13:20 +08:00
|
|
|
|
|
|
|
/* Some servers (e.g. thredds and columbia) appear to require a place
|
|
|
|
to put cookies in order for some security functions to work
|
|
|
|
*/
|
2014-12-29 06:54:02 +08:00
|
|
|
if(state->curlflags.cookiejar != NULL
|
|
|
|
&& strlen(state->curlflags.cookiejar) == 0) {
|
|
|
|
free(state->curlflags.cookiejar);
|
|
|
|
state->curlflags.cookiejar = NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
if(state->curlflags.cookiejar == NULL) {
|
2013-11-15 06:13:20 +08:00
|
|
|
/* If no cookie file was defined, define a default */
|
2014-12-25 01:22:47 +08:00
|
|
|
char tmp[OCPATHMAX+1];
|
2013-11-16 06:41:17 +08:00
|
|
|
int stat;
|
2015-05-25 07:31:39 +08:00
|
|
|
pid_t pid = getpid();
|
|
|
|
snprintf(tmp,sizeof(tmp)-1,"%s/%s.%ld/",ocglobalstate.tempdir,OCDIR,(long)pid);
|
2015-01-13 02:05:45 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
stat = mkdir(tmp);
|
|
|
|
#else
|
2013-11-15 06:13:20 +08:00
|
|
|
stat = mkdir(tmp,S_IRUSR | S_IWUSR | S_IXUSR);
|
2015-01-13 02:05:45 +08:00
|
|
|
#endif
|
2013-11-15 06:13:20 +08:00
|
|
|
if(stat != 0 && errno != EEXIST) {
|
2014-12-25 01:22:47 +08:00
|
|
|
fprintf(stderr,"Cannot create cookie directory\n");
|
|
|
|
goto fail;
|
2013-11-15 06:13:20 +08:00
|
|
|
}
|
|
|
|
errno = 0;
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Create the unique cookie file name */
|
|
|
|
stat = ocmktmp(tmp,&state->curlflags.cookiejar);
|
2015-01-13 03:09:34 +08:00
|
|
|
state->curlflags.createdflags |= COOKIECREATED;
|
2014-12-29 06:54:02 +08:00
|
|
|
if(stat != OC_NOERR && errno != EEXIST) {
|
|
|
|
fprintf(stderr,"Cannot create cookie file\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-03-03 11:26:39 +08:00
|
|
|
errno = 0;
|
2014-12-25 01:22:47 +08:00
|
|
|
}
|
2014-12-29 06:54:02 +08:00
|
|
|
OCASSERT(state->curlflags.cookiejar != NULL);
|
2015-01-13 03:09:34 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Make sure the cookie jar exists and can be read and written */
|
|
|
|
{
|
2015-01-13 03:09:34 +08:00
|
|
|
FILE* f = NULL;
|
2014-12-25 01:22:47 +08:00
|
|
|
char* fname = state->curlflags.cookiejar;
|
|
|
|
/* See if the file exists already */
|
|
|
|
f = fopen(fname,"r");
|
|
|
|
if(f == NULL) {
|
|
|
|
/* Ok, create it */
|
|
|
|
f = fopen(fname,"w+");
|
|
|
|
if(f == NULL) {
|
|
|
|
fprintf(stderr,"Cookie file cannot be read and written: %s\n",fname);
|
|
|
|
{stat = OC_EPERM; goto fail;}
|
|
|
|
}
|
|
|
|
} else { /* test if file can be written */
|
|
|
|
fclose(f);
|
|
|
|
f = fopen(fname,"r+");
|
|
|
|
if(f == NULL) {
|
|
|
|
fprintf(stderr,"Cookie file is cannot be written: %s\n",fname);
|
|
|
|
{stat = OC_EPERM; goto fail;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(f != NULL) fclose(f);
|
|
|
|
}
|
2013-11-16 02:38:54 +08:00
|
|
|
|
|
|
|
#if 0
|
2014-12-25 01:22:47 +08:00
|
|
|
/* Create a netrc file if specified and required,
|
|
|
|
where required => >1 NETRC triples exist */
|
|
|
|
if(ocrc_netrc_required(state)) {
|
|
|
|
/* WARNING: it appears that a user+pwd was specified specifically, then
|
|
|
|
the netrc file will be completely disabled. */
|
|
|
|
if(state->creds.userpwd != NULL) {
|
|
|
|
oclog(OCLOGWARN,"The rc file specifies both netrc and user+pwd; this will cause curl to ignore the netrc file");
|
|
|
|
}
|
|
|
|
stat = oc_build_netrc(state);
|
2013-11-16 02:38:54 +08:00
|
|
|
}
|
2014-12-25 01:22:47 +08:00
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
return stat;
|
2012-12-04 11:32:41 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
fail:
|
|
|
|
return OCTHROW(stat);
|
2012-12-04 11:32:41 +08:00
|
|
|
}
|
2013-02-08 06:14:57 +08:00
|
|
|
|
|
|
|
static char* ERROR_TAG = "Error ";
|
|
|
|
|
|
|
|
static int
|
|
|
|
dataError(XXDR* xdrs, OCstate* state)
|
|
|
|
{
|
2013-02-20 07:29:41 +08:00
|
|
|
int depth=0;
|
2013-02-12 07:32:46 +08:00
|
|
|
int errfound = 0;
|
2014-03-09 11:41:30 +08:00
|
|
|
off_t ckp=0,avail=0;
|
|
|
|
int i=0;
|
2013-02-20 07:29:41 +08:00
|
|
|
char* errmsg = NULL;
|
2013-02-08 06:14:57 +08:00
|
|
|
char errortext[16]; /* bigger thant |ERROR_TAG|*/
|
|
|
|
avail = xxdr_getavail(xdrs);
|
|
|
|
if(avail < strlen(ERROR_TAG))
|
2013-02-12 07:32:46 +08:00
|
|
|
goto done; /* assume it is ok */
|
2013-02-08 06:14:57 +08:00
|
|
|
ckp = xxdr_getpos(xdrs);
|
|
|
|
/* Read enough characters to test for 'ERROR ' */
|
|
|
|
errortext[0] = '\0';
|
|
|
|
xxdr_getbytes(xdrs,errortext,(off_t)strlen(ERROR_TAG));
|
|
|
|
if(ocstrncmp(errortext,ERROR_TAG,strlen(ERROR_TAG)) != 0)
|
2013-02-12 07:32:46 +08:00
|
|
|
goto done; /* not an immediate error */
|
2013-02-08 06:14:57 +08:00
|
|
|
/* Try to locate the whole error body */
|
|
|
|
xxdr_setpos(xdrs,ckp);
|
|
|
|
for(depth=0,i=0;i<avail;i++) {
|
2014-03-09 11:41:30 +08:00
|
|
|
xxdr_getbytes(xdrs,errortext,(off_t)1);
|
2013-02-08 06:14:57 +08:00
|
|
|
if(errortext[0] == CLBRACE) depth++;
|
|
|
|
else if(errortext[0] == CRBRACE) {
|
|
|
|
depth--;
|
|
|
|
if(depth == 0) {i++; break;}
|
|
|
|
}
|
2015-01-13 03:09:34 +08:00
|
|
|
}
|
2014-03-09 11:41:30 +08:00
|
|
|
errmsg = (char*)malloc((size_t)i+1);
|
2013-02-12 07:32:46 +08:00
|
|
|
if(errmsg == NULL) {errfound = 1; goto done;}
|
2013-02-08 06:14:57 +08:00
|
|
|
xxdr_setpos(xdrs,ckp);
|
2014-03-09 11:41:30 +08:00
|
|
|
xxdr_getbytes(xdrs,errmsg,(off_t)i);
|
2013-02-08 06:14:57 +08:00
|
|
|
errmsg[i] = '\0';
|
|
|
|
state->error.message = errmsg;
|
|
|
|
state->error.code = strdup("?");
|
|
|
|
state->error.httpcode = 404;
|
|
|
|
xxdr_setpos(xdrs,ckp);
|
2013-02-12 07:32:46 +08:00
|
|
|
errfound = 1;
|
|
|
|
done:
|
|
|
|
xxdr_setpos(xdrs,ckp);
|
|
|
|
return errfound;
|
2013-02-08 06:14:57 +08:00
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
/**************************************************/
|
2015-01-13 03:09:34 +08:00
|
|
|
/* Curl option functions */
|
2014-12-25 01:22:47 +08:00
|
|
|
/*
|
|
|
|
Note that if we set the option in curlflags,
|
|
|
|
then we need to also invoke the ocset_curlopt
|
|
|
|
to update the curl flags in libcurl.
|
|
|
|
*/
|
|
|
|
|
2014-12-01 11:30:23 +08:00
|
|
|
OCerror
|
2014-12-25 01:22:47 +08:00
|
|
|
ocset_useragent(OCstate* state, const char* agent)
|
2014-12-01 11:30:23 +08:00
|
|
|
{
|
|
|
|
OCerror stat = OC_NOERR;
|
2014-12-25 01:22:47 +08:00
|
|
|
if(state->curlflags.useragent != NULL)
|
|
|
|
free(state->curlflags.useragent);
|
|
|
|
state->curlflags.useragent = strdup(agent);
|
|
|
|
if(state->curlflags.useragent == NULL)
|
|
|
|
return OCTHROW(OC_ENOMEM);
|
|
|
|
stat = ocset_curlflag(state,CURLOPT_USERAGENT);
|
|
|
|
return stat;
|
|
|
|
}
|
2014-12-01 11:30:23 +08:00
|
|
|
|
2014-12-25 01:22:47 +08:00
|
|
|
OCerror
|
|
|
|
ocset_netrc(OCstate* state, const char* path)
|
|
|
|
{
|
|
|
|
OCerror stat = OC_NOERR;
|
|
|
|
if(state->curlflags.netrc != NULL)
|
|
|
|
free(state->curlflags.netrc);
|
|
|
|
state->curlflags.netrc = strdup(path);
|
|
|
|
if(state->curlflags.netrc == NULL)
|
|
|
|
return OCTHROW(OC_ENOMEM);
|
|
|
|
stat = ocset_curlflag(state,CURLOPT_NETRC);
|
|
|
|
return stat;
|
2014-12-01 11:30:23 +08:00
|
|
|
}
|