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"
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
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
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2017-09-03 08:09:36 +08:00
|
|
|
#ifdef _MSC_VER
|
2012-11-22 07:57:42 +08:00
|
|
|
#include <io.h>
|
2017-07-19 02:44:39 +08:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY _O_BINARY
|
|
|
|
#endif
|
2012-11-22 07:57:42 +08:00
|
|
|
#endif
|
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"
|
|
|
|
#include "ochttp.h"
|
|
|
|
#include "ocread.h"
|
|
|
|
#include "occurlfunctions.h"
|
2017-07-14 00:40:07 +08:00
|
|
|
#include "ncwinpath.h"
|
2012-08-01 04:34:13 +08:00
|
|
|
|
|
|
|
/*Forward*/
|
2017-03-09 08:01:10 +08:00
|
|
|
static int readpacket(OCstate* state, NCURI*, NCbytes*, OCdxd, long*);
|
|
|
|
static int readfile(const char* path, const char* suffix, NCbytes* packet);
|
2012-08-01 04:34:13 +08:00
|
|
|
static int readfiletofile(const char* path, const char* suffix, FILE* stream, off_t*);
|
|
|
|
|
|
|
|
int
|
|
|
|
readDDS(OCstate* state, OCtree* tree)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
long lastmodified = -1;
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
ncurisetquery(state->uri,tree->constraint);
|
2012-08-01 04:34:13 +08:00
|
|
|
|
2012-08-22 00:46:10 +08:00
|
|
|
#ifdef OCDEBUG
|
|
|
|
fprintf(stderr,"readDDS:\n");
|
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
stat = readpacket(state,state->uri,state->packet,OCDDS,
|
|
|
|
&lastmodified);
|
|
|
|
if(stat == OC_NOERR) state->ddslastmodified = lastmodified;
|
|
|
|
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readDAS(OCstate* state, OCtree* tree)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
ncurisetquery(state->uri,tree->constraint);
|
2012-08-22 00:46:10 +08:00
|
|
|
#ifdef OCDEBUG
|
|
|
|
fprintf(stderr,"readDAS:\n");
|
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
stat = readpacket(state,state->uri,state->packet,OCDAS,NULL);
|
|
|
|
|
|
|
|
return stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
int
|
2017-03-09 08:01:10 +08:00
|
|
|
readversion(OCstate* state, NCURI* url, NCbytes* packet)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
return readpacket(state,url,packet,OCVER,NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char*
|
|
|
|
ocdxdextension(OCdxd dxd)
|
|
|
|
{
|
|
|
|
switch(dxd) {
|
|
|
|
case OCDDS: return ".dds";
|
|
|
|
case OCDAS: return ".das";
|
|
|
|
case OCDATADDS: return ".dods";
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-03-09 08:01:10 +08:00
|
|
|
readpacket(OCstate* state, NCURI* url,NCbytes* packet,OCdxd dxd,long* lastmodified)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
int fileprotocol = 0;
|
|
|
|
const char* suffix = ocdxdextension(dxd);
|
|
|
|
char* fetchurl = NULL;
|
|
|
|
CURL* curl = state->curl;
|
|
|
|
|
|
|
|
fileprotocol = (strcmp(url->protocol,"file")==0);
|
|
|
|
|
2017-09-03 08:09:36 +08:00
|
|
|
if(fileprotocol) {
|
|
|
|
/* Short circuit file://... urls and read directly */
|
2017-03-09 08:01:10 +08:00
|
|
|
fetchurl = ncuribuild(url,NULL,NULL,NCURIBASE);
|
2012-08-01 04:34:13 +08:00
|
|
|
stat = readfile(fetchurl,suffix,packet);
|
|
|
|
} else {
|
2017-03-09 08:01:10 +08:00
|
|
|
int flags = NCURIBASE;
|
|
|
|
if(!fileprotocol) flags |= NCURIQUERY;
|
|
|
|
flags |= NCURIENCODE;
|
|
|
|
fetchurl = ncuribuild(url,NULL,suffix,flags);
|
2012-08-01 04:34:13 +08:00
|
|
|
MEMCHECK(fetchurl,OC_ENOMEM);
|
|
|
|
if(ocdebug > 0)
|
|
|
|
{fprintf(stderr,"fetch url=%s\n",fetchurl); fflush(stderr);}
|
2017-09-01 04:19:56 +08:00
|
|
|
stat = ocfetchurl(curl,fetchurl,packet,lastmodified);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(stat)
|
|
|
|
oc_curl_printerror(state);
|
|
|
|
if(ocdebug > 0)
|
|
|
|
{fprintf(stderr,"fetch complete\n"); fflush(stderr);}
|
|
|
|
}
|
|
|
|
free(fetchurl);
|
2012-08-22 00:46:10 +08:00
|
|
|
#ifdef OCDEBUG
|
2014-01-22 04:04:52 +08:00
|
|
|
{
|
2012-08-22 00:46:10 +08:00
|
|
|
fprintf(stderr,"readpacket: packet.size=%lu\n",
|
2017-03-09 08:01:10 +08:00
|
|
|
(unsigned long)ncbyteslength(packet));
|
2014-01-22 04:04:52 +08:00
|
|
|
}
|
2012-08-22 00:46:10 +08:00
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
readDATADDS(OCstate* state, OCtree* tree, OCflags flags)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
long lastmod = -1;
|
|
|
|
|
2012-08-22 00:46:10 +08:00
|
|
|
#ifdef OCDEBUG
|
|
|
|
fprintf(stderr,"readDATADDS:\n");
|
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
if((flags & OCONDISK) == 0) {
|
2017-03-09 08:01:10 +08:00
|
|
|
ncurisetquery(state->uri,tree->constraint);
|
2012-08-01 04:34:13 +08:00
|
|
|
stat = readpacket(state,state->uri,state->packet,OCDATADDS,&lastmod);
|
|
|
|
if(stat == OC_NOERR)
|
|
|
|
state->datalastmodified = lastmod;
|
2017-03-09 08:01:10 +08:00
|
|
|
tree->data.datasize = ncbyteslength(state->packet);
|
2012-08-01 04:34:13 +08:00
|
|
|
} else { /*((flags & OCONDISK) != 0) */
|
2017-03-09 08:01:10 +08:00
|
|
|
NCURI* url = state->uri;
|
2012-08-01 04:34:13 +08:00
|
|
|
int fileprotocol = 0;
|
|
|
|
char* readurl = NULL;
|
|
|
|
|
|
|
|
fileprotocol = (strcmp(url->protocol,"file")==0);
|
|
|
|
|
2017-09-03 08:09:36 +08:00
|
|
|
if(fileprotocol) {
|
2017-03-09 08:01:10 +08:00
|
|
|
readurl = ncuribuild(url,NULL,NULL,NCURIBASE);
|
2012-08-01 04:34:13 +08:00
|
|
|
stat = readfiletofile(readurl, ".dods", tree->data.file, &tree->data.datasize);
|
|
|
|
} else {
|
2017-03-09 08:01:10 +08:00
|
|
|
int flags = NCURIBASE;
|
|
|
|
if(!fileprotocol) flags |= NCURIQUERY;
|
|
|
|
flags |= NCURIENCODE;
|
|
|
|
ncurisetquery(url,tree->constraint);
|
|
|
|
readurl = ncuribuild(url,NULL,".dods",flags);
|
2012-08-01 04:34:13 +08:00
|
|
|
MEMCHECK(readurl,OC_ENOMEM);
|
|
|
|
if (ocdebug > 0)
|
|
|
|
{fprintf(stderr, "fetch url=%s\n", readurl);fflush(stderr);}
|
|
|
|
stat = ocfetchurl_file(state->curl, readurl, tree->data.file,
|
|
|
|
&tree->data.datasize, &lastmod);
|
|
|
|
if(stat == OC_NOERR)
|
|
|
|
state->datalastmodified = lastmod;
|
|
|
|
if (ocdebug > 0)
|
|
|
|
{fprintf(stderr,"fetch complete\n"); fflush(stderr);}
|
|
|
|
}
|
|
|
|
free(readurl);
|
|
|
|
}
|
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
readfiletofile(const char* path, const char* suffix, FILE* stream, off_t* sizep)
|
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
2017-03-09 08:01:10 +08:00
|
|
|
NCbytes* packet = ncbytesnew();
|
2012-08-01 04:34:13 +08:00
|
|
|
size_t len;
|
|
|
|
/* check for leading file:/// */
|
|
|
|
if(ocstrncmp(path,"file:///",8)==0) path += 7; /* assume absolute path*/
|
|
|
|
stat = readfile(path,suffix,packet);
|
2012-08-22 00:46:10 +08:00
|
|
|
#ifdef OCDEBUG
|
|
|
|
fprintf(stderr,"readfiletofile: packet.size=%lu\n",
|
2017-03-09 08:01:10 +08:00
|
|
|
(unsigned long)ncbyteslength(packet));
|
2012-08-22 00:46:10 +08:00
|
|
|
#endif
|
2012-08-01 04:34:13 +08:00
|
|
|
if(stat != OC_NOERR) goto unwind;
|
2017-03-09 08:01:10 +08:00
|
|
|
len = nclistlength(packet);
|
2012-08-01 04:34:13 +08:00
|
|
|
if(stat == OC_NOERR) {
|
|
|
|
size_t written;
|
|
|
|
fseek(stream,0,SEEK_SET);
|
2017-03-09 08:01:10 +08:00
|
|
|
written = fwrite(ncbytescontents(packet),1,len,stream);
|
2012-08-22 00:46:10 +08:00
|
|
|
if(written != len) {
|
|
|
|
#ifdef OCDEBUG
|
|
|
|
fprintf(stderr,"readfiletofile: written!=length: %lu :: %lu\n",
|
|
|
|
(unsigned long)written,(unsigned long)len);
|
|
|
|
#endif
|
|
|
|
stat = OC_EIO;
|
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
}
|
|
|
|
if(sizep != NULL) *sizep = len;
|
|
|
|
unwind:
|
2017-03-09 08:01:10 +08:00
|
|
|
ncbytesfree(packet);
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-03-09 08:01:10 +08:00
|
|
|
readfile(const char* path, const char* suffix, NCbytes* packet)
|
2012-08-01 04:34:13 +08:00
|
|
|
{
|
|
|
|
int stat = OC_NOERR;
|
|
|
|
char filename[1024];
|
|
|
|
/* check for leading file:/// */
|
|
|
|
if(ocstrncmp(path,"file://",7)==0) path += 7; /* assume absolute path*/
|
2012-12-19 05:08:23 +08:00
|
|
|
if(!occopycat(filename,sizeof(filename),2,path,(suffix != NULL ? suffix : "")))
|
|
|
|
return OCTHROW(OC_EOVERRUN);
|
2017-09-03 08:09:36 +08:00
|
|
|
stat = NC_readfile(filename,packet);
|
2012-08-01 04:34:13 +08:00
|
|
|
return OCTHROW(stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
|