XS.xs

filter: Statementstmt 40.9% 18 / 44 Branchbran 33.3% 8 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 38.2% 26 / 68 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary src/yuarel.c »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <string.h>
7 #include "src/yuarel.c"
8  
9 #define ERR_PREFIX "[URL_ERROR]"
10 #define MAX_URL_LENGTH 1024
11 #define MAX_PATH_ELEMENTS 256
12 #define MAX_QUERY_PARAMS 256
13  
14  
15 MODULE = URL::XS PACKAGE = URL::XS
16 PROTOTYPES: DISABLE
17  
18 SV* parse_url(SV *src_url)
19 PREINIT:
20 struct yuarel y;
21 unsigned long url_len;
22 char url[MAX_URL_LENGTH];
23 CODE:
24 18 HV *result = newHV();
25 18 char *_url = SvPV(src_url, url_len);
26  
27 18 ▶ if (url_len > MAX_URL_LENGTH)
Branch: 1 branch
BranchTrueFalseTotal
if (url_len > MAX_URL_LENGTH) 0 18 18
28 0 Perl_croak(aTHX_ "%s: url too long (max %d symbols)", ERR_PREFIX, MAX_URL_LENGTH);
29  
30 18 strcpy(url, _url);
31  
32 18 ▶ if (yuarel_parse(&y, url) == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (yuarel_parse(&y, url) == -1) 0 18 18
33 0 Perl_croak(aTHX_ "%s: Could not parse url: %s", ERR_PREFIX, url);
34  
35 18 hv_store(result, "scheme", 6, newSVpv(y.scheme, strlen(y.scheme)), 0);
36 18 hv_store(result, "host", 4, newSVpv(y.host, strlen(y.host) ), 0);
37 18 hv_store(result, "port", 4, newSViv(y.port), 0);
38  
39 18 ▶ if (y.path == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (y.path == NULL) { 12 6 18
40 12 hv_store(result, "path", 4, &PL_sv_undef, 0);
41 } else {
42 6 hv_store(result, "path", 4, newSVpv(y.path, strlen(y.path)), 0);
43 }
44  
45 18 ▶ if (y.query == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (y.query == NULL) { 12 6 18
46 12 hv_store(result, "query", 5, &PL_sv_undef, 0);
47 } else {
48 6 hv_store(result, "query", 5, newSVpv(y.query, strlen(y.query)), 0);
49 }
50  
51 18 ▶ if (y.fragment == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (y.fragment == NULL) { 13 5 18
52 13 hv_store(result, "fragment", 8, &PL_sv_undef, 0);
53 } else {
54 5 hv_store(result, "fragment", 8, newSVpv(y.fragment, strlen(y.fragment)), 0);
55 }
56  
57 18 RETVAL = newRV_noinc((SV*) result);
58 OUTPUT:
59 RETVAL
60  
61  
62 SV* split_url_path(url_path, max_paths)
63 char *url_path
64 unsigned short max_paths
65 INIT:
66 0 ▶ if (max_paths > MAX_PATH_ELEMENTS)
Branch: 1 branch
BranchTrueFalseTotal
if (max_paths > MAX_PATH_ELEMENTS) 0 0 0
67 0 Perl_croak(aTHX_ "%s: max_paths too much (max 256)", ERR_PREFIX);
68 0 ▶ if (max_paths == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (max_paths == 0) 0 0 0
69 0 Perl_croak(aTHX_ "%s: max_paths must be a positive integer (from 1 to 256)", ERR_PREFIX);
70 CODE:
71 0 AV *result = newAV();
72 0 char *paths[max_paths];
73 0 unsigned short int p = yuarel_split_path(url_path, paths, max_paths);
74  
75 0 ▶ if (p > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p > 0) 0 0 0
76 0 p--;
77  
78 0 ▶ for(int i=0; i <= p; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(int i=0; i <= p; i++) 0 0 0
79 0 av_push(result, newSVpv(paths[i], strlen(paths[i])));
80  
81 0 RETVAL = newRV_noinc((SV*) result);
82 OUTPUT:
83 RETVAL
84  
85  
86 SV* parse_url_query(url_query, ...)
87 char *url_query
88 PREINIT:
89 0 char *sep = "&";
90 unsigned short q;
91 struct yuarel_param params[MAX_QUERY_PARAMS];
92 CODE:
93 0 HV *result = newHV();
94  
95 0 ▶ if (items > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) 0 0 0
96 0 sep = (char*)SvPV_nolen(ST(1));
97  
98 0 q = yuarel_parse_query(url_query, *sep, params, MAX_QUERY_PARAMS);
99  
100 0 ▶ if (q > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (q > 0) 0 0 0
101 0 q--;
102  
103 char *k, *v;
104  
105 0 ▶ for (int i=0; i <= q; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i=0; i <= q; i++) { 0 0 0
106 0 k = params[i].key;
107 0 v = params[i].val;
108 0 hv_store(result, k, strlen(k), newSVpv(v, strlen(v)), 0);
109 }
110  
111 0 RETVAL = newRV_noinc((SV*) result);
112 OUTPUT:
113 RETVAL
↑ Summary src/yuarel.c »