-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathdurlmodel.c
270 lines (244 loc) · 6.85 KB
/
durlmodel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* @file
*
* URL Model Management.
*
* These functions support inferring the format X implementation for urls.
* It looks at various fragment (#...) pairs.
*
* Copyright 2018 University Corporation for Atmospheric
* Research/Unidata. See COPYRIGHT file for more info.
*/
#include "config.h"
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "ncdispatch.h"
#include "ncpathmgr.h"
#include "ncurlmodel.h"
/*
Define a table of legal mode string values.
Note that only cases that can currently
take URLs are included.
*/
static struct LEGALMODES {
const char* tag;
const int format; /* NC_FORMAT_XXX value */
const int implementation; /* NC_FORMATX_XXX value */
} legalmodes[] = {
/* Format X Implementation */
{"netcdf-3",NC_FORMAT_CLASSIC,NC_FORMATX_NC3},
{"classic",NC_FORMAT_CLASSIC,NC_FORMATX_NC3},
{"netcdf-4",NC_FORMAT_NETCDF4,NC_FORMATX_NC4},
{"enhanced",NC_FORMAT_NETCDF4,NC_FORMATX_NC4},
{"dap2",NC_FORMAT_CLASSIC,NC_FORMATX_DAP2},
{"dap4",NC_FORMAT_NETCDF4,NC_FORMATX_DAP4},
{"nczarr",NC_FORMAT_NETCDF4,NC_FORMATX_DAP4},
{NULL,0,0},
};
/* Define the known URL protocols and their interpretation */
static struct NCPROTOCOLLIST {
char* protocol;
char* substitute;
int implementation;
} ncprotolist[] = {
{"http",NULL,0},
{"https",NULL,0},
{"file",NULL,0},
{"dods","http",NC_FORMATX_DAP2},
{"dap4","http",NC_FORMATX_DAP4},
{NULL,NULL,0} /* Terminate search */
};
/* Parse a mode string at the commas nul terminate each tag */
static int
parseurlmode(const char* modestr0, char** listp)
{
int stat = NC_NOERR;
char* modestr = NULL;
char* p = NULL;
char* endp = NULL;
/* Make modifiable copy */
if((modestr=strdup(modestr0)) == NULL)
{stat=NC_ENOMEM; goto done;}
/* Split modestr at the commas or EOL */
p = modestr;
for(;;) {
endp = strchr(p,',');
if(endp == NULL) break;
/* Null terminate each comma-separated string */
*endp++ = '\0';
p = endp;
}
if(listp) *listp = modestr;
modestr = NULL;
done:
if(stat) {nullfree(modestr);}
return stat;
}
/* Parse url fragment for format etc. */
static int
url_getmodel(const char* modestr, NCmode* model)
{
int stat = NC_NOERR;
char* args = NULL;
char* p = NULL;
model->format = 0;
model->implementation = 0;
if((stat=parseurlmode(modestr,&args))) goto done;
p = args;
for(;*p;) {
struct LEGALMODES* legal = legalmodes;
while(legal->tag) {
if(strcmp(legal->tag,p)==0) {
if(model->format != 0 && legal->format != 0)
{stat = NC_EINVAL; goto done;}
if(model->implementation != 0 && legal->implementation != 0)
{stat = NC_EINVAL; goto done;}
if(legal->format != 0) model->format = legal->format;
if(legal->implementation != 0)
model->implementation = legal->implementation;
}
}
}
done:
nullfree(args);
return stat;
}
/**************************************************/
/**
* Provide a hidden interface to allow utilities
* to check if a given path name is really an ncdap3 url.
* If no, put null in basenamep, else put basename of the url
* minus any extension into basenamep; caller frees.
* Return 1 if it looks like a url, 0 otherwise.
*/
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;
}
/*
Fill in the model fields to degree possible.
Assumes that the path is known to be a url
*/
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_NCZARR:
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;
}
/* 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;
}