forked from SWI-Prolog/packages-clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uri.c
1635 lines (1317 loc) · 36.7 KB
/
uri.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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2009-2012, VU University Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef __WINDOWS__
#define inline __inline
#endif
#include <SWI-Prolog.h>
#include <string.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
#include <assert.h>
static size_t removed_dot_segments(size_t len, const pl_wchar_t *in,
pl_wchar_t *out);
static pl_wchar_t *remove_last_segment(const pl_wchar_t *base,
const pl_wchar_t *o);
static char *_utf8_put_char(char *out, int chr);
#define ISUTF8_MB(c) ((unsigned)(c) >= 0xc0 && (unsigned)(c) <= 0xfd)
#define utf8_put_char(out, chr) \
((chr) < 0x80 ? out[0]=(char)(chr), out+1 \
: _utf8_put_char(out, (chr)))
/*******************************
* ERRORS *
*******************************/
static atom_t ATOM_query_value;
static atom_t ATOM_fragment;
static atom_t ATOM_path;
static functor_t FUNCTOR_equal2; /* =/2 */
static functor_t FUNCTOR_pair2; /* -/2 */
static functor_t FUNCTOR_uri_components5;
static functor_t FUNCTOR_uri_authority4;
static functor_t FUNCTOR_error2;
static functor_t FUNCTOR_syntax_error1;
static functor_t FUNCTOR_type_error2;
static functor_t FUNCTOR_domain_error2;
static int
syntax_error(const char *culprit)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_syntax_error1,
PL_CHARS, culprit,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
type_error(const char *expected, term_t found)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_type_error2,
PL_CHARS, expected,
PL_TERM, found,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
domain_error(const char *expected, term_t found)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_domain_error2,
PL_CHARS, expected,
PL_TERM, found,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
/*******************************
* ESCAPING *
*******************************/
#define ESC_PATH (CH_PCHAR|CH_EX_PATH)
#define ESC_QUERY (CH_UNRESERVED|CH_PSUBDELIM|CH_EX_PCHAR|CH_EX_QF)
#define ESC_QVALUE (CH_UNRESERVED|CH_QSUBDELIM|CH_EX_PCHAR|CH_EX_QF)
#define ESC_QNAME (CH_PCHAR)
#define ESC_FRAGMENT (CH_PCHAR|CH_EX_QF)
#define ESC_AUTH (CH_PCHAR)
#define ESC_PASSWD (CH_PCHAR)
#define ESC_USER (CH_PCHAR)
#define ESC_SCHEME (CH_SCHEME)
#define ESC_PORT (CH_DIGIT)
#define ESC_HOST (CH_UNRESERVED|CH_SUBDELIM)
#define CH_ALPHA 0x0001
#define CH_DIGIT 0x0002
#define CH_EX_UNRES 0x0004
#define CH_GENDELIM 0x0008
#define CH_SUBDELIM 0x0010
#define CH_URL 0x0020
#define CH_EX_PCHAR 0x0040
#define CH_EX_QF 0x0080 /* Extra query and fragment chars */
#define CH_EX_SCHEME 0x0100
#define CH_QSUBDELIM 0x0200
#define CH_PSUBDELIM 0x0400
#define CH_EX_PATH 0x0800
#define CH_SCHEME (CH_ALPHA|CH_DIGIT|CH_EX_SCHEME)
#define CH_UNRESERVED (CH_ALPHA|CH_DIGIT|CH_EX_UNRES)
#define CH_PCHAR (CH_UNRESERVED|CH_SUBDELIM|CH_EX_PCHAR)
static int charflags[128] = {0};
static int flags_done = 0;
static void
set_flags(const char *from, int flag)
{ for(; *from; from++)
charflags[from[0]&0xff] |= flag;
}
static void
fill_flags()
{ if ( !flags_done )
{ int c;
for(c='a'; c<='z'; c++)
charflags[c] |= CH_ALPHA;
for(c='A'; c<='Z'; c++)
charflags[c] |= CH_ALPHA;
for(c='0'; c<='9'; c++)
charflags[c] |= CH_DIGIT;
set_flags("-._~", CH_EX_UNRES);
set_flags(":/?#[]@", CH_GENDELIM);
set_flags("!$&'()+*,;=", CH_SUBDELIM);
set_flags("!$&'()*,;=", CH_PSUBDELIM); /* = CH_SUBDELIM - "+" */
set_flags("!$'()*,", CH_QSUBDELIM); /* = CH_SUBDELIM - "&=+" */
set_flags(":@", CH_EX_PCHAR);
set_flags("/", CH_EX_PATH);
set_flags("/?", CH_EX_QF);
set_flags("+-.", CH_EX_SCHEME);
set_flags("/:?#&=", CH_URL);
flags_done = TRUE;
}
}
#define no_escape(c, f) \
(((c) < 128) && (charflags[(int)c] & (f)))
#define iri_no_escape(c, f) \
(((c) >= 128) || (c) == '%' || (charflags[(int)c] & (f)))
/* hex(const pl_wchar_t *in, int digits, int *value)
Get <digits> characters from in and interpret them as a hexadecimal
integer. Returns pointer to the end on success or NULL if error.
*/
static const pl_wchar_t *
hex(const pl_wchar_t *in, int digits, int *value)
{ int v = 0;
while(digits-- > 0)
{ int c = *in++;
if ( c >= '0' && c <= '9' )
v = (v<<4) + c - '0';
else if ( c >= 'A' && c <= 'F' )
v = (v<<4) + c + 10 - 'A';
else if ( c >= 'a' && c <= 'f' )
v = (v<<4) + c + 10 - 'a';
else
return NULL;
}
*value = v;
return in;
}
static const pl_wchar_t *
get_encoded_utf8_cont_1(const pl_wchar_t *in, int *val)
{ int c;
if ( in[0] == '%' && hex(in+1, 2, &c) )
{ if ( (c&0xc0) == 0x80 )
{ *val = (c&0x3f);
return in+3;
}
}
return NULL;
}
static const pl_wchar_t *
get_encoded_utf8_cont(const pl_wchar_t *in, int cnt, int *val)
{ int shift = cnt*6;
*val <<= shift;
shift -= 6;
while(cnt-->0)
{ int v0;
if ( (in = get_encoded_utf8_cont_1(in, &v0)) )
{ *val |= (v0<<shift);
shift -= 6;
} else
return NULL;
}
return in;
}
static const pl_wchar_t *
get_encoded_utf8(const pl_wchar_t *in, int *chr)
{ int c1;
if ( in[0] == '%' && hex(in+1, 2, &c1) )
{ in += 3;
if ( ISUTF8_MB(c1) )
{ if ( (c1&0xe0) == 0xc0 ) /* 2-byte */
{ *chr = (c1&0x1f);
return get_encoded_utf8_cont(in, 1, chr);
} else if ( (c1&0xf0) == 0xe0 ) /* 3-byte */
{ *chr = (c1&0xf);
return get_encoded_utf8_cont(in, 2, chr);
} else if ( (c1&0xf8) == 0xf0 ) /* 4-byte */
{ *chr = (c1&0x7);
return get_encoded_utf8_cont(in, 3, chr);
} else if ( (c1&0xfc) == 0xf8 ) /* 5-byte */
{ *chr = (c1&0x3);
return get_encoded_utf8_cont(in, 4, chr);
} else if ( (c1&0xfe) == 0xfc ) /* 6-byte */
{ *chr = (c1&0x1);
return get_encoded_utf8_cont(in, 5, chr);
} else
return NULL;
} else
{ *chr = c1;
return in; /* Encoded ASCII character */
}
}
return NULL;
}
/*******************************
* RANGES *
*******************************/
typedef struct range
{ const pl_wchar_t *start;
const pl_wchar_t *end;
} range;
/*******************************
* CHARACTER BUFFER *
*******************************/
typedef struct charbuf
{ pl_wchar_t *base;
pl_wchar_t *here;
pl_wchar_t *end;
pl_wchar_t tmp[256];
} charbuf;
static void
init_charbuf(charbuf *cb)
{ cb->base = cb->here = cb->tmp;
cb->end = &cb->tmp[sizeof(cb->tmp)/sizeof(pl_wchar_t)];
}
static int
init_charbuf_at_size(charbuf *cb, size_t size)
{ size++;
if ( size < sizeof(cb->tmp)/sizeof(pl_wchar_t) )
cb->base = cb->here = cb->tmp;
else
cb->base = cb->here = PL_malloc(size*sizeof(pl_wchar_t));
return TRUE;
}
static int
add_charbuf(charbuf *cb, int c)
{ if ( cb->here < cb->end )
{ *cb->here++ = c;
} else
{ size_t len = (cb->end-cb->base);
if ( cb->base == cb->tmp )
{ pl_wchar_t *n = PL_malloc(len*2*sizeof(pl_wchar_t));
memcpy(n, cb->base, sizeof(cb->tmp));
cb->base = n;
} else
{ cb->base = PL_realloc(cb->base, len*2*sizeof(pl_wchar_t));
}
cb->here = &cb->base[len];
cb->end = &cb->base[len*2];
*cb->here++ = c;
}
return TRUE;
}
static inline int
hexdigit(int val)
{ if ( val < 10 )
return '0'+val;
return 'A'-10+val;
}
static int
add_encoded_charbuf(charbuf *cb, int c, int flags)
{ if ( no_escape(c, flags) )
{ add_charbuf(cb, c);
} else
{ char tmp[6];
const char *end = utf8_put_char(tmp, c);
const char *s;
for(s=tmp; s<end; s++)
{ int b = s[0]&0xff;
add_charbuf(cb, '%');
add_charbuf(cb, hexdigit(b>>4));
add_charbuf(cb, hexdigit(b&0xf));
}
}
return TRUE;
}
static int
iri_add_encoded_charbuf(charbuf *cb, int c, int flags)
{ if ( iri_no_escape(c, flags) )
{ add_charbuf(cb, c);
} else
{ assert(c < 128);
add_charbuf(cb, '%');
add_charbuf(cb, hexdigit(c>>4));
add_charbuf(cb, hexdigit(c&0xf));
}
return TRUE;
}
static int
add_nchars_charbuf(charbuf *cb, size_t len, const pl_wchar_t *s)
{ if ( cb->here+len <= cb->end )
{ wcsncpy(cb->here, s, len);
cb->here += len;
} else
{ size_t n;
for(n=0; n<len; n++)
add_charbuf(cb, s[n]);
}
return TRUE;
}
static int
range_has_escape(const range *r, int flags)
{ const pl_wchar_t *s = r->start;
for(; s<r->end; s++)
{ if ( s[0] == '%' || (s[0] == '+' && flags == ESC_QVALUE) )
return TRUE;
}
return FALSE;
}
static int
range_is_unreserved(const range *r, int iri, int flags)
{ const pl_wchar_t *s = r->start;
if ( iri )
{ for(; s<r->end; s++)
{ if ( !iri_no_escape(s[0], flags) )
return FALSE;
}
} else
{ for(; s<r->end; s++)
{ if ( !no_escape(s[0], flags) )
return FALSE;
}
}
return TRUE;
}
static int
add_verb_range_charbuf(charbuf *cb, const range *r)
{ return add_nchars_charbuf(cb, r->end-r->start, r->start);
}
static int
add_decoded_range_charbuf(charbuf *cb, const range *r, int flags)
{ const pl_wchar_t *s = r->start;
while(s<r->end)
{ int c;
if ( *s == '%' )
{ const pl_wchar_t *e;
if ( (e=get_encoded_utf8(s, &c)) )
{ s = e;
} else if (hex(s+1, 2, &c) )
{ s += 3;
} else
{ c = *s++;
}
} else if ( *s == '+' && flags == ESC_QVALUE )
{ s++;
c = ' ';
} else
{ c = *s++;
}
add_charbuf(cb, c);
}
return TRUE;
}
static int
add_normalized_range_charbuf(charbuf *cb, const range *r, int iri, int flags)
{ const pl_wchar_t *s = r->start;
while(s<r->end)
{ int c;
if ( *s == '%' )
{ const pl_wchar_t *e;
if ( (e=get_encoded_utf8(s, &c)) )
{ s = e;
} else if (hex(s+1, 2, &c) )
{ s += 3;
} else
{ c = *s++;
}
} else if ( *s == '+' && flags == ESC_QVALUE )
{ s++;
c = ' ';
} else
{ c = *s++;
}
if ( iri )
{ iri_add_encoded_charbuf(cb, c, flags);
} else
{ add_encoded_charbuf(cb, c, flags);
}
}
return TRUE;
}
/* add_range_charbuf(charbuf *cb, const range *r, int iri, int flags)
Add a range of characters while normalizing %-encoding. This
implies not to use encoding if it is not needed and upcase
%xx to %XX otherwise.
If iri == TRUE, values >= 128 are not escaped. Otherwise they
use %-encoded UTF-8
*/
static int
add_range_charbuf(charbuf *cb, const range *r, int unesc, int iri, int flags)
{ if ( unesc && range_has_escape(r, flags) )
{ return add_normalized_range_charbuf(cb, r, iri, flags);
} else if ( range_is_unreserved(r, iri, flags) )
{ add_nchars_charbuf(cb, r->end-r->start, r->start);
} else
{ const pl_wchar_t *s = r->start;
if ( iri )
{ while(s<r->end)
iri_add_encoded_charbuf(cb, *s++, flags);
} else
{ while(s<r->end)
add_encoded_charbuf(cb, *s++, flags);
}
}
return TRUE;
}
/* add_lwr_range_charbuf(charbuf *cb, const range *r,
int unesc, int iri, int flags)
Add a range of characters while normalizing %-encoding and
mapping all characters to lowercase.
FIXME: encoding and decoding compatible to add_range_charbuf();
*/
static int
add_lwr_range_charbuf(charbuf *cb, const range *r, int unesc, int iri, int flags)
{ const pl_wchar_t *s = r->start;
while(s<r->end)
{ int c;
if ( unesc && *s == '%' )
{ const pl_wchar_t *e;
if ( (e=get_encoded_utf8(s, &c)) )
{ s = e;
} else if (hex(s+1, 2, &c) )
{ s += 3;
} else
{ c = *s++;
}
} else
{ c = *s++;
}
if ( iri )
iri_add_encoded_charbuf(cb, towlower((wint_t)c), flags);
else
add_encoded_charbuf(cb, towlower((wint_t)c), flags);
}
return TRUE;
}
static void
free_charbuf(charbuf *cb)
{ if ( cb->base != cb->tmp )
PL_free(cb->base);
}
#define TXT_EX_TEXT (CVT_ATOM|CVT_STRING|CVT_EXCEPTION)
static int
get_text_arg(term_t term, int pos, size_t *len, pl_wchar_t **s, int flags)
{ term_t tmp = PL_new_term_ref();
_PL_get_arg(pos, term, tmp);
if ( PL_is_variable(tmp) )
return FALSE;
if ( !PL_get_wchars(tmp, len, s, flags) )
return -1;
return TRUE;
}
/** uri_components(+URI, -Components)
Based on RFC-3986 regular expression:
==
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
12 3 4 5 6 7 8 9
==
*/
typedef struct uri_component_ranges
{ range scheme;
range authority;
range path;
range query;
range fragment;
} uri_component_ranges;
static const pl_wchar_t *
skip_not(const pl_wchar_t *in, const pl_wchar_t *end, const pl_wchar_t *chars)
{ if ( !chars[1] )
{ for(; in < end; in++)
{ if ( chars[0] == in[0] )
return in;
}
} else
{ for(; in < end; in++)
{ if ( wcschr(chars, in[0]) )
return in;
}
}
return in;
}
static int
unify_range(term_t t, const range *r)
{ if ( r->start )
return PL_unify_wchars(t, PL_ATOM, r->end - r->start, r->start);
return TRUE;
}
static int
parse_uri(uri_component_ranges *ranges, size_t len, const pl_wchar_t *s)
{ const pl_wchar_t *end = &s[len];
const pl_wchar_t *here = s;
const pl_wchar_t *e;
memset(ranges, 0, sizeof(*ranges));
e = skip_not(here, end, L":/?#");
if ( e > s && e[0] == ':' ) /* 1&2 */
{ ranges->scheme.start = s;
ranges->scheme.end = e;
here = e+1;
}
if ( here[0] == '/' && here[1] == '/' ) /* 3 */
{ here += 2; /* 4 */
e = skip_not(here, end, L"/?#");
ranges->authority.start = here;
ranges->authority.end = e;
here = e; /* 5 */
}
e = skip_not(here, end, L"?#");
ranges->path.start = here;
ranges->path.end = e;
here = e; /* 6 */
if ( here[0] == '?' )
{ here++; /* 7 */
e = skip_not(here, end, L"#");
ranges->query.start = here;
ranges->query.end = e;
here = e; /* 8 */
}
if ( here[0] == '#' )
{ here++; /* 9 */
ranges->fragment.start = here;
ranges->fragment.end = end;
}
return TRUE;
}
static foreign_t
uri_components(term_t URI, term_t components)
{ pl_wchar_t *s;
size_t len;
if ( PL_get_wchars(URI, &len, &s, CVT_ATOM|CVT_STRING|CVT_LIST) )
{ uri_component_ranges ranges;
term_t rt = PL_new_term_refs(6);
term_t av = rt+1;
parse_uri(&ranges, len, s);
unify_range(av+0, &ranges.scheme);
unify_range(av+1, &ranges.authority);
unify_range(av+2, &ranges.path);
unify_range(av+3, &ranges.query);
unify_range(av+4, &ranges.fragment);
return (PL_cons_functor_v(rt, FUNCTOR_uri_components5, av) &&
PL_unify(components, rt));
} else if ( PL_is_functor(components, FUNCTOR_uri_components5) )
{ charbuf b;
int rc;
init_charbuf(&b);
/* schema */
if ( (rc=get_text_arg(components, 1, &len, &s, TXT_EX_TEXT)) == TRUE )
{ add_nchars_charbuf(&b, len, s);
add_charbuf(&b, ':');
} else if ( rc == -1 )
{ free_charbuf(&b);
return FALSE;
}
/* authority */
if ( (rc=get_text_arg(components, 2, &len, &s, TXT_EX_TEXT)) == TRUE )
{ add_charbuf(&b, '/');
add_charbuf(&b, '/');
add_nchars_charbuf(&b, len, s);
} else if ( rc == -1 )
{ free_charbuf(&b);
return FALSE;
}
/* path */
if ( (rc=get_text_arg(components, 3, &len, &s, TXT_EX_TEXT)) == TRUE )
{ add_nchars_charbuf(&b, len, s);
} else if ( rc == -1 )
{ free_charbuf(&b);
return FALSE;
}
/* query */
if ( (rc=get_text_arg(components, 4, &len, &s, TXT_EX_TEXT)) == TRUE )
{ if ( len > 0 )
{ add_charbuf(&b, '?');
add_nchars_charbuf(&b, len, s);
}
} else if ( rc == -1 )
{ free_charbuf(&b);
return FALSE;
}
/* fragment */
if ( (rc=get_text_arg(components, 5, &len, &s, TXT_EX_TEXT)) == TRUE )
{ add_charbuf(&b, '#');
add_nchars_charbuf(&b, len, s);
} else if ( rc == -1 )
{ free_charbuf(&b);
return FALSE;
}
rc = PL_unify_wchars(URI, PL_ATOM, b.here-b.base, b.base);
free_charbuf(&b);
return rc;
} else /* generate an error */
{ return PL_get_wchars(URI, &len, &s,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION);
}
}
/** uri_is_global(+URI) is semidet.
*/
static foreign_t
uri_is_global(term_t URI)
{ pl_wchar_t *s;
size_t len;
if ( PL_get_wchars(URI, &len, &s,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) )
{ const pl_wchar_t *e;
const pl_wchar_t *end = &s[len];
range r;
fill_flags();
e = skip_not(s, end, L":/?#");
if ( e > s+1 && e[0] == ':' )
{ r.start = s;
r.end = e;
if ( range_is_unreserved(&r, FALSE, CH_SCHEME) )
return TRUE;
}
}
return FALSE;
}
/*******************************
* QUERY-STRING *
*******************************/
static int
unify_decoded_atom(term_t t, range *r, int flags)
{ if ( range_has_escape(r, flags) )
{ charbuf b;
int rc;
init_charbuf(&b);
add_decoded_range_charbuf(&b, r, flags);
rc = PL_unify_wchars(t, PL_ATOM, b.here - b.base, b.base);
free_charbuf(&b);
return rc;
} else
{ return unify_range(t, r);
}
}
static int
unify_query_string_components(term_t list, size_t len, const pl_wchar_t *qs)
{ if ( len == 0 )
{ return PL_unify_nil(list);
} else
{ term_t tail = PL_copy_term_ref(list);
term_t head = PL_new_term_ref();
term_t eq = PL_new_term_refs(3);
term_t nv = eq+1;
const pl_wchar_t *end = &qs[len];
while(qs < end)
{ range name, value;
name.start = qs;
name.end = skip_not(qs, end, L"=");
if ( name.end < end )
{ value.start = name.end+1;
value.end = skip_not(value.start, end, L"&;");
qs = value.end+1;
} else
{ return syntax_error("illegal_uri_query");
}
PL_put_variable(nv+0);
PL_put_variable(nv+1);
unify_decoded_atom(nv+0, &name, ESC_QNAME);
unify_decoded_atom(nv+1, &value, ESC_QVALUE);
if ( !PL_cons_functor_v(eq, FUNCTOR_equal2, nv) ||
!PL_unify_list(tail, head, tail) ||
!PL_unify(head, eq) )
return FALSE;
}
return PL_unify_nil(tail);
}
}
static int
add_encoded_term_charbuf(charbuf *cb, term_t value, int iri, int flags)
{ pl_wchar_t *s;
range r;
size_t len;
if ( !PL_get_wchars(value, &len, &s, CVT_ATOMIC|CVT_EXCEPTION) )
return FALSE;
r.start = s;
r.end = r.start+len;
if ( range_is_unreserved(&r, iri, flags) )
{ add_nchars_charbuf(cb, r.end-r.start, r.start);
} else
{ const pl_wchar_t *s = r.start;
while(s<r.end)
add_encoded_charbuf(cb, *s++, flags);
}
return TRUE;
}
/** uri_query_components(+QueryString, -ValueList) is det.
*/
static foreign_t
uri_query_components(term_t string, term_t list)
{ pl_wchar_t *s;
size_t len;
if ( PL_get_wchars(string, &len, &s, CVT_ATOM|CVT_STRING|CVT_LIST) )
{ return unify_query_string_components(list, len, s);
} else if ( PL_is_list(list) )
{ term_t tail = PL_copy_term_ref(list);
term_t head = PL_new_term_ref();
term_t nv = PL_new_term_refs(2);
charbuf out;
int rc;
fill_flags();
init_charbuf(&out);
while( PL_get_list(tail, head, tail) )
{ atom_t fname;
int arity;
if ( PL_is_functor(head, FUNCTOR_equal2) ||
PL_is_functor(head, FUNCTOR_pair2) )
{ _PL_get_arg(1, head, nv+0);
_PL_get_arg(2, head, nv+1);
} else if ( PL_get_name_arity(head, &fname, &arity) && arity == 1 )
{ PL_put_atom(nv+0, fname);
_PL_get_arg(1, head, nv+1);
} else
{ free_charbuf(&out);
return type_error("name_value", head);
}
if ( out.here != out.base )
add_charbuf(&out, '&');
if ( !add_encoded_term_charbuf(&out, nv+0, FALSE, ESC_QNAME) )
{ free_charbuf(&out);
return FALSE;
}
add_charbuf(&out, '=');
if ( !add_encoded_term_charbuf(&out, nv+1, FALSE, ESC_QVALUE) )
{ free_charbuf(&out);
return FALSE;
}
}
rc = PL_unify_wchars(string, PL_ATOM, out.here-out.base, out.base);
free_charbuf(&out);
return rc;
} else
{ return PL_get_wchars(string, &len, &s,
CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION);
}
return FALSE;
}
/** uri_encoded(+What, +String, -Encoded)
*/
static foreign_t
uri_encoded(term_t what, term_t qv, term_t enc)
{ pl_wchar_t *s;
size_t len;
atom_t w;
int flags;
if ( !PL_get_atom(what, &w) )
return type_error("atom", what);
if ( w == ATOM_query_value )
flags = ESC_QVALUE;
else if ( w == ATOM_fragment )
flags = ESC_FRAGMENT;
else if ( w == ATOM_path )
flags = ESC_PATH;
else
return domain_error("uri_component", what);
fill_flags();
if ( !PL_is_variable(qv) )
{ charbuf out;
int rc;
init_charbuf(&out);
if ( !add_encoded_term_charbuf(&out, qv, FALSE, flags) )
{ free_charbuf(&out);
return FALSE;
}
rc = PL_unify_wchars(enc, PL_ATOM, out.here-out.base, out.base);
free_charbuf(&out);
return rc;
} else if ( PL_get_wchars(enc, &len, &s, CVT_ATOM|CVT_STRING|CVT_EXCEPTION) )