XS.xs

filter: Statementstmt 98.1% 103 / 105 Branchbran 66.2% 94 / 142 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.7% 197 / 247 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Router/XS.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6 #include "uthash.h"
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10  
11 #define WORDLEN 32
12 #define PATHLEN 1024
13  
14 #ifdef DEBUG
15 #define DEBUG_PRINT(...) fprintf( stderr, __VA_ARGS__ )
16 #else
17 #define DEBUG_PRINT(...) do{ } while ( 0 )
18 #endif
19  
20 typedef struct Node {
21 char word[WORDLEN];
22 struct Node *children;
23 void *coderef;
24 size_t *captures;
25 size_t numcaptures;
26 UT_hash_handle hh; // makes this structure hashable
27 } Node;
28  
29 static char * WILD = "*";
30 static char SPLITBUF[WORDLEN][WORDLEN];
31 static size_t SPLITBUFLEN;
32 static Node * MATCHBUF[WORDLEN][WORDLEN];
33 static char SPLITCPY[PATHLEN];
34 static char CAPBUF[WORDLEN][WORDLEN];
35 Node *ROOT;
36  
37 35 Node * NodeNew(char *word) {
38 35 Node *n = malloc(sizeof(Node));
39 35 strcpy(n->word, word);
40 35 n->children = NULL;
41 35 n->coderef = NULL;
42 35 return n;
43 }
44  
45 38 Node * NodeAddChild(Node *n, char *word) {
46 DEBUG_PRINT("adding child %s\n", word);
47 Node *child;
48 38 ▶ HASH_FIND_STR(n->children, word, child);
Branch: 8 branches
BranchTrueFalseTotal
HASH_FIND_STR(n->children, word, child); 0 38 38
HASH_FIND_STR(n->children, word, child); 15 23 38
HASH_FIND_STR(n->children, word, child); 4 11 15
HASH_FIND_STR(n->children, word, child); 4 0 4
HASH_FIND_STR(n->children, word, child); 4 0 4
HASH_FIND_STR(n->children, word, child); 4 0 4
HASH_FIND_STR(n->children, word, child); 0 0 0
HASH_FIND_STR(n->children, word, child); 4 11 15
49  
50 38 ▶ if (!child) {
Branch: 1 branch
BranchTrueFalseTotal
if (!child) { 34 4 38
51 34 child = NodeNew(word);
52 34 ▶ HASH_ADD_STR(n->children, word, child);
Branch: 15 branches
BranchTrueFalseTotal
HASH_ADD_STR(n->children, word, child); 0 34 34
HASH_ADD_STR(n->children, word, child); 23 11 34
HASH_ADD_STR(n->children, word, child); 0 23 23
HASH_ADD_STR(n->children, word, child); 0 23 23
HASH_ADD_STR(n->children, word, child); 0 34 34
HASH_ADD_STR(n->children, word, child); 0 34 34
HASH_ADD_STR(n->children, word, child); 0 0 0
HASH_ADD_STR(n->children, word, child); 0 0 0
HASH_ADD_STR(n->children, word, child); 0 0 0
HASH_ADD_STR(n->children, word, child); 0 0 0
HASH_ADD_STR(n->children, word, child); 0 0 0
HASH_ADD_STR(n->children, word, child); 0 0 0
HASH_ADD_STR(n->children, word, child); 0 0 0
HASH_ADD_STR(n->children, word, child); 0 0 0
HASH_ADD_STR(n->children, word, child); 0 0 0
53 }
54 38 return child;
55 }
56  
57 47 void pathsplit(char* path, size_t pathlen, char* delim, size_t* numwords) {
58 // +1 for the null terminator \0
59 47 memcpy(SPLITCPY, path, pathlen + 1);
60 47 size_t tokens_used = 0;
61 47 char *token, *rest = SPLITCPY;
62 203 ▶ while ((token = strsep(&rest, delim)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((token = strsep(&rest, delim)) != NULL) { 156 47 203
63 156 memcpy(SPLITBUF[tokens_used++], token, WORDLEN);
64 }
65 47 *numwords = tokens_used;
66 47 }
67  
68 13 void NodeAddPath(Node *n, char *path, size_t pathlen, void *coderef) {
69 DEBUG_PRINT("\nadding path: %s\n", path);
70 13 pathsplit(path, pathlen, "/", &SPLITBUFLEN);
71 13 size_t captures[WORDLEN], numcaptures = 0, skipped = 0;
72  
73 62 ▶ for (size_t i = 0; i < SPLITBUFLEN; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < SPLITBUFLEN; i++) { 49 13 62
74 // skip zero length strings
75 49 ▶ if (! *(SPLITBUF[i]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (! *(SPLITBUF[i]) ) { 11 38 49
76 11 skipped += 1;
77 11 continue;
78 }
79 // the first word is HTTP method: it can be wildcard but not captured
80 38 ▶ if (i && strcmp(WILD, SPLITBUF[i]) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (i && strcmp(WILD, SPLITBUF[i]) == 0) { 25 13 38
if (i && strcmp(WILD, SPLITBUF[i]) == 0) { 4 21 25
81 4 captures[numcaptures] = i - skipped;
82 4 numcaptures += 1;
83 }
84 38 n = NodeAddChild(n, SPLITBUF[i]);
85 }
86 13 n->coderef = coderef;
87 13 n->numcaptures = numcaptures;
88 13 n->captures = malloc(sizeof(size_t) * numcaptures);
89 17 ▶ for (int i = 0; i < numcaptures; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < numcaptures; i++) { 4 13 17
90 4 *(n->captures + i) = captures[i];
91 }
92 13 }
93  
94 91 Node * NodeSearch(size_t bufidx, size_t matchidx, size_t matchcount) {
95 DEBUG_PRINT("starting NodeSearch part %zu matchcount: %zu\n", bufidx, matchcount);
96 91 size_t newmatches = 0;
97  
98 // skip zero length strings
99 91 ▶ if (! *(SPLITBUF[bufidx])) {
Branch: 1 branch
BranchTrueFalseTotal
if (! *(SPLITBUF[bufidx])) { 12 79 91
100 DEBUG_PRINT("Found zero length string\n");
101 // if there are more words in the split buffer, keep going
102 12 ▶ if (bufidx + 1 < SPLITBUFLEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (bufidx + 1 < SPLITBUFLEN) { 9 3 12
103 9 bufidx += 1;
104 }
105 3 ▶ else if (matchcount) {
Branch: 1 branch
BranchTrueFalseTotal
else if (matchcount) { 3 0 3
106 // we're at the end of the path
107 // update vars to the previous iteration
108 3 newmatches = matchcount;
109 3 matchidx -= 1;
110 DEBUG_PRINT("goto MATCHES\n");
111 3 goto MATCHES;
112 }
113 else {
114 0 goto ENDNODESEARCH;
115 }
116 }
117 203 ▶ for (int i = 0; i < matchcount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < matchcount; i++) { 115 88 203
118 DEBUG_PRINT("searching %s\n", SPLITBUF[bufidx]);
119  
120 Node *n;
121 115 ▶ HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n);
Branch: 8 branches
BranchTrueFalseTotal
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n); 0 115 115
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n); 90 25 115
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n); 48 42 90
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n); 48 0 48
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n); 48 0 48
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n); 48 0 48
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n); 0 0 0
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, SPLITBUF[bufidx], n); 48 42 90
122 115 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 48 67 115
123 48 MATCHBUF[matchidx + 1][newmatches] = n;
124 48 newmatches += 1;
125 }
126 DEBUG_PRINT("searching %s\n", WILD);
127 // check for a wildcard match
128 Node *w;
129 115 ▶ HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w);
Branch: 8 branches
BranchTrueFalseTotal
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w); 0 115 115
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w); 90 25 115
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w); 53 37 90
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w); 53 0 53
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w); 53 0 53
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w); 53 0 53
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w); 0 0 0
HASH_FIND_STR(MATCHBUF[matchidx][i]->children, WILD, w); 53 37 90
130 115 ▶ if (w) {
Branch: 1 branch
BranchTrueFalseTotal
if (w) { 53 62 115
131 53 MATCHBUF[matchidx + 1][newmatches] = w;
132 53 newmatches += 1;
133  
134 // the first word is always the HTTP method, which we never capture
135 if (matchidx) {
136 DEBUG_PRINT("capturing %s\n", SPLITBUF[bufidx]);
137 }
138 }
139 }
140 DEBUG_PRINT("Found %zu matches\n", newmatches);
141 // if there are no new matches
142 88 ▶ if (newmatches) {
Branch: 1 branch
BranchTrueFalseTotal
if (newmatches) { 14 74 88
143 // keep going if there are more words in the path to match
144 74 ▶ if (bufidx + 1 < SPLITBUFLEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (bufidx + 1 < SPLITBUFLEN) { 57 17 74
145 57 return NodeSearch(bufidx + 1, matchidx + 1, newmatches);
146 }
147 else {
148 17 MATCHES:
149 // we've matched all the words in the path return the first match
150 24 ▶ for (int i = 0; i < newmatches; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < newmatches; i++) { 20 4 24
151 20 Node *n = MATCHBUF[matchidx + 1][i];
152 // only a complete path match will have the coderef set in the leaf node
153 20 ▶ if (n->coderef)
Branch: 1 branch
BranchTrueFalseTotal
if (n->coderef) 16 4 20
154 16 return n;
155 }
156 }
157 }
158 18 ENDNODESEARCH:
159 DEBUG_PRINT("returning NULL\n");
160 18 return NULL;
161 }
162  
163 34 void * NodeTestPath(Node *r, char *path, size_t pathlen, size_t *numcaptures) {
164 DEBUG_PRINT("\ntesting path: %s\n", path);
165 34 pathsplit(path, pathlen, "/", &SPLITBUFLEN);
166  
167 34 MATCHBUF[0][0] = r;
168 34 Node *n = NodeSearch(0, 0, 1);
169  
170 34 ▶ if (n) {
Branch: 1 branch
BranchTrueFalseTotal
if (n) { 16 18 34
171 DEBUG_PRINT("matched full path\n");
172 20 ▶ for (int i = 0; i < n->numcaptures; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n->numcaptures; i++) { 4 16 20
173 4 memcpy(CAPBUF[i], SPLITBUF[ *(n->captures + i) ], WORDLEN);
174 }
175 16 *numcaptures = n->numcaptures;
176 16 return n->coderef;
177 }
178 18 return NULL;
179 }
180  
181 MODULE = Router::XS PACKAGE = Router::XS
182 PROTOTYPES: DISABLED
183  
184 BOOT:
185 1 ROOT = NodeNew("");
186  
187 void
188 add_route(path, coderef)
189 SV *path
190 SV *coderef
191 PREINIT:
192 char *pathstr;
193 15 size_t pathlen, numcaptures = 0;
194  
195 PPCODE:
196 15 SvGETMAGIC(path);
197  
198 /* check for undef, empty string */
199 15 ▶ if (!SvOK(path) || !SvCUR(path) || !SvROK(coderef))
Branch: 3 branches
BranchTrueFalseTotal
if (!SvOK(path) || !SvCUR(path) || !SvROK(coderef)) 15 0 15
if (!SvOK(path) || !SvCUR(path) || !SvROK(coderef)) 14 1 15
if (!SvOK(path) || !SvCUR(path) || !SvROK(coderef)) 1 13 14
200 {
201 2 croak("requires path and coderef arguments");
202 }
203  
204 13 pathstr = SvPV_nomg(path, pathlen);
205  
206 13 ▶ if (NodeTestPath(ROOT, pathstr, pathlen, &numcaptures)) {
Branch: 1 branch
BranchTrueFalseTotal
if (NodeTestPath(ROOT, pathstr, pathlen, &numcaptures)) { 0 13 13
207 0 croak("Attempted to add duplicate path %s", pathstr);
208 }
209  
210 // increment the refcount to stop Perl deleting it
211 13 SvREFCNT_inc(coderef);
212  
213 13 NodeAddPath(ROOT, pathstr, pathlen, coderef);
214  
215 // return undef
216 13 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 13 13
217 13 PUSHs(sv_newmortal());
218  
219 void
220 check_route(path)
221 SV *path
222 PREINIT:
223 char *pathstr;
224 SV *coderef;
225 23 size_t numcaptures = 0, caplen, pathlen;
226  
227 PPCODE:
228 23 SvGETMAGIC(path);
229  
230 /* check for undef, empty string */
231 23 ▶ if (!SvOK(path) || !SvCUR(path))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(path) || !SvCUR(path)) 22 1 23
if (!SvOK(path) || !SvCUR(path)) 1 21 22
232 {
233 2 croak("requires path");
234 }
235  
236 21 pathstr = SvPV_nomg(path, pathlen);
237 21 coderef = NodeTestPath(ROOT, pathstr, pathlen, &numcaptures);
238 21 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 21 21
239  
240 21 ▶ if (coderef) {
Branch: 1 branch
BranchTrueFalseTotal
if (coderef) { 16 5 21
241 16 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 16 16
242 16 PUSHs(coderef);
243 16 ▶ if (numcaptures) {
Branch: 1 branch
BranchTrueFalseTotal
if (numcaptures) { 2 14 16
244 2 ▶ EXTEND(SP, numcaptures);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, numcaptures); 0 2 2
245 6 ▶ for (int i = 0; i < numcaptures; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < numcaptures; i++) { 4 2 6
246 4 caplen = strlen(CAPBUF[i]);
247 4 PUSHs( sv_2mortal(newSVpvn(CAPBUF[i], caplen)) );
248 }
249 }
250 16 PUTBACK;
251 }
252 else {
253 // return undef
254 5 PUSHs(sv_newmortal());
255 }
↑ Summary blib/lib/Router/XS.pm »