XS.xs

filter: Statementstmt 48.0% 24 / 50 Branchbran 42.8% 12 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 46.1% 36 / 78 CC 0 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.username == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (y.username == NULL) { 12 6 18
40 12 hv_store(result, "username", 8, &PL_sv_undef, 0);
41 } else {
42 6 hv_store(result, "username", 8, newSVpv(y.username, strlen(y.username)), 0);
43 }
44  
45 18 ▶ if (y.password == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (y.password == NULL) { 12 6 18
46 12 hv_store(result, "password", 8, &PL_sv_undef, 0);
47 } else {
48 6 hv_store(result, "password", 8, newSVpv(y.password, strlen(y.password)), 0);
49 }
50  
51 18 ▶ if (y.path == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (y.path == NULL) { 12 6 18
52 12 hv_store(result, "path", 4, &PL_sv_undef, 0);
53 } else {
54 6 hv_store(result, "path", 4, newSVpv(y.path, strlen(y.path)), 0);
55 }
56  
57 18 ▶ if (y.query == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (y.query == NULL) { 12 6 18
58 12 hv_store(result, "query", 5, &PL_sv_undef, 0);
59 } else {
60 6 hv_store(result, "query", 5, newSVpv(y.query, strlen(y.query)), 0);
61 }
62  
63 18 ▶ if (y.fragment == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (y.fragment == NULL) { 13 5 18
64 13 hv_store(result, "fragment", 8, &PL_sv_undef, 0);
65 } else {
66 5 hv_store(result, "fragment", 8, newSVpv(y.fragment, strlen(y.fragment)), 0);
67 }
68  
69 18 RETVAL = newRV_noinc((SV*) result);
70 OUTPUT:
71 RETVAL
72  
73  
74 SV* split_url_path(url_path, max_paths)
75 char *url_path
76 unsigned short max_paths
77 INIT:
78 0 ▶ if (max_paths > MAX_PATH_ELEMENTS)
Branch: 1 branch
BranchTrueFalseTotal
if (max_paths > MAX_PATH_ELEMENTS) 0 0 0
79 0 Perl_croak(aTHX_ "%s: max_paths too much (max 256)", ERR_PREFIX);
80 0 ▶ if (max_paths == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (max_paths == 0) 0 0 0
81 0 Perl_croak(aTHX_ "%s: max_paths must be a positive integer (from 1 to 256)", ERR_PREFIX);
82 CODE:
83 0 AV *result = newAV();
84 0 char *paths[max_paths];
85 0 unsigned short int p = yuarel_split_path(url_path, paths, max_paths);
86  
87 0 ▶ if (p > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (p > 0) 0 0 0
88 0 p--;
89  
90 0 ▶ for(int i=0; i <= p; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(int i=0; i <= p; i++) 0 0 0
91 0 av_push(result, newSVpv(paths[i], strlen(paths[i])));
92  
93 0 RETVAL = newRV_noinc((SV*) result);
94 OUTPUT:
95 RETVAL
96  
97  
98 SV* parse_url_query(url_query, ...)
99 char *url_query
100 PREINIT:
101 0 char *sep = "&";
102 unsigned short q;
103 struct yuarel_param params[MAX_QUERY_PARAMS];
104 CODE:
105 0 HV *result = newHV();
106  
107 0 ▶ if (items > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (items > 1) 0 0 0
108 0 sep = (char*)SvPV_nolen(ST(1));
109  
110 0 q = yuarel_parse_query(url_query, *sep, params, MAX_QUERY_PARAMS);
111  
112 0 ▶ if (q > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (q > 0) 0 0 0
113 0 q--;
114  
115 char *k, *v;
116  
117 0 ▶ for (int i=0; i <= q; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i=0; i <= q; i++) { 0 0 0
118 0 k = params[i].key;
119 0 v = params[i].val;
120 0 hv_store(result, k, strlen(k), newSVpv(v, strlen(v)), 0);
121 }
122  
123 0 RETVAL = newRV_noinc((SV*) result);
124 OUTPUT:
125 RETVAL
↑ Summary src/yuarel.c »