| 1 |
|
| #define PERL_NO_GET_CONTEXT /* we want efficiency */ |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| #include "glog.h" |
| 7 |
|
| #include "gmem.h" |
| 8 |
|
| #include "util.h" |
| 9 |
|
| #include "header.h" |
| 10 |
|
| |
| 11 |
|
| #if defined(USE_ITHREADS) && !defined(sv_dup_inc) |
| 12 |
|
| # define sv_dup_inc(sv, param) SvREFCNT_inc(sv_dup(sv, param)) |
| 13 |
|
| #endif |
| 14 |
|
| |
| 15 |
|
| #ifndef PERL_UNUSED_ARG |
| 16 |
|
| # define PERL_UNUSED_ARG(x) ((void)x) |
| 17 |
|
| #endif |
| 18 |
|
| |
| 19 |
496 |
| static inline bool http_is_token_char(char c) { |
| 20 |
|
| // RFC 9110 §5.6.2 ABNF tchar: |
| 21 |
|
| // "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / |
| 22 |
|
| // "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA |
| 23 |
|
| static const uint64_t T[4] = { |
| 24 |
|
| UINT64_C(0x03FF6CFA00000000), |
| 25 |
|
| UINT64_C(0x57FFFFFFC7FFFFFE), |
| 26 |
|
| UINT64_C(0x0000000000000000), |
| 27 |
|
| UINT64_C(0x0000000000000000), |
| 28 |
|
| }; |
| 29 |
496 |
| unsigned char u = (unsigned char)c; |
| 30 |
496 |
| return (T[u >> 6] & (UINT64_C(1) << (u & 0x3F))) != 0; |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
55 |
| static inline bool http_is_token(const char *str, size_t len) { |
| 34 |
|
| // RFC 9110 §5.6.2 ABNF token = 1*tchar |
| 35 |
55 |
▶ | if (!len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!len) |
0 |
55 |
55 |
|
| 36 |
0 |
| return false; |
| 37 |
|
| // Full scan, no early exit: keeps the loop shape branchless and |
| 38 |
|
| // data-independent so GCC/Clang can auto-vectorize it. |
| 39 |
55 |
| size_t count = 0; |
| 40 |
551 |
▶ | for (size_t i = 0; i < len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (size_t i = 0; i < len; i++) |
496 |
55 |
551 |
|
| 41 |
496 |
| count += http_is_token_char(str[i]); |
| 42 |
55 |
| return count == len; |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
|
| // Destructor wrapper for SAVEDESTRUCTOR_X |
| 46 |
50 |
| static void THX_hlist_destroy(pTHX_ void *p) { |
| 47 |
50 |
| hlist_destroy((HList *)p); |
| 48 |
50 |
| } |
| 49 |
|
| |
| 50 |
252 |
| static MAGIC* THX_mg_find(pTHX_ SV* sv, const MGVTBL* const vtbl) { |
| 51 |
|
| MAGIC* mg; |
| 52 |
|
| |
| 53 |
252 |
▶ | if (SvTYPE(sv) < SVt_PVMG) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(sv) < SVt_PVMG) |
0 |
252 |
252 |
|
| 54 |
0 |
| return NULL; |
| 55 |
|
| |
| 56 |
256 |
▶ | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
252 |
4 |
256 |
|
| 57 |
252 |
▶ | if(mg->mg_virtual == vtbl) |
Branch: 1 branch
| Branch | True | False | Total |
| if(mg->mg_virtual == vtbl) |
248 |
4 |
252 |
|
| 58 |
248 |
| return mg; |
| 59 |
|
| } |
| 60 |
4 |
| return NULL; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
36 |
| static int THX_mg_free(pTHX_ SV* const sv, MAGIC* const mg) { |
| 64 |
36 |
| HList* const hl = (HList*)mg->mg_ptr; |
| 65 |
|
| int j, k; |
| 66 |
|
| |
| 67 |
|
| GLOG(("=X= @@@ mg_free(%p|%d)", hl, hlist_size(hl))); |
| 68 |
|
| |
| 69 |
118 |
▶ | for (j = 0; j < hl->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hl->ulen; ++j) { |
82 |
36 |
118 |
|
| 70 |
82 |
| HNode* hn = &hl->data[j]; |
| 71 |
82 |
| PList* pl = hn->values; |
| 72 |
181 |
▶ | for (k = 0; k < pl->ulen; ++k) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < pl->ulen; ++k) { |
99 |
82 |
181 |
|
| 73 |
99 |
| PNode* pn = &pl->data[k]; |
| 74 |
99 |
| SvREFCNT_dec((SV*)pn->ptr); |
| 75 |
|
| } |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
36 |
| hlist_destroy(hl); |
| 79 |
36 |
| return 0; |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
0 |
| static int THX_mg_dup(pTHX_ MAGIC* const mg, CLONE_PARAMS* const param) { |
| 83 |
|
| #ifdef USE_ITHREADS |
| 84 |
|
| HList* const hl = (HList*)mg->mg_ptr; |
| 85 |
|
| HList* clone; |
| 86 |
|
| int j, k; |
| 87 |
|
| |
| 88 |
|
| GLOG(("=X= @@@ mg_dup(%p|%d)", hl, hlist_size(hl))); |
| 89 |
|
| |
| 90 |
|
| if (!(clone = hlist_clone(hl))) |
| 91 |
|
| croak("Could not clone HList object"); |
| 92 |
|
| |
| 93 |
|
| for (j = 0; j < clone->ulen; ++j) { |
| 94 |
|
| HNode* hnode = &clone->data[j]; |
| 95 |
|
| PList* plist = hnode->values; |
| 96 |
|
| for (k = 0; k < plist->ulen; ++k) { |
| 97 |
|
| PNode* pnode = &plist->data[k]; |
| 98 |
|
| pnode->ptr = sv_dup_inc((SV*)pnode->ptr, param); |
| 99 |
|
| } |
| 100 |
|
| } |
| 101 |
|
| mg->mg_ptr = (char *)clone; |
| 102 |
|
| #else |
| 103 |
|
| PERL_UNUSED_ARG(mg); |
| 104 |
|
| PERL_UNUSED_ARG(param); |
| 105 |
|
| #endif |
| 106 |
0 |
| return 0; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
|
| static MGVTBL const hlist_mgvtbl = { |
| 110 |
|
| NULL, /* get */ |
| 111 |
|
| NULL, /* set */ |
| 112 |
|
| NULL, /* len */ |
| 113 |
|
| NULL, /* clear */ |
| 114 |
|
| THX_mg_free, /* free */ |
| 115 |
|
| NULL, /* copy */ |
| 116 |
|
| THX_mg_dup, /* dup */ |
| 117 |
|
| #ifdef MGf_LOCAL |
| 118 |
|
| NULL, /* local */ |
| 119 |
|
| #endif |
| 120 |
|
| }; |
| 121 |
|
| |
| 122 |
36 |
| static SV * THX_newSV_HList(pTHX_ HList* const hl, HV * const stash) { |
| 123 |
|
| MAGIC *mg; |
| 124 |
|
| HV *hv; |
| 125 |
|
| SV *rv; |
| 126 |
|
| |
| 127 |
|
| GLOG(("=X= Will bless new object")); |
| 128 |
|
| |
| 129 |
36 |
| hv = newHV(); |
| 130 |
36 |
| rv = newRV_noinc((SV*)hv); |
| 131 |
36 |
| mg = sv_magicext((SV*)hv, NULL, PERL_MAGIC_ext, &hlist_mgvtbl, (char *)hl, 0); |
| 132 |
36 |
| mg->mg_flags |= MGf_DUP; |
| 133 |
36 |
| sv_bless(rv, stash); |
| 134 |
36 |
| sv_2mortal(rv); |
| 135 |
36 |
| return rv; |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
269 |
| static HList * THX_sv_2HList(pTHX_ SV* const sv, const char *name) { |
| 139 |
269 |
| MAGIC* mg = NULL; |
| 140 |
|
| |
| 141 |
269 |
| SvGETMAGIC(sv); |
| 142 |
269 |
▶ | if (SvROK(sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(sv)) |
248 |
21 |
269 |
|
| 143 |
248 |
| mg = THX_mg_find(aTHX_ SvRV(sv), &hlist_mgvtbl); |
| 144 |
|
| |
| 145 |
269 |
▶ | if (!mg) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mg) |
21 |
248 |
269 |
|
| 146 |
21 |
| croak("%s is not an instance of HTTP::XSHeaders", name); |
| 147 |
|
| |
| 148 |
248 |
| return (HList*)mg->mg_ptr; |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
|
| #define newSV_HList(hl, stash) \ |
| 152 |
|
| THX_newSV_HList(aTHX_ hl, stash) |
| 153 |
|
| |
| 154 |
|
| #define sv_2HList(sv, name) \ |
| 155 |
|
| THX_sv_2HList(aTHX_ sv, name) |
| 156 |
|
| |
| 157 |
|
| MODULE = HTTP::XSHeaders PACKAGE = HTTP::XSHeaders |
| 158 |
|
| PROTOTYPES: DISABLE |
| 159 |
|
| |
| 160 |
|
| |
| 161 |
|
| ################################################################# |
| 162 |
|
| |
| 163 |
|
| int |
| 164 |
|
| _is_xsheaders(SV* sv) |
| 165 |
|
| PREINIT: |
| 166 |
4 |
▶ | MAGIC* mg = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| MAGIC* mg = NULL; |
4 |
0 |
4 |
|
| 167 |
|
| CODE: |
| 168 |
4 |
| SvGETMAGIC(sv); |
| 169 |
4 |
▶ | if (SvROK(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(sv)) { |
4 |
0 |
4 |
|
| 170 |
4 |
| mg = THX_mg_find(aTHX_ SvRV(sv), &hlist_mgvtbl); |
| 171 |
|
| } |
| 172 |
4 |
▶ | RETVAL = mg ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = mg ? 1 : 0; |
3 |
1 |
4 |
|
| 173 |
|
| OUTPUT: RETVAL |
| 174 |
|
| |
| 175 |
|
| |
| 176 |
|
| void |
| 177 |
|
| new( SV* klass, ... ) |
| 178 |
|
| PREINIT: |
| 179 |
19 |
| int argc = 0; |
| 180 |
19 |
▶ | HList* hl = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| HList* hl = 0; |
19 |
0 |
19 |
|
| 181 |
|
| int j; |
| 182 |
|
| SV* pkey; |
| 183 |
|
| SV* pval; |
| 184 |
|
| char* ckey; |
| 185 |
|
| |
| 186 |
|
| CODE: |
| 187 |
19 |
▶ | if (!SvOK(klass) || !SvPOK(klass)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(klass) || !SvPOK(klass)) { |
19 |
0 |
19 |
| if (!SvOK(klass) || !SvPOK(klass)) { |
0 |
19 |
19 |
|
| 188 |
0 |
| XSRETURN_EMPTY; |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
19 |
| argc = items - 1; |
| 192 |
19 |
▶ | if ( argc % 2 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( argc % 2 ) { |
0 |
19 |
19 |
|
| 193 |
0 |
| croak("Expecting a hash as input to constructor"); |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
|
| GLOG(("=X= @@@ new()")); |
| 197 |
19 |
▶ | if (!(hl = hlist_create())) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(hl = hlist_create())) |
0 |
19 |
19 |
|
| 198 |
0 |
| croak("Could not create new HList object"); |
| 199 |
|
| |
| 200 |
19 |
| ST(0) = newSV_HList(hl, gv_stashpv(SvPV_nolen(klass), 0)); |
| 201 |
|
| |
| 202 |
|
| /* create the initial list */ |
| 203 |
61 |
▶ | for (j = 1; j <= argc; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 1; j <= argc; ) { |
42 |
19 |
61 |
|
| 204 |
42 |
| pkey = ST(j++); |
| 205 |
|
| |
| 206 |
|
| /* did we reach the end by any chance? */ |
| 207 |
42 |
▶ | if (j > argc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j > argc) { |
0 |
42 |
42 |
|
| 208 |
0 |
| break; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
42 |
| pval = ST(j++); |
| 212 |
42 |
| ckey = SvPV_nolen(pkey); |
| 213 |
|
| GLOG(("=X= Will set [%s] to [%s]", ckey, SvPV_nolen(pval))); |
| 214 |
42 |
| set_value(aTHX_ hl, ckey, pval); |
| 215 |
|
| } |
| 216 |
19 |
| XSRETURN(1); |
| 217 |
|
| |
| 218 |
|
| |
| 219 |
|
| void |
| 220 |
|
| clone(HList* hl) |
| 221 |
|
| PREINIT: |
| 222 |
|
| HList* clone; |
| 223 |
|
| int j; |
| 224 |
|
| int k; |
| 225 |
|
| CODE: |
| 226 |
|
| GLOG(("=X= @@@ clone(%p|%d)", hl, hlist_size(hl))); |
| 227 |
|
| |
| 228 |
13 |
▶ | if (!(clone = hlist_clone(hl))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(clone = hlist_clone(hl))) |
0 |
13 |
13 |
|
| 229 |
0 |
| croak("Could not clone HList object"); |
| 230 |
|
| |
| 231 |
13 |
| ST(0) = newSV_HList(clone, SvSTASH(SvRV(ST(0)))); |
| 232 |
|
| |
| 233 |
|
| /* Clone the SVs into new ones */ |
| 234 |
59 |
▶ | for (j = 0; j < clone->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < clone->ulen; ++j) { |
46 |
13 |
59 |
|
| 235 |
46 |
| HNode* hnode = &clone->data[j]; |
| 236 |
46 |
| PList* plist = hnode->values; |
| 237 |
104 |
▶ | for (k = 0; k < plist->ulen; ++k) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < plist->ulen; ++k) { |
58 |
46 |
104 |
|
| 238 |
58 |
| PNode* pnode = &plist->data[k]; |
| 239 |
58 |
| pnode->ptr = newSVsv( (SV*)pnode->ptr ); |
| 240 |
|
| } |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
13 |
| XSRETURN(1); |
| 244 |
|
| |
| 245 |
|
| |
| 246 |
|
| # |
| 247 |
|
| # Clear object, leaving it as freshly created. |
| 248 |
|
| # |
| 249 |
|
| void |
| 250 |
|
| clear(HList* hl, ...) |
| 251 |
|
| CODE: |
| 252 |
|
| GLOG(("=X= @@@ clear(%p|%d)", hl, hlist_size(hl))); |
| 253 |
6 |
| hlist_clear(hl); |
| 254 |
|
| |
| 255 |
|
| |
| 256 |
|
| # |
| 257 |
|
| # Get all the keys in an existing HList. |
| 258 |
|
| # |
| 259 |
|
| void |
| 260 |
|
| header_field_names(HList* hl) |
| 261 |
|
| PPCODE: |
| 262 |
|
| GLOG(("=X= @@@ header_field_names(%p|%d), want %d", |
| 263 |
|
| hl, hlist_size(hl), GIMME_V)); |
| 264 |
9 |
| hlist_sort(hl); |
| 265 |
9 |
| PUTBACK; |
| 266 |
9 |
| return_hlist(aTHX_ hl, "header_field_names", GIMME_V); |
| 267 |
9 |
| SPAGAIN; |
| 268 |
|
| |
| 269 |
|
| |
| 270 |
|
| # |
| 271 |
|
| # init_header |
| 272 |
|
| # |
| 273 |
|
| void |
| 274 |
|
| init_header(HList* hl, ...) |
| 275 |
|
| PREINIT: |
| 276 |
12 |
| int argc = 0; |
| 277 |
|
| SV* pkey; |
| 278 |
|
| SV* pval; |
| 279 |
|
| STRLEN len; |
| 280 |
|
| char* ckey; |
| 281 |
|
| |
| 282 |
|
| CODE: |
| 283 |
|
| GLOG(("=X= @@@ init_header(%p|%d), %d params, want %d", |
| 284 |
|
| hl, hlist_size(hl), argc, GIMME_V)); |
| 285 |
10 |
| argc = items - 1; |
| 286 |
10 |
▶ | if (argc != 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (argc != 2) { |
3 |
7 |
10 |
|
| 287 |
3 |
| croak("init_header needs two arguments"); |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
|
| /* TODO: apply this check everywhere! */ |
| 291 |
7 |
| pkey = ST(1); |
| 292 |
7 |
▶ | if (!SvOK(pkey) || !SvPOK(pkey)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(pkey) || !SvPOK(pkey)) { |
6 |
1 |
7 |
| if (!SvOK(pkey) || !SvPOK(pkey)) { |
0 |
6 |
6 |
|
| 293 |
1 |
| croak("init_header not called with a first string argument"); |
| 294 |
|
| } |
| 295 |
6 |
| ckey = SvPV(pkey, len); |
| 296 |
6 |
| pval = ST(2); |
| 297 |
|
| |
| 298 |
6 |
▶ | if (!hlist_get(hl, ckey)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hlist_get(hl, ckey)) { |
4 |
2 |
6 |
|
| 299 |
4 |
| set_value(aTHX_ hl, ckey, pval); |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
|
| # |
| 303 |
|
| # push_header |
| 304 |
|
| # |
| 305 |
|
| void |
| 306 |
|
| push_header(HList* hl, ...) |
| 307 |
|
| PREINIT: |
| 308 |
9 |
| int argc = 0; |
| 309 |
|
| int j; |
| 310 |
|
| SV* pkey; |
| 311 |
|
| SV* pval; |
| 312 |
|
| STRLEN len; |
| 313 |
|
| char* ckey; |
| 314 |
|
| |
| 315 |
|
| CODE: |
| 316 |
|
| GLOG(("=X= @@@ push_header(%p|%d), %d params, want %d", |
| 317 |
|
| hl, hlist_size(hl), argc, GIMME_V)); |
| 318 |
|
| |
| 319 |
7 |
| argc = items - 1; |
| 320 |
7 |
▶ | if (argc % 2 != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (argc % 2 != 0) { |
0 |
7 |
7 |
|
| 321 |
0 |
| croak("push_header needs an even number of arguments"); |
| 322 |
|
| } |
| 323 |
|
| |
| 324 |
14 |
▶ | for (j = 1; j <= argc; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 1; j <= argc; ) { |
7 |
7 |
14 |
|
| 325 |
7 |
▶ | if (j > argc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j > argc) { |
0 |
7 |
7 |
|
| 326 |
0 |
| break; |
| 327 |
|
| } |
| 328 |
7 |
| pkey = ST(j++); |
| 329 |
|
| |
| 330 |
7 |
▶ | if (j > argc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j > argc) { |
0 |
7 |
7 |
|
| 331 |
0 |
| break; |
| 332 |
|
| } |
| 333 |
7 |
| pval = ST(j++); |
| 334 |
|
| |
| 335 |
7 |
| ckey = SvPV(pkey, len); |
| 336 |
7 |
| set_value(aTHX_ hl, ckey, pval); |
| 337 |
|
| } |
| 338 |
|
| |
| 339 |
|
| |
| 340 |
|
| # |
| 341 |
|
| # header |
| 342 |
|
| # |
| 343 |
|
| void |
| 344 |
|
| header(HList* hl, ...) |
| 345 |
|
| PREINIT: |
| 346 |
140 |
| int argc = 0; |
| 347 |
|
| int j; |
| 348 |
140 |
| SV* pkey = 0; |
| 349 |
140 |
| SV* pval = 0; |
| 350 |
|
| STRLEN len; |
| 351 |
140 |
| char* ckey = 0; |
| 352 |
140 |
| HNode* n = 0; |
| 353 |
140 |
| HList* seen = 0; /* TODO: make this more efficient; use Perl hash? */ |
| 354 |
|
| |
| 355 |
|
| PPCODE: |
| 356 |
|
| GLOG(("=X= @@@ header(%p|%d), %d params, want %d", |
| 357 |
|
| hl, hlist_size(hl), argc, GIMME_V)); |
| 358 |
|
| |
| 359 |
138 |
| argc = items - 1; |
| 360 |
|
| do { |
| 361 |
138 |
▶ | if (argc == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (argc == 0) { |
1 |
137 |
138 |
|
| 362 |
1 |
| croak("header called with no arguments"); |
| 363 |
|
| } |
| 364 |
|
| |
| 365 |
137 |
▶ | if (argc == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (argc == 1) { |
87 |
50 |
137 |
|
| 366 |
87 |
| pkey = ST(1); |
| 367 |
87 |
| ckey = SvPV(pkey, len); |
| 368 |
87 |
| n = hlist_get(hl, ckey); |
| 369 |
87 |
▶ | if (n && plist_size(n->values) > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (n && plist_size(n->values) > 0) { |
65 |
22 |
87 |
| if (n && plist_size(n->values) > 0) { |
65 |
0 |
65 |
|
| 370 |
65 |
| PUTBACK; |
| 371 |
65 |
| return_plist(aTHX_ n->values, "header1", GIMME_V); |
| 372 |
65 |
| SPAGAIN; |
| 373 |
|
| } |
| 374 |
87 |
| break; |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
50 |
▶ | if (argc % 2 != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (argc % 2 != 0) { |
0 |
50 |
50 |
|
| 378 |
0 |
| croak("init_header needs one or an even number of arguments"); |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
50 |
| seen = hlist_create(); |
| 382 |
50 |
| SAVEDESTRUCTOR_X(THX_hlist_destroy, seen); |
| 383 |
103 |
▶ | for (j = 1; j <= argc; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 1; j <= argc; ) { |
55 |
48 |
103 |
|
| 384 |
55 |
▶ | if (j > argc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j > argc) { |
0 |
55 |
55 |
|
| 385 |
0 |
| break; |
| 386 |
|
| } |
| 387 |
55 |
| pkey = ST(j++); |
| 388 |
|
| |
| 389 |
55 |
▶ | if (j > argc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (j > argc) { |
0 |
55 |
55 |
|
| 390 |
0 |
| break; |
| 391 |
|
| } |
| 392 |
55 |
| pval = ST(j++); |
| 393 |
|
| |
| 394 |
55 |
| ckey = SvPV(pkey, len); |
| 395 |
55 |
▶ | if (!http_is_token(ckey, len)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!http_is_token(ckey, len)) |
2 |
53 |
55 |
|
| 396 |
2 |
| croak("Illegal field name '%.*s'", (int)len, ckey); |
| 397 |
53 |
| int clear = 0; |
| 398 |
53 |
▶ | if (! hlist_get(seen, ckey)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! hlist_get(seen, ckey)) { |
52 |
1 |
53 |
|
| 399 |
52 |
| clear = 1; |
| 400 |
52 |
| hlist_add(seen, ckey, 0); |
| 401 |
|
| } |
| 402 |
|
| |
| 403 |
53 |
| n = hlist_get(hl, ckey); |
| 404 |
53 |
▶ | if (n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) { |
17 |
36 |
53 |
|
| 405 |
17 |
▶ | if (j > argc && plist_size(n->values) > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (j > argc && plist_size(n->values) > 0) { |
15 |
2 |
17 |
| if (j > argc && plist_size(n->values) > 0) { |
15 |
0 |
15 |
|
| 406 |
|
| /* Last value, return its current contents */ |
| 407 |
15 |
| PUTBACK; |
| 408 |
15 |
| return_plist(aTHX_ n->values, "header2", GIMME_V); |
| 409 |
15 |
| SPAGAIN; |
| 410 |
|
| } |
| 411 |
17 |
▶ | if (clear) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (clear) { |
16 |
1 |
17 |
|
| 412 |
16 |
| plist_clear(n->values); |
| 413 |
|
| } |
| 414 |
|
| } |
| 415 |
|
| |
| 416 |
53 |
| set_value(aTHX_ hl, ckey, pval); |
| 417 |
|
| } |
| 418 |
48 |
| break; |
| 419 |
|
| } while (0); |
| 420 |
|
| |
| 421 |
|
| |
| 422 |
|
| # |
| 423 |
|
| # _header |
| 424 |
|
| # |
| 425 |
|
| # Yes, this is an internal function, but it is used by some modules! |
| 426 |
|
| # So far, I am aware of HTTP::Cookies as one of the culprits. |
| 427 |
|
| # Luckily, they only use it with a single arg, which will be the |
| 428 |
|
| # ONLY usecase supported, at least for now. |
| 429 |
|
| # |
| 430 |
|
| void |
| 431 |
|
| _header(HList* hl, ...) |
| 432 |
|
| PREINIT: |
| 433 |
5 |
| int argc = 0; |
| 434 |
5 |
| SV* pkey = 0; |
| 435 |
|
| STRLEN len; |
| 436 |
5 |
| char* ckey = 0; |
| 437 |
5 |
| HNode* n = 0; |
| 438 |
|
| |
| 439 |
|
| PPCODE: |
| 440 |
|
| GLOG(("=X= @@@ header(%p|%d), %d params, want %d", |
| 441 |
|
| hl, hlist_size(hl), argc, GIMME_V)); |
| 442 |
|
| |
| 443 |
4 |
| argc = items - 1; |
| 444 |
4 |
▶ | if (argc != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (argc != 1) { |
1 |
3 |
4 |
|
| 445 |
1 |
| croak("_header not called with one argument"); |
| 446 |
|
| } |
| 447 |
|
| |
| 448 |
3 |
| pkey = ST(1); |
| 449 |
3 |
▶ | if (!SvOK(pkey) || !SvPOK(pkey)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(pkey) || !SvPOK(pkey)) { |
2 |
1 |
3 |
| if (!SvOK(pkey) || !SvPOK(pkey)) { |
0 |
2 |
2 |
|
| 450 |
1 |
| croak("_header not called with one string argument"); |
| 451 |
|
| } |
| 452 |
2 |
| ckey = SvPV(pkey, len); |
| 453 |
2 |
| n = hlist_get(hl, ckey); |
| 454 |
2 |
▶ | if (n && plist_size(n->values) > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (n && plist_size(n->values) > 0) { |
2 |
0 |
2 |
| if (n && plist_size(n->values) > 0) { |
2 |
0 |
2 |
|
| 455 |
2 |
| PUTBACK; |
| 456 |
2 |
| return_plist(aTHX_ n->values, "_header", GIMME_V); |
| 457 |
2 |
| SPAGAIN; |
| 458 |
|
| } |
| 459 |
|
| |
| 460 |
|
| |
| 461 |
|
| # |
| 462 |
|
| # remove_header |
| 463 |
|
| # |
| 464 |
|
| void |
| 465 |
|
| remove_header(HList* hl, ...) |
| 466 |
|
| PREINIT: |
| 467 |
17 |
| int argc = 0; |
| 468 |
|
| int j; |
| 469 |
|
| SV* pkey; |
| 470 |
|
| STRLEN len; |
| 471 |
|
| char* ckey; |
| 472 |
17 |
| int size = 0; |
| 473 |
17 |
| int total = 0; |
| 474 |
|
| |
| 475 |
|
| PPCODE: |
| 476 |
|
| GLOG(("=X= @@@ remove_header(%p|%d), %d params, want %d", |
| 477 |
|
| hl, hlist_size(hl), argc, GIMME_V)); |
| 478 |
|
| |
| 479 |
15 |
| argc = items - 1; |
| 480 |
37 |
▶ | for (j = 1; j <= argc; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 1; j <= argc; ++j) { |
22 |
15 |
37 |
|
| 481 |
22 |
| pkey = ST(j); |
| 482 |
22 |
| ckey = SvPV(pkey, len); |
| 483 |
|
| |
| 484 |
22 |
| HNode* n = hlist_get(hl, ckey); |
| 485 |
22 |
▶ | if (!n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) { |
5 |
17 |
22 |
|
| 486 |
5 |
| continue; |
| 487 |
|
| } |
| 488 |
|
| |
| 489 |
17 |
| size = plist_size(n->values); |
| 490 |
17 |
▶ | if (size > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size > 0) { |
17 |
0 |
17 |
|
| 491 |
17 |
| total += size; |
| 492 |
17 |
▶ | if (GIMME_V == G_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_ARRAY) { |
7 |
10 |
17 |
|
| 493 |
7 |
| PUTBACK; |
| 494 |
7 |
| return_plist(aTHX_ n->values, "remove_header", G_ARRAY); |
| 495 |
7 |
| SPAGAIN; |
| 496 |
|
| } |
| 497 |
|
| } |
| 498 |
|
| |
| 499 |
17 |
| hlist_del(hl, ckey); |
| 500 |
|
| GLOG(("=X= remove_header: deleted key [%s]", ckey)); |
| 501 |
|
| } |
| 502 |
|
| |
| 503 |
15 |
▶ | if (GIMME_V == G_SCALAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_SCALAR) { |
5 |
10 |
15 |
|
| 504 |
|
| GLOG(("=X= remove_header: returning count %d", total)); |
| 505 |
5 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
5 |
5 |
|
| 506 |
5 |
| PUSHs(sv_2mortal(newSViv(total))); |
| 507 |
|
| } |
| 508 |
|
| |
| 509 |
|
| |
| 510 |
|
| # |
| 511 |
|
| # remove_content_headers |
| 512 |
|
| # |
| 513 |
|
| void |
| 514 |
|
| remove_content_headers(HList* hl, ...) |
| 515 |
|
| PREINIT: |
| 516 |
6 |
| HList* to = 0; |
| 517 |
6 |
| HNode* n = 0; |
| 518 |
|
| int j; |
| 519 |
|
| |
| 520 |
|
| CODE: |
| 521 |
|
| GLOG(("=X= @@@ remove_content_headers(%p|%d)", |
| 522 |
|
| hl, hlist_size(hl))); |
| 523 |
|
| |
| 524 |
4 |
▶ | if (!(to = hlist_create())) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(to = hlist_create())) |
0 |
4 |
4 |
|
| 525 |
0 |
| croak("Could not create new HList object"); |
| 526 |
|
| |
| 527 |
4 |
| ST(0) = newSV_HList(to, SvSTASH(SvRV(ST(0)))); |
| 528 |
|
| |
| 529 |
38 |
▶ | for (j = 0; j < hl->ulen; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hl->ulen; ) { |
34 |
4 |
38 |
|
| 530 |
34 |
| n = &hl->data[j]; |
| 531 |
34 |
▶ | if (! header_is_entity(n->header)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (! header_is_entity(n->header)) { |
14 |
20 |
34 |
|
| 532 |
14 |
| ++j; |
| 533 |
14 |
| continue; |
| 534 |
|
| } |
| 535 |
20 |
| hlist_transfer_header(hl, j, to); |
| 536 |
|
| } |
| 537 |
|
| |
| 538 |
4 |
| XSRETURN(1); |
| 539 |
|
| |
| 540 |
|
| |
| 541 |
|
| const char* |
| 542 |
|
| as_string(HList* hl, ...) |
| 543 |
|
| PREINIT: |
| 544 |
32 |
| char* str = 0; |
| 545 |
32 |
▶ | int size = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int size = 0; |
32 |
0 |
32 |
|
| 546 |
|
| |
| 547 |
|
| CODE: |
| 548 |
|
| GLOG(("=X= @@@ as_string(%p|%d) %d", hl, hlist_size(hl), items)); |
| 549 |
|
| |
| 550 |
30 |
| const char* cendl = "\n"; |
| 551 |
30 |
▶ | if ( items > 1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( items > 1 ) { |
3 |
27 |
30 |
|
| 552 |
3 |
| SV* pendl = ST(1); |
| 553 |
3 |
| cendl = SvPV_nolen(pendl); |
| 554 |
|
| } |
| 555 |
|
| |
| 556 |
30 |
| str = format_all(aTHX_ hl, 1, cendl, &size); |
| 557 |
30 |
| RETVAL = str; |
| 558 |
|
| |
| 559 |
|
| OUTPUT: RETVAL |
| 560 |
|
| |
| 561 |
|
| CLEANUP: |
| 562 |
30 |
| GMEM_DEL(str, char*, size); |
| 563 |
|
| |
| 564 |
|
| |
| 565 |
|
| const char* |
| 566 |
|
| as_string_without_sort(HList* hl, ...) |
| 567 |
|
| PREINIT: |
| 568 |
4 |
| char* str = 0; |
| 569 |
4 |
▶ | int size = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int size = 0; |
4 |
0 |
4 |
|
| 570 |
|
| |
| 571 |
|
| CODE: |
| 572 |
|
| GLOG(("=X= @@@ as_string_without_sort(%p|%d) %d", hl, hlist_size(hl), items)); |
| 573 |
|
| |
| 574 |
2 |
| const char* cendl = "\n"; |
| 575 |
2 |
▶ | if ( items > 1 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( items > 1 ) { |
0 |
2 |
2 |
|
| 576 |
0 |
| SV* pendl = ST(1); |
| 577 |
0 |
| cendl = SvPV_nolen(pendl); |
| 578 |
|
| } |
| 579 |
|
| |
| 580 |
2 |
| str = format_all(aTHX_ hl, 0, cendl, &size); |
| 581 |
2 |
| RETVAL = str; |
| 582 |
|
| |
| 583 |
|
| OUTPUT: RETVAL |
| 584 |
|
| |
| 585 |
|
| CLEANUP: |
| 586 |
2 |
| GMEM_DEL(str, char*, size); |
| 587 |
|
| |
| 588 |
|
| |
| 589 |
|
| SV* |
| 590 |
|
| psgi_flatten(HList* hl) |
| 591 |
|
| PREINIT: |
| 592 |
2 |
| AV* av = 0; |
| 593 |
|
| int j; |
| 594 |
|
| int k; |
| 595 |
|
| CODE: |
| 596 |
|
| GLOG(("=X= @@@ psgi_flatten(%p|%d)", hl, hlist_size(hl))); |
| 597 |
2 |
| hlist_sort(hl); |
| 598 |
2 |
| av = newAV(); |
| 599 |
5 |
▶ | for (j = 0; j < hl->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hl->ulen; ++j) { |
3 |
2 |
5 |
|
| 600 |
3 |
| HNode* hn = &hl->data[j]; |
| 601 |
3 |
| const char* header = hn->header->name; |
| 602 |
3 |
| PList* pl = hn->values; |
| 603 |
7 |
▶ | for (k = 0; k < pl->ulen; ++k) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < pl->ulen; ++k) { |
4 |
3 |
7 |
|
| 604 |
4 |
| PNode* pn = &pl->data[k]; |
| 605 |
4 |
| SV* value = (SV*) pn->ptr; |
| 606 |
4 |
| av_push(av, newSVpv(header, 0)); |
| 607 |
4 |
| av_push(av, newSVsv(value)); |
| 608 |
|
| } |
| 609 |
|
| } |
| 610 |
2 |
| RETVAL = newRV_noinc((SV*) av); |
| 611 |
|
| OUTPUT: RETVAL |
| 612 |
|
| |
| 613 |
|
| |
| 614 |
|
| SV* |
| 615 |
|
| psgi_flatten_without_sort(HList* hl) |
| 616 |
|
| PREINIT: |
| 617 |
2 |
| AV* av = 0; |
| 618 |
|
| int j; |
| 619 |
|
| int k; |
| 620 |
|
| CODE: |
| 621 |
|
| GLOG(("=X= @@@ psgi_flatten_without_sort(%p|%d)", hl, hlist_size(hl))); |
| 622 |
2 |
| av = newAV(); |
| 623 |
5 |
▶ | for (j = 0; j < hl->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hl->ulen; ++j) { |
3 |
2 |
5 |
|
| 624 |
3 |
| HNode* hn = &hl->data[j]; |
| 625 |
3 |
| const char* header = hn->header->name; |
| 626 |
3 |
| PList* pl = hn->values; |
| 627 |
7 |
▶ | for (k = 0; k < pl->ulen; ++k) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < pl->ulen; ++k) { |
4 |
3 |
7 |
|
| 628 |
4 |
| PNode* pn = &pl->data[k]; |
| 629 |
4 |
| SV* value = (SV*) pn->ptr; |
| 630 |
4 |
| av_push(av, newSVpv(header, 0)); |
| 631 |
4 |
| av_push(av, newSVsv(value)); |
| 632 |
|
| } |
| 633 |
|
| } |
| 634 |
2 |
| RETVAL = newRV_noinc((SV*) av); |
| 635 |
|
| OUTPUT: RETVAL |
| 636 |
|
| |
| 637 |
|
| |
| 638 |
|
| void |
| 639 |
|
| scan(HList* hl, SV* sub) |
| 640 |
|
| PREINIT: |
| 641 |
|
| int j; |
| 642 |
|
| int k; |
| 643 |
|
| |
| 644 |
|
| CODE: |
| 645 |
|
| GLOG(("=X= @@@ scan(%p|%d)", hl, hlist_size(hl))); |
| 646 |
|
| |
| 647 |
6 |
▶ | if (!SvOK(sub) || !SvRV(sub) || SvTYPE( SvRV(sub) ) != SVt_PVCV ) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvOK(sub) || !SvRV(sub) || SvTYPE( SvRV(sub) ) != SVt_PVCV ) { |
5 |
1 |
6 |
| if (!SvOK(sub) || !SvRV(sub) || SvTYPE( SvRV(sub) ) != SVt_PVCV ) { |
5 |
0 |
5 |
| if (!SvOK(sub) || !SvRV(sub) || SvTYPE( SvRV(sub) ) != SVt_PVCV ) { |
0 |
5 |
5 |
|
| 648 |
1 |
| croak("Second argument must be a CODE reference"); |
| 649 |
|
| } |
| 650 |
|
| |
| 651 |
5 |
| hlist_sort(hl); |
| 652 |
19 |
▶ | for (j = 0; j < hl->ulen; ++j) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < hl->ulen; ++j) { |
15 |
4 |
19 |
|
| 653 |
15 |
| HNode* hn = &hl->data[j]; |
| 654 |
15 |
| const char* header = hn->header->name; |
| 655 |
15 |
| SV* pheader = sv_2mortal(newSVpv(header, 0)); |
| 656 |
15 |
| PList* pl = hn->values; |
| 657 |
33 |
▶ | for (k = 0; k < pl->ulen; ++k) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < pl->ulen; ++k) { |
19 |
14 |
33 |
|
| 658 |
19 |
| PNode* pn = &pl->data[k]; |
| 659 |
19 |
| SV* value = (SV*) pn->ptr; |
| 660 |
|
| |
| 661 |
19 |
| ENTER; |
| 662 |
19 |
| SAVETMPS; |
| 663 |
|
| |
| 664 |
19 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
19 |
19 |
|
| 665 |
19 |
| PUSHs( pheader ); |
| 666 |
19 |
| PUSHs( value ); |
| 667 |
19 |
| PUTBACK; |
| 668 |
19 |
| call_sv( (SV *)SvRV(sub), G_DISCARD ); |
| 669 |
|
| |
| 670 |
18 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
0 |
18 |
18 |
|
| 671 |
18 |
| LEAVE; |
| 672 |
|
| } |
| 673 |
|
| } |