-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathncuri.c
1335 lines (1204 loc) · 34.6 KB
/
ncuri.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
* Copyright 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header$
*********************************************************************/
#include "config.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "ncuri.h"
#include "ncbytes.h"
#include "nclist.h"
/* Include netcdf.h to allow access to
NC_ error return codes. */
#include "netcdf.h"
#define NCURIDEBUG
/* Extra debug info */
#undef NCXDEBUG
#ifdef NCURIDEBUG
#define THROW(n) {ret=(n); goto done;}
#else
#define THROW(n) {goto done;}
#endif
#define PADDING 8
#define LBRACKET '['
#define RBRACKET ']'
#define EOFCHAR '\0'
#define RBRACKETSTR "]"
#define DRIVELETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#ifndef FIX
#define FIX(s) ((s)==NULL?"NULL":(s))
#endif
#ifndef NILLEN
#define NILLEN(s) ((s)==NULL?0:strlen(s))
#endif
#ifndef nulldup
#define nulldup(s) ((s)==NULL?NULL:strdup(s))
#endif
#define terminate(p) {*(p) = EOFCHAR;}
#define endof(p) ((p)+strlen(p))
#define lshift(buf,buflen) {memmove(buf,buf+1,buflen+1);}
#define rshift(buf,buflen) {memmove(buf+1,buf,buflen+1);}
/* Allowable character sets for encode */
static char* ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
/* Classes according to the URL RFC" */
#define RFCRESERVED " !*'();:@&=+$,/?#[]"
#define RFCUNRESERVED "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
#define RFCOTHER "\"%<>\\^`{|}"
/* I really hate the URL encoding mess */
static const char* pathallow =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$&'()*+,-./:;=?@_~";
static const char* queryallow =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$&'()*+,-./:;=?@_~";
/* user+pwd allow = path allow - "@:" */
static const char* userpwdallow =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!$&'()*+,-.;=_~?#/";
#ifndef HAVE_STRNDUP
#define strndup ncstrndup
/* Not all systems have strndup, so provide one*/
char*
ncstrndup(const char* s, size_t len)
{
char* dup;
if(s == NULL) return NULL;
dup = (char*)malloc(len+1);
if(dup == NULL) return NULL;
memcpy((void*)dup,s,len);
dup[len] = '\0';
return dup;
}
#endif
/* Forward */
static int collectprefixparams(char* text, char** nextp);
static void freestringlist(NClist* list);
static int ncfind(NClist* params, const char* key);
static char* nclocate(char* p, const char* charlist);
static int parselist(const char* ptext, NClist* list);
static int unparselist(const NClist* vec, const char* prefix, int encode, NCbytes*);
static int ensurefraglist(NCURI* uri);
static int ensurequerylist(NCURI* uri);
static void removedups(NClist* list);
/**************************************************/
/*
A note about parameter support:
In the original url format for opendap (dap2), client parameters were
assumed to be one or more instances of bracketed pairs: e.g
"[...][...]...".
These were assumed to be placed at the front of the url. In this newer
version, the parameters may be encoded after a trailing # character each
separated by ampersand (&). For back compatibility, the bracketed
parameter form is supported. However, if ncuribuild is used, all
parameters will be converted to the
#...&...& format.
In any case, each parameter in turn is assumed to be a of the form
<name>=<value> or <name>; e.g. #x=y&z&a=b&w.
If the same parameter is specified more than once, then the first
occurrence is used; this is so that is possible to forcibly override
user specified parameters by prefixing.
IMPORTANT: the client parameter string is assumed to have blanks compressed out.
*/
/**************************************************/
/* Do a simple uri parse: return NC_NOERR if success, NC_EXXX if failed */
int
ncuriparse(const char* uri0, NCURI** durip)
{
int ret = NC_NOERR;
NCURI tmp;
char* p;
char* q;
int isfile;
int hashost;
char* uri = NULL;
NCURI* duri = NULL;
char* prefix = NULL;
char* next = NULL;
NClist* params = nclistnew();
NClist* querylist = nclistnew();
size_t len0;
char pathchar;
if(uri0 == NULL)
{THROW(NC_EURL);}
len0 = strlen(uri0);
if(len0 == 0)
{THROW(NC_EURL);}
/* Create a local NCURI instance to hold
pointers into the parsed string
*/
memset(&tmp,0,sizeof(tmp));
/* make mutable copy. Add some extra space
because we will need to null terminate the host section
without losing the first character of the path section.
*/
uri = (char*)malloc(len0+1+1); /* +2 for nul term and for host section terminator */
if(uri == NULL)
{THROW(NC_ENOMEM);}
/* Safe because we allocated enough space right above (and */
/* `strdup` isn't usable because we need "one more char"). */
strcpy(uri,uri0);
/* Walk the uri and do the following:
1. remove leading and trailing whitespace
2. convert all '\\' -> '\' (Temp hack to remove escape characters
inserted by Windows or MinGW)
*/
p = uri;
while(*p == ' ') p++;
for(q=uri;*p;p++) {if((*p == '\\' && p[1] == '\\')) {continue;} else {*q++ = *p;}}
while((q - 1) >= uri && *(q - 1) == ' ') q--;
*q = '\0';
p = uri;
/* break up the url into coarse pieces */
if(*p == LBRACKET) {
prefix = p;
ret = collectprefixparams(p,&next); /* collect the prefix; convert to & form */
if(ret != NC_NOERR)
{THROW(NC_EURL);}
p = next;
} else {
prefix = NULL;
}
tmp.uri = p; /* will be the core */
/* Skip past the core of the url */
next = nclocate(p,"?#");
if(next != NULL) {
int c = *next;
terminate(next);
next++;
if(c == '?') {
tmp.query = next;
next = nclocate(next,"#");
if(next == NULL)
tmp.fragment = NULL;
else {
terminate(next);
next++;
tmp.fragment = next;
}
} else { /*c == '#'*/
tmp.fragment = next;
}
}
/* Parse the prefix parameters */
if(prefix != NULL) {
if(parselist(prefix,params) != NC_NOERR)
{THROW(NC_EURL);}
}
/* Parse the fragment parameters into the params list */
if(tmp.fragment != NULL) {
if(parselist(tmp.fragment,params) != NC_NOERR)
{THROW(NC_EURL);}
}
/* Remove duplicates */
removedups(params);
tmp.fraglist = params;
params = NULL;
/* Parse the query */
if(tmp.query != NULL) {
if(parselist(tmp.query,querylist) != NC_NOERR)
{THROW(NC_EURL);}
tmp.querylist = querylist;
querylist = NULL;
}
/* Now parse the core of the url */
p = tmp.uri;
/* Mark the protocol */
tmp.protocol = p;
p = strchr(p,':');
if(!p)
{THROW(NC_EURL);}
terminate(p); /*overwrite colon*/
p++; /* skip the colon */
if(strlen(tmp.protocol)==0)
{THROW(NC_EURL);}
/*
The legal formats for file: urls are a problem since
many variants are often accepted.
By RFC, the proper general format is: file://host/path,
where the 'host' can be omitted and defaults to 'localhost'.
and the path includes the leading '/'.
So, assuming no host, the format is: "file:///path".
Some implementations, however, ignore the host, and allow
the format: file:/path.
We also simplify things by assuming the host part is always empty.
which means we can have file:///path, but not file://..../path.
Note also in all cases, the leading '/' is considered part of the path,
which is then assumed to be an absolute path. But also note that
the windows drive letter has to be taken into account. Our rule is that
if the path looks like D:...,
where D is a single alphabetic letter (a-z or A-Z),
then it is a windows path and can be use in place of a /path.
Note also that it is desirable to support relative paths even
though the RFC technically does not allow this. This will occur
if the form is file://path where path does not start with '/'.
The rules implemented here (for file:) are then as follows
1. file:D:... : assume D: is a windows drive letter and treat D:... as the path
2. file:/X, where X does not start with a slash: treat /X as the path.
3. file://D:... : assume D: is a windows drive letter and treat as the path
4. file:///X, where X does not start with a slash: treat /X as the path.
5. file://X, where X does not start with a slash: treat X as the
relative path.
All other cases are disallowed.
*/
isfile = (strcmp(tmp.protocol,"file")==0);
if(isfile) {
size_t l = strlen(p); /* to test if we have enough characters */
hashost = 0; /* always */
if(l >= 2 && p[1] == ':' && strchr(DRIVELETTERS,p[0]) != NULL) { /* case 1 */
; /* p points to the start of the path */
} else if(l >= 2 && p[0] == '/' && p[1] != '/') { /* case 2 */
; /* p points to the start of the path */
} else if(l >= 4 && p[0] == '/' && p[1] == '/'
&& p[3] == ':' && strchr(DRIVELETTERS,p[2]) != NULL) { /* case 3 */
p = p+2; /* points to the start of the windows path */
} else if(l >= 4 && p[0] == '/' && p[1] == '/' && p[2] == '/' && p[3] != '/') { /* case 4 */
p += 2; /* points to the start of the path */
} else if(l >= 4 && p[0] == '/' && p[1] == '/' && p[2] != '/') { /* case 5 */
p += 2; /* points to the start of the path */
} else /* everything else is illegal */
{THROW(NC_EURL);}
} else {
if(p[0] != '/' || p[1] != '/') /* must be proto:// */
{THROW(NC_EURL);}
p += 2;
hashost = 1; /* Assume we have a hostname */
}
if(!hashost) {
tmp.path = p;
pathchar = EOFCHAR;
} else { /* assume there should be a host section */
/* We already extracted the query and/or fragment sections above,
so locate the end of the host section and therefore the start
of the path.
*/
tmp.host = p;
p = nclocate(p,"/");
if(p == NULL) { /* no path */
tmp.path = NULL; /* default */
pathchar = EOFCHAR;
} else {
tmp.path = p; /* save ptr to rest of the path */
pathchar = *p; /* save leading char of the path */
terminate(p); /* overwrite the leading char of the path; restored below */
}
}
/* Nullify tmp.host for consistency */
if(tmp.host != NULL && strlen(tmp.host)==0) {tmp.host = NULL;}
if(tmp.host != NULL) {/* Parse the host section */
char* pp;
/* Check for leading user:pwd@ */
char* newhost = strchr(tmp.host,'@');
if(newhost != NULL) {
if(newhost == tmp.host)
{THROW(NC_EURL);} /* we have proto://@ */
terminate(newhost); /* overwrite '@' */
newhost++; /* should point past usr+pwd */
tmp.user = tmp.host;
/* Break user+pwd into two pieces */
pp = strchr(tmp.user,':');
if(pp == NULL)
{THROW(NC_EURL);} /* we have user only */
terminate(pp); /* overwrite ':' */
pp++;
if(strlen(tmp.user)==0)
{THROW(NC_EURL);} /* we have empty user */
if(strlen(pp)==0)
{THROW(NC_EURL);} /* we have empty password */
tmp.password = pp;
tmp.host = newhost;
}
/* Breakup host into host + port */
pp = tmp.host;
pp = strchr(pp,':');
if(pp != NULL) { /* there is a port */
terminate(pp); /* overwrite ':' */
pp++; /* skip colon */
if(strlen(tmp.host) == 0)
{THROW(NC_EURL);} /* empty host */
if(strlen(pp)==0)
{THROW(NC_EURL);} /* empty port */
tmp.port = pp;
/* The port must look something like a number */
for(pp=tmp.port;*pp;pp++) {
if(strchr("0123456789-",*pp) == NULL)
{THROW(NC_EURL);} /* probably not a real port, fail */
}
} /* else no port */
}
/* Fill in duri from tmp */
duri = (NCURI*)calloc(1,sizeof(NCURI));
if(duri == NULL)
{THROW(NC_ENOMEM);}
/* save original uri */
duri->uri = strdup(uri0);
duri->protocol = nulldup(tmp.protocol);
/* before saving, we need to decode the user+pwd */
duri->user = NULL;
duri->password = NULL;
if(tmp.user != NULL)
duri->user = ncuridecode(tmp.user);
if(tmp.password != NULL)
duri->password = ncuridecode(tmp.password);
duri->host = nulldup(tmp.host);
duri->port = nulldup(tmp.port);
if(tmp.path != NULL) {
/* We need to add back the previously overwritten path lead char (if necessary);
this must be done after all host section related pieces have been captured */
if(pathchar != EOFCHAR)
*tmp.path = pathchar;
duri->path = nulldup(tmp.path);
}
duri->query = NULL; /* let ensurequery fix this */
duri->fragment = NULL; /* let ensurefrag fix this */
duri->fraglist = tmp.fraglist; tmp.fraglist = NULL;
duri->querylist = tmp.querylist; tmp.querylist = NULL;
/* make sure query and fragment strings are defined */
ensurequerylist(duri);
ensurefraglist(duri);
if(durip)
*durip = duri;
else
free(duri);
#ifdef NCXDEBUG
{
fprintf(stderr,"duri:");
fprintf(stderr," protocol=|%s|",FIX(duri->protocol));
fprintf(stderr," user=|%s|",FIX(duri->user));
fprintf(stderr," password=|%s|",FIX(duri->password));
fprintf(stderr," host=|%s|",FIX(duri->host));
fprintf(stderr," port=|%s|",FIX(duri->port));
fprintf(stderr," path=|%s|",FIX(duri->path));
fprintf(stderr," query=|%s|",FIX(duri->query));
fprintf(stderr," fragment=|%s|",FIX(duri->fragment));
fprintf(stderr,"\n");
}
#endif
done:
if(uri != NULL)
free(uri);
freestringlist(params);
freestringlist(querylist);
if(tmp.fraglist)
nclistfreeall(tmp.fraglist);
if(tmp.querylist)
nclistfreeall(tmp.querylist);
return ret;
}
static void
freestringlist(NClist* list)
{
if(list != NULL) {
size_t i;
for(i=0;i<nclistlength(list);i++) {
void* p = nclistget(list,i);
nullfree(p);
}
nclistfree(list);
}
}
#if 0
static void
freestringvec(char** list)
{
if(list != NULL) {
char** p;
for(p=list;*p;p++) {nullfree(*p);}
nullfree(list);
}
}
#endif
void
ncurifree(NCURI* duri)
{
if(duri == NULL) return;
nullfree(duri->uri);
nullfree(duri->protocol);
nullfree(duri->user);
nullfree(duri->password);
nullfree(duri->host);
nullfree(duri->port);
nullfree(duri->path);
nullfree(duri->query);
nullfree(duri->fragment);
nclistfreeall(duri->querylist);
nclistfreeall(duri->fraglist);
free(duri);
}
/* Replace the protocol */
int
ncurisetprotocol(NCURI* duri,const char* protocol)
{
nullfree(duri->protocol);
duri->protocol = strdup(protocol);
return (NC_NOERR);
}
/* Replace the host */
int
ncurisethost(NCURI* duri,const char* host)
{
nullfree(duri->host);
duri->host = strdup(host);
return (NC_NOERR);
}
/* Replace the path */
int
ncurisetpath(NCURI* duri,const char* newpath)
{
nullfree(duri->path);
duri->path = strdup(newpath);
return (NC_NOERR);
}
/* Replace the query */
int
ncurisetquery(NCURI* duri,const char* query)
{
int ret = NC_NOERR;
nclistfreeall((NClist*)duri->querylist);
nullfree(duri->query);
duri->query = NULL;
duri->querylist = NULL;
if(query != NULL && strlen(query) > 0) {
duri->query = strdup(query);
ensurequerylist(duri);
}
return ret;
}
/* Replace the fragments*/
int
ncurisetfragments(NCURI* duri,const char* fragments)
{
int ret = NC_NOERR;
nclistfreeall((NClist*)duri->fraglist);
nullfree(duri->fragment);
duri->fragment = NULL;
duri->fraglist = NULL;
if(fragments != NULL && strlen(fragments) > 0) {
duri->fragment = strdup(fragments);
ensurefraglist(duri);
}
return ret;
}
/* Replace the path */
int
ncurirebuild(NCURI* duri)
{
char* surl = ncuribuild(duri,NULL,NULL,NCURIALL);
nullfree(duri->uri);
duri->uri = surl;
return (NC_NOERR);
}
/* Replace a specific fragment key*/
int
ncurisetfragmentkey(NCURI* duri, const char* key, const char* value)
{
int ret = NC_NOERR;
int pos = -1;
ensurefraglist(duri);
pos = ncfind(duri->fraglist, key);
if(pos < 0) { /* does not exist */
if(duri->fraglist == NULL) duri->fraglist = nclistnew();
nclistpush(duri->fraglist,strdup(key));
nclistpush(duri->fraglist,strdup(value));
} else {
nullfree(nclistget(duri->fraglist,(size_t)pos+1));
nclistset(duri->fraglist,(size_t)pos+1,strdup(value));
}
/* Rebuild the fragment */
nullfree(duri->fragment); duri->fragment = NULL;
if((ret = ensurefraglist(duri))) goto done;
done:
return ret;
}
/* Replace or add a specific fragment key*/
int
ncuriappendfragmentkey(NCURI* duri,const char* key, const char* value)
{
int ret = NC_NOERR;
int pos = -1;
ensurefraglist(duri);
pos = ncfind(duri->fraglist, key);
if(pos < 0) { /* does not exist */
nclistpush((NClist*)duri->fraglist,strdup(key));
nclistpush((NClist*)duri->fraglist,nulldup(value));
} else {
nullfree(nclistget(duri->fraglist,(size_t)pos+1));
nclistset(duri->fraglist,(size_t)pos+1,nulldup(value));
}
/* Rebuild the fragment */
nullfree(duri->fraglist); duri->fraglist = NULL;
if((ret=ensurefraglist(duri))) goto done;
done:
return ret;
}
/* Replace a specific query key*/
int
ncurisetquerykey(NCURI* duri,const char* key, const char* value)
{
int ret = NC_NOERR;
int pos = -1;
ensurequerylist(duri);
pos = ncfind(duri->querylist, key);
if(pos < 0) { /* does not exist */
if(duri->querylist == NULL) duri->querylist = nclistnew();
nclistpush(duri->querylist,key);
nclistpush(duri->querylist,value);
} else {
nullfree(nclistget(duri->querylist,(size_t)pos+1));
nclistset(duri->querylist,(size_t)pos+1,strdup(value));
}
/* Rebuild the query */
nullfree(duri->query); duri->query = NULL;
if((ret = ensurequerylist(duri))) goto done;
done:
return ret;
}
/* Replace or add a specific query key*/
int
ncuriappendquerykey(NCURI* duri,const char* key, const char* value)
{
int ret = NC_NOERR;
int pos = -1;
ensurequerylist(duri);
pos = ncfind(duri->querylist, key);
if(pos < 0) { /* does not exist */
nclistpush((NClist*)duri->querylist,strdup(key));
nclistpush((NClist*)duri->querylist,nulldup(value));
} else {
nullfree(nclistget(duri->querylist,(size_t)pos+1));
nclistset(duri->querylist,(size_t)pos+1,nulldup(value));
}
/* Rebuild the query */
nullfree(duri->querylist); duri->querylist = NULL;
if((ret=ensurequerylist(duri))) goto done;
done:
return ret;
}
#if 0
/* Replace the constraints */
int
ncurisetconstraints(NCURI* duri,const char* constraints)
{
char* proj = NULL;
char* select = NULL;
const char* p;
if(duri->constraint != NULL) free(duri->constraint);
if(duri->projection != NULL) free(duri->projection);
if(duri->selection != NULL) free(duri->selection);
duri->constraint = NULL;
duri->projection = NULL;
duri->selection = NULL;
if(constraints == NULL || strlen(constraints)==0) return (NC_ECONSTRAINTS);
duri->constraint = nulldup(constraints);
if(*duri->constraint == '?')
nclshift1(duri->constraint);
p = duri->constraint;
proj = (char*) p;
select = strchr(proj,'&');
if(select != NULL) {
size_t plen = (size_t)(select - proj);
if(plen == 0) {
proj = NULL;
} else {
proj = (char*)malloc(plen+1);
memcpy((void*)proj,p,plen);
proj[plen] = EOFCHAR;
}
select = nulldup(select);
} else {
proj = nulldup(proj);
select = NULL;
}
duri->projection = proj;
duri->selection = select;
return NC_NOERR;
}
#endif
/* Construct a complete NC URI.
Optionally with the constraints.
Optionally with the user parameters.
Caller frees returned string.
Optionally encode the pieces.
*/
char*
ncuribuild(NCURI* duri, const char* prefix, const char* suffix, int flags)
{
char* newuri = NULL;
NCbytes* buf = ncbytesnew();
const int encodepath = (flags&NCURIENCODEPATH ? 1 : 0);
const int encodequery = (flags&NCURIENCODEQUERY ? 1 : 0);
if(prefix != NULL)
ncbytescat(buf,prefix);
ncbytescat(buf,duri->protocol);
ncbytescat(buf,"://"); /* this will produce file:///... */
if((flags & NCURIPWD) && duri->user != NULL && duri->password != NULL) {
/* The user and password must be encoded */
char* encoded = ncuriencodeonly(duri->user,userpwdallow);
ncbytescat(buf,encoded);
nullfree(encoded);
ncbytescat(buf,":");
encoded = ncuriencodeonly(duri->password,userpwdallow);
ncbytescat(buf,encoded);
nullfree(encoded);
ncbytescat(buf,"@");
}
if(duri->host != NULL) ncbytescat(buf,duri->host);
if(duri->port != NULL) {
ncbytescat(buf,":");
ncbytescat(buf,duri->port);
}
if((flags & NCURIPATH)) {
if(duri->path == NULL)
ncbytescat(buf,"/");
else if(encodepath) {
char* encoded = ncuriencodeonly(duri->path,pathallow);
ncbytescat(buf,encoded);
nullfree(encoded);
} else
ncbytescat(buf,duri->path);
}
/* The suffix is intended to some kind of path extension (e.g. .dds)
so insert here
*/
if(suffix != NULL)
ncbytescat(buf,suffix);
/* The query and the querylist are assumed to be unencoded */
if(flags & NCURIQUERY) {
ensurequerylist(duri);
if(duri->query != NULL) {
ncbytescat(buf,"?");
if(encodequery) {
char* encoded = ncuriencodeonly(duri->query,queryallow);
ncbytescat(buf,encoded);
nullfree(encoded);
} else
ncbytescat(buf,duri->query);
}
}
if(flags & NCURIFRAG) {
ensurefraglist(duri);
if(duri->fragment != NULL) {
ncbytescat(buf,"#");
ncbytescat(buf,duri->fragment);
}
}
ncbytesnull(buf);
newuri = ncbytesextract(buf);
ncbytesfree(buf);
return newuri;
}
const char*
ncurifragmentlookup(NCURI* uri, const char* key)
{
int i;
char* value = NULL;
if(uri == NULL || key == NULL) return NULL;
if(ensurefraglist(uri)) return NULL;
i = ncfind(uri->fraglist,key);
if(i < 0) return NULL;
value = nclistget(uri->fraglist,(size_t)i+1);
return value;
}
const char*
ncuriquerylookup(NCURI* uri, const char* key)
{
int i;
char* value = NULL;
if(uri == NULL || key == NULL) return NULL;
if(ensurequerylist(uri)) return NULL;
i = ncfind(uri->querylist,key);
if(i < 0) return NULL;
value = nclistget(uri->querylist,(size_t)i+1);
return value;
}
#if 0
/* Obtain the complete list of fragment pairs in envv format */
const char**
ncurifragmentparams(NCURI* uri)
{
ensurefraglist(uri);
return (const char**)nclistcontents(uri->fraglist;
}
/* Obtain the complete list of query pairs in envv format */
const char**
ncuriqueryparams(NCURI* uri)
{
ensurequerylist(uri);
return (const char**)uri->querylist;
}
int
ncuriremoveparam(NCURI* uri, const char* key)
{
char** p;
char** q = NULL;
if(uri->fraglist == NULL) return NC_NOERR;
for(q=uri->fraglist,p=uri->fraglist;*p;) {
if(strcmp(key,*p)==0) {
p += 2; /* skip this entry */
} else {
*q++ = *p++; /* move key */
*q++ = *p++; /* move value */
}
}
return NC_NOERR;
}
#endif
/* Internal version of lookup; returns the paired index of the key;
case insensitive
*/
static int
ncfind(NClist* params, const char* key)
{
size_t i;
if(key == NULL) return -1;
if(params == NULL) return -1;
for(i=0;i<nclistlength(params);i+=2) {
char* p=nclistget(params,(size_t)i);
if(strcasecmp(key,p)==0) return i;
}
return -1;
}
#if 0
static void
ncparamfree(char** params)
{
char** p;
if(params == NULL) return;
for(p=params;*p;p+=2) {
free(*p);
if(p[1] != NULL) free(p[1]);
}
free(params);
}
#endif
/* Return the ptr to the first occurrence of
any char in the list. Return NULL if no
occurrences
*/
static char*
nclocate(char* p, const char* charlist)
{
for(;*p;p++) {
if(*p == '\\') p++;
else if(strchr(charlist,*p) != NULL)
return p;
}
return NULL;
}
#if 0
/* Shift every char starting at p 1 place to the left */
static void
nclshift1(char* p)
{
if(p != NULL && *p != EOFCHAR) {
char* q = p++;
while((*q++=*p++));
}
}
/* Shift every char starting at p 1 place to the right */
static void
ncrshift1(char* p)
{
char cur;
cur = 0;
do {
char next = *p;
*p++ = cur;
cur = next;
} while(cur != 0);
*p = 0; /* make sure we are still null terminated */
}
#endif
/* Provide % encoders and decoders */
static const char* hexchars = "0123456789abcdefABCDEF";
static void
toHex(char b, char hex[2])
{
hex[0] = hexchars[(b >> 4) & 0xf];
hex[1] = hexchars[(b) & 0xf];
}
static int
fromHex(int c)
{
if(c >= '0' && c <= '9') return (int) (c - '0');
if(c >= 'a' && c <= 'f') return (int) (10 + (c - 'a'));
if(c >= 'A' && c <= 'F') return (int) (10 + (c - 'A'));
return 0;
}
/*
Support encode of user and password fields
*/
char*
ncuriencodeuserpwd(const char* s)
{
return ncuriencodeonly(s,userpwdallow);
}
/* Return a string representing encoding of input; caller must free;
watch out: will encode whole string, so watch what you give it.
Allowable argument specifies characters that do not need escaping.
*/
char*
ncuriencodeonly(const char* s, const char* allowable)
{
size_t slen;
char* encoded;
const char* inptr;
char* outptr;
if(s == NULL) return NULL;
slen = strlen(s);
encoded = (char*)malloc((3*slen) + 1); /* max possible size */
for(inptr=s,outptr=encoded;*inptr;) {
char c = *inptr++;
{
/* search allowable */
char* p = strchr(allowable,c);
if(p != NULL) {
*outptr++ = (char)c;
} else {
char hex[2];
toHex(c,hex);
*outptr++ = '%';
*outptr++ = hex[0];
*outptr++ = hex[1];
}
}
}
*outptr = EOFCHAR;
return encoded;
}
/* Return a string representing decoding of input; caller must free;*/
char*
ncuridecode(const char* s)
{
size_t slen;
char* decoded;
char* outptr;
const char* inptr;
unsigned int c;
if (s == NULL) return NULL;
slen = strlen(s);
decoded = (char*)malloc(slen+1); /* Should be max we need */
outptr = decoded;
inptr = s;
while((c = (unsigned int)*inptr++)) {
if(c == '%') {
/* try to pull two hex more characters */
if(inptr[0] != EOFCHAR && inptr[1] != EOFCHAR
&& strchr(hexchars,inptr[0]) != NULL
&& strchr(hexchars,inptr[1]) != NULL) {
/* test conversion */
int xc = (fromHex(inptr[0]) << 4) | (fromHex(inptr[1]));
inptr += 2; /* decode it */
c = (unsigned int)xc;
}
}
*outptr++ = (char)c;
}
*outptr = EOFCHAR;
return decoded;