-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFast.xs
2586 lines (2186 loc) · 66.3 KB
/
Fast.xs
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
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
/*------------------------------------------------------------------------------
*
* Macros and definitions
*
-----------------------------------------------------------------------------*/
// Permitted characters
#define URI_CHARS_AUTH "!$&'()*+,;:=@"
#define URI_CHARS_USER "!$&'()*+,;="
#define URI_CHARS_PATH "!$&'()*+,;:=@/"
#define URI_CHARS_PATH_SEGMENT "!$&'()*+,;:=@"
#define URI_CHARS_HOST "!$&'()[]*+,.;=@/"
#define URI_CHARS_QUERY ":@?/&=;"
#define URI_CHARS_FRAG ":@?/"
// Returns the uri_t* referenced by the blessed URI::Fast object in the SV ref.
// Croaks if the SV does not point to a URI::Fast object.
#define URI(obj) \
(((sv_isobject(obj) && sv_derived_from(obj, "URI::Fast")) ? NULL : croak("error: expected instance of URI::Fast")), \
((uri_t*) SvIV(SvRV((obj)))))
// Size constants
#define URI_SIZE_scheme 32UL
#define URI_SIZE_usr 32UL
#define URI_SIZE_pwd 32UL
#define URI_SIZE_host 64UL
#define URI_SIZE_port 8UL
#define URI_SIZE_path 64UL
#define URI_SIZE_query 64UL
#define URI_SIZE_frag 32UL
// Enough to fit all pieces + 3 chars for separators (2 colons + @)
#define URI_SIZE_auth (3 + URI_SIZE_usr + URI_SIZE_pwd + URI_SIZE_host + URI_SIZE_port)
// Returns the size of the member in bytes
#define URI_SIZE(member) (URI_SIZE_##member)
// Defines a clearer method
#define URI_SIMPLE_CLEARER(member) \
static void clear_##member(pTHX_ SV *uri) { \
str_clear(aTHX_ URI(uri)->member); \
}
// Returns a (non-mortal) SV from a uri_str_t
#define URI_STR_2SV(str) (newSVpvn((str)->length == 0 ? "" : (str)->string, (str)->length))
// Defines a setter method that accepts an unencoded value, encodes it,
// ignoring characters in string 'allowed', and copies the encoded value into
// slot 'member'.
#define URI_SIMPLE_SETTER(member, allowed) \
static void set_##member(pTHX_ SV *sv_uri, SV *sv_value) { \
uri_t *uri = URI(sv_uri); \
if (is_defined(aTHX_ sv_value)) { \
size_t len_value, len_enc; \
const char *value = SvPV_const(sv_value, len_value); \
char enc[len_value * 3 + 1]; \
len_enc = uri_encode(value, len_value, enc, allowed, uri->is_iri); \
str_set(aTHX_ uri->member, enc, len_enc); \
} \
else { \
str_clear(aTHX_ uri->member); \
} \
}
// Defines a setter method that accepts an arbitrary string value and copies it
// into the slot 'member' without changing it in any way.
#define URI_RAW_SETTER(member) \
static void set_raw_##member(pTHX_ SV *sv_uri, SV *sv_value) { \
uri_t *uri = URI(sv_uri); \
if (is_defined(aTHX_ sv_value)) { \
size_t len_value; \
const char *value = SvPV_const(sv_value, len_value); \
str_set(aTHX_ uri->member, value, len_value); \
} \
else { \
str_clear(aTHX_ uri->member); \
} \
}
// Defines a getter method that returns the raw, encoded value of the member
// slot. If the object is an IRI, decodes utf8 characters from hex sequences if
// present.
#define URI_RAW_GETTER(member) \
static SV* get_raw_##member(pTHX_ SV *sv_uri) { \
uri_t *uri = URI(sv_uri); \
uri_str_t *str = uri->member; \
if (uri->is_iri) { \
if (str->length == 0) return newSVpvn("", 0); \
char decoded[ str->length + 1 ]; \
size_t len = uri_decode_utf8(str->string, str->length, decoded); \
SV *out = newSVpvn(decoded, len); \
sv_utf8_decode(out); \
return out; \
} else { \
return URI_STR_2SV(str); \
} \
}
// Defines a getter method that returns the decoded value of the member slot.
#define URI_SIMPLE_GETTER(member) \
static SV* get_##member(pTHX_ SV *uri) { \
uri_str_t *str = URI(uri)->member; \
if (str->length == 0) return newSVpvn("", 0); \
char decoded[ str->length + 1 ]; \
size_t len = uri_decode(str->string, str->length, decoded, ""); \
SV *out = newSVpvn(decoded, len); \
sv_utf8_decode(out); \
return out; \
}
// Defines a getter method for a structured field that returns the value of the
// member slot with non-ASCII character decoded, while leaving reserved
// characters encoded.
#define URI_COMPOUND_GETTER(member) \
static SV* get_##member(pTHX_ SV *uri) { \
uri_str_t *str = URI(uri)->member; \
if (str->length == 0) return newSVpvn("", 0); \
char decoded[ str->length + 1 ]; \
size_t len = uri_decode_utf8(str->string, str->length, decoded); \
SV *out = newSVpvn(decoded, len); \
sv_utf8_decode(out); \
return out; \
}
// Warns out info about a uri_str_t
#define URI_STR_DEBUG(str) \
(warn( \
"STRING< chunk=%lu, allocated=%lu, length=%lu, string='%.*s' >\n", \
str->chunk, \
str->allocated, \
str->length, \
(int)str->length, \
str->string \
)); \
/*
* Allocate memory with Newx if it's
* available - if it's an older perl
* that doesn't have Newx then we
* resort to using New.
*/
#ifndef Newx
#define Newx(v,n,t) New(0,v,n,t)
#endif
// av_top_index not available on Perls < 5.18
#ifndef av_top_index
#define av_top_index(av) av_len(av)
#endif
/*------------------------------------------------------------------------------
*
* Internal API
*
-----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
* Utilities
-----------------------------------------------------------------------------*/
// Returns the class name of a blessed object. Gets magic before evaluating.
// Returns an empty string if the object is not defined or is not a reference.
static
const char* class_name(pTHX_ SV *sv) {
SvGETMAGIC(sv);
if (!SvOK(sv) || !SvROK(sv)) {
return "";
}
return HvNAME(SvSTASH(SvRV(sv)));
}
// Returns true if the SV is defined. Gets magic before evaluating the
// definedness of the SV.
static
bool is_defined(pTHX_ SV *sv) {
SvGETMAGIC(sv);
return SvOK(sv) ? 1 : 0;
}
// Returns true if the SV is an RV. Gets magic before evaluating.
static
bool is_ref(pTHX_ SV *sv) {
SvGETMAGIC(sv);
return SvROK(sv) ? 1 : 0;
}
// Replacement for strspn that is length-aware
static
size_t strnspn(const char *s, size_t s_len, const char *c)
{
size_t res = strspn(s, c);
return s_len < res ? s_len : res;
}
// Replacement for strcspn that is length-aware
static
size_t strncspn(const char *s, size_t s_len, const char *c)
{
size_t res = strcspn(s, c);
return s_len < res ? s_len : res;
}
// Returns true if char c is in char* set. It is up to the caller to ensure
// that *set is nul-terminated.
static inline
bool char_in_str(const char c, const char *set) {
size_t i;
for (i = 0; set[i] != '\0'; ++i) {
if (set[i] == c) {
return 1;
}
}
return 0;
}
// returns true for an ASCII whitespace char
static inline
bool my_isspace(const char c) {
switch (c) {
case ' ': case '\t':
case '\r': case '\n':
case '\f': case '\v':
return 1;
default:
return 0;
}
}
// min of two numbers
static inline
size_t minnum(size_t x, size_t y) {
return x <= y ? x : y;
}
// max of two numbers
static inline
size_t maxnum(size_t x, size_t y) {
return x >= y ? x : y;
}
/*------------------------------------------------------------------------------
* Resizable strings
-----------------------------------------------------------------------------*/
typedef struct {
size_t chunk; // bytes to allocate at a time
size_t allocated; // number of currently allocated bytes
size_t length; // length of the string within the allocated buffer
char *string; // pointer to the allocated string
} uri_str_t;
#define str_len(str) ((str)->length)
#define str_get(str) (str_len(str) == 0 ? "" : (const char*)str->string)
// Truncates the string from the right-most occurence of r_char by setting that
// index to nul. Does not zero out the rest of the string.
static
void str_rtrim(pTHX_ uri_str_t *str, const char r_char) {
size_t i;
for (i = str->length; i > 0; --i) {
if (str->string[i - 1] == r_char) {
str->string[i - 1] = '\0';
str->length = i - 1;
break;
}
}
}
// Sets str to the first len chars of value. Reallocates another block of
// memory to fit it if necessary.
static
void str_set(pTHX_ uri_str_t *str, const char *value, size_t len) {
size_t allocate = str->chunk * (((len + 1) / str->chunk) + 1);
if (str->string == NULL) {
Newx(str->string, allocate, char);
str->allocated = allocate;
}
else if (len > str->allocated) {
Renew(str->string, allocate, char);
str->allocated = allocate;
}
if (value == NULL) {
Zero(str->string, len + 1, char);
str->length = 0;
}
else {
Copy(value, str->string, len, char);
str->string[len] = '\0';
str->length = len;
}
}
// Appends the first len chars of value to str, allocating more memory if
// necessary.
static
void str_append(pTHX_ uri_str_t *str, const char *value, size_t len) {
if (str->string == NULL) {
str_set(aTHX_ str, value, len);
return;
}
if (value != NULL && len > 0) {
size_t allocate = str->chunk * (((str->length + len + 1) / str->chunk) + 1);
if (allocate != str->allocated) {
Renew(str->string, allocate, char);
str->allocated = allocate;
}
Copy(value, &str->string[str->length], len, char);
str->string[str->length + len] = '\0';
str->length += len;
}
}
// Zeroes out the contents of str. Does not release memory.
static
void str_clear(pTHX_ uri_str_t *str) {
str_set(aTHX_ str, NULL, 0);
}
// Copies the contents of from into to. Does not clear to first, but will set
// the terminating nul and length.
static
void str_copy(pTHX_ uri_str_t *from, uri_str_t *to) {
str_set(aTHX_ to, from->string, from->length);
}
// Initializes a uri_str_t.
static
void str_init(pTHX_ uri_str_t *str, size_t alloc_size) {
str->chunk = alloc_size;
str->allocated = 0;
str->length = 0;
str->string = NULL;
}
// Allocates and initializes a new uri_str_t.
static
uri_str_t* str_new(pTHX_ size_t alloc_size) {
uri_str_t *str;
Newx(str, 1, uri_str_t);
str_init(aTHX_ str, alloc_size);
return str;
}
// Release an allocated uri_str_t and free's its contents.
static
void str_free(pTHX_ uri_str_t *str) {
if (str->string != NULL) {
Safefree(str->string);
}
Safefree(str);
}
/*-------------------------------------------------------------------------------
* Percent encoding
-----------------------------------------------------------------------------*/
// Taken with respect from URI::Escape::XS. Adapted to accept a configurable
// string of permissible characters.
#define _______ "\0\0\0\0"
static const char uri_encode_tbl[ sizeof(U32) * 0x100 ] = {
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
"%00\0" "%01\0" "%02\0" "%03\0" "%04\0" "%05\0" "%06\0" "%07\0" "%08\0" "%09\0" "%0A\0" "%0B\0" "%0C\0" "%0D\0" "%0E\0" "%0F\0" /* 0: 0 ~ 15 */
"%10\0" "%11\0" "%12\0" "%13\0" "%14\0" "%15\0" "%16\0" "%17\0" "%18\0" "%19\0" "%1A\0" "%1B\0" "%1C\0" "%1D\0" "%1E\0" "%1F\0" /* 1: 16 ~ 31 */
"%20\0" "%21\0" "%22\0" "%23\0" "%24\0" "%25\0" "%26\0" "%27\0" "%28\0" "%29\0" "%2A\0" "%2B\0" "%2C\0" _______ _______ "%2F\0" /* 2: 32 ~ 47 */
_______ _______ _______ _______ _______ _______ _______ _______ _______ _______ "%3A\0" "%3B\0" "%3C\0" "%3D\0" "%3E\0" "%3F\0" /* 3: 48 ~ 63 */
"%40\0" _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ /* 4: 64 ~ 79 */
_______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ "%5B\0" "%5C\0" "%5D\0" "%5E\0" _______ /* 5: 80 ~ 95 */
"%60\0" _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ /* 6: 96 ~ 111 */
_______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ "%7B\0" "%7C\0" "%7D\0" _______ "%7F\0" /* 7: 112 ~ 127 */
"%80\0" "%81\0" "%82\0" "%83\0" "%84\0" "%85\0" "%86\0" "%87\0" "%88\0" "%89\0" "%8A\0" "%8B\0" "%8C\0" "%8D\0" "%8E\0" "%8F\0" /* 8: 128 ~ 143 */
"%90\0" "%91\0" "%92\0" "%93\0" "%94\0" "%95\0" "%96\0" "%97\0" "%98\0" "%99\0" "%9A\0" "%9B\0" "%9C\0" "%9D\0" "%9E\0" "%9F\0" /* 9: 144 ~ 159 */
"%A0\0" "%A1\0" "%A2\0" "%A3\0" "%A4\0" "%A5\0" "%A6\0" "%A7\0" "%A8\0" "%A9\0" "%AA\0" "%AB\0" "%AC\0" "%AD\0" "%AE\0" "%AF\0" /* A: 160 ~ 175 */
"%B0\0" "%B1\0" "%B2\0" "%B3\0" "%B4\0" "%B5\0" "%B6\0" "%B7\0" "%B8\0" "%B9\0" "%BA\0" "%BB\0" "%BC\0" "%BD\0" "%BE\0" "%BF\0" /* B: 176 ~ 191 */
"%C0\0" "%C1\0" "%C2\0" "%C3\0" "%C4\0" "%C5\0" "%C6\0" "%C7\0" "%C8\0" "%C9\0" "%CA\0" "%CB\0" "%CC\0" "%CD\0" "%CE\0" "%CF\0" /* C: 192 ~ 207 */
"%D0\0" "%D1\0" "%D2\0" "%D3\0" "%D4\0" "%D5\0" "%D6\0" "%D7\0" "%D8\0" "%D9\0" "%DA\0" "%DB\0" "%DC\0" "%DD\0" "%DE\0" "%DF\0" /* D: 208 ~ 223 */
"%E0\0" "%E1\0" "%E2\0" "%E3\0" "%E4\0" "%E5\0" "%E6\0" "%E7\0" "%E8\0" "%E9\0" "%EA\0" "%EB\0" "%EC\0" "%ED\0" "%EE\0" "%EF\0" /* E: 224 ~ 239 */
"%F0\0" "%F1\0" "%F2\0" "%F3\0" "%F4\0" "%F5\0" "%F6\0" "%F7\0" "%F8\0" "%F9\0" "%FA\0" "%FB\0" "%FC\0" "%FD\0" "%FE\0" "%FF" /* F: 240 ~ 255 */
};
#undef _______
static
size_t uri_encode(const char* in, size_t len, char* out, const char* allow, int allow_utf8) {
size_t i = 0;
size_t j = 0;
size_t k, skip;
U8 octet;
U32 code;
while (i < len) {
octet = in[i];
if (allow_utf8 && octet & 0xc0) {
skip = UTF8SKIP(&in[i]);
if (skip > 0) {
for (k = 0; k < skip; ++k) {
out[j++] = in[i++];
}
continue;
}
}
if (char_in_str(octet, allow)) {
out[j++] = octet;
++i;
}
else {
code = ((U32*) uri_encode_tbl)[(unsigned char) octet];
if (code) {
*((U32*) &out[j]) = code;
j += 3;
}
else {
out[j++] = octet;
}
++i;
}
}
out[j] = '\0';
return j;
}
/*-------------------------------------------------------------------------------
* Percent decoding
-----------------------------------------------------------------------------*/
#define __ 0xFF
static const unsigned char hex[0x100] = {
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 00-0F */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 10-1F */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 20-2F */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,__,__,__,__,__,__, /* 30-3F */
__,10,11,12,13,14,15,__,__,__,__,__,__,__,__,__, /* 40-4F */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 50-5F */
__,10,11,12,13,14,15,__,__,__,__,__,__,__,__,__, /* 60-6F */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 70-7F */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 80-8F */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 90-9F */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* A0-AF */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* B0-BF */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* C0-CF */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* D0-DF */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* E0-EF */
__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* F0-FF */
};
#undef __
static inline
char unhex(const char *in) {
unsigned char v1 = hex[ (unsigned char) in[0] ];
unsigned char v2 = hex[ (unsigned char) in[1] ];
/* skip invalid hex sequences */
if ((v1 | v2) != 0xFF) {
return (v1 << 4) | v2;
}
return '\0';
}
static
size_t uri_decode(const char *in, size_t len, char *out, const char *ignore) {
size_t i = 0, j = 0;
char decoded;
while (i < len) {
decoded = '\0';
switch (in[i]) {
case '+':
decoded = ' ';
if (!char_in_str(decoded, ignore)) {
++i;
break;
}
case '%':
if (i + 2 < len) {
decoded = unhex( &in[i + 1] );
if (decoded != '\0' && !char_in_str(decoded, ignore)) {
i += 3;
break;
}
}
default:
decoded = in[i++];
}
if (decoded != '\0') {
out[j++] = decoded;
}
}
out[j] = '\0';
return j;
}
static
size_t uri_decode_utf8(const char *in, size_t len, char *out) {
size_t i = 0, j = 0;
char decoded;
while (i < len) {
decoded = '\0';
switch (in[i]) {
case '%':
if (i + 2 < len) {
decoded = unhex( &in[i + 1] );
if (decoded != '\0' && (U32)decoded > 127) {
i += 3;
break;
}
}
default:
decoded = in[i++];
}
if (decoded != '\0') {
out[j++] = decoded;
}
}
out[j] = '\0';
return j;
}
// EOT (end of theft)
/*
* External API for encode/decode.
*/
static
SV* encode(pTHX_ SV *in, SV *sv_allowed) {
size_t ilen, olen, alen;
const char *allowed;
SV* out;
if (!is_defined(aTHX_ in)) {
return newSVpvn("", 0);
}
const char *src = SvPV_nomg_const(in, ilen);
char dest[(ilen * 3) + 1];
if (sv_allowed == NULL) {
allowed = "";
} else {
allowed = SvPV_nomg_const(sv_allowed, alen);
}
olen = uri_encode(src, ilen, dest, allowed, 0);
out = newSVpvn(dest, olen);
sv_utf8_downgrade(out, FALSE);
return out;
}
static
SV* decode(pTHX_ SV *in) {
size_t ilen, olen;
const char *src;
SV *out;
if (!is_defined(aTHX_ in)) {
return newSVpvn("", 0);
}
if (DO_UTF8(in)) {
in = sv_mortalcopy(in);
SvUTF8_on(in);
if (!sv_utf8_downgrade(in, TRUE)) {
croak("decode: wide character in input octet string");
}
src = SvPV_nomg_const(in, ilen);
}
else {
src = SvPV_nomg_const(in, ilen);
}
char dest[ilen + 1];
olen = uri_decode(src, ilen, dest, "");
out = newSVpvn(dest, olen);
sv_utf8_decode(out);
return out;
}
/*------------------------------------------------------------------------------
* Query string parsing
-----------------------------------------------------------------------------*/
typedef enum {
KEY = 1,
PARAM = 2,
DONE = 3,
} uri_query_token_type_t;
typedef struct {
uri_query_token_type_t type;
const char *key; size_t key_length;
const char *value; size_t value_length; // only present when type=PARAM
} uri_query_token_t;
typedef struct {
size_t length;
size_t cursor;
const char *source;
} uri_query_scanner_t;
// Initializes a uri_query_scanner_t with input string *source of at least
// length characters. It is the caller's responsibility to ensure the lifetime
// of source matches the lifetime of the scanner.
void query_scanner_init(
uri_query_scanner_t *scanner,
const char *source,
size_t length
)
{
scanner->source = source;
scanner->length = length;
scanner->cursor = 0;
}
// Returns true if the scanner has reached the end of the input string.
static
int query_scanner_done(uri_query_scanner_t *scanner) {
return scanner->cursor >= scanner->length;
}
/*
* Fills the token struct with the next token information. Does not decode
* any values.
*/
static
void query_scanner_next(uri_query_scanner_t *scanner, uri_query_token_t *token) {
size_t brk;
const char key_sep[4] = {'&', ';', '=', '\0'};
const char val_sep[4] = {'&', ';', '\0'};
SCAN_KEY:
if (scanner->cursor >= scanner->length) {
token->key = NULL; token->key_length = 0;
token->value = NULL; token->value_length = 0;
token->type = DONE;
return;
}
// Scan to end of token
brk = strncspn(
&scanner->source[ scanner->cursor ],
scanner->length - scanner->cursor,
key_sep
);
// Set key members in token struct
token->key = &scanner->source[ scanner->cursor ];
token->key_length = brk;
// Move cursor to end of token
scanner->cursor += brk;
// If there is an associate value, add it to the token
if (scanner->source[ scanner->cursor ] == '=') {
// Advance past '='
++scanner->cursor;
// Find the end of the value
brk = strncspn(&scanner->source[ scanner->cursor ], scanner->length - scanner->cursor, val_sep);
// Set the value and token type
token->value = &scanner->source[ scanner->cursor ];
token->value_length = brk;
token->type = PARAM;
// Move cursor to the end of the value, eating the separator terminating it
scanner->cursor += brk + 1;
}
// No value assigned to this key
else {
token->type = KEY;
++scanner->cursor; // advance past terminating separator
}
// No key was found; try again
if (token->key_length == 0) {
goto SCAN_KEY;
}
return;
}
/*------------------------------------------------------------------------------
* URI parsing
-----------------------------------------------------------------------------*/
typedef struct {
U8 is_iri;
uri_str_t *scheme;
uri_str_t *query;
uri_str_t *path;
uri_str_t *host;
uri_str_t *port;
uri_str_t *frag;
uri_str_t *usr;
uri_str_t *pwd;
} uri_t;
/*
* Scans the authorization portion of the URI string
*/
static
void uri_scan_auth(pTHX_ uri_t* uri, const char* auth, const size_t len) {
size_t idx = 0;
size_t brk1 = 0;
size_t brk2 = 0;
size_t i;
unsigned char flag;
if (len > 0) {
// Credentials
brk1 = strncspn(&auth[idx], len - idx, "@");
if (brk1 > 0 && brk1 != (len - idx)) {
brk2 = strncspn(&auth[idx], len - idx, ":");
if (brk2 > 0 && brk2 < brk1) {
// user
str_set(aTHX_ uri->usr, &auth[idx], brk2);
idx += brk2 + 1;
// password
str_set(aTHX_ uri->pwd, &auth[idx], brk1 - brk2 - 1);
idx += brk1 - brk2;
}
else {
// user only
str_set(aTHX_ uri->usr, &auth[idx], brk1);
idx += brk1 + 1;
}
}
// Location
// Maybe an IPV6 address
flag = 0;
if (auth[idx] == '[') {
brk1 = strncspn(&auth[idx], len - idx, "]");
if (auth[idx + brk1] == ']') {
// Copy, including the square brackets
str_set(aTHX_ uri->host, &auth[idx], brk1 + 1);
idx += brk1 + 1;
flag = 1;
}
}
if (flag == 0) {
brk1 = strncspn(&auth[idx], len - idx, ":");
if (brk1 > 0) {
str_set(aTHX_ uri->host, &auth[idx], brk1);
idx += brk1;
}
}
if (auth[idx] == ':') {
++idx;
str_set(aTHX_ uri->port, &auth[idx], len - idx);
}
}
}
/*
* Scans a URI string and populates the uri_t struct.
*
* Correct:
* scheme:[//[usr[:pwd]@]host[:port]]path[?query][#fragment]
*
* Incorrect but supported:
* /path[?query][#fragment]
*
*/
static
void uri_scan(pTHX_ uri_t *uri, const char *src, size_t len) {
size_t idx = 0;
size_t brk;
size_t i;
while (my_isspace(src[idx]) == 1) ++idx; // Trim leading whitespace
while (my_isspace(src[len - 1]) == 1) --len; // Trim trailing whitespace
// scheme
brk = strncspn(&src[idx], len - idx, ":/@?#");
if (brk > 0 && src[idx + brk] == ':') {
str_set(aTHX_ uri->scheme, &src[idx], brk);
idx += brk;
++idx; // skip past ":"
}
// authority
if (idx + 1 < len // src is long enough to hold two slashes
&& src[idx] == '/' // next char is a slash
&& src[idx + 1] == '/') // char after that is a slash
{
idx += 2; // skip past the double slashes
brk = strncspn(&src[idx], len - idx, "/?#");
uri_scan_auth(aTHX_ uri, &src[idx], brk);
if (brk > 0) {
idx += brk;
}
}
// path
brk = strncspn(&src[idx], len - idx, "?#");
if (brk > 0) {
str_set(aTHX_ uri->path, &src[idx], brk);
idx += brk;
}
// query
if (src[idx] == '?') {
++idx; // skip past ?
brk = strncspn(&src[idx], len - idx, "#");
if (brk > 0) {
str_set(aTHX_ uri->query, &src[idx], brk);
idx += brk;
}
}
// fragment
if (src[idx] == '#') {
++idx; // skip past #
brk = len - idx;
if (brk > 0) {
str_set(aTHX_ uri->frag, &src[idx], brk);
}
}
}
/*
* Helper function that returns true if any part of the authority section is
* set.
*/
static inline
int has_authority(pTHX_ uri_t *uri) {
return uri->host->length > 0
|| uri->usr->length > 0
|| uri->pwd->length > 0
|| uri->port->length > 0;
}
/*------------------------------------------------------------------------------
*
* Perl API
*
-----------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------
* Clearers
-----------------------------------------------------------------------------*/
URI_SIMPLE_CLEARER(scheme);
URI_SIMPLE_CLEARER(path);
URI_SIMPLE_CLEARER(query);
URI_SIMPLE_CLEARER(frag);
URI_SIMPLE_CLEARER(usr);
URI_SIMPLE_CLEARER(pwd);
URI_SIMPLE_CLEARER(host);
URI_SIMPLE_CLEARER(port);
static
void clear_auth(pTHX_ SV *uri_obj) {
clear_usr(aTHX_ uri_obj);
clear_pwd(aTHX_ uri_obj);
clear_host(aTHX_ uri_obj);
clear_port(aTHX_ uri_obj);
}
/*------------------------------------------------------------------------------
* Raw getters
-----------------------------------------------------------------------------*/
// Raw getters
URI_RAW_GETTER(scheme);
URI_RAW_GETTER(usr);
URI_RAW_GETTER(pwd);
URI_RAW_GETTER(host);
URI_RAW_GETTER(port);
URI_RAW_GETTER(path);
URI_RAW_GETTER(query);
URI_RAW_GETTER(frag);
static
SV* get_raw_auth(pTHX_ SV *uri_obj) {
uri_t *uri = URI(uri_obj);
SV *out = newSVpvn("", 0);
if (uri->is_iri) {
SvUTF8_on(out);
}
if (str_len(uri->usr) > 0) {
if (str_len(uri->pwd) > 0) {
sv_catpvn(out, str_get(uri->usr), str_len(uri->usr));
sv_catpvn(out, ":", 1);
sv_catpvn(out, str_get(uri->pwd), str_len(uri->pwd));
sv_catpvn(out, "@", 1);
} else {
sv_catpvn(out, str_get(uri->usr), str_len(uri->usr));
sv_catpvn(out, "@", 1);
}
}
if (str_len(uri->host) > 0) {
if (str_len(uri->port) > 0) {
sv_catpvn(out, str_get(uri->host), str_len(uri->host));
sv_catpvn(out, ":", 1);
sv_catpvn(out, str_get(uri->port), str_len(uri->port));
} else {
sv_catpvn(out, str_get(uri->host), str_len(uri->host));
}
}
return out;
}
/*------------------------------------------------------------------------------
* Decoding getters
-----------------------------------------------------------------------------*/
URI_SIMPLE_GETTER(scheme);
URI_SIMPLE_GETTER(usr);
URI_SIMPLE_GETTER(pwd);
URI_SIMPLE_GETTER(host);
URI_SIMPLE_GETTER(port);
URI_SIMPLE_GETTER(frag);
URI_COMPOUND_GETTER(path);
URI_COMPOUND_GETTER(query);
static
SV* get_auth(pTHX_ SV *uri_obj) {
uri_t *uri = URI(uri_obj);
SV *out = newSVpvn("", 0);
if (uri->is_iri) {
SvUTF8_on(out);
}
if (str_len(uri->usr) > 0) {
if (str_len(uri->pwd) > 0) {
sv_catsv(out, sv_2mortal(get_usr(aTHX_ uri_obj)));
sv_catpvn(out, ":", 1);
sv_catsv(out, sv_2mortal(get_pwd(aTHX_ uri_obj)));
sv_catpvn(out, "@", 1);
} else {
sv_catsv(out, sv_2mortal(get_usr(aTHX_ uri_obj)));
sv_catpvn(out, "@", 1);
}
}
if (str_len(uri->host) > 0) {
if (str_len(uri->port) > 0) {
sv_catsv(out, sv_2mortal(get_host(aTHX_ uri_obj)));
sv_catpvn(out, ":", 1);
sv_catsv(out, sv_2mortal(get_port(aTHX_ uri_obj)));
} else {
sv_catsv(out, sv_2mortal(get_host(aTHX_ uri_obj)));
}
}