Example #1
0
int
ocfetchurl(CURL* curl, const char* url, OCbytes* buf, long* filetime)
{
	int stat = OC_NOERR;
	CURLcode cstat = CURLE_OK;
	size_t len;

	/* 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, 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);

	cstat = curl_easy_perform(curl);
	if(cstat == CURLE_PARTIAL_FILE) {
	    /* Log it but otherwise ignore */
	    oc_log(LOGWARN, "curl error: %s; ignored",
		   curl_easy_strerror(cstat));
	    cstat = CURLE_OK;
	}
	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 = ocbyteslength(buf);
	ocbytesappend(buf, '\0');
	ocbytessetlength(buf, len); /* dont count null in buffer size*/
#ifdef OCDEBUG
	oc_log(LOGNOTE,"buffersize: %lu bytes",(unsigned long)ocbyteslength(buf));
#endif

	return OCTHROW(stat);

fail:
	oc_log(LOGERR, "curl error: %s", curl_easy_strerror(cstat));
	return OCTHROW(OC_ECURL);
}
Example #2
0
static int
readfiletofile(const char* path, const char* suffix, FILE* stream, off_t* sizep)
{
    int stat = OC_NOERR;
    OCbytes* packet = ocbytesnew();
    size_t len;
    /* check for leading file:/// */
    if(ocstrncmp(path,"file:///",8)==0) path += 7; /* assume absolute path*/
    stat = readfile(path,suffix,packet);
#ifdef OCDEBUG
fprintf(stderr,"readfiletofile: packet.size=%lu\n",
		(unsigned long)ocbyteslength(packet));
#endif
    if(stat != OC_NOERR) goto unwind;
    len = oclistlength(packet);
    if(stat == OC_NOERR) {
	size_t written;
        fseek(stream,0,SEEK_SET);
	written = fwrite(ocbytescontents(packet),1,len,stream);
	if(written != len) {
#ifdef OCDEBUG
fprintf(stderr,"readfiletofile: written!=length: %lu :: %lu\n",
	(unsigned long)written,(unsigned long)len);
#endif
	    stat = OC_EIO;
	}
    }
    if(sizep != NULL) *sizep = len;
unwind:
    ocbytesfree(packet);
    return OCTHROW(stat);
}
Example #3
0
int
ocfindbod(OCbytes* buffer, size_t* bodp, size_t* ddslenp)
{
    unsigned int i;
    char* content;
    size_t len = ocbyteslength(buffer);
    char** marks;
    
    content = ocbytescontents(buffer);

    for(marks = DDSdatamarks;*marks;marks++) {
	char* mark = *marks;
        int tlen = strlen(mark);
        for(i=0;i<len;i++) {
	    if((i+tlen) <= len 
	        && (ocstrncmp(content+i,mark,tlen)==0)) {
	       *ddslenp = i;
	        i += tlen;
	        *bodp = i;
	        return 1;
	    }
	}
    }
    *ddslenp = 0;
    *bodp = 0;
    return 0; /* tag not found; not necessarily an error*/
}
Example #4
0
static void
tabto(int pos, OCbytes* buffer)
{
    int bol,len,pad;
    len = ocbyteslength(buffer);
    /* find preceding newline */
    for(bol=len-1;;bol--) {
	int c = ocbytesget(buffer,bol);
	if(c < 0) break;
	if(c == '\n') {bol++; break;}
    }
    len = (len - bol);
    pad = (pos - len);
    while(pad-- > 0) ocbytescat(buffer," ");
}
Example #5
0
int
readDATADDS(OCstate* state, OCtree* tree, OCflags flags)
{
    int stat = OC_NOERR;
    long lastmod = -1;

#ifdef OCDEBUG
fprintf(stderr,"readDATADDS:\n");
#endif
    if((flags & OCONDISK) == 0) {
        ocurisetconstraints(state->uri,tree->constraint);
        stat = readpacket(state,state->uri,state->packet,OCDATADDS,&lastmod);
        if(stat == OC_NOERR)
            state->datalastmodified = lastmod;
        tree->data.datasize = ocbyteslength(state->packet);
    } else { /*((flags & OCONDISK) != 0) */
        OCURI* url = state->uri;
        int fileprotocol = 0;
        char* readurl = NULL;

        fileprotocol = (strcmp(url->protocol,"file")==0);

        if(fileprotocol && !state->curlflags.proto_file) {
            readurl = ocuribuild(url,NULL,NULL,0);
            stat = readfiletofile(readurl, ".dods", tree->data.file, &tree->data.datasize);
        } else {
            int flags = 0;
            if(!fileprotocol) flags |= OCURICONSTRAINTS;
            flags |= OCURIENCODE;
	    flags |= OCURIUSERPWD;
            ocurisetconstraints(url,tree->constraint);
            readurl = ocuribuild(url,NULL,".dods",flags);
            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);
}
Example #6
0
static int
readpacket(OCstate* state, OCURI* url,OCbytes* packet,OCdxd dxd,long* lastmodified)
{
   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);

   if(fileprotocol && !state->curlflags.proto_file) {
        /* Short circuit file://... urls*/
	/* We do this because the test code always needs to read files*/
	fetchurl = ocuribuild(url,NULL,NULL,0);
	stat = readfile(fetchurl,suffix,packet);
    } else {
	int flags = 0;
	if(!fileprotocol) {
	    flags |= OCURICONSTRAINTS;
	}
	flags |= OCURIENCODE;
        fetchurl = ocuribuild(url,NULL,suffix,flags);
	MEMCHECK(fetchurl,OC_ENOMEM);
	if(ocdebug > 0)
            {fprintf(stderr,"fetch url=%s\n",fetchurl); fflush(stderr);}
        stat = ocfetchurl(curl,fetchurl,packet,lastmodified,&state->creds);
	if(stat)
	    oc_curl_printerror(state);
	if(ocdebug > 0)
            {fprintf(stderr,"fetch complete\n"); fflush(stderr);}
    }
    free(fetchurl);
#ifdef OCDEBUG
  {
fprintf(stderr,"readpacket: packet.size=%lu\n",
		(unsigned long)ocbyteslength(packet));
  }
#endif
    return OCTHROW(stat);
}
Example #7
0
int
readDATADDS(OCstate* state, OCtree* tree)
{
   int stat;
   long lastmod = -1;
#ifndef OC_DISK_STORAGE
   dapurlsetconstraints(&state->url,tree->constraint);
   stat = readpacket(state->curl,&state->url,state->packet,OCDATADDS,&lastmod);
   if(stat == OC_NOERR)
        state->datalastmodified = lastmod;
   tree->data.datasize = ocbyteslength(state->packet);
#else /*OC_DISK_STORAGE*/
   DAPURL* url = &state->url;
   int fileprotocol = 0;

   fileprotocol = (strncmp(url->base,"file:",5)==0);

    if(fileprotocol && !oc_curl_file_supported) {
	stat = readfiletofile(url->base, ".dods", tree->data.file, &tree->data.datasize);
    } else {
        char* fetchurl;
        dapurlsetconstraints(url,tree->constraint);
        fetchurl = dapurlgeturl(url,NULL,".dods",!fileprotocol);
        MEMCHECK(fetchurl,OC_ENOMEM);
	if (ocdebug > 0) 
	    {fprintf(stderr, "fetch url=%s\n", fetchurl);fflush(stderr);}
	stat = ocfetchurl_file(state->curl, fetchurl, 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(fetchurl);
    }
#endif /*OC_DISK_STORAGE*/
    return THROW(stat);
}
Example #8
0
int
daplex(YYSTYPE* lvalp, DAPparsestate* state)
{
    DAPlexstate* lexstate = state->lexstate;
    int token;
    int c;
    unsigned int i;
    char* p;
    char* tmp;
    YYSTYPE lval = NULL;

    token = 0;
    ocbytesclear(lexstate->yytext);
    /* invariant: p always points to current char */
    for(p=lexstate->next; token==0&&(c=*p); p++) {
        if(c == '\n') {
            lexstate->lineno++;
        } else if(c <= ' ' || c == '\177') {
            /* whitespace: ignore */
        } else if(c == '#') {
            /* single line comment */
            while((c=*(++p))) {
                if(c == '\n') break;
            }
        } else if(strchr(lexstate->worddelims,c) != NULL) {
            /* don't put in lexstate->yytext to avoid memory leak */
            token = c;
        } else if(c == '"') {
            int more = 1;
            /* We have a string token; will be reported as WORD_STRING */
            while(more && (c=*(++p))) {
                if(c == '"') {
                    more = 0;
                    continue;
                }
#ifdef DAP2ENCODE
                if(c == '\\') {
                    /* Resolve spec ambiguity about handling of \c:
                    1. !KEEPSLASH: convert \c to c for any character c
                    2. KEEPSLASH: convert \c to \c for any character c;
                       that is, keep the backslash.
                    It is clear that the problem being addressed was \".
                    But it is unclear what to to do about \n: convert to
                                Ascii LF or leave as \n.
                                This code will leave as \n and assume higher levels
                                of code will address the issue.
                    */
#ifdef KEEPSLASH
                    dapaddyytext(lexstate,c);
#endif
                    c=*(++p);
                    if(c == '\0') more = 0;
                }
#else /*Non-standard*/
                switch (c) {
                case '\\':
                    c=*(++p);
                    switch (c) {
                    case 'r':
                        c = '\r';
                        break;
                    case 'n':
                        c = '\n';
                        break;
                    case 'f':
                        c = '\f';
                        break;
                    case 't':
                        c = '\t';
                        break;
                    case 'x': {
                        int d1,d2;
                        c = '?';
                        ++p;
                        d1 = tohex(*p++);
                        if(d1 < 0) {
                            daperror(state,"Illegal \\xDD in TOKEN_STRING");
                        } else {
                            d2 = tohex(*p++);
                            if(d2 < 0) {
                                daperror(state,"Illegal \\xDD in TOKEN_STRING");
                            } else {
                                c=(((unsigned int)d1)<<4) | (unsigned int)d2;
                            }
                        }
                    }
                    break;
                    default:
                        break;
                    }
                    break;
                default:
                    break;
                }
#endif /*!DAP2ENCODE*/
                if(more) dapaddyytext(lexstate,c);
            }
            token=WORD_STRING;
        } else if(strchr(lexstate->wordchars1,c) != NULL) {
            int isdatamark = 0;
            /* we have a WORD_WORD */
            dapaddyytext(lexstate,c);
            while((c=*(++p))) {
#ifdef URLCVT
                if(c == '%' && p[1] != 0 && p[2] != 0
                        && strchr(hexdigits,p[1]) != NULL
                        && strchr(hexdigits,p[2]) != NULL) {
                    int d1,d2;
                    d1 = tohex(p[1]);
                    d2 = tohex(p[2]);
                    if(d1 >= 0 || d2 >= 0) {
                        c=(((unsigned int)d1)<<4) | (unsigned int)d2;
                        p+=2;
                    }
                } else {
                    if(strchr(lexstate->wordcharsn,c) == NULL) {
                        p--;
                        break;
                    }
                }
                dapaddyytext(lexstate,c);
#else
                if(strchr(lexstate->wordcharsn,c) == NULL) {
                    p--;
                    break;
                }
                dapaddyytext(lexstate,c);
#endif
            }
            /* Special check for Data: */
            tmp = ocbytescontents(lexstate->yytext);
            if(strcmp(tmp,"Data")==0 && *p == ':') {
                dapaddyytext(lexstate,*p);
                p++;
                if(p[0] == '\n') {
                    token = SCAN_DATA;
                    isdatamark = 1;
                    p++;
                } else if(p[0] == '\r' && p[1] == '\n') {
                    token = SCAN_DATA;
                    isdatamark = 1;
                    p+=2;
                }
            }
            if(!isdatamark) {
                /* check for keyword */
                token=WORD_WORD; /* assume */
                for(i=0;; i++) {
                    if(keywords[i] == NULL) break;
                    if(strcasecmp(keywords[i],tmp)==0) {
                        token=keytokens[i];
                        break;
                    }
                }
            }
        } else { /* illegal */
        }
    }
    lexstate->next = p;
    strncpy(lexstate->lasttokentext,ocbytescontents(lexstate->yytext),MAX_TOKEN_LENGTH);
    lexstate->lasttoken = token;
    if(ocdebug >= 2)
        dumptoken(lexstate);

    /*Put return value onto Bison stack*/

    if(ocbyteslength(lexstate->yytext) == 0)
        lval = NULL;
    else {
        lval = ocbytesdup(lexstate->yytext);
        oclistpush(lexstate->reclaim,(void*)lval);
    }
    if(lvalp) *lvalp = lval;
    return token;      /* Return the type of the token.  */
}
Example #9
0
int
ocfetchurl(CURL* curl, const char* url, OCbytes* buf, long* filetime,
           struct OCcredentials* creds)
{
    int stat = OC_NOERR;
    CURLcode cstat = CURLE_OK;
    size_t len;
    long httpcode = 0;
    char tbuf[1024];
    /* Set the URL */
    cstat = curl_easy_setopt(curl, CURLOPT_URL, (void*)url);
    if (cstat != CURLE_OK)
        goto fail;

    if(creds != NULL && creds->password != NULL  && creds->username != NULL) {
        /* Set user and password */
#if defined (HAVE_CURLOPT_USERNAME) && defined (HAVE_CURLOPT_PASSWORD)
        cstat = curl_easy_setopt(curl, CURLOPT_USERNAME, creds->username);
        if (cstat != CURLE_OK)
            goto fail;
        cstat = curl_easy_setopt(curl, CURLOPT_PASSWORD, creds->password);
        if (cstat != CURLE_OK)
            goto fail;
#else
        snprintf(tbuf,1023,"%s:%s",creds->username,creds->password);
        cstat = curl_easy_setopt(curl, CURLOPT_USERPWD, tbuf);
        if (cstat != CURLE_OK)
            goto fail;
#endif
    }

    /* 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);

    cstat = curl_easy_perform(curl);

    if(cstat == CURLE_PARTIAL_FILE) {
        /* Log it but otherwise ignore */
        oclog(OCLOGWARN, "curl error: %s; ignored",
              curl_easy_strerror(cstat));
        cstat = CURLE_OK;
    }
    httpcode = ocfetchhttpcode(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 = ocbyteslength(buf);
    ocbytesappend(buf, '\0');
    ocbytessetlength(buf, len); /* dont count null in buffer size*/
#ifdef OCDEBUG
    oclog(OCLOGNOTE,"buffersize: %lu bytes",(off_t)ocbyteslength(buf));
#endif

    return OCTHROW(stat);

fail:
    oclog(OCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
    switch (httpcode) {
    case 401:
        stat = OC_EAUTH;
        break;
    case 404:
        stat = OC_ENOFILE;
        break;
    case 500:
        stat = OC_EDAPSVC;
        break;
    case 200:
        break;
    default:
        stat = OC_ECURL;
        break;
    }
    return OCTHROW(stat);
}
Example #10
0
int
daplex(YYSTYPE* lvalp, DAPparsestate* state)
{
    DAPlexstate* lexstate = state->lexstate;
    int token;
    int c;
    unsigned int i;
    char* p=lexstate->next;
    char* tmp;

    token = 0;
    ocbytesclear(lexstate->yytext);
    /* invariant: p always points to current char */
    for(p=lexstate->next;token==0&&(c=*p);p++) {
	if(c == '\n') {
	    lexstate->lineno++;
	} else if(c <= ' ' || c == '\177') {
	    /* whitespace: ignore */
	} else if(c == '#') {
	    /* single line comment */
	    while((c=*(++p))) {if(c == '\n') break;}
	} else if(strchr(lexstate->worddelims,c) != NULL) {
	    /* don't put in lexstate->yytext to avoid memory leak */
	    token = c;
	} else if(c == '"') {
	    int more = 1;
	    /* We have a string token; will be reported as SCAN_WORD */
	    while(more && (c=*(++p))) {
#ifdef NONSTDCVT
		switch (c) {
		case '"': more=0; break;
		case '\\':
		    c=*(++p);
		    switch (c) {
		    case 'r': c = '\r'; break;
		    case 'n': c = '\n'; break;
		    case 'f': c = '\f'; break;
		    case 't': c = '\t'; break;
		    case 'x': {
			int d1,d2;
			c = '?';
			++p;
		        d1 = tohex(*p++);
			if(d1 < 0) {
			    daperror(state,"Illegal \\xDD in TOKEN_STRING");
			} else {
			    d2 = tohex(*p++);
			    if(d2 < 0) {
			        daperror(state,"Illegal \\xDD in TOKEN_STRING");
			    } else {
				c=(((unsigned int)d1)<<4) | (unsigned int)d2;
			    }
			}
		    } break;
		    default: break;
		    }
		    break;
		default: break;
		}
#else /*!NONSTDCVT*/
	        if(c == '"')
		    more = 0;
		else if(c == '\\') {
		    c=*(++p);
		    if(c == '\0') more = false;
		    if(c != '"') {c = '\\'; --p;}
		}
#endif /*!NONSTDCVT*/
		if(more) dapaddyytext(lexstate,c);
	    }
	    token=SCAN_WORD;
	} else if(strchr(lexstate->wordchars1,c) != NULL) {
	    /* we have a SCAN_WORD */
	    dapaddyytext(lexstate,c);
	    while((c=*(++p))) {
#ifdef URLCVT
		if(c == '%' && p[1] != 0 && p[2] != 0
			    && strchr(hexdigits,p[1]) != NULL
                            && strchr(hexdigits,p[2]) != NULL) {
#ifdef WRONG /* Should not unescape %xx occurrences */
		    int d1,d2;
		    d1 = tohex(p[1]);
		    d2 = tohex(p[2]);
		    if(d1 >= 0 || d2 >= 0) {
			c=(((unsigned int)d1)<<4) | (unsigned int)d2;
			p+=2;
		    }
#endif
		} else {
		    if(strchr(lexstate->wordcharsn,c) == NULL) {p--; break;}
		}
		dapaddyytext(lexstate,c);
#else
		if(strchr(lexstate->wordcharsn,c) == NULL) {p--; break;}
		dapaddyytext(lexstate,c);
#endif
	    }
	    /* Special check for Data: */
	    tmp = ocbytescontents(lexstate->yytext);
	    if(strcmp(tmp,"Data")==0 && *p == ':') {
		dapaddyytext(lexstate,*p); p++;
		token = SCAN_DATA;
	    } else {
	        /* check for keyword */
	        token=SCAN_WORD; /* assume */
	        for(i=0;;i++) {
		    if(keywords[i] == NULL) break;
		    if(strcasecmp(keywords[i],tmp)==0) {
		        token=keytokens[i];
		        break;
		    }
		}
	    }
	} else { /* illegal */
	}
    }
    lexstate->next = p;
    strncpy(lexstate->lasttokentext,ocbytescontents(lexstate->yytext),MAX_TOKEN_LENGTH);
    lexstate->lasttoken = token;
    if(ocdebug >= 2)
	dumptoken(lexstate);

    /*Put return value onto Bison stack*/

    if(ocbyteslength(lexstate->yytext) == 0)
        *lvalp = NULL;
    else {
        *lvalp = ocbytesdup(lexstate->yytext);
	oclistpush(lexstate->reclaim,(ocelem)*lvalp);
    }
    return token;      /* Return the type of the token.  */
}