Fast.xs

filter: Statementstmt 92.6% 852 / 920 Branchbran 67.0% 540 / 806 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.6% 1392 / 1726 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/URI/Fast/IRI.pm ↑ Summary blib/lib/URI/Fast/Test.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6  
7 /*------------------------------------------------------------------------------
8 *
9 * Macros and definitions
10 *
11 -----------------------------------------------------------------------------*/
12  
13 // Permitted characters
14 #define URI_CHARS_AUTH "!$&'()*+,;:=@"
15 #define URI_CHARS_PATH "!$&'()*+,;:=@/"
16 #define URI_CHARS_PATH_SEGMENT "!$&'()*+,;:=@"
17 #define URI_CHARS_HOST "!$&'()[]*+,.;=@/"
18 #define URI_CHARS_QUERY ":@?/&=;"
19 #define URI_CHARS_FRAG ":@?/"
20 #define URI_CHARS_USER "!$&'()*+,;="
21  
22 // Returns the uri_t* referenced by the blessed URI::Fast object in the SV ref.
23 // Croaks if the SV does not point to a URI::Fast object.
24 #define URI(obj) \
25 (((sv_isobject(obj) && sv_derived_from(obj, "URI::Fast")) ? NULL : croak("error")), \
26 ((uri_t*) SvIV(SvRV((obj)))))
27  
28 // Size constants
29 #define URI_SIZE_scheme 32UL
30 #define URI_SIZE_usr 32UL
31 #define URI_SIZE_pwd 32UL
32 #define URI_SIZE_host 64UL
33 #define URI_SIZE_port 8UL
34 #define URI_SIZE_path 64UL
35 #define URI_SIZE_query 64UL
36 #define URI_SIZE_frag 32UL
37  
38 // Enough to fit all pieces + 3 chars for separators (2 colons + @)
39 #define URI_SIZE_auth (3 + URI_SIZE_usr + URI_SIZE_pwd + URI_SIZE_host + URI_SIZE_port)
40  
41 // Returns the size of the member in bytes
42 #define URI_SIZE(member) (URI_SIZE_##member)
43  
44 // Defines a clearer method
45 #define URI_SIMPLE_CLEARER(member) \
46 static void clear_##member(pTHX_ SV *uri) { \
47 str_clear(aTHX_ URI(uri)->member); \
48 }
49  
50 // Returns a (non-mortal) SV from a uri_str_t
51 #define URI_STR_2SV(str) (newSVpvn((str)->length == 0 ? "" : (str)->string, (str)->length))
52  
53 // Defines a setter method that accepts an unencoded value, encodes it,
54 // ignoring characters in string 'allowed', and copies the encoded value into
55 // slot 'member'.
56 #define URI_SIMPLE_SETTER(member, allowed) \
57 static void set_##member(pTHX_ SV *sv_uri, SV *sv_value) { \
58 uri_t *uri = URI(sv_uri); \
59 if (is_defined(aTHX_ sv_value)) { \
60 size_t len_value, len_enc; \
61 const char *value = SvPV_const(sv_value, len_value); \
62 char enc[len_value * 3 + 1]; \
63 len_enc = uri_encode(value, len_value, enc, allowed, uri->is_iri); \
64 str_set(aTHX_ uri->member, enc, len_enc); \
65 } \
66 else { \
67 str_clear(aTHX_ uri->member); \
68 } \
69 }
70  
71 // Defines a getter method that returns the raw, encoded value of the member
72 // slot. If the object is an IRI, decodes utf8 characters from hex sequences if
73 // present.
74 #define URI_RAW_GETTER(member) \
75 static SV* get_raw_##member(pTHX_ SV *sv_uri) { \
76 uri_t *uri = URI(sv_uri); \
77 uri_str_t *str = uri->member; \
78 if (uri->is_iri) { \
79 if (str->length == 0) return newSVpvn("", 0); \
80 char decoded[ str->length + 1 ]; \
81 size_t len = uri_decode_utf8(str->string, str->length, decoded); \
82 SV *out = newSVpvn(decoded, len); \
83 sv_utf8_decode(out); \
84 return out; \
85 } else { \
86 return URI_STR_2SV(str); \
87 } \
88 }
89  
90 // Defines a getter method that returns the decoded value of the member slot.
91 #define URI_SIMPLE_GETTER(member) \
92 static SV* get_##member(pTHX_ SV *uri) { \
93 uri_str_t *str = URI(uri)->member; \
94 if (str->length == 0) return newSVpvn("", 0); \
95 char decoded[ str->length + 1 ]; \
96 size_t len = uri_decode(str->string, str->length, decoded, ""); \
97 SV *out = newSVpvn(decoded, len); \
98 sv_utf8_decode(out); \
99 return out; \
100 }
101  
102 // Defines a getter method for a structured field that returns the value of the
103 // member slot with non-ASCII character decoded, while leaving reserved
104 // characters encoded.
105 #define URI_COMPOUND_GETTER(member) \
106 static SV* get_##member(pTHX_ SV *uri) { \
107 uri_str_t *str = URI(uri)->member; \
108 if (str->length == 0) return newSVpvn("", 0); \
109 char decoded[ str->length + 1 ]; \
110 size_t len = uri_decode_utf8(str->string, str->length, decoded); \
111 SV *out = newSVpvn(decoded, len); \
112 sv_utf8_decode(out); \
113 return out; \
114 }
115  
116 // Warns out info about a uri_str_t
117 #define URI_STR_DEBUG(str) \
118 (warn( \
119 "STRING< chunk=%lu, allocated=%lu, length=%lu, string='%.*s' >\n", \
120 str->chunk, \
121 str->allocated, \
122 str->length, \
123 str->length, \
124 str->string \
125 )); \
126  
127 /*
128 * Allocate memory with Newx if it's
129 * available - if it's an older perl
130 * that doesn't have Newx then we
131 * resort to using New.
132 */
133 #ifndef Newx
134 #define Newx(v,n,t) New(0,v,n,t)
135 #endif
136  
137 // av_top_index not available on Perls < 5.18
138 #ifndef av_top_index
139 #define av_top_index(av) av_len(av)
140 #endif
141  
142 /*------------------------------------------------------------------------------
143 *
144 * Internal API
145 *
146 -----------------------------------------------------------------------------*/
147  
148 /*------------------------------------------------------------------------------
149 * Utilities
150 -----------------------------------------------------------------------------*/
151  
152 // Returns the class name of a blessed object. Gets magic before evaluating.
153 // Returns an empty string if the object is not defined or is not a reference.
154 static
155 174 const char* class_name(pTHX_ SV *sv) {
156 174 SvGETMAGIC(sv);
157  
158 174 ▶ if (!SvOK(sv) || !SvROK(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(sv) || !SvROK(sv)) { 174 0 174
if (!SvOK(sv) || !SvROK(sv)) { 0 174 174
159 0 return "";
160 }
161  
162 174 ▶ return HvNAME(SvSTASH(SvRV(sv)));
Branch: 6 branches
BranchTrueFalseTotal
return HvNAME(SvSTASH(SvRV(sv))); 174 0 174
return HvNAME(SvSTASH(SvRV(sv))); 174 0 174
return HvNAME(SvSTASH(SvRV(sv))); 0 174 174
return HvNAME(SvSTASH(SvRV(sv))); 0 0 0
return HvNAME(SvSTASH(SvRV(sv))); 174 0 174
return HvNAME(SvSTASH(SvRV(sv))); 0 174 174
163 }
164  
165 // Returns true if the SV is defined. Gets magic before evaluating the
166 // definedness of the SV.
167 static
168 473 bool is_defined(pTHX_ SV *sv) {
169 473 SvGETMAGIC(sv);
170 473 return SvOK(sv) ? 1 : 0;
171 }
172  
173 // Returns true if the SV is an RV. Gets magic before evaluating.
174 static
175 74 bool is_ref(pTHX_ SV *sv) {
176 74 SvGETMAGIC(sv);
177 74 return SvROK(sv) ? 1 : 0;
178 }
179  
180 // Replacement for strspn that is length-aware
181 static
182 0 size_t strnspn(const char *s, size_t s_len, const char *c)
183 {
184 0 size_t res = strspn(s, c);
185 0 return s_len < res ? s_len : res;
186 }
187  
188 // Replacement for strcspn that is length-aware
189 static
190 2674 size_t strncspn(const char *s, size_t s_len, const char *c)
191 {
192 2674 size_t res = strcspn(s, c);
193 2674 return s_len < res ? s_len : res;
194 }
195  
196 // Returns true if char c is in char* set. It is up to the caller to ensure
197 // that *set is nul-terminated.
198 static inline
199 1794 bool char_in_str(const char c, const char *set) {
200 size_t i;
201  
202 11301 ▶ for (i = 0; set[i] != '\0'; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; set[i] != '\0'; ++i) { 9558 1743 11301
203 9558 ▶ if (set[i] == c) {
Branch: 1 branch
BranchTrueFalseTotal
if (set[i] == c) { 51 9507 9558
204 51 return 1;
205 }
206 }
207  
208 1743 return 0;
209 }
210  
211 // returns true for an ASCII whitespace char
212 static inline
213 1082 bool my_isspace(const char c) {
214 1082 ▶ switch (c) {
Branch: 1 branch
BranchTrueFalseTotal
switch (c) { 18 1064 1082
215 18 case ' ': case '\t':
216 case '\r': case '\n':
217 case '\f': case '\v':
218 18 return 1;
219 1064 default:
220 1064 return 0;
221 }
222 }
223  
224 // min of two numbers
225 static inline
226 226 size_t minnum(size_t x, size_t y) {
227 226 return x <= y ? x : y;
228 }
229  
230 // max of two numbers
231 static inline
232 188 size_t maxnum(size_t x, size_t y) {
233 188 return x >= y ? x : y;
234 }
235  
236  
237 /*------------------------------------------------------------------------------
238 * Resizable strings
239 -----------------------------------------------------------------------------*/
240 typedef struct {
241 size_t chunk; // bytes to allocate at a time
242 size_t allocated; // number of currently allocated bytes
243 size_t length; // length of the string within the allocated buffer
244 char *string; // pointer to the allocated string
245 } uri_str_t;
246  
247 #define str_len(str) ((str)->length)
248 #define str_get(str) (str_len(str) == 0 ? "" : (const char*)str->string)
249  
250 // Searchs str for occurences of string *find. It is up to the caller to ensure
251 // that *find is at least len chars long. Returns -1 if not found.
252 static
253 70 int str_index(pTHX_ uri_str_t *str, const char *find, size_t len) {
254 size_t i, j;
255 70 bool found = 0;
256  
257 626 ▶ for (i = 0; i < str->length; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < str->length; ++i) { 556 70 626
258 762 ▶ for (j = 0; j < len; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < len; ++j) { 556 206 762
259 556 ▶ if (str->string[i + j] != find[j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (str->string[i + j] != find[j]) { 350 206 556
260 350 goto STRCHR;
261 }
262 }
263  
264 206 found = 1;
265  
266 556 STRCHR:
267 ;
268 }
269  
270 70 ▶ if (found) {
Branch: 1 branch
BranchTrueFalseTotal
if (found) { 70 0 70
271 70 return i;
272 } else {
273 0 return -1;
274 }
275 }
276  
277 // Truncates the string from the right-most occurence of r_char by setting that
278 // index to nul. Does not zero out the rest of the string.
279 static
280 109 void str_rtrim(pTHX_ uri_str_t *str, const char r_char) {
281 size_t i;
282 364 ▶ for (i = str->length; i > 0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = str->length; i > 0; --i) { 360 4 364
283 360 ▶ if (str->string[i - 1] == r_char) {
Branch: 1 branch
BranchTrueFalseTotal
if (str->string[i - 1] == r_char) { 105 255 360
284 105 str->string[i - 1] = '\0';
285 105 str->length = i - 1;
286 105 break;
287 }
288 }
289 109 }
290  
291 // Sets str to the first len chars of value. Reallocates another block of
292 // memory to fit it if necessary.
293 static
294 1998 void str_set(pTHX_ uri_str_t *str, const char *value, size_t len) {
295 1998 size_t allocate = str->chunk * (((len + 1) / str->chunk) + 1);
296  
297 1998 ▶ if (str->string == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (str->string == NULL) { 1855 143 1998
298 1855 Newx(str->string, allocate, char);
299 1855 str->allocated = allocate;
300 }
301 143 ▶ else if (len > str->allocated) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len > str->allocated) { 3 140 143
302 3 Renew(str->string, allocate, char);
303 3 str->allocated = allocate;
304 }
305  
306 1998 ▶ if (value == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (value == NULL) { 513 1485 1998
307 513 Zero(str->string, len + 1, char);
308 513 str->length = 0;
309 }
310 else {
311 1485 Copy(value, str->string, len, char);
312 1485 str->string[len] = '\0';
313 1485 str->length = len;
314 }
315 1998 }
316  
317 // Appends the first len chars of value to str, allocating more memory if
318 // necessary.
319 static
320 865 void str_append(pTHX_ uri_str_t *str, const char *value, size_t len) {
321 865 ▶ if (str->string == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (str->string == NULL) { 216 649 865
322 216 str_set(aTHX_ str, value, len);
323 216 return;
324 }
325  
326 649 ▶ if (value != NULL && len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (value != NULL && len > 0) { 649 0 649
if (value != NULL && len > 0) { 641 8 649
327 641 size_t allocate = str->chunk * (((str->length + len + 1) / str->chunk) + 1);
328  
329 641 ▶ if (allocate != str->allocated) {
Branch: 1 branch
BranchTrueFalseTotal
if (allocate != str->allocated) { 23 618 641
330 23 Renew(str->string, allocate, char);
331 23 str->allocated = allocate;
332 }
333  
334 641 Copy(value, &str->string[str->length], len, char);
335 641 str->string[str->length + len] = '\0';
336 641 str->length += len;
337 }
338 }
339  
340 // Zeroes out the contents of str. Does not release memory.
341 static
342 112 void str_clear(pTHX_ uri_str_t *str) {
343 112 str_set(aTHX_ str, NULL, 0);
344 112 }
345  
346 // Copies the contents of from into to. Does not clear to first, but will set
347 // the terminating nul and length.
348 static
349 618 void str_copy(pTHX_ uri_str_t *from, uri_str_t *to) {
350 618 str_set(aTHX_ to, from->string, from->length);
351 618 }
352  
353 // Initializes a uri_str_t.
354 static
355 4408 void str_init(pTHX_ uri_str_t *str, size_t alloc_size) {
356 4408 str->chunk = alloc_size;
357 4408 str->allocated = 0;
358 4408 str->length = 0;
359 4408 str->string = NULL;
360 4408 }
361  
362 // Allocates and initializes a new uri_str_t.
363 static
364 4408 uri_str_t* str_new(pTHX_ size_t alloc_size) {
365 uri_str_t *str;
366 4408 Newx(str, 1, uri_str_t);
367 4408 str_init(aTHX_ str, alloc_size);
368 4408 return str;
369 }
370  
371 // Release an allocated uri_str_t and free's its contents.
372 static
373 4406 void str_free(pTHX_ uri_str_t *str) {
374 4406 ▶ if (str->string != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (str->string != NULL) { 1855 2551 4406
375 1855 Safefree(str->string);
376 }
377  
378 4406 Safefree(str);
379 4406 }
380  
381  
382 /*-------------------------------------------------------------------------------
383 * Percent encoding
384 -----------------------------------------------------------------------------*/
385  
386 // Taken with respect from URI::Escape::XS. Adapted to accept a configurable
387 // string of permissible characters.
388 #define _______ "\0\0\0\0"
389 static const char uri_encode_tbl[ sizeof(U32) * 0x100 ] = {
390 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
391 "%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 */
392 "%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 */
393 "%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 */
394 _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ "%3A\0" "%3B\0" "%3C\0" "%3D\0" "%3E\0" "%3F\0" /* 3: 48 ~ 63 */
395 "%40\0" _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ /* 4: 64 ~ 79 */
396 _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ "%5B\0" "%5C\0" "%5D\0" "%5E\0" _______ /* 5: 80 ~ 95 */
397 "%60\0" _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ /* 6: 96 ~ 111 */
398 _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ _______ "%7B\0" "%7C\0" "%7D\0" _______ "%7F\0" /* 7: 112 ~ 127 */
399 "%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 */
400 "%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 */
401 "%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 */
402 "%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 */
403 "%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 */
404 "%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 */
405 "%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 */
406 "%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 */
407 };
408 #undef _______
409  
410 static
411 347 size_t uri_encode(const char* in, size_t len, char* out, const char* allow, int allow_utf8) {
412 347 size_t i = 0;
413 347 size_t j = 0;
414 size_t k, skip;
415 U8 octet;
416 U32 code;
417  
418 1927 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 1580 347 1927
419 1580 octet = in[i];
420  
421 1580 ▶ if (allow_utf8 && octet & 0xc0) {
Branch: 2 branches
BranchTrueFalseTotal
if (allow_utf8 && octet & 0xc0) { 41 1539 1580
if (allow_utf8 && octet & 0xc0) { 38 3 41
422 38 skip = UTF8SKIP(&in[i]);
423  
424 38 ▶ if (skip > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (skip > 0) { 38 0 38
425 114 ▶ for (k = 0; k < skip; ++k) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < skip; ++k) { 76 38 114
426 76 out[j++] = in[i++];
427 }
428  
429 38 continue;
430 }
431 }
432  
433 1542 ▶ if (char_in_str(octet, allow)) {
Branch: 1 branch
BranchTrueFalseTotal
if (char_in_str(octet, allow)) { 51 1491 1542
434 51 out[j++] = octet;
435 51 ++i;
436 }
437 else {
438 1491 code = ((U32*) uri_encode_tbl)[(unsigned char) octet];
439  
440 1491 ▶ if (code) {
Branch: 1 branch
BranchTrueFalseTotal
if (code) { 371 1120 1491
441 371 *((U32*) &out[j]) = code;
442 371 j += 3;
443 }
444 else {
445 1120 out[j++] = octet;
446 }
447  
448 1491 ++i;
449 }
450 }
451  
452 347 out[j] = '\0';
453  
454 347 return j;
455 }
456  
457 /*-------------------------------------------------------------------------------
458 * Percent decoding
459 -----------------------------------------------------------------------------*/
460  
461 #define __ 0xFF
462 static const unsigned char hex[0x100] = {
463 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 00-0F */
464 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 10-1F */
465 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 20-2F */
466 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,__,__,__,__,__,__, /* 30-3F */
467 __,10,11,12,13,14,15,__,__,__,__,__,__,__,__,__, /* 40-4F */
468 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 50-5F */
469 __,10,11,12,13,14,15,__,__,__,__,__,__,__,__,__, /* 60-6F */
470 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 70-7F */
471 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 80-8F */
472 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* 90-9F */
473 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* A0-AF */
474 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* B0-BF */
475 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* C0-CF */
476 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* D0-DF */
477 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* E0-EF */
478 __,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__, /* F0-FF */
479 };
480 #undef __
481  
482 static inline
483 321 char unhex(const char *in) {
484 321 unsigned char v1 = hex[ (unsigned char) in[0] ];
485 321 unsigned char v2 = hex[ (unsigned char) in[1] ];
486  
487 /* skip invalid hex sequences */
488 321 ▶ if ((v1 | v2) != 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v1 | v2) != 0xFF) { 320 1 321
489 320 return (v1 << 4) | v2;
490 }
491  
492 1 return '\0';
493 }
494  
495 static
496 552 size_t uri_decode(const char *in, size_t len, char *out, const char *ignore) {
497 552 size_t i = 0, j = 0;
498 char decoded;
499  
500 3493 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 2941 552 3493
501 2941 decoded = '\0';
502  
503 2941 switch (in[i]) {
504 5 case '+':
505 5 decoded = ' ';
506 5 ▶ if (!char_in_str(decoded, ignore)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!char_in_str(decoded, ignore)) { 5 0 5
507 5 ++i;
508 5 break;
509 }
510 case '%':
511 249 ▶ if (i + 2 < len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i + 2 < len) { 248 1 249
512 248 decoded = unhex( &in[i + 1] );
513 248 ▶ if (decoded != '\0' && !char_in_str(decoded, ignore)) {
Branch: 2 branches
BranchTrueFalseTotal
if (decoded != '\0' && !char_in_str(decoded, ignore)) { 247 1 248
if (decoded != '\0' && !char_in_str(decoded, ignore)) { 247 0 247
514 247 i += 3;
515 247 break;
516 }
517 }
518 default:
519 2689 decoded = in[i++];
520 }
521  
522 2941 ▶ if (decoded != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (decoded != '\0') { 2941 0 2941
523 2941 out[j++] = decoded;
524 }
525 }
526  
527 552 out[j] = '\0';
528  
529 552 return j;
530 }
531  
532 static
533 64 size_t uri_decode_utf8(const char *in, size_t len, char *out) {
534 64 size_t i = 0, j = 0;
535 char decoded;
536  
537 1076 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 1012 64 1076
538 1012 decoded = '\0';
539  
540 1012 ▶ switch (in[i]) {
Branch: 1 branch
BranchTrueFalseTotal
switch (in[i]) { 73 939 1012
541 73 case '%':
542 73 ▶ if (i + 2 < len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i + 2 < len) { 73 0 73
543 73 decoded = unhex( &in[i + 1] );
544 73 ▶ if (decoded != '\0' && (U32)decoded > 127) {
Branch: 2 branches
BranchTrueFalseTotal
if (decoded != '\0' && (U32)decoded > 127) { 73 0 73
if (decoded != '\0' && (U32)decoded > 127) { 55 18 73
545 55 i += 3;
546 55 break;
547 }
548 }
549 default:
550 957 decoded = in[i++];
551 }
552  
553 1012 ▶ if (decoded != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (decoded != '\0') { 1012 0 1012
554 1012 out[j++] = decoded;
555 }
556 }
557  
558 64 out[j] = '\0';
559  
560 64 return j;
561 }
562  
563 // EOT (end of theft)
564  
565 /*
566 * External API for encode/decode.
567 */
568 static
569 45 SV* encode(pTHX_ SV *in, SV *sv_allowed) {
570 size_t ilen, olen, alen;
571 const char *allowed;
572 SV* out;
573  
574 45 ▶ if (!is_defined(aTHX_ in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_defined(aTHX_ in)) { 2 43 45
575 2 return newSVpvn("", 0);
576 }
577  
578 43 const char *src = SvPV_nomg_const(in, ilen);
579 43 char dest[(ilen * 3) + 1];
580  
581 43 ▶ if (sv_allowed == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_allowed == NULL) { 41 2 43
582 41 allowed = "";
583 } else {
584 2 allowed = SvPV_nomg_const(sv_allowed, alen);
585 }
586  
587 43 olen = uri_encode(src, ilen, dest, allowed, 0);
588 43 out = newSVpvn(dest, olen);
589 43 sv_utf8_downgrade(out, FALSE);
590  
591 43 return out;
592 }
593  
594 static
595 18 SV* decode(pTHX_ SV *in) {
596 size_t ilen, olen;
597 const char *src;
598 SV *out;
599  
600 18 ▶ if (!is_defined(aTHX_ in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_defined(aTHX_ in)) { 2 16 18
601 2 return newSVpvn("", 0);
602 }
603  
604 16 ▶ if (DO_UTF8(in)) {
Branch: 2 branches
BranchTrueFalseTotal
if (DO_UTF8(in)) { 0 16 16
if (DO_UTF8(in)) { 0 0 0
605 0 in = sv_mortalcopy(in);
606 0 SvUTF8_on(in);
607  
608 0 ▶ if (!sv_utf8_downgrade(in, TRUE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sv_utf8_downgrade(in, TRUE)) { 0 0 0
609 0 croak("decode: wide character in input octet string");
610 }
611  
612 0 src = SvPV_nomg_const(in, ilen);
613 }
614 else {
615 16 src = SvPV_nomg_const(in, ilen);
616 }
617  
618 16 char dest[ilen + 1];
619 16 olen = uri_decode(src, ilen, dest, "");
620 16 out = newSVpvn(dest, olen);
621 16 sv_utf8_decode(out);
622  
623 16 return out;
624 }
625  
626  
627 /*------------------------------------------------------------------------------
628 * Query string parsing
629 -----------------------------------------------------------------------------*/
630  
631 typedef enum {
632 KEY = 1,
633 PARAM = 2,
634 DONE = 3,
635 } uri_query_token_type_t;
636  
637 typedef struct {
638 uri_query_token_type_t type;
639 const char *key; size_t key_length;
640 const char *value; size_t value_length; // only present when type=PARAM
641 } uri_query_token_t;
642  
643 typedef struct {
644 size_t length;
645 size_t cursor;
646 const char *source;
647 } uri_query_scanner_t;
648  
649 // Initializes a uri_query_scanner_t with input string *source of at least
650 // length characters. It is the caller's responsibility to ensure the lifetime
651 // of source matches the lifetime of the scanner.
652 215 void query_scanner_init(
653 uri_query_scanner_t *scanner,
654 const char *source,
655 size_t length
656 )
657 {
658 215 scanner->source = source;
659 215 scanner->length = length;
660 215 scanner->cursor = 0;
661 215 }
662  
663 // Returns true if the scanner has reached the end of the input string.
664 static
665 516 int query_scanner_done(uri_query_scanner_t *scanner) {
666 516 return scanner->cursor >= scanner->length;
667 }
668  
669 /*
670 * Fills the token struct with the next token information. Does not decode
671 * any values.
672 */
673 static
674 301 void query_scanner_next(uri_query_scanner_t *scanner, uri_query_token_t *token) {
675 size_t brk;
676 301 const char key_sep[4] = {'&', ';', '=', '\0'};
677 301 const char val_sep[4] = {'&', ';', '\0'};
678  
679 306 SCAN_KEY:
680 306 ▶ if (scanner->cursor >= scanner->length) {
Branch: 1 branch
BranchTrueFalseTotal
if (scanner->cursor >= scanner->length) { 5 301 306
681 5 token->key = NULL; token->key_length = 0;
682 5 token->value = NULL; token->value_length = 0;
683 5 token->type = DONE;
684 5 return;
685 }
686  
687 // Scan to end of token
688 301 brk = strncspn(
689 301 &scanner->source[ scanner->cursor ],
690 301 scanner->length - scanner->cursor,
691 key_sep
692 );
693  
694 // Set key members in token struct
695 301 token->key = &scanner->source[ scanner->cursor ];
696 301 token->key_length = brk;
697  
698 // Move cursor to end of token
699 301 scanner->cursor += brk;
700  
701 // If there is an associate value, add it to the token
702 301 ▶ if (scanner->source[ scanner->cursor ] == '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (scanner->source[ scanner->cursor ] == '=') { 267 34 301
703 // Advance past '='
704 267 ++scanner->cursor;
705  
706 // Find the end of the value
707 267 brk = strncspn(&scanner->source[ scanner->cursor ], scanner->length - scanner->cursor, val_sep);
708  
709 // Set the value and token type
710 267 token->value = &scanner->source[ scanner->cursor ];
711 267 token->value_length = brk;
712 267 token->type = PARAM;
713  
714 // Move cursor to the end of the value, eating the separator terminating it
715 267 scanner->cursor += brk + 1;
716 }
717 // No value assigned to this key
718 else {
719 34 token->type = KEY;
720 34 ++scanner->cursor; // advance past terminating separator
721 }
722  
723 // No key was found; try again
724 301 ▶ if (token->key_length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (token->key_length == 0) { 5 296 301
725 5 goto SCAN_KEY;
726 }
727  
728 296 return;
729 }
730  
731  
732 /*------------------------------------------------------------------------------
733 * URI parsing
734 -----------------------------------------------------------------------------*/
735  
736 typedef struct {
737 U8 is_iri;
738 uri_str_t *scheme;
739 uri_str_t *query;
740 uri_str_t *path;
741 uri_str_t *host;
742 uri_str_t *port;
743 uri_str_t *frag;
744 uri_str_t *usr;
745 uri_str_t *pwd;
746 } uri_t;
747  
748 /*
749 * Scans the authorization portion of the URI string
750 */
751 static
752 212 void uri_scan_auth(pTHX_ uri_t* uri, const char* auth, const size_t len) {
753 212 size_t idx = 0;
754 212 size_t brk1 = 0;
755 212 size_t brk2 = 0;
756 size_t i;
757 unsigned char flag;
758  
759 212 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 203 9 212
760 // Credentials
761 203 brk1 = strncspn(&auth[idx], len - idx, "@");
762  
763 203 ▶ if (brk1 > 0 && brk1 != (len - idx)) {
Branch: 2 branches
BranchTrueFalseTotal
if (brk1 > 0 && brk1 != (len - idx)) { 203 0 203
if (brk1 > 0 && brk1 != (len - idx)) { 21 182 203
764 21 brk2 = strncspn(&auth[idx], len - idx, ":");
765  
766 21 ▶ if (brk2 > 0 && brk2 < brk1) {
Branch: 2 branches
BranchTrueFalseTotal
if (brk2 > 0 && brk2 < brk1) { 21 0 21
if (brk2 > 0 && brk2 < brk1) { 21 0 21
767 // user
768 21 str_set(aTHX_ uri->usr, &auth[idx], brk2);
769 21 idx += brk2 + 1;
770  
771 // password
772 21 str_set(aTHX_ uri->pwd, &auth[idx], brk1 - brk2 - 1);
773 21 idx += brk1 - brk2;
774 }
775 else {
776 // user only
777 0 str_set(aTHX_ uri->usr, &auth[idx], brk1);
778 0 idx += brk1 + 1;
779 }
780 }
781  
782 // Location
783  
784 // Maybe an IPV6 address
785 203 flag = 0;
786 203 ▶ if (auth[idx] == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (auth[idx] == '[') { 1 202 203
787 1 brk1 = strncspn(&auth[idx], len - idx, "]");
788  
789 1 ▶ if (auth[idx + brk1] == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (auth[idx + brk1] == ']') { 1 0 1
790 // Copy, including the square brackets
791 1 str_set(aTHX_ uri->host, &auth[idx], brk1 + 1);
792 1 idx += brk1 + 1;
793 1 flag = 1;
794 }
795 }
796  
797 203 ▶ if (flag == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (flag == 0) { 202 1 203
798 202 brk1 = strncspn(&auth[idx], len - idx, ":");
799  
800 202 ▶ if (brk1 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk1 > 0) { 202 0 202
801 202 str_set(aTHX_ uri->host, &auth[idx], brk1);
802 202 idx += brk1;
803 }
804 }
805  
806 203 ▶ if (auth[idx] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (auth[idx] == ':') { 25 178 203
807 25 ++idx;
808 25 str_set(aTHX_ uri->port, &auth[idx], len - idx);
809 }
810 }
811 212 }
812  
813 /*
814 * Scans a URI string and populates the uri_t struct.
815 *
816 * Correct:
817 * scheme:[//[usr[:pwd]@]host[:port]]path[?query][#fragment]
818 *
819 * Incorrect but supported:
820 * /path[?query][#fragment]
821 *
822 */
823 static
824 532 void uri_scan(pTHX_ uri_t *uri, const char *src, size_t len) {
825 532 size_t idx = 0;
826 size_t brk;
827 size_t i;
828  
829 541 ▶ while (my_isspace(src[idx]) == 1) ++idx; // Trim leading whitespace
Branch: 1 branch
BranchTrueFalseTotal
while (my_isspace(src[idx]) == 1) ++idx; // Trim leading whitespace 9 532 541
830 541 ▶ while (my_isspace(src[len - 1]) == 1) --len; // Trim trailing whitespace
Branch: 1 branch
BranchTrueFalseTotal
while (my_isspace(src[len - 1]) == 1) --len; // Trim trailing whitespace 9 532 541
831  
832 // Scheme
833 532 brk = strncspn(&src[idx], len - idx, ":/@?#");
834  
835 532 ▶ if (brk > 0 && src[idx + brk] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (brk > 0 && src[idx + brk] == ':') { 312 220 532
if (brk > 0 && src[idx + brk] == ':') { 220 92 312
836 220 str_set(aTHX_ uri->scheme, &src[idx], brk);
837 220 idx += brk + 1;
838  
839 // Authority section following scheme must be separated by //
840 220 ▶ if (idx + 1 < len && src[idx] == '/' && src[idx + 1] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (idx + 1 < len && src[idx] == '/' && src[idx + 1] == '/') { 219 1 220
if (idx + 1 < len && src[idx] == '/' && src[idx + 1] == '/') { 203 16 219
if (idx + 1 < len && src[idx] == '/' && src[idx + 1] == '/') { 203 0 203
841 203 idx += 2;
842  
843 // Authority
844 203 brk = strncspn(&src[idx], len - idx, "/?#");
845 203 uri_scan_auth(aTHX_ uri, &src[idx], brk);
846  
847 203 ▶ if (brk > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk > 0) { 195 8 203
848 195 idx += brk;
849 }
850 }
851 }
852  
853 // path
854 532 brk = strncspn(&src[idx], len - idx, "?#");
855 532 ▶ if (brk > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk > 0) { 276 256 532
856 276 str_set(aTHX_ uri->path, &src[idx], brk);
857 276 idx += brk;
858 }
859  
860 // query
861 532 ▶ if (src[idx] == '?') {
Branch: 1 branch
BranchTrueFalseTotal
if (src[idx] == '?') { 186 346 532
862 186 ++idx; // skip past ?
863 186 brk = strncspn(&src[idx], len - idx, "#");
864 186 ▶ if (brk > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk > 0) { 184 2 186
865 184 str_set(aTHX_ uri->query, &src[idx], brk);
866 184 idx += brk;
867 }
868 }
869  
870 // fragment
871 532 ▶ if (src[idx] == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (src[idx] == '#') { 49 483 532
872 49 ++idx; // skip past #
873 49 brk = len - idx;
874 49 ▶ if (brk > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk > 0) { 46 3 49
875 46 str_set(aTHX_ uri->frag, &src[idx], brk);
876 }
877 }
878 532 }
879  
880 /*------------------------------------------------------------------------------
881 *
882 * Perl API
883 *
884 -----------------------------------------------------------------------------*/
885  
886 /*------------------------------------------------------------------------------
887 * Clearers
888 -----------------------------------------------------------------------------*/
889  
890 8 ▶URI_SIMPLE_CLEARER(scheme);
Branch: 2 branches
BranchTrueFalseTotal
URI_SIMPLE_CLEARER(scheme); 8 0 8
URI_SIMPLE_CLEARER(scheme); 0 8 8
891 1 ▶URI_SIMPLE_CLEARER(path);
Branch: 2 branches
BranchTrueFalseTotal
URI_SIMPLE_CLEARER(path); 1 0 1
URI_SIMPLE_CLEARER(path); 0 1 1
892 5 ▶URI_SIMPLE_CLEARER(query);
Branch: 2 branches
BranchTrueFalseTotal
URI_SIMPLE_CLEARER(query); 5 0 5
URI_SIMPLE_CLEARER(query); 0 5 5
893 1 ▶URI_SIMPLE_CLEARER(frag);
Branch: 2 branches
BranchTrueFalseTotal
URI_SIMPLE_CLEARER(frag); 1 0 1
URI_SIMPLE_CLEARER(frag); 0 1 1
894 9 ▶URI_SIMPLE_CLEARER(usr);
Branch: 2 branches
BranchTrueFalseTotal
URI_SIMPLE_CLEARER(usr); 9 0 9
URI_SIMPLE_CLEARER(usr); 0 9 9
895 9 ▶URI_SIMPLE_CLEARER(pwd);
Branch: 2 branches
BranchTrueFalseTotal
URI_SIMPLE_CLEARER(pwd); 9 0 9
URI_SIMPLE_CLEARER(pwd); 0 9 9
896 9 ▶URI_SIMPLE_CLEARER(host);
Branch: 2 branches
BranchTrueFalseTotal
URI_SIMPLE_CLEARER(host); 9 0 9
URI_SIMPLE_CLEARER(host); 0 9 9
897 9 ▶URI_SIMPLE_CLEARER(port);
Branch: 2 branches
BranchTrueFalseTotal
URI_SIMPLE_CLEARER(port); 9 0 9
URI_SIMPLE_CLEARER(port); 0 9 9
898  
899 static
900 8 void clear_auth(pTHX_ SV *uri_obj) {
901 8 clear_usr(aTHX_ uri_obj);
902 8 clear_pwd(aTHX_ uri_obj);
903 8 clear_host(aTHX_ uri_obj);
904 8 clear_port(aTHX_ uri_obj);
905 8 }
906  
907 /*------------------------------------------------------------------------------
908 * Raw getters
909 -----------------------------------------------------------------------------*/
910  
911 // Raw getters
912 0 ▶URI_RAW_GETTER(scheme);
Branch: 5 branches
BranchTrueFalseTotal
URI_RAW_GETTER(scheme); 0 0 0
URI_RAW_GETTER(scheme); 0 0 0
URI_RAW_GETTER(scheme); 0 0 0
URI_RAW_GETTER(scheme); 0 0 0
URI_RAW_GETTER(scheme); 0 0 0
913 1 ▶URI_RAW_GETTER(usr);
Branch: 5 branches
BranchTrueFalseTotal
URI_RAW_GETTER(usr); 1 0 1
URI_RAW_GETTER(usr); 0 1 1
URI_RAW_GETTER(usr); 0 1 1
URI_RAW_GETTER(usr); 0 0 0
URI_RAW_GETTER(usr); 1 0 1
914 1 ▶URI_RAW_GETTER(pwd);
Branch: 5 branches
BranchTrueFalseTotal
URI_RAW_GETTER(pwd); 1 0 1
URI_RAW_GETTER(pwd); 0 1 1
URI_RAW_GETTER(pwd); 0 1 1
URI_RAW_GETTER(pwd); 0 0 0
URI_RAW_GETTER(pwd); 1 0 1
915 1 ▶URI_RAW_GETTER(host);
Branch: 5 branches
BranchTrueFalseTotal
URI_RAW_GETTER(host); 1 0 1
URI_RAW_GETTER(host); 0 1 1
URI_RAW_GETTER(host); 0 1 1
URI_RAW_GETTER(host); 0 0 0
URI_RAW_GETTER(host); 1 0 1
916 0 ▶URI_RAW_GETTER(port);
Branch: 5 branches
BranchTrueFalseTotal
URI_RAW_GETTER(port); 0 0 0
URI_RAW_GETTER(port); 0 0 0
URI_RAW_GETTER(port); 0 0 0
URI_RAW_GETTER(port); 0 0 0
URI_RAW_GETTER(port); 0 0 0
917 2 ▶URI_RAW_GETTER(path);
Branch: 5 branches
BranchTrueFalseTotal
URI_RAW_GETTER(path); 2 0 2
URI_RAW_GETTER(path); 0 2 2
URI_RAW_GETTER(path); 0 2 2
URI_RAW_GETTER(path); 0 0 0
URI_RAW_GETTER(path); 2 0 2
918 2 ▶URI_RAW_GETTER(query);
Branch: 5 branches
BranchTrueFalseTotal
URI_RAW_GETTER(query); 2 0 2
URI_RAW_GETTER(query); 0 2 2
URI_RAW_GETTER(query); 0 2 2
URI_RAW_GETTER(query); 0 0 0
URI_RAW_GETTER(query); 2 0 2
919 0 ▶URI_RAW_GETTER(frag);
Branch: 5 branches
BranchTrueFalseTotal
URI_RAW_GETTER(frag); 0 0 0
URI_RAW_GETTER(frag); 0 0 0
URI_RAW_GETTER(frag); 0 0 0
URI_RAW_GETTER(frag); 0 0 0
URI_RAW_GETTER(frag); 0 0 0
920  
921 static
922 168 SV* get_raw_auth(pTHX_ SV *uri_obj) {
923 168 ▶ uri_t *uri = URI(uri_obj);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(uri_obj); 168 0 168
uri_t *uri = URI(uri_obj); 0 168 168
924 168 SV *out = newSVpvn("", 0);
925  
926 168 ▶ if (uri->is_iri) {
Branch: 1 branch
BranchTrueFalseTotal
if (uri->is_iri) { 4 164 168
927 4 SvUTF8_on(out);
928 }
929  
930 168 ▶ if (str_len(uri->usr) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->usr) > 0) { 26 142 168
931 26 ▶ if (str_len(uri->pwd) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->pwd) > 0) { 26 0 26
932 26 ▶ sv_catpvn(out, str_get(uri->usr), str_len(uri->usr));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->usr), str_len(uri->usr)); 26 0 26
933 26 sv_catpvn(out, ":", 1);
934 26 ▶ sv_catpvn(out, str_get(uri->pwd), str_len(uri->pwd));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->pwd), str_len(uri->pwd)); 26 0 26
935 26 sv_catpvn(out, "@", 1);
936 } else {
937 0 ▶ sv_catpvn(out, str_get(uri->usr), str_len(uri->usr));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->usr), str_len(uri->usr)); 0 0 0
938 0 sv_catpvn(out, "@", 1);
939 }
940 }
941  
942 168 ▶ if (str_len(uri->host) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->host) > 0) { 142 26 168
943 142 ▶ if (str_len(uri->port) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->port) > 0) { 26 116 142
944 26 ▶ sv_catpvn(out, str_get(uri->host), str_len(uri->host));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->host), str_len(uri->host)); 26 0 26
945 26 sv_catpvn(out, ":", 1);
946 26 ▶ sv_catpvn(out, str_get(uri->port), str_len(uri->port));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->port), str_len(uri->port)); 26 0 26
947 } else {
948 116 ▶ sv_catpvn(out, str_get(uri->host), str_len(uri->host));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->host), str_len(uri->host)); 116 0 116
949 }
950 }
951  
952 168 return out;
953 }
954  
955 /*------------------------------------------------------------------------------
956 * Decoding getters
957 -----------------------------------------------------------------------------*/
958  
959 135 ▶URI_SIMPLE_GETTER(scheme);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_GETTER(scheme); 135 0 135
URI_SIMPLE_GETTER(scheme); 0 135 135
URI_SIMPLE_GETTER(scheme); 79 56 135
960 119 ▶URI_SIMPLE_GETTER(usr);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_GETTER(usr); 119 0 119
URI_SIMPLE_GETTER(usr); 0 119 119
URI_SIMPLE_GETTER(usr); 94 25 119
961 117 ▶URI_SIMPLE_GETTER(pwd);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_GETTER(pwd); 117 0 117
URI_SIMPLE_GETTER(pwd); 0 117 117
URI_SIMPLE_GETTER(pwd); 94 23 117
962 131 ▶URI_SIMPLE_GETTER(host);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_GETTER(host); 131 0 131
URI_SIMPLE_GETTER(host); 0 131 131
URI_SIMPLE_GETTER(host); 76 55 131
963 116 ▶URI_SIMPLE_GETTER(port);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_GETTER(port); 116 0 116
URI_SIMPLE_GETTER(port); 0 116 116
URI_SIMPLE_GETTER(port); 93 23 116
964 108 ▶URI_SIMPLE_GETTER(frag);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_GETTER(frag); 108 0 108
URI_SIMPLE_GETTER(frag); 0 108 108
URI_SIMPLE_GETTER(frag); 77 31 108
965  
966 23 ▶URI_COMPOUND_GETTER(path);
Branch: 3 branches
BranchTrueFalseTotal
URI_COMPOUND_GETTER(path); 23 0 23
URI_COMPOUND_GETTER(path); 0 23 23
URI_COMPOUND_GETTER(path); 3 20 23
967 49 ▶URI_COMPOUND_GETTER(query);
Branch: 3 branches
BranchTrueFalseTotal
URI_COMPOUND_GETTER(query); 49 0 49
URI_COMPOUND_GETTER(query); 0 49 49
URI_COMPOUND_GETTER(query); 5 44 49
968  
969 static
970 19 SV* get_auth(pTHX_ SV *uri_obj) {
971 19 ▶ uri_t *uri = URI(uri_obj);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(uri_obj); 19 0 19
uri_t *uri = URI(uri_obj); 0 19 19
972 19 SV *out = newSVpvn("", 0);
973  
974 19 ▶ if (uri->is_iri) {
Branch: 1 branch
BranchTrueFalseTotal
if (uri->is_iri) { 0 19 19
975 0 SvUTF8_on(out);
976 }
977  
978 19 ▶ if (str_len(uri->usr) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->usr) > 0) { 11 8 19
979 11 ▶ if (str_len(uri->pwd) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->pwd) > 0) { 10 1 11
980 10 sv_catsv(out, sv_2mortal(get_usr(aTHX_ uri_obj)));
981 10 sv_catpvn(out, ":", 1);
982 10 sv_catsv(out, sv_2mortal(get_pwd(aTHX_ uri_obj)));
983 10 sv_catpvn(out, "@", 1);
984 } else {
985 1 sv_catsv(out, sv_2mortal(get_usr(aTHX_ uri_obj)));
986 1 sv_catpvn(out, "@", 1);
987 }
988 }
989  
990 19 ▶ if (str_len(uri->host) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->host) > 0) { 17 2 19
991 17 ▶ if (str_len(uri->port) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->port) > 0) { 10 7 17
992 10 sv_catsv(out, sv_2mortal(get_host(aTHX_ uri_obj)));
993 10 sv_catpvn(out, ":", 1);
994 10 sv_catsv(out, sv_2mortal(get_port(aTHX_ uri_obj)));
995 } else {
996 7 sv_catsv(out, sv_2mortal(get_host(aTHX_ uri_obj)));
997 }
998 }
999  
1000 19 return out;
1001 }
1002  
1003 static
1004 97 SV* split_path(pTHX_ SV* sv_uri, int include_leading) {
1005 97 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 97 0 97
uri_t *uri = URI(sv_uri); 0 97 97
1006 97 size_t len, segment_len, brk, idx = 0;
1007 97 AV* arr = newAV();
1008 SV* tmp;
1009  
1010 97 const char *str = uri->path->string;
1011 97 len = uri->path->length;
1012  
1013 97 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 69 28 97
1014 69 ▶ if (str[0] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (str[0] == '/') { 41 28 69
1015 41 ▶ if (include_leading) {
Branch: 1 branch
BranchTrueFalseTotal
if (include_leading) { 1 40 41
1016 1 tmp = newSVpvn("", 0);
1017 1 av_push(arr, tmp);
1018 }
1019  
1020 41 ++idx; // skip past leading /
1021 }
1022  
1023 173 ▶ while (idx < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < len) { 104 69 173
1024 // Find the next separator
1025 104 brk = strcspn(&str[idx], "/");
1026  
1027 // Decode the segment
1028 104 char segment[brk + 1];
1029 104 segment_len = uri_decode(&str[idx], brk, segment, "");
1030  
1031 // Push new SV to AV
1032 104 tmp = newSVpvn(segment, segment_len);
1033 104 sv_utf8_decode(tmp);
1034 104 av_push(arr, tmp);
1035  
1036 104 idx += brk + 1;
1037 }
1038 }
1039  
1040 97 return newRV_noinc((SV*) arr);
1041 }
1042  
1043 static
1044 9 SV* get_query_keys(pTHX_ SV* sv_uri) {
1045 9 ▶ uri_str_t *str_query = URI(sv_uri)->query;
Branch: 2 branches
BranchTrueFalseTotal
uri_str_t *str_query = URI(sv_uri)->query; 9 0 9
uri_str_t *str_query = URI(sv_uri)->query; 0 9 9
1046 9 const char *query = str_query->string;
1047 9 size_t klen, qlen = str_query->length;
1048 9 HV* out = newHV();
1049 uri_query_scanner_t scanner;
1050 uri_query_token_t token;
1051  
1052 9 query_scanner_init(&scanner, query, qlen);
1053  
1054 29 ▶ while (!query_scanner_done(&scanner)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!query_scanner_done(&scanner)) { 20 9 29
1055 20 query_scanner_next(&scanner, &token);
1056 20 ▶ if (token.type == DONE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == DONE) continue; 0 20 20
1057 20 char key[token.key_length + 1];
1058 20 klen = uri_decode(token.key, token.key_length, key, "");
1059 20 hv_store(out, key, -klen, &PL_sv_undef, 0);
1060 }
1061  
1062 9 return newRV_noinc((SV*) out);
1063 }
1064  
1065 static
1066 62 SV* query_hash(pTHX_ SV *sv_uri) {
1067 62 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 62 0 62
uri_t *uri = URI(sv_uri); 0 62 62
1068 SV *tmp, **refval;
1069 AV *arr;
1070 62 HV *out = newHV();
1071 size_t klen, vlen;
1072  
1073 uri_query_scanner_t scanner;
1074 uri_query_token_t token;
1075  
1076 62 query_scanner_init(&scanner, uri->query->string, uri->query->length);
1077  
1078 133 ▶ while (!query_scanner_done(&scanner)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!query_scanner_done(&scanner)) { 71 62 133
1079 71 query_scanner_next(&scanner, &token);
1080 71 ▶ if (token.type == DONE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == DONE) continue; 2 69 71
1081  
1082 // Get decoded key
1083 69 char key[token.key_length + 1];
1084 69 klen = uri_decode(token.key, token.key_length, key, "");
1085  
1086 // Values are stored in an array; this block is the rough equivalent of:
1087 // $out{$key} = [] unless exists $out{$key};
1088 69 ▶ if (!hv_exists(out, key, klen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_exists(out, key, klen)) { 61 8 69
1089 61 arr = newAV();
1090 61 hv_store(out, key, -klen, newRV_noinc((SV*) arr), 0);
1091 }
1092 else {
1093 8 refval = hv_fetch(out, key, -klen, 0);
1094 8 ▶ if (refval == NULL) croak("query_hash: something went wrong");
Branch: 1 branch
BranchTrueFalseTotal
if (refval == NULL) croak("query_hash: something went wrong"); 0 8 8
1095 8 arr = (AV*) SvRV(*refval);
1096 }
1097  
1098 // Get decoded value if there is one
1099 69 ▶ if (token.type == PARAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == PARAM) { 61 8 69
1100 61 char val[token.value_length + 1];
1101 61 vlen = uri_decode(token.value, token.value_length, val, "");
1102 61 tmp = newSVpvn(val, vlen);
1103 61 sv_utf8_decode(tmp);
1104 61 av_push(arr, tmp);
1105 }
1106 }
1107  
1108 62 return newRV_noinc((SV*) out);
1109 }
1110  
1111 static
1112 72 SV* get_param(pTHX_ SV* sv_uri, SV* sv_key) {
1113 72 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 72 0 72
uri_t *uri = URI(sv_uri); 0 72 72
1114 size_t klen, vlen, elen;
1115 const char *key;
1116 uri_query_scanner_t scanner;
1117 uri_query_token_t token;
1118 72 AV* out = newAV();
1119 SV* value;
1120  
1121 // Read key to search
1122 72 ▶ if (!is_defined(aTHX_ sv_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_defined(aTHX_ sv_key)) { 1 71 72
1123 1 croak("get_param: expected key to search");
1124 }
1125 else {
1126 // Copy input string *before* calling DO_UTF8() in case the SV is an object
1127 // with string overloading, which may trigger the utf8 flag.
1128 71 key = SvPV_const(sv_key, klen);
1129  
1130 71 ▶ if (!DO_UTF8(sv_key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!DO_UTF8(sv_key)) { 5 66 71
if (!DO_UTF8(sv_key)) { 0 5 5
1131 66 sv_key = sv_2mortal(newSVpvn(key, klen));
1132 66 sv_utf8_encode(sv_key);
1133 66 key = SvPV_const(sv_key, klen);
1134 }
1135 }
1136  
1137 71 char enc_key[(klen * 3) + 2];
1138 71 elen = uri_encode(key, klen, enc_key, ":@?/", uri->is_iri);
1139  
1140 71 query_scanner_init(&scanner, uri->query->string, uri->query->length);
1141  
1142 195 ▶ while (!query_scanner_done(&scanner)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!query_scanner_done(&scanner)) { 124 71 195
1143 124 query_scanner_next(&scanner, &token);
1144 124 ▶ if (token.type == DONE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == DONE) continue; 3 121 124
1145  
1146 121 ▶ if (strncmp(enc_key, token.key, maxnum(elen, token.key_length)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(enc_key, token.key, maxnum(elen, token.key_length)) == 0) { 71 50 121
1147 71 ▶ if (token.type == PARAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == PARAM) { 69 2 71
1148 69 char val[token.value_length + 1];
1149 69 vlen = uri_decode(token.value, token.value_length, val, "");
1150 69 value = newSVpvn(val, vlen);
1151 69 sv_utf8_decode(value);
1152 69 av_push(out, value);
1153 }
1154 else {
1155 2 av_push(out, newSV(0));
1156 }
1157 }
1158 }
1159  
1160 71 return newRV_noinc((SV*) out);
1161 }
1162  
1163 /*------------------------------------------------------------------------------
1164 * Setters
1165 -----------------------------------------------------------------------------*/
1166  
1167 13 ▶URI_SIMPLE_SETTER(scheme, "");
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_SETTER(scheme, ""); 7 0 7
URI_SIMPLE_SETTER(scheme, ""); 0 7 7
URI_SIMPLE_SETTER(scheme, ""); 6 1 7
1168 37 ▶URI_SIMPLE_SETTER(path, URI_CHARS_PATH);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_SETTER(path, URI_CHARS_PATH); 19 0 19
URI_SIMPLE_SETTER(path, URI_CHARS_PATH); 0 19 19
URI_SIMPLE_SETTER(path, URI_CHARS_PATH); 18 1 19
1169 13 ▶URI_SIMPLE_SETTER(query, URI_CHARS_QUERY);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_SETTER(query, URI_CHARS_QUERY); 7 0 7
URI_SIMPLE_SETTER(query, URI_CHARS_QUERY); 0 7 7
URI_SIMPLE_SETTER(query, URI_CHARS_QUERY); 6 1 7
1170 15 ▶URI_SIMPLE_SETTER(frag, URI_CHARS_FRAG);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_SETTER(frag, URI_CHARS_FRAG); 8 0 8
URI_SIMPLE_SETTER(frag, URI_CHARS_FRAG); 0 8 8
URI_SIMPLE_SETTER(frag, URI_CHARS_FRAG); 7 1 8
1171 11 ▶URI_SIMPLE_SETTER(usr, URI_CHARS_USER);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_SETTER(usr, URI_CHARS_USER); 6 0 6
URI_SIMPLE_SETTER(usr, URI_CHARS_USER); 0 6 6
URI_SIMPLE_SETTER(usr, URI_CHARS_USER); 5 1 6
1172 9 ▶URI_SIMPLE_SETTER(pwd, URI_CHARS_USER);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_SETTER(pwd, URI_CHARS_USER); 5 0 5
URI_SIMPLE_SETTER(pwd, URI_CHARS_USER); 0 5 5
URI_SIMPLE_SETTER(pwd, URI_CHARS_USER); 4 1 5
1173 13 ▶URI_SIMPLE_SETTER(host, URI_CHARS_HOST);
Branch: 3 branches
BranchTrueFalseTotal
URI_SIMPLE_SETTER(host, URI_CHARS_HOST); 7 0 7
URI_SIMPLE_SETTER(host, URI_CHARS_HOST); 0 7 7
URI_SIMPLE_SETTER(host, URI_CHARS_HOST); 6 1 7
1174  
1175 static
1176 5 void set_port(pTHX_ SV *sv_uri, SV *sv_value) {
1177 5 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 5 0 5
uri_t *uri = URI(sv_uri); 0 5 5
1178 5 ▶ if (!is_defined(aTHX_ sv_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_defined(aTHX_ sv_value)) { 1 4 5
1179 1 str_clear(aTHX_ uri->port);
1180 1 return;
1181 }
1182  
1183 size_t vlen, i;
1184 4 const char *value = SvPV_const(sv_value, vlen);
1185 4 str_set(aTHX_ uri->port, value, vlen);
1186 }
1187  
1188 static
1189 10 void set_auth(pTHX_ SV *sv_uri, SV *sv_value) {
1190 10 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 10 0 10
uri_t *uri = URI(sv_uri); 0 10 10
1191  
1192 10 str_clear(aTHX_ uri->usr);
1193 10 str_clear(aTHX_ uri->pwd);
1194 10 str_clear(aTHX_ uri->host);
1195 10 str_clear(aTHX_ uri->port);
1196  
1197 10 ▶ if (is_defined(aTHX_ sv_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_defined(aTHX_ sv_value)) { 9 1 10
1198 size_t vlen;
1199 9 const char *value = SvPV_const(sv_value, vlen);
1200  
1201 9 ▶ if (vlen > URI_SIZE_auth) {
Branch: 1 branch
BranchTrueFalseTotal
if (vlen > URI_SIZE_auth) { 0 9 9
1202 0 croak("set_auth: size of auth string exceeds max of %lu", URI_SIZE_auth);
1203 }
1204  
1205 // auth isn't stored as an individual field, so encode to local array and rescan
1206 char auth[URI_SIZE_auth];
1207 9 size_t len = uri_encode(value, vlen, (char*) &auth, URI_CHARS_AUTH, uri->is_iri);
1208  
1209 9 uri_scan_auth(aTHX_ uri, auth, len);
1210 }
1211 10 }
1212  
1213 static
1214 10 void set_path_array(pTHX_ SV *sv_uri, SV *sv_path) {
1215 10 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 10 0 10
uri_t *uri = URI(sv_uri); 0 10 10
1216 SV **refval, *tmp;
1217 AV *av_path;
1218 size_t i, av_idx, seg_len;
1219 const char *seg;
1220 10 uri_str_t *path = uri->path;
1221  
1222 10 str_clear(aTHX_ path);
1223  
1224 10 ▶ if (!is_defined(aTHX_ sv_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_defined(aTHX_ sv_path)) { 1 9 10
1225 2 return;
1226 }
1227  
1228 // Inspect input array
1229 9 av_path = (AV*) SvRV(sv_path);
1230 9 av_idx = av_top_index(av_path);
1231  
1232 9 ▶ if (av_idx == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_idx == -1) { 1 8 9
1233 1 return;
1234 }
1235  
1236 // Build the new path
1237 29 ▶ for (i = 0; i <= av_idx; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_idx; ++i) { 21 8 29
1238 // Add separator. If the next value fetched from the array is invalid, it
1239 // just gets an empty segment.
1240 21 str_append(aTHX_ path, "/", 1);
1241  
1242 // Fetch next segment
1243 21 refval = av_fetch(av_path, (SSize_t) i, 0);
1244 21 ▶ if (refval == NULL) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (refval == NULL) continue; 0 21 21
1245 21 ▶ if (!is_defined(aTHX_ *refval)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_defined(aTHX_ *refval)) continue; 0 21 21
1246  
1247 // Copy value over
1248 21 ▶ if (is_defined(aTHX_ *refval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_defined(aTHX_ *refval)) { 21 0 21
1249 21 seg = SvPV_nomg_const(*refval, seg_len);
1250  
1251 // Convert octets to utf8 if necessary
1252 21 ▶ if (!DO_UTF8(*refval)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!DO_UTF8(*refval)) { 1 20 21
if (!DO_UTF8(*refval)) { 0 1 1
1253 20 tmp = sv_2mortal(newSVpvn(seg, seg_len));
1254 20 sv_utf8_encode(tmp);
1255 20 seg = SvPV_const(tmp, seg_len);
1256 }
1257  
1258 21 char out[seg_len * 3 + 1];
1259 21 size_t out_len = uri_encode(seg, seg_len, out, URI_CHARS_PATH_SEGMENT, uri->is_iri);
1260 21 str_append(aTHX_ path, out, out_len);
1261 }
1262 }
1263 }
1264  
1265 static
1266 11 void update_query_keyset(pTHX_ SV *sv_uri, SV *sv_key_set, SV *sv_separator) {
1267 11 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 11 0 11
uri_t *uri = URI(sv_uri); 0 11 11
1268 HE *ent;
1269 HV *keys, *enc_keys;
1270 I32 iterlen, i, klen;
1271 SV *val, **refval;
1272 bool copy;
1273 char *key;
1274 11 size_t off = 0;
1275 11 uri_str_t *query = uri->query;
1276 11 uri_str_t *dest = str_new(aTHX_ URI_SIZE_query);
1277  
1278 11 size_t slen = 1;
1279 21 ▶ const char *separator = is_defined(aTHX_ sv_separator) ? SvPV_const(sv_separator, slen) : "&";
Branch: 1 branch
BranchTrueFalseTotal
const char *separator = is_defined(aTHX_ sv_separator) ? SvPV_const(sv_separator, slen) : "&"; 10 1 11
1280  
1281 uri_query_scanner_t scanner;
1282 uri_query_token_t token;
1283  
1284 // Validate reference parameters
1285 11 ▶ if (!is_ref(aTHX_ sv_key_set) || SvTYPE(SvRV(sv_key_set)) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_ref(aTHX_ sv_key_set) || SvTYPE(SvRV(sv_key_set)) != SVt_PVHV) { 11 0 11
if (!is_ref(aTHX_ sv_key_set) || SvTYPE(SvRV(sv_key_set)) != SVt_PVHV) { 0 11 11
1286 0 croak("set_query_keys: expected hash ref");
1287 }
1288  
1289 // Dereference key set hash
1290 11 keys = (HV*) SvRV(sv_key_set);
1291  
1292 // Create new HV with all keys uri-encoded
1293 11 enc_keys = newHV();
1294 11 iterlen = hv_iterinit(keys);
1295  
1296 27 ▶ for (i = 0; i < iterlen; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < iterlen; ++i) { 16 11 27
1297 16 ent = hv_iternext(keys);
1298 16 key = hv_iterkey(ent, &klen);
1299 16 val = hv_iterval(keys, ent);
1300  
1301 16 SvGETMAGIC(val);
1302  
1303 16 ▶ if (klen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (klen > 0) { 15 1 16
1304 15 char enc_key[(3 * klen) + 1];
1305 15 klen = uri_encode(key, klen, enc_key, ":@?/", uri->is_iri);
1306 15 ▶ hv_store(enc_keys, enc_key, klen * (uri->is_iri ? -1 : 1), val, 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store(enc_keys, enc_key, klen * (uri->is_iri ? -1 : 1), val, 0); 0 15 15
1307 }
1308 }
1309  
1310 // Begin building the new query string from the existing one. As each key is
1311 // encountered in the query string, exclude ones with a falsish value in the
1312 // hash and keep the ones with a truish value. Any not present in the hash
1313 // are kept unchanged.
1314 11 ▶ query_scanner_init(&scanner, str_get(query), str_len(query));
Branch: 1 branch
BranchTrueFalseTotal
query_scanner_init(&scanner, str_get(query), str_len(query)); 8 3 11
1315  
1316 30 ▶ while (!query_scanner_done(&scanner)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!query_scanner_done(&scanner)) { 19 11 30
1317 19 query_scanner_next(&scanner, &token);
1318 19 ▶ if (token.type == DONE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == DONE) continue; 0 19 19
1319  
1320 // Use the encoded keys hash to decide whether to copy this key (and
1321 // value if present) over to dest. If the key exists, skip. It will be
1322 // added to the filtered query string last.
1323 19 copy = 1;
1324 19 ▶ if (hv_exists(enc_keys, token.key, token.key_length * (uri->is_iri ? -1 : 1))) {
Branch: 2 branches
BranchTrueFalseTotal
if (hv_exists(enc_keys, token.key, token.key_length * (uri->is_iri ? -1 : 1))) { 0 19 19
if (hv_exists(enc_keys, token.key, token.key_length * (uri->is_iri ? -1 : 1))) { 6 13 19
1325 6 ▶ refval = hv_fetch(enc_keys, token.key, token.key_length * (uri->is_iri ? -1 : 1), 0);
Branch: 1 branch
BranchTrueFalseTotal
refval = hv_fetch(enc_keys, token.key, token.key_length * (uri->is_iri ? -1 : 1), 0); 0 6 6
1326 // NULL shouldn't be possible since this is guarded with hv_exists, but
1327 // perlguts, amirite?
1328 6 ▶ copy = refval == NULL || SvTRUE(*refval);
Branch: 2 branches
BranchTrueFalseTotal
copy = refval == NULL || SvTRUE(*refval); 6 0 6
copy = refval == NULL || SvTRUE(*refval); 0 6 6
1329 }
1330  
1331 19 ▶ if (copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (copy) { 13 6 19
1332 13 ▶ if (off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (off > 0) { 5 8 13
1333 5 str_append(aTHX_ dest, separator, slen);
1334 5 off += slen;
1335 }
1336  
1337 13 str_append(aTHX_ dest, token.key, token.key_length);
1338 13 off += token.key_length;
1339  
1340 13 ▶ if (token.type == PARAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == PARAM) { 4 9 13
1341 4 str_append(aTHX_ dest, "=", 1);
1342 4 str_append(aTHX_ dest, token.value, token.value_length);
1343 4 off += token.value_length;
1344 }
1345 }
1346 }
1347  
1348 // Walk through the encoded-key hash, adding remaining keys.
1349 11 iterlen = hv_iterinit(enc_keys);
1350  
1351 26 ▶ for (i = 0; i < iterlen; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < iterlen; ++i) { 15 11 26
1352 15 ent = hv_iternext(enc_keys);
1353 15 key = hv_iterkey(ent, &klen);
1354 15 val = hv_iterval(enc_keys, ent);
1355  
1356 15 ▶ if (SvTRUE(val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(val)) { 9 6 15
1357 // Add separator if the new query string is not empty
1358 9 ▶ if (off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (off > 0) { 7 2 9
1359 7 str_append(aTHX_ dest, separator, slen);
1360 7 off += slen;
1361 }
1362  
1363 9 str_append(aTHX_ dest, key, klen);
1364 9 off += klen;
1365 }
1366 }
1367  
1368 11 str_free(aTHX_ query);
1369 11 uri->query = dest;
1370 11 }
1371  
1372 static
1373 64 void set_param(pTHX_ SV *sv_uri, SV *sv_key, SV *sv_values, SV *sv_separator) {
1374 64 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 64 0 64
uri_t *uri = URI(sv_uri); 0 64 64
1375 const char *strval;
1376 64 size_t vlen, reflen, av_idx, i = 0, off = 0;
1377 AV *av_values;
1378 SV **refval;
1379 64 uri_str_t *dest = str_new(aTHX_ URI_SIZE_query);
1380 uri_query_scanner_t scanner;
1381 uri_query_token_t token;
1382  
1383 64 size_t slen = 1;
1384 124 ▶ const char *separator = is_defined(aTHX_ sv_separator) ? SvPV_const(sv_separator, slen) : "&";
Branch: 1 branch
BranchTrueFalseTotal
const char *separator = is_defined(aTHX_ sv_separator) ? SvPV_const(sv_separator, slen) : "&"; 60 4 64
1385  
1386 // Build encoded key string
1387 64 ▶ if (!is_defined(aTHX_ sv_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_defined(aTHX_ sv_key)) { 1 63 64
1388 1 croak("set_param: expected key");
1389 }
1390  
1391 size_t klen;
1392 63 const char *key = SvPV_const(sv_key, klen);
1393 63 char enc_key[(3 * klen) + 1];
1394 63 klen = uri_encode(key, strlen(key), enc_key, ":@?/", uri->is_iri);
1395  
1396 // Get array of values to set
1397 63 ▶ if (!is_ref(aTHX_ sv_values) || SvTYPE(SvRV(sv_values)) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_ref(aTHX_ sv_values) || SvTYPE(SvRV(sv_values)) != SVt_PVAV) { 62 1 63
if (!is_ref(aTHX_ sv_values) || SvTYPE(SvRV(sv_values)) != SVt_PVAV) { 0 62 62
1398 1 croak("set_param: expected array of values");
1399 }
1400  
1401 62 av_values = (AV*) SvRV(sv_values);
1402 62 av_idx = av_top_index(av_values);
1403  
1404 // Begin building the new query string from the existing one, skipping
1405 // keys (and their values, if any) matching sv_key.
1406 62 query_scanner_init(&scanner, uri->query->string, uri->query->length);
1407  
1408 129 ▶ while (!query_scanner_done(&scanner)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!query_scanner_done(&scanner)) { 67 62 129
1409 67 query_scanner_next(&scanner, &token);
1410 67 ▶ if (token.type == DONE) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == DONE) continue; 0 67 67
1411  
1412 // The key does not match the key being set
1413 67 ▶ if (strncmp(enc_key, token.key, maxnum(klen, token.key_length)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(enc_key, token.key, maxnum(klen, token.key_length)) != 0) { 28 39 67
1414 // Add separator if this is not the first key being written
1415 28 ▶ if (off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (off > 0) { 7 21 28
1416 7 str_append(aTHX_ dest, separator, slen);
1417 7 off += slen;
1418 }
1419  
1420 // Write the key to the buffer
1421 28 str_append(aTHX_ dest, token.key, token.key_length);
1422 28 off += token.key_length;
1423  
1424 // The key has a value
1425 28 ▶ if (token.type == PARAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (token.type == PARAM) { 28 0 28
1426 28 str_append(aTHX_ dest, "=", 1);
1427  
1428 // If the value's length is 0, it was parsed from "key=", so the value
1429 // is not written after the '=' is added above.
1430 28 ▶ if (token.value_length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (token.value_length > 0) { 28 0 28
1431 // Otherwise, write the value to the buffer
1432 28 str_append(aTHX_ dest, token.value, token.value_length);
1433 28 off += token.value_length;
1434 }
1435 }
1436 }
1437 }
1438  
1439 // Add the new values to the query
1440 135 ▶ for (i = 0; i <= av_idx; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_idx; ++i) { 82 53 135
1441 // Fetch next value from the array
1442 82 refval = av_fetch(av_values, (SSize_t) i, 0);
1443 82 ▶ if (refval == NULL) break;
Branch: 1 branch
BranchTrueFalseTotal
if (refval == NULL) break; 9 73 82
1444 73 ▶ if (!is_defined(aTHX_ *refval)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_defined(aTHX_ *refval)) break; 0 73 73
1445  
1446 // Add separator if needed to separate pairs
1447 73 ▶ if (off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (off > 0) { 35 38 73
1448 35 str_append(aTHX_ dest, separator, slen);
1449 35 off += slen;
1450 }
1451  
1452 // Copy key over
1453 73 str_append(aTHX_ dest, enc_key, klen);
1454 73 off += klen;
1455  
1456 73 str_append(aTHX_ dest, "=", 1);
1457  
1458 // Copy value over
1459 73 SvGETMAGIC(*refval);
1460 73 strval = SvPV_const(*refval, reflen);
1461  
1462 73 char tmp[reflen * 3 + 1];
1463 73 vlen = uri_encode(strval, reflen, tmp, ":@?/", uri->is_iri);
1464 73 str_append(aTHX_ dest, tmp, vlen);
1465 73 off += vlen;
1466 }
1467  
1468 62 str_free(aTHX_ uri->query);
1469 62 uri->query = dest;
1470 62 }
1471  
1472 /*------------------------------------------------------------------------------
1473 * Other stuff
1474 -----------------------------------------------------------------------------*/
1475  
1476 static
1477 167 SV* to_string(pTHX_ SV *uri_obj) {
1478 167 ▶ uri_t *uri = URI(uri_obj);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(uri_obj); 167 0 167
uri_t *uri = URI(uri_obj); 0 167 167
1479 167 SV *out = newSVpvn("", 0);
1480 167 SV *auth = sv_2mortal(get_raw_auth(aTHX_ uri_obj));
1481  
1482 167 ▶ if (uri->is_iri) {
Branch: 1 branch
BranchTrueFalseTotal
if (uri->is_iri) { 4 163 167
1483 4 SvUTF8_on(out);
1484 }
1485  
1486 167 ▶ if (str_len(uri->scheme) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->scheme) > 0) { 144 23 167
1487 144 ▶ sv_catpvn(out, str_get(uri->scheme), str_len(uri->scheme));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->scheme), str_len(uri->scheme)); 144 0 144
1488 144 sv_catpvn(out, ":", 1);
1489  
1490 144 ▶ if (SvTRUE(auth)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(auth)) { 141 3 144
1491 // When the authority section is present, the scheme must be followed by
1492 // two forward slashes
1493 141 sv_catpvn(out, "//", 2);
1494 }
1495 }
1496  
1497 167 ▶ if (SvTRUE(auth)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(auth)) { 141 26 167
1498 141 sv_catsv(out, auth);
1499  
1500 // When the authority section is present, any path must be separated from
1501 // the authority section by a forward slash
1502 141 ▶ if (str_len(uri->path) > 0 && (str_get(uri->path))[0] != '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (str_len(uri->path) > 0 && (str_get(uri->path))[0] != '/') { 101 40 141
if (str_len(uri->path) > 0 && (str_get(uri->path))[0] != '/') { 101 0 101
if (str_len(uri->path) > 0 && (str_get(uri->path))[0] != '/') { 5 96 101
1503 5 sv_catpvn(out, "/", 1);
1504 }
1505 }
1506  
1507 167 ▶ sv_catpvn(out, str_get(uri->path), str_len(uri->path));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->path), str_len(uri->path)); 115 52 167
1508  
1509 167 ▶ if (str_len(uri->query) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->query) > 0) { 68 99 167
1510 68 sv_catpvn(out, "?", 1);
1511 68 ▶ sv_catpvn(out, str_get(uri->query), str_len(uri->query));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->query), str_len(uri->query)); 68 0 68
1512 }
1513  
1514 167 ▶ if (str_len(uri->frag) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_len(uri->frag) > 0) { 36 131 167
1515 36 sv_catpvn(out, "#", 1);
1516 36 ▶ sv_catpvn(out, str_get(uri->frag), str_len(uri->frag));
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(out, str_get(uri->frag), str_len(uri->frag)); 36 0 36
1517 }
1518  
1519 167 return out;
1520 }
1521  
1522 static
1523 0 void explain(pTHX_ SV* sv_uri) {
1524 0 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 0 0 0
uri_t *uri = URI(sv_uri); 0 0 0
1525 0 printf("scheme: %s\n", uri->scheme->string);
1526 0 printf("auth:\n");
1527 0 printf(" -usr: %s\n", uri->usr->string);
1528 0 printf(" -pwd: %s\n", uri->pwd->string);
1529 0 printf(" -host: %s\n", uri->host->string);
1530 0 printf(" -port: %s\n", uri->port->string);
1531 0 printf("path: %s\n", uri->path->string);
1532 0 printf("query: %s\n", uri->query->string);
1533 0 printf("frag: %s\n", uri->frag->string);
1534 0 }
1535  
1536 static
1537 0 void debug(pTHX_ SV* sv_uri) {
1538 0 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 0 0 0
uri_t *uri = URI(sv_uri); 0 0 0
1539 0 warn("scheme: %s\n", uri->scheme->string);
1540 0 warn("auth:\n");
1541 0 warn(" -usr: %s\n", uri->usr->string);
1542 0 warn(" -pwd: %s\n", uri->pwd->string);
1543 0 warn(" -host: %s\n", uri->host->string);
1544 0 warn(" -port: %s\n", uri->port->string);
1545 0 warn("path: %s\n", uri->path->string);
1546 0 warn("query: %s\n", uri->query->string);
1547 0 warn("frag: %s\n", uri->frag->string);
1548 0 }
1549  
1550 static
1551 532 SV* new(pTHX_ const char* class, SV* uri_str, int is_iri) {
1552 const char* src;
1553 size_t len;
1554 uri_t* uri;
1555 SV* obj;
1556 SV* obj_ref;
1557  
1558 // Initialize the struct
1559 532 Newx(uri, 1, uri_t);
1560 532 Zero(uri, 1, uri_t);
1561  
1562 532 uri->is_iri = is_iri;
1563 532 uri->scheme = str_new(aTHX_ URI_SIZE_scheme);
1564 532 uri->usr = str_new(aTHX_ URI_SIZE_usr);
1565 532 uri->pwd = str_new(aTHX_ URI_SIZE_pwd);
1566 532 uri->host = str_new(aTHX_ URI_SIZE_host);
1567 532 uri->port = str_new(aTHX_ URI_SIZE_port);
1568 532 uri->path = str_new(aTHX_ URI_SIZE_path);
1569 532 uri->query = str_new(aTHX_ URI_SIZE_query);
1570 532 uri->frag = str_new(aTHX_ URI_SIZE_frag);
1571  
1572 // Build the blessed instance
1573 532 obj = newSViv((IV) uri);
1574 532 obj_ref = newRV_noinc(obj);
1575 532 sv_bless(obj_ref, gv_stashpv(class, GV_ADD));
1576 532 SvREADONLY_on(obj);
1577  
1578 // Scan the input string to fill the struct
1579 532 ▶ if (!SvTRUE(uri_str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(uri_str)) { 147 385 532
1580 147 src = "";
1581 147 len = 0;
1582 }
1583 else {
1584 // Copy input string *before* calling DO_UTF8() in case the SV is an object
1585 // with string overloading, which may trigger the utf8 flag.
1586 385 src = SvPV_nomg_const(uri_str, len);
1587  
1588 // Ensure the pv bytes are utf8-encoded
1589 385 ▶ if (!DO_UTF8(uri_str)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!DO_UTF8(uri_str)) { 1 384 385
if (!DO_UTF8(uri_str)) { 0 1 1
1590 384 uri_str = sv_2mortal(newSVpvn(src, len));
1591 384 sv_utf8_encode(uri_str);
1592 384 src = SvPV_const(uri_str, len);
1593 }
1594 }
1595  
1596 532 uri_scan(aTHX_ uri, src, len);
1597  
1598 532 return obj_ref;
1599 }
1600  
1601 static
1602 532 void DESTROY(pTHX_ SV *sv_uri) {
1603 532 ▶ uri_t *uri = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(sv_uri); 532 0 532
uri_t *uri = URI(sv_uri); 0 532 532
1604 532 str_free(aTHX_ uri->scheme);
1605 532 str_free(aTHX_ uri->usr);
1606 532 str_free(aTHX_ uri->pwd);
1607 532 str_free(aTHX_ uri->host);
1608 532 str_free(aTHX_ uri->port);
1609 532 str_free(aTHX_ uri->path);
1610 532 str_free(aTHX_ uri->query);
1611 532 str_free(aTHX_ uri->frag);
1612 532 Safefree(uri);
1613 532 }
1614  
1615 /*
1616 * Extras
1617 */
1618  
1619 /*
1620 * Splits a uri string into its component sections: scheme, authority, path,
1621 * query, fragment. Pushes those values directly onto the results stack.
1622 */
1623 static
1624 27 void uri_split(pTHX_ SV *uri) {
1625 27 dXSARGS;
1626 27 sp = mark;
1627  
1628 // If the object has already been parsed, there is no need to reparse it.
1629 27 ▶ if (sv_isobject(uri) && sv_derived_from(uri, "URI::Fast")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(uri) && sv_derived_from(uri, "URI::Fast")) { 2 25 27
if (sv_isobject(uri) && sv_derived_from(uri, "URI::Fast")) { 1 1 2
1630 1 ▶ XPUSHs(sv_2mortal(get_scheme(aTHX_ uri)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(get_scheme(aTHX_ uri))); 0 1 1
1631 1 ▶ XPUSHs(sv_2mortal(get_auth(aTHX_ uri)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(get_auth(aTHX_ uri))); 0 1 1
1632 1 ▶ XPUSHs(sv_2mortal(get_path(aTHX_ uri)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(get_path(aTHX_ uri))); 0 1 1
1633 1 ▶ XPUSHs(sv_2mortal(get_query(aTHX_ uri)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(get_query(aTHX_ uri))); 0 1 1
1634 1 ▶ XPUSHs(sv_2mortal(get_frag(aTHX_ uri)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(get_frag(aTHX_ uri))); 0 1 1
1635 }
1636 // The object is defined
1637 26 ▶ else if (SvOK(uri)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOK(uri)) { 24 2 26
1638 const char *src;
1639 24 size_t idx = 0;
1640 24 size_t brk = 0;
1641 size_t len;
1642  
1643 24 ▶ if (!SvTRUE(uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(uri)) { 0 24 24
1644 0 src = "";
1645 0 len = 0;
1646 }
1647 else {
1648 // Copy string into new SV
1649 24 SV* str = sv_2mortal(newSV(0));
1650 24 sv_copypv(str, ST(0));
1651 24 src = SvPV_const(str, len);
1652  
1653 24 ▶ if (!DO_UTF8(str)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!DO_UTF8(str)) { 0 24 24
if (!DO_UTF8(str)) { 0 0 0
1654 24 sv_utf8_encode(str);
1655 24 src = SvPV_const(str, len);
1656 }
1657 }
1658  
1659 // Scheme
1660 24 brk = strcspn(&src[idx], ":/@?#");
1661  
1662 24 ▶ if (brk > 0 && src[idx + brk] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (brk > 0 && src[idx + brk] == ':') { 23 1 24
if (brk > 0 && src[idx + brk] == ':') { 20 3 23
1663 20 ▶ XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk))); 0 20 20
1664 20 idx += brk + 1;
1665  
1666 // Authority section following scheme must be separated by //
1667 20 ▶ if (idx + 1 < len && src[idx] == '/' && src[idx + 1] == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (idx + 1 < len && src[idx] == '/' && src[idx + 1] == '/') { 20 0 20
if (idx + 1 < len && src[idx] == '/' && src[idx + 1] == '/') { 20 0 20
if (idx + 1 < len && src[idx] == '/' && src[idx + 1] == '/') { 20 0 20
1668 20 idx += 2;
1669  
1670 // Authority
1671 20 brk = strcspn(&src[idx], "/?#");
1672  
1673 20 ▶ if (brk > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk > 0) { 19 1 20
1674 19 ▶ XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk))); 0 19 19
1675 19 idx += brk;
1676 }
1677 else {
1678 1 ▶ XPUSHs(sv_2mortal(newSVpvn("", 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn("", 0))); 0 1 1
1679 }
1680 }
1681 }
1682 else {
1683 4 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 4 4
1684 4 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 4 4
1685 }
1686  
1687 // path
1688 24 brk = strcspn(&src[idx], "?#");
1689 24 ▶ if (brk > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk > 0) { 20 4 24
1690 20 ▶ XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk))); 0 20 20
1691 20 idx += brk;
1692 } else {
1693 4 ▶ XPUSHs(sv_2mortal(newSVpvn("", 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn("", 0))); 0 4 4
1694 }
1695  
1696 // query
1697 24 ▶ if (src[idx] == '?') {
Branch: 1 branch
BranchTrueFalseTotal
if (src[idx] == '?') { 10 14 24
1698 10 ++idx; // skip past ?
1699 10 brk = strcspn(&src[idx], "#");
1700 10 ▶ if (brk > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk > 0) { 10 0 10
1701 10 ▶ XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk))); 0 10 10
1702 10 idx += brk;
1703 } else {
1704 0 ▶ XPUSHs(sv_2mortal(newSVpvn("", 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn("", 0))); 0 0 0
1705 }
1706 } else {
1707 14 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 14 14
1708 }
1709  
1710 // fragment
1711 24 ▶ if (src[idx] == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (src[idx] == '#') { 8 16 24
1712 8 ++idx; // skip past #
1713 8 brk = len - idx;
1714 8 ▶ if (brk > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (brk > 0) { 8 0 8
1715 8 ▶ XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(&src[idx], brk))); 0 8 8
1716 } else {
1717 0 ▶ XPUSHs(sv_2mortal(newSVpvn("", 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn("", 0))); 0 0 0
1718 }
1719 } else {
1720 16 ▶ XPUSHs(&PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(&PL_sv_undef); 0 16 16
1721 }
1722 }
1723  
1724 27 PUTBACK;
1725 27 }
1726  
1727 /*
1728 * Collapses dotted segments in a path string based on the rules defined in RFC
1729 * 3986 section 5.2.
1730 */
1731 static
1732 85 void remove_dot_segments(pTHX_ uri_str_t *out, const char *path, size_t len) {
1733 85 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 8 77 85
1734 8 return;
1735 }
1736  
1737 77 size_t brk, idx = 0;
1738 77 char in[len];
1739 77 Copy(path, in, len + 1, char);
1740  
1741 357 ▶ while (idx < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < len) { 280 77 357
1742 // in begins with "./" or "../": ignore prefix completely
1743 280 ▶ if (strncmp(&in[idx], "./", 2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(&in[idx], "./", 2) == 0) { 0 280 280
1744 0 idx += 2;
1745 }
1746 280 ▶ else if (strncmp(&in[idx], "../", 3) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(&in[idx], "../", 3) == 0) { 0 280 280
1747 0 idx += 3;
1748 }
1749  
1750 // in begins with /./: replace with /
1751 280 ▶ else if (strncmp(&in[idx], "/./", 3) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(&in[idx], "/./", 3) == 0) { 11 269 280
1752 11 idx += 2; // inc to the final / in /./ instead of editing the buffer
1753 }
1754  
1755 // in begins with /. and . is a complete segment: replace with /
1756 269 ▶ else if (strncmp(&in[idx], "/.", 2) == 0 && idx + 2 == len) {
Branch: 2 branches
BranchTrueFalseTotal
else if (strncmp(&in[idx], "/.", 2) == 0 && idx + 2 == len) { 45 224 269
else if (strncmp(&in[idx], "/.", 2) == 0 && idx + 2 == len) { 4 41 45
1757 4 idx += 1;
1758 4 in[idx] = '/';
1759 }
1760  
1761 // in begins with /../: replace with /, remove final segment from out
1762 265 ▶ else if (strncmp(&in[idx], "/../", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(&in[idx], "/../", 4) == 0) { 33 232 265
1763 33 idx += 3; // inc to the final / in /./ instead of editing the buffer
1764 33 str_rtrim(aTHX_ out, '/');
1765 }
1766  
1767 // in begins with /.. and .. is a complete $in segment: replace with /, remove final segment from out
1768 232 ▶ else if (strncmp(&in[idx], "/..", 3) == 0 && idx + 3 == len) {
Branch: 2 branches
BranchTrueFalseTotal
else if (strncmp(&in[idx], "/..", 3) == 0 && idx + 3 == len) { 7 225 232
else if (strncmp(&in[idx], "/..", 3) == 0 && idx + 3 == len) { 6 1 7
1769 6 idx += 2;
1770 6 in[idx] = '/';
1771 6 str_rtrim(aTHX_ out, '/');
1772 }
1773  
1774 // in is "." or "..": done
1775 226 ▶ else if ((in[idx] == '.' && idx + 1 == len)
Branch: 2 branches
BranchTrueFalseTotal
else if ((in[idx] == '.' && idx + 1 == len) 0 226 226
else if ((in[idx] == '.' && idx + 1 == len) 0 0 0
1776 226 ▶ || (in[idx] == '.' && in[idx + 1] == '.' && idx + 2 == len)) {
Branch: 3 branches
BranchTrueFalseTotal
|| (in[idx] == '.' && in[idx + 1] == '.' && idx + 2 == len)) { 0 226 226
|| (in[idx] == '.' && in[idx + 1] == '.' && idx + 2 == len)) { 0 0 0
|| (in[idx] == '.' && in[idx + 1] == '.' && idx + 2 == len)) { 0 0 0
1777 break;
1778 }
1779  
1780 // else copy everything up to but not including the next '/' from in to out
1781 else {
1782 226 ▶ if (in[idx] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (in[idx] == '/') { 226 0 226
1783 226 brk = minnum(len - idx, 1 + strncspn(&in[idx + 1], len - idx, "/"));
1784 }
1785 else {
1786 0 brk = strncspn(&in[idx], len - idx, "/");
1787 }
1788  
1789 226 str_append(aTHX_ out, &in[idx], brk);
1790 226 idx += brk;
1791 }
1792 }
1793 }
1794  
1795 /*------------------------------------------------------------------------------
1796 * Absolution
1797 *
1798 * As defined in https://www.rfc-editor.org/rfc/rfc3986.txt section 5.2
1799 *----------------------------------------------------------------------------*/
1800 static
1801 87 void absolute(pTHX_ SV *sv_target, SV *sv_uri, SV *sv_base) {
1802 87 ▶ uri_t *rel = URI(sv_uri);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *rel = URI(sv_uri); 87 0 87
uri_t *rel = URI(sv_uri); 0 87 87
1803 87 ▶ uri_t *base = URI(sv_base);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *base = URI(sv_base); 87 0 87
uri_t *base = URI(sv_base); 0 87 87
1804 87 ▶ uri_t *target = URI(sv_target);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *target = URI(sv_target); 87 0 87
uri_t *target = URI(sv_target); 0 87 87
1805  
1806 87 const char *class = class_name(aTHX_ sv_target);
1807  
1808 // Relative URIs may begin with // to indicate an authority section without a
1809 // scheme, which is illegal in standard URI syntax (authority may only come
1810 // after a scheme, which is required, separated by //). This workaround helps
1811 // the parser along by identifying the authority section as such.
1812 87 ▶ if (rel->scheme->length == 0
Branch: 1 branch
BranchTrueFalseTotal
if (rel->scheme->length == 0 87 0 87
1813 87 ▶ && rel->host->length == 0
Branch: 1 branch
BranchTrueFalseTotal
&& rel->host->length == 0 87 0 87
1814 87 ▶ && rel->path->length >= 2
Branch: 1 branch
BranchTrueFalseTotal
&& rel->path->length >= 2 58 29 87
1815 58 ▶ && strncmp(rel->path->string, "//", 2) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& strncmp(rel->path->string, "//", 2) == 0) 3 55 58
1816 {
1817 3 SV *fixed = newSVpvn("x:", 2);
1818 3 sv_catsv(fixed, sv_2mortal(to_string(aTHX_ sv_uri)));
1819  
1820 3 SV *sv_tmp = sv_2mortal(new(aTHX_ class, sv_2mortal(fixed), 0));
1821 3 ▶ rel = URI(sv_tmp);
Branch: 2 branches
BranchTrueFalseTotal
rel = URI(sv_tmp); 3 0 3
rel = URI(sv_tmp); 0 3 3
1822  
1823 3 str_clear(aTHX_ rel->scheme);
1824 }
1825  
1826 87 ▶ if (rel->scheme->length != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rel->scheme->length != 0) { 0 87 87
1827 0 remove_dot_segments(aTHX_ target->path, rel->path->string, rel->path->length);
1828 0 str_copy(aTHX_ rel->scheme, target->scheme);
1829 0 str_copy(aTHX_ rel->usr, target->usr);
1830 0 str_copy(aTHX_ rel->pwd, target->pwd);
1831 0 str_copy(aTHX_ rel->host, target->host);
1832 0 str_copy(aTHX_ rel->port, target->port);
1833 0 str_copy(aTHX_ rel->query, target->query);
1834 }
1835 else {
1836 87 ▶ if (rel->usr->length > 0 || rel->host->length > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (rel->usr->length > 0 || rel->host->length > 0) { 87 0 87
if (rel->usr->length > 0 || rel->host->length > 0) { 3 84 87
1837 3 remove_dot_segments(aTHX_ target->path, rel->path->string, rel->path->length);
1838 3 str_copy(aTHX_ rel->usr, target->usr);
1839 3 str_copy(aTHX_ rel->pwd, target->pwd);
1840 3 str_copy(aTHX_ rel->host, target->host);
1841 3 str_copy(aTHX_ rel->port, target->port);
1842 3 str_copy(aTHX_ rel->query, target->query);
1843 }
1844 else {
1845 84 ▶ if (rel->path->length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rel->path->length == 0) { 9 75 84
1846 9 str_copy(aTHX_ base->path, target->path);
1847  
1848 9 ▶ if (rel->query->length != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rel->query->length != 0) { 3 6 9
1849 3 str_copy(aTHX_ rel->query, target->query);
1850 } else {
1851 6 str_copy(aTHX_ base->query, target->query);
1852 }
1853 }
1854 else {
1855 75 ▶ if (rel->path->string[0] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (rel->path->string[0] == '/') { 5 70 75
1856 5 remove_dot_segments(aTHX_ target->path, rel->path->string, rel->path->length);
1857 }
1858 else {
1859 70 uri_str_t *merged = str_new(aTHX_ rel->path->length + base->path->length);
1860  
1861 70 ▶ if (base->scheme->length > 0 && base->path->length == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (base->scheme->length > 0 && base->path->length == 0) { 70 0 70
if (base->scheme->length > 0 && base->path->length == 0) { 0 70 70
1862 0 str_append(aTHX_ merged, "/", 1);
1863 0 str_append(aTHX_ merged, rel->path->string, rel->path->length);
1864 }
1865 else {
1866 70 ▶ if (str_index(aTHX_ base->path, "/", 1) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_index(aTHX_ base->path, "/", 1) >= 0) { 70 0 70
1867 // truncate base path at right-most /, inclusive
1868 70 str_append(aTHX_ merged, base->path->string, base->path->length);
1869 70 str_rtrim(aTHX_ merged, '/');
1870 } else {
1871 // if there is no / in the base path, truncate it completely
1872 }
1873  
1874 70 str_append(aTHX_ merged, "/", 1);
1875 70 str_append(aTHX_ merged, rel->path->string, rel->path->length);
1876 }
1877  
1878 70 remove_dot_segments(aTHX_ target->path, merged->string, merged->length);
1879 70 str_free(aTHX_ merged);
1880 }
1881  
1882 75 str_copy(aTHX_ rel->query, target->query);
1883 }
1884  
1885 84 str_copy(aTHX_ base->usr, target->usr);
1886 84 str_copy(aTHX_ base->pwd, target->pwd);
1887 84 str_copy(aTHX_ base->host, target->host);
1888 84 str_copy(aTHX_ base->port, target->port);
1889 }
1890  
1891 87 str_copy(aTHX_ base->scheme, target->scheme);
1892 }
1893  
1894 87 str_copy(aTHX_ rel->frag, target->frag);
1895 87 }
1896  
1897 /*
1898 * Uppercases a 3-digit hex sequence, if present, in the first 3 indices of
1899 * *buf. It is the caller's responsibility to ensure that *buf is at least 3
1900 * chars in length.
1901 */
1902 static inline
1903 62 bool uc_hex_3ch(pTHX_ char *buf) {
1904 62 ▶ if (buf[0] != '%') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (buf[0] != '%') return 0; 56 6 62
1905 6 ▶ buf[1] = toUPPER(buf[1]);
Branch: 1 branch
BranchTrueFalseTotal
buf[1] = toUPPER(buf[1]); 0 6 6
1906 6 ▶ buf[2] = toUPPER(buf[2]);
Branch: 1 branch
BranchTrueFalseTotal
buf[2] = toUPPER(buf[2]); 6 0 6
1907 6 return 1;
1908 }
1909  
1910 /*
1911 * Uppercases 3-character hex codes over an entire uri_str_t.
1912 */
1913 static inline
1914 56 void uc_hex(pTHX_ uri_str_t *str) {
1915 56 size_t i = 0;
1916 136 ▶ while (i < str->length) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < str->length) { 80 56 136
1917 80 ▶ if (i + 2 < str->length && uc_hex_3ch(aTHX_ &str->string[i]) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (i + 2 < str->length && uc_hex_3ch(aTHX_ &str->string[i]) == 1) { 62 18 80
if (i + 2 < str->length && uc_hex_3ch(aTHX_ &str->string[i]) == 1) { 6 56 62
1918 6 i += 3;
1919 } else {
1920 74 ++i;
1921 }
1922 }
1923 56 }
1924  
1925 /*
1926 * Performs minimal normalization. Scheme and hostname are lower cased. All
1927 * members are scanned for lower case percent-encoded sequences.
1928 */
1929 static
1930 7 void normalize(pTHX_ SV *uri_obj) {
1931 7 ▶ uri_t *uri = URI(uri_obj);
Branch: 2 branches
BranchTrueFalseTotal
uri_t *uri = URI(uri_obj); 7 0 7
uri_t *uri = URI(uri_obj); 0 7 7
1932 size_t i;
1933  
1934 // (6.2.2.1) lower case scheme
1935 23 ▶ for (i = 0; i < uri->scheme->length; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < uri->scheme->length; ++i) { 16 7 23
1936 16 ▶ uri->scheme->string[i] = toLOWER(uri->scheme->string[i]);
Branch: 1 branch
BranchTrueFalseTotal
uri->scheme->string[i] = toLOWER(uri->scheme->string[i]); 4 12 16
1937 }
1938  
1939 // (6.2.2.1) lower case hostname
1940 53 ▶ for (i = 0; i < uri->host->length; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < uri->host->length; ++i) { 46 7 53
1941 46 ▶ uri->host->string[i] = toLOWER(uri->host->string[i]);
Branch: 1 branch
BranchTrueFalseTotal
uri->host->string[i] = toLOWER(uri->host->string[i]); 7 39 46
1942 }
1943  
1944 // (6.2.2) remove dot segments from path
1945 7 uri_str_t *tmp = str_new(aTHX_ uri->path->length);
1946 7 remove_dot_segments(aTHX_ tmp, uri->path->string, uri->path->length);
1947 7 str_free(aTHX_ uri->path);
1948 7 uri->path = tmp;
1949  
1950 // (6.2.2.1) upper case hex codes in each section of the uri
1951 7 uc_hex(aTHX_ uri->scheme);
1952 7 uc_hex(aTHX_ uri->query);
1953 7 uc_hex(aTHX_ uri->path);
1954 7 uc_hex(aTHX_ uri->host);
1955 7 uc_hex(aTHX_ uri->port);
1956 7 uc_hex(aTHX_ uri->frag);
1957 7 uc_hex(aTHX_ uri->usr);
1958 7 uc_hex(aTHX_ uri->pwd);
1959  
1960 // TODO (6.2.2.2) decode any percent-encoded sequences decoding to unreserved
1961 // characters.
1962 7 }
1963  
1964  
1965 MODULE = URI::Fast PACKAGE = URI::Fast
1966  
1967 PROTOTYPES: DISABLE
1968  
1969 FALLBACK: TRUE
1970  
1971 #-------------------------------------------------------------------------------
1972 # URL-encoding
1973 #-------------------------------------------------------------------------------
1974 SV* encode(in, ...)
1975 SV *in
1976 ALIAS:
1977 uri_encode = 1
1978 url_encode = 2
1979 PREINIT:
1980 45 SV *temp = NULL;
1981 CODE:
1982 45 ▶ if (items > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) { 2 43 45
1983 2 temp = ST(1);
1984 }
1985 45 RETVAL = encode(aTHX_ in, temp);
1986 OUTPUT:
1987 RETVAL
1988  
1989 SV* decode(in)
1990 SV* in
1991 ALIAS:
1992 uri_decode = 1
1993 url_decode = 2
1994 CODE:
1995 18 RETVAL = decode(aTHX_ in);
1996 OUTPUT:
1997 RETVAL
1998  
1999 #-------------------------------------------------------------------------------
2000 # Constructors and destructors
2001 #-------------------------------------------------------------------------------
2002 SV* new(class, uri_str)
2003 const char* class
2004 SV* uri_str
2005 ALIAS:
2006 new_iri = 1
2007 CODE:
2008 11 ▶ if (ix == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 1) { 0 11 11
2009 0 RETVAL = new(aTHX_ "URI::Fast::IRI", uri_str, 1);
2010 } else {
2011 11 RETVAL = new(aTHX_ class, uri_str, 0);
2012 }
2013 OUTPUT:
2014 RETVAL
2015  
2016 void DESTROY(uri_obj)
2017 SV* uri_obj
2018 CODE:
2019 532 DESTROY(aTHX_ uri_obj);
2020  
2021 SV* uri(...)
2022 ALIAS:
2023 iri = 1
2024 clone = 2
2025 PREINIT:
2026 SV *str;
2027 CODE:
2028 428 ▶ if (items > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 0) { 396 32 428
2029 396 ▶ if (sv_isobject(ST(0)) && sv_derived_from(ST(0), "URI::Fast")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(ST(0)) && sv_derived_from(ST(0), "URI::Fast")) { 4 392 396
if (sv_isobject(ST(0)) && sv_derived_from(ST(0), "URI::Fast")) { 3 1 4
2030 3 str = sv_2mortal(to_string(aTHX_ ST(0)));
2031 } else {
2032 393 str = ST(0);
2033 }
2034 }
2035 else {
2036 32 str = sv_2mortal(newSVpvn("", 0));
2037 }
2038  
2039 428 ▶ RETVAL = new(aTHX_ (ix == 1) ? "URI::Fast::IRI" : "URI::Fast", str, (ix == 1) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = new(aTHX_ (ix == 1) ? "URI::Fast::IRI" : "URI::Fast", str, (ix == 1) ? 1 : 0); 1 427 428
2040 OUTPUT:
2041 RETVAL
2042  
2043  
2044 #-------------------------------------------------------------------------------
2045 # Clearers
2046 #-------------------------------------------------------------------------------
2047 void clear_scheme(uri_obj)
2048 SV* uri_obj
2049 CODE:
2050 8 clear_scheme(aTHX_ uri_obj);
2051  
2052 void clear_path(uri_obj)
2053 SV* uri_obj
2054 CODE:
2055 1 clear_path(aTHX_ uri_obj);
2056  
2057 void clear_query (uri_obj)
2058 SV* uri_obj
2059 CODE:
2060 5 clear_query(aTHX_ uri_obj);
2061  
2062 void clear_frag(uri_obj)
2063 SV* uri_obj
2064 CODE:
2065 1 clear_frag(aTHX_ uri_obj);
2066  
2067 void clear_usr(uri_obj)
2068 SV* uri_obj
2069 CODE:
2070 1 clear_usr(aTHX_ uri_obj);
2071  
2072 void clear_pwd(uri_obj)
2073 SV* uri_obj
2074 CODE:
2075 1 clear_pwd(aTHX_ uri_obj);
2076  
2077 void clear_host(uri_obj)
2078 SV* uri_obj
2079 CODE:
2080 1 clear_host(aTHX_ uri_obj);
2081  
2082 void clear_port(uri_obj)
2083 SV* uri_obj
2084 CODE:
2085 1 clear_port(aTHX_ uri_obj);
2086  
2087 void clear_auth(uri_obj)
2088 SV* uri_obj
2089 CODE:
2090 8 clear_auth(aTHX_ uri_obj);
2091  
2092 #-------------------------------------------------------------------------------
2093 # Raw getters
2094 #-------------------------------------------------------------------------------
2095 SV* raw_scheme(uri_obj)
2096 SV *uri_obj
2097 CODE:
2098 0 RETVAL = get_raw_scheme(aTHX_ uri_obj);
2099 OUTPUT:
2100 RETVAL
2101  
2102 SV* raw_auth(uri_obj)
2103 SV *uri_obj
2104 CODE:
2105 1 RETVAL = get_raw_auth(aTHX_ uri_obj);
2106 OUTPUT:
2107 RETVAL
2108  
2109 SV* raw_path(uri_obj)
2110 SV *uri_obj
2111 CODE:
2112 2 RETVAL = get_raw_path(aTHX_ uri_obj);
2113 OUTPUT:
2114 RETVAL
2115  
2116 SV* raw_query(uri_obj)
2117 SV *uri_obj
2118 CODE:
2119 2 RETVAL = get_raw_query(aTHX_ uri_obj);
2120 OUTPUT:
2121 RETVAL
2122  
2123 SV* raw_frag(uri_obj)
2124 SV *uri_obj
2125 CODE:
2126 0 RETVAL = get_raw_frag(aTHX_ uri_obj);
2127 OUTPUT:
2128 RETVAL
2129  
2130 SV* raw_usr(uri_obj)
2131 SV *uri_obj
2132 CODE:
2133 1 RETVAL = get_raw_usr(aTHX_ uri_obj);
2134 OUTPUT:
2135 RETVAL
2136  
2137 SV* raw_pwd(uri_obj)
2138 SV *uri_obj
2139 CODE:
2140 1 RETVAL = get_raw_pwd(aTHX_ uri_obj);
2141 OUTPUT:
2142 RETVAL
2143  
2144 SV* raw_host(uri_obj)
2145 SV *uri_obj
2146 CODE:
2147 1 RETVAL = get_raw_host(aTHX_ uri_obj);
2148 OUTPUT:
2149 RETVAL
2150  
2151 SV* raw_port(uri_obj)
2152 SV *uri_obj
2153 CODE:
2154 0 RETVAL = get_raw_port(aTHX_ uri_obj);
2155 OUTPUT:
2156 RETVAL
2157  
2158  
2159 #-------------------------------------------------------------------------------
2160 # Compound getters
2161 #-------------------------------------------------------------------------------
2162 SV* get_path(uri_obj)
2163 SV *uri_obj
2164 CODE:
2165 22 RETVAL = get_path(aTHX_ uri_obj);
2166 OUTPUT:
2167 RETVAL
2168  
2169 SV* get_query(uri_obj)
2170 SV *uri_obj
2171 CODE:
2172 48 RETVAL = get_query(aTHX_ uri_obj);
2173 OUTPUT:
2174 RETVAL
2175  
2176 SV* get_auth(uri_obj)
2177 SV *uri_obj
2178 CODE:
2179 18 RETVAL = get_auth(aTHX_ uri_obj);
2180 OUTPUT:
2181 RETVAL
2182  
2183 SV* split_path(uri)
2184 SV* uri
2185 ALIAS:
2186 split_path_compat = 1
2187 CODE:
2188 97 RETVAL = split_path(aTHX_ uri, ix == 1 ? 1 : 0);
2189 OUTPUT:
2190 RETVAL
2191  
2192 SV* get_query_keys(uri)
2193 SV* uri
2194 CODE:
2195 9 RETVAL = get_query_keys(aTHX_ uri);
2196 OUTPUT:
2197 RETVAL
2198  
2199 SV* get_query_hash(uri)
2200 SV* uri
2201 CODE:
2202 62 RETVAL = query_hash(aTHX_ uri);
2203 OUTPUT:
2204 RETVAL
2205  
2206 SV* get_param(uri, sv_key)
2207 SV* uri
2208 SV* sv_key
2209 CODE:
2210 72 RETVAL = get_param(aTHX_ uri, sv_key);
2211 OUTPUT:
2212 RETVAL
2213  
2214  
2215 #-------------------------------------------------------------------------------
2216 # Compound setters
2217 #-------------------------------------------------------------------------------
2218 void set_auth(uri_obj, value)
2219 SV *uri_obj
2220 SV *value
2221 CODE:
2222 10 set_auth(aTHX_ uri_obj, value);
2223  
2224 void set_path(uri_obj, value)
2225 SV *uri_obj
2226 SV *value
2227 CODE:
2228 19 set_path(aTHX_ uri_obj, value);
2229  
2230 void set_path_array(uri_obj, segments)
2231 SV *uri_obj
2232 SV *segments
2233 CODE:
2234 10 set_path_array(aTHX_ uri_obj, segments);
2235  
2236 void set_query(uri_obj, value)
2237 SV *uri_obj
2238 SV *value
2239 CODE:
2240 7 set_query(aTHX_ uri_obj, value);
2241  
2242 void set_param(uri, sv_key, sv_values, sv_separator)
2243 SV *uri
2244 SV *sv_key
2245 SV *sv_values
2246 SV *sv_separator
2247 CODE:
2248 64 set_param(aTHX_ uri, sv_key, sv_values, sv_separator);
2249  
2250 void query_keyset(self, sv_key_set, ...)
2251 SV *self
2252 SV *sv_key_set
2253 CODE:
2254 11 ▶ SV *sv_separator = items > 2 ? ST(2) : sv_2mortal(newSVpvn("&", 1));
Branch: 1 branch
BranchTrueFalseTotal
SV *sv_separator = items > 2 ? ST(2) : sv_2mortal(newSVpvn("&", 1)); 2 9 11
2255 11 update_query_keyset(aTHX_ self, sv_key_set, sv_separator);
2256  
2257  
2258 #-------------------------------------------------------------------------------
2259 # Unified accessors
2260 #-------------------------------------------------------------------------------
2261 SV* scheme(self, ...)
2262 SV *self
2263 CODE:
2264 134 ▶ if (items > 1) set_scheme(aTHX_ self, ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) set_scheme(aTHX_ self, ST(1)); 7 127 134
2265 134 RETVAL = get_scheme(aTHX_ self);
2266 OUTPUT:
2267 RETVAL
2268  
2269 SV* usr(self, ...)
2270 SV *self
2271 CODE:
2272 108 ▶ if (items > 1) set_usr(aTHX_ self, ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) set_usr(aTHX_ self, ST(1)); 6 102 108
2273 108 RETVAL = get_usr(aTHX_ self);
2274 OUTPUT:
2275 RETVAL
2276  
2277 SV* pwd(self, ...)
2278 SV *self
2279 CODE:
2280 107 ▶ if (items > 1) set_pwd(aTHX_ self, ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) set_pwd(aTHX_ self, ST(1)); 5 102 107
2281 107 RETVAL = get_pwd(aTHX_ self);
2282 OUTPUT:
2283 RETVAL
2284  
2285 SV* host(self, ...)
2286 SV *self
2287 CODE:
2288 114 ▶ if (items > 1) set_host(aTHX_ self, ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) set_host(aTHX_ self, ST(1)); 7 107 114
2289 114 RETVAL = get_host(aTHX_ self);
2290 OUTPUT:
2291 RETVAL
2292  
2293 SV* port(self, ...)
2294 SV *self
2295 CODE:
2296 106 ▶ if (items > 1) set_port(aTHX_ self, ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) set_port(aTHX_ self, ST(1)); 5 101 106
2297 106 RETVAL = get_port(aTHX_ self);
2298 OUTPUT:
2299 RETVAL
2300  
2301 SV* frag(self, ...)
2302 SV *self
2303 CODE:
2304 107 ▶ if (items > 1) set_frag(aTHX_ self, ST(1));
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) set_frag(aTHX_ self, ST(1)); 8 99 107
2305 107 RETVAL = get_frag(aTHX_ self);
2306 OUTPUT:
2307 RETVAL
2308  
2309  
2310 #-------------------------------------------------------------------------------
2311 # Extras
2312 #-------------------------------------------------------------------------------
2313 SV* to_string(self, ...)
2314 SV *self
2315 ALIAS:
2316 as_string = 1
2317 OVERLOAD:
2318 to_string \"\"
2319 CODE:
2320 161 RETVAL = to_string(aTHX_ self);
2321 OUTPUT:
2322 RETVAL
2323  
2324 SV* normalize(uri)
2325 SV *uri
2326 ALIAS:
2327 canonical = 1
2328 CODE:
2329 7 normalize(aTHX_ uri);
2330 OUTPUT:
2331 uri
2332  
2333 SV* absolute(uri, base)
2334 SV *uri
2335 SV *base
2336 PREINIT:
2337 const char *class;
2338 SV *sv_target;
2339 CODE:
2340 87 class = class_name(aTHX_ uri);
2341 87 sv_target = new(aTHX_ class, sv_2mortal(newSVpvn("", 0)), 0);
2342  
2343 87 ▶ if (!sv_isobject(base) || !sv_derived_from(base, class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!sv_isobject(base) || !sv_derived_from(base, class)) { 84 3 87
if (!sv_isobject(base) || !sv_derived_from(base, class)) { 0 84 84
2344 3 absolute(aTHX_ sv_target, uri, sv_2mortal(new(aTHX_ class, base, 0)));
2345 } else {
2346 84 absolute(aTHX_ sv_target, uri, base);
2347 }
2348  
2349 87 RETVAL = sv_target;
2350 OUTPUT:
2351 RETVAL
2352  
2353 void explain(uri_obj)
2354 SV* uri_obj
2355 CODE:
2356 0 explain(aTHX_ uri_obj);
2357  
2358 void debug(uri_obj)
2359 SV* uri_obj
2360 CODE:
2361 0 debug(aTHX_ uri_obj);
2362  
2363 void uri_split(uri)
2364 SV* uri
2365 PREINIT:
2366 I32* temp;
2367 PPCODE:
2368 27 temp = PL_markstack_ptr++;
2369 27 uri_split(aTHX_ uri);
2370  
2371 27 ▶ if (PL_markstack_ptr != temp) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_markstack_ptr != temp) { 0 27 27
2372 0 PL_markstack_ptr = temp;
2373 0 XSRETURN_EMPTY;
2374 }
2375  
2376 27 return;
« blib/lib/URI/Fast/IRI.pm ↑ Summary blib/lib/URI/Fast/Test.pm »