/* return 1 if path looks like a url; 0 otherwise */ int NC_testurl(const char* path) { int isurl = 0; NCURI* tmpurl = NULL; char* p; if(path == NULL) return 0; /* find leading non-blank */ for(p=(char*)path;*p;p++) {if(*p != ' ') break;} /* Do some initial checking to see if this looks like a file path */ if(*p == '/') return 0; /* probably an absolute file path */ /* Ok, try to parse as a url */ if(ncuriparse(path,&tmpurl)) { /* Do some extra testing to make sure this really is a url */ /* Look for a knownprotocol */ struct NCPROTOCOLLIST* protolist; for(protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(tmpurl->protocol,protolist->protocol) == 0) { isurl=1; break; } } ncurifree(tmpurl); return isurl; } return 0; }
int NC_parseproxy(NCauth* auth, const char* surl) { int ret = NC_NOERR; NCURI* uri = NULL; if(surl == NULL || strlen(surl) == 0) return (NC_NOERR); /* nothing there*/ if(ncuriparse(surl,&uri) != NCU_OK) return (NC_EURL); auth->proxy.user = uri->user; auth->proxy.pwd = uri->password; auth->proxy.host = strdup(uri->host); if(uri->port != NULL) auth->proxy.port = atoi(uri->port); else auth->proxy.port = 80; return (ret); }
int NC_urlmodel(const char* path) { int model = 0; NCURI* tmpurl = NULL; struct NCPROTOCOLLIST* protolist; if(!ncuriparse(path,&tmpurl)) goto done; /* Look at any prefixed parameters */ if(ncurilookup(tmpurl,"netcdf4",NULL) || ncurilookup(tmpurl,"netcdf-4",NULL)) { model = (NC_DISPATCH_NC4|NC_DISPATCH_NCD); } else if(ncurilookup(tmpurl,"netcdf3",NULL) || ncurilookup(tmpurl,"netcdf-3",NULL)) { model = (NC_DISPATCH_NC3|NC_DISPATCH_NCD); } else if(ncurilookup(tmpurl,"cdmremote",NULL) || ncurilookup(tmpurl,"cdmr",NULL)) { model = (NC_DISPATCH_NCR|NC_DISPATCH_NC4); } if(model == 0) { /* Now look at the protocol */ for(protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(tmpurl->protocol,protolist->protocol) == 0) { model |= protolist->modelflags; if(protolist->substitute) { if(tmpurl->protocol) free(tmpurl->protocol); tmpurl->protocol = strdup(protolist->substitute); } break; } } } /* Force NC_DISPATCH_NC3 if necessary */ if((model & NC_DISPATCH_NC4) == 0) model |= (NC_DISPATCH_NC3 | NC_DISPATCH_NCD); done: ncurifree(tmpurl); return model; }
int nc__testurl(const char* path, char** basenamep) { NCURI* uri; int ok = 0; if(ncuriparse(path,&uri) == NCU_OK) { char* slash = (uri->path == NULL ? NULL : strrchr(uri->path, '/')); char* dot; if(slash == NULL) slash = (char*)path; else slash++; slash = nulldup(slash); if(slash == NULL) dot = NULL; else dot = strrchr(slash, '.'); if(dot != NULL && dot != slash) *dot = '\0'; if(basenamep) *basenamep=slash; else if(slash) free(slash); ncurifree(uri); ok = 1; } return ok; }
/* return 1 if path looks like a url; 0 otherwise */ int NC_testurl(const char* path) { int isurl = 0; NCURI* tmpurl = NULL; if(path == NULL) return 0; /* Ok, try to parse as a url */ if(ncuriparse(path,&tmpurl)==NCU_OK) { /* Do some extra testing to make sure this really is a url */ /* Look for a known/accepted protocol */ struct NCPROTOCOLLIST* protolist; for(protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(tmpurl->protocol,protolist->protocol) == 0) { isurl=1; break; } } ncurifree(tmpurl); return isurl; } return 0; }
static int processuri(const char* path, NCURI** urip, char** newpathp, NClist* modeargs) { int stat = NC_NOERR; int found = 0; const char** fragp = NULL; struct NCPROTOCOLLIST* protolist; NCURI* uri = NULL; size_t pathlen = strlen(path); if(path == NULL || pathlen == 0) {stat = NC_EURL; goto done;} /* Defaults */ if(newpathp) *newpathp = NULL; if(urip) *urip = NULL; if(ncuriparse(path,&uri) != NCU_OK) goto done; /* not url */ /* Look up the protocol */ for(found=0,protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(uri->protocol,protolist->protocol) == 0) { found = 1; break; } } if(!found) {stat = NC_EINVAL; goto done;} /* unrecognized URL form */ /* process the corresponding mode arg */ if(protolist->mode != NULL) nclistpush(modeargs,strdup(protolist->mode)); /* Substitute the protocol in any case */ if(protolist->substitute) ncurisetprotocol(uri,protolist->substitute); /* Iterate over the url fragment parameters */ for(fragp=ncurifragmentparams(uri);fragp && *fragp;fragp+=2) { const char* name = fragp[0]; const char* value = fragp[1]; if(strcmp(name,"protocol")==0) { nclistpush(modeargs,strdup(value)); } else if(strcmp(name,"mode")==0) { if((stat = parseurlmode(value,modeargs))) goto done; } else if(issingleton(name) && (value == NULL || strlen(value)==0)) { nclistpush(modeargs,strdup(name)); } /*else ignore*/ } /* At this point modeargs should contain all mode args from the URL */ /* Rebuild the path (including fragment)*/ if(newpathp) *newpathp = ncuribuild(uri,NULL,NULL,NCURIALL); if(urip) { *urip = uri; uri = NULL; } done: if(uri != NULL) ncurifree(uri); return check(stat); }
int NC_urlmodel(const char* path, int cmode, char** newurl, NCmode* model) { int stat = NC_NOERR; int found = 0; struct NCPROTOCOLLIST* protolist; NCURI* url = NULL; const char** fragp = NULL; if(path == NULL) return 0; /* Parse the url */ if(ncuriparse(path,&url) != NCU_OK) return NC_EINVAL; /* Not parseable as url */ /* Look up the protocol */ for(found=0,protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(url->protocol,protolist->protocol) == 0) { found = 1; break; } } if(found) { model->implementation = protolist->implementation; /* Substitute the protocol in any case */ if(protolist->substitute) ncurisetprotocol(url,protolist->substitute); } else {stat = NC_EINVAL; goto done;} /* Again, does not look like a url */ /* Iterate over the url fragment parameters */ for(fragp=ncurifragmentparams(url);fragp && *fragp;fragp+=2) { const char* name = fragp[0]; const char* value = fragp[1]; if(strcmp(name,"protocol")==0) name = value; if(strcasecmp(name,"dap2") == 0) { model->format = NC_FORMAT_NC3; model->implementation = NC_FORMATX_DAP2; /* No need to set iosp field */ } else if(strcasecmp(name,"dap4") == 0) { model->format = NC_FORMAT_NETCDF4; model->implementation = NC_FORMATX_DAP4; /* No need to set iosp field */ } else if(strcmp(name,"mode")==0) { if((stat = url_getmodel(value,model))) goto done; } } if(model->implementation == 0) {/* Last resort: use the cmode */ /* If mode specifies netcdf-4, then this is assumed to be dap4 */ if(cmode & NC_NETCDF4) { model->implementation = NC_FORMATX_DAP4; } else {/* Default is DAP2 */ model->implementation = NC_FORMATX_DAP2; } } if(model->implementation == 0) goto done; /* could not interpret */ switch (model->implementation) { case NC_FORMATX_NC3: model->format = NC_FORMAT_NC3; break; case NC_FORMATX_NC4: model->format = NC_FORMAT_NETCDF4; break; case NC_FORMATX_DAP2: model->format = NC_FORMAT_NC3; break; case NC_FORMATX_DAP4: model->format = NC_FORMAT_NETCDF4; break; case NC_FORMATX_ZARR: model->format = NC_FORMAT_NETCDF4; break; default: stat = NC_EINVAL; goto done; } done: if(newurl) *newurl = ncuribuild(url,NULL,NULL,NCURIALL); if(url) ncurifree(url); return stat; }
int NC_urlmodel(const char* path, int mode, char** newurl) { int found, model = 0; struct NCPROTOCOLLIST* protolist; NCURI* url = NULL; char* p; if(path == NULL) return 0; /* find leading non-blank */ for(p=(char*)path;*p;p++) {if(*p != ' ') break;} /* Do some initial checking to see if this looks like a file path */ if(*p == '/') return 0; /* probably an absolute file path */ /* Parse the url */ if(ncuriparse(path,&url) != NCU_OK) goto fail; /* Not parseable as url */ /* Look up the protocol */ for(found=0,protolist=ncprotolist;protolist->protocol;protolist++) { if(strcmp(url->protocol,protolist->protocol) == 0) { found = 1; break; } } if(found) { model = protolist->model; /* Substitute the protocol in any case */ if(protolist->substitute) ncurisetprotocol(url,protolist->substitute); } else goto fail; /* Again, does not look like a url */ if(model != NC_FORMATX_DAP2 && model != NC_FORMATX_DAP4) { /* Look for and of the following params: "dap2", "protocol=dap2", "dap4", "protocol=dap4" */ const char* proto = NULL; const char* match = NULL; if((proto=ncurilookup(url,"protocol")) == NULL) proto = NULL; if(proto == NULL) proto = ""; if((match=ncurilookup(url,"dap2")) != NULL || strcmp(proto,"dap2") == 0) model = NC_FORMATX_DAP2; else if((match=ncurilookup(url,"dap4")) != NULL || strcmp(proto,"dap4") == 0) model = NC_FORMATX_DAP4; else model = 0; /* Still don't know */ } if(model == 0) {/* Last resort: use the mode */ /* If mode specifies netcdf-4, then this is assumed to be dap4 */ if(mode & NC_NETCDF4) model = NC_FORMATX_DAP4; else model = NC_FORMATX_DAP2; /* Default */ } if(newurl) *newurl = ncuribuild(url,NULL,NULL,NCURIALL); if(url) ncurifree(url); return model; fail: if(url) ncurifree(url); return 0; }