xs/response.xs

filter: Statementstmt 88.8% 32 / 36 Branchbran 58.6% 34 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.2% 66 / 94 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/abi.xs ↑ Summary include/fetch/ft_json.h »
1 MODULE = Fetch PACKAGE = Fetch::Response
2  
3 # A Fetch::Response is a blessed hashref built in C (ft_http.h):
4 # status => integer status code
5 # headers => arrayref [ key, value, key, value, ... ]
6 # content => response body bytes
7  
8 SV *
9 status(self)
10 SV *self
11 CODE:
12 {
13 37 SV **e = hv_fetchs((HV *)SvRV(self), "status", 0);
14 37 ▶ RETVAL = (e && *e) ? newSVsv(*e) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); 37 0 37
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); 37 0 37
15 }
16 OUTPUT:
17 RETVAL
18  
19 SV *
20 content(self)
21 SV *self
22 CODE:
23 {
24 71 SV **e = hv_fetchs((HV *)SvRV(self), "content", 0);
25 71 ▶ RETVAL = (e && *e) ? newSVsv(*e) : newSV(0);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); 71 0 71
RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); 71 0 71
26 }
27 OUTPUT:
28 RETVAL
29  
30 # Decode the body as JSON into a Perl structure (dies on malformed JSON).
31 SV *
32 json(self)
33 SV *self
34 CODE:
35 {
36 1 SV **e = hv_fetchs((HV *)SvRV(self), "content", 0);
37 1 ▶ RETVAL = ft_json_decode(aTHX_ (e && *e) ? *e : &PL_sv_undef);
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = ft_json_decode(aTHX_ (e && *e) ? *e : &PL_sv_undef); 1 0 1
RETVAL = ft_json_decode(aTHX_ (e && *e) ? *e : &PL_sv_undef); 1 0 1
38 }
39 OUTPUT:
40 RETVAL
41  
42 int
43 is_success(self)
44 SV *self
45 ALIAS:
46 is_redirect = 1
47 CODE:
48 {
49 4 SV **e = hv_fetchs((HV *)SvRV(self), "status", 0);
50 4 ▶ IV s = (e && *e) ? SvIV(*e) : 0;
Branch: 2 branches
BranchTrueFalseTotal
IV s = (e && *e) ? SvIV(*e) : 0; 4 0 4
IV s = (e && *e) ? SvIV(*e) : 0; 4 0 4
51 4 ▶ RETVAL = ix == 0 ? (s >= 200 && s < 300) : (s >= 300 && s < 400);
Branch: 6 branches
BranchTrueFalseTotal
RETVAL = ix == 0 ? (s >= 200 && s < 300) : (s >= 300 && s < 400); 3 1 4
RETVAL = ix == 0 ? (s >= 200 && s < 300) : (s >= 300 && s < 400); 3 0 3
RETVAL = ix == 0 ? (s >= 200 && s < 300) : (s >= 300 && s < 400); 2 1 3
RETVAL = ix == 0 ? (s >= 200 && s < 300) : (s >= 300 && s < 400); 1 0 1
RETVAL = ix == 0 ? (s >= 200 && s < 300) : (s >= 300 && s < 400); 1 0 1
RETVAL = ix == 0 ? (s >= 200 && s < 300) : (s >= 300 && s < 400); 0 4 4
52 }
53 OUTPUT:
54 RETVAL
55  
56 # The response headers as a Fetch::Headers. The C core builds a plain
57 # [k, v, k, v, ...] arrayref; bless it in place on first access (that class is
58 # itself such an arrayref, so @{$res->headers} still yields the flat list).
59 SV *
60 headers(self)
61 SV *self
62 CODE:
63 {
64 4 HV *hv = (HV *)SvRV(self);
65 4 SV **hp = hv_fetchs(hv, "headers", 0);
66 SV *h;
67 4 ▶ if (!(hp && *hp && SvROK(*hp))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(hp && *hp && SvROK(*hp))) { 4 0 4
if (!(hp && *hp && SvROK(*hp))) { 4 0 4
if (!(hp && *hp && SvROK(*hp))) { 0 4 4
68 /* defensive: hand back an empty Fetch::Headers */
69 0 AV *av = newAV();
70 0 h = sv_bless(newRV_noinc((SV *)av),
71 gv_stashpv("Fetch::Headers", GV_ADD));
72 0 (void)hv_stores(hv, "headers", h);
73 0 RETVAL = newSVsv(h);
74 } else {
75 4 h = *hp;
76 4 ▶ if (!SvOBJECT(SvRV(h)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOBJECT(SvRV(h))) 2 2 4
77 2 (void)sv_bless(h, gv_stashpv("Fetch::Headers", GV_ADD));
78 4 RETVAL = newSVsv(h);
79 }
80 }
81 OUTPUT:
82 RETVAL
83  
84 # Case-insensitive single-header lookup (first value).
85 SV *
86 header(self, name)
87 SV *self
88 SV *name
89 CODE:
90 {
91 STRLEN nl;
92 4 const char *ns = SvPV_const(name, nl);
93 4 SV **hp = hv_fetchs((HV *)SvRV(self), "headers", 0);
94 4 RETVAL = newSV(0);
95 4 ▶ if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 4 0 4
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 4 0 4
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 4 0 4
if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { 4 0 4
96 4 AV *av = (AV *)SvRV(*hp);
97 4 SSize_t idx = ft_hdr_find(aTHX_ av, ns, nl);
98 4 ▶ if (idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx >= 0) { 4 0 4
99 4 SV **v = av_fetch(av, idx + 1, 0);
100 4 ▶ if (v && *v) { SvREFCNT_dec(RETVAL); RETVAL = newSVsv(*v); }
Branch: 2 branches
BranchTrueFalseTotal
if (v && *v) { SvREFCNT_dec(RETVAL); RETVAL = newSVsv(*v); } 4 0 4
if (v && *v) { SvREFCNT_dec(RETVAL); RETVAL = newSVsv(*v); } 4 0 4
101 }
102 }
103 }
104 OUTPUT:
105 RETVAL
106  
107 # The response's trailer fields as a Fetch::Headers, or undef when there
108 # were none, which is the common case over every transport. Over HTTP/2 they
109 # come from the trailing HEADERS frame. Over HTTP/1.1 they come from the
110 # section after the last chunk (ft_chunk_feed), which a server sends to a
111 # client whose TE asked for it.
112 #
113 # Kept separate from headers() rather than merged: a trailer arrived AFTER the
114 # body, and a consumer that cares about the difference has to be able to tell.
115 # The case that needs them is gRPC, where the call status lives in the
116 # trailers and nowhere else - a gRPC response is HTTP 200 whether it succeeded
117 # or failed, so a client that cannot read them can only report success.
118 SV *
119 trailers(self)
120 SV *self
121 CODE:
122 {
123 6 HV *hv = (HV *)SvRV(self);
124 6 SV **hp = hv_fetchs(hv, "trailers", 0);
125 SV *h;
126 6 ▶ if (!(hp && *hp && SvROK(*hp))) XSRETURN_UNDEF;
Branch: 3 branches
BranchTrueFalseTotal
if (!(hp && *hp && SvROK(*hp))) XSRETURN_UNDEF; 5 1 6
if (!(hp && *hp && SvROK(*hp))) XSRETURN_UNDEF; 5 0 5
if (!(hp && *hp && SvROK(*hp))) XSRETURN_UNDEF; 0 5 5
127 5 h = *hp;
128 5 ▶ if (!SvOBJECT(SvRV(h)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOBJECT(SvRV(h))) 4 1 5
129 4 (void)sv_bless(h, gv_stashpv("Fetch::Headers", GV_ADD));
130 5 RETVAL = newSVsv(h);
131 }
132 OUTPUT:
133 RETVAL
« xs/abi.xs ↑ Summary include/fetch/ft_json.h »