| 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 |
36 |
| SV **e = hv_fetchs((HV *)SvRV(self), "status", 0); |
| 14 |
36 |
▶ | RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
36 |
0 |
36 |
| RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
36 |
0 |
36 |
|
| 15 |
|
| } |
| 16 |
|
| OUTPUT: |
| 17 |
|
| RETVAL |
| 18 |
|
| |
| 19 |
|
| SV * |
| 20 |
|
| content(self) |
| 21 |
|
| SV *self |
| 22 |
|
| CODE: |
| 23 |
|
| { |
| 24 |
65 |
| SV **e = hv_fetchs((HV *)SvRV(self), "content", 0); |
| 25 |
65 |
▶ | RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
65 |
0 |
65 |
| RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
65 |
0 |
65 |
|
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
3 |
| HV *hv = (HV *)SvRV(self); |
| 65 |
3 |
| SV **hp = hv_fetchs(hv, "headers", 0); |
| 66 |
|
| SV *h; |
| 67 |
3 |
▶ | if (!(hp && *hp && SvROK(*hp))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(hp && *hp && SvROK(*hp))) { |
3 |
0 |
3 |
| if (!(hp && *hp && SvROK(*hp))) { |
3 |
0 |
3 |
| if (!(hp && *hp && SvROK(*hp))) { |
0 |
3 |
3 |
|
| 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 |
3 |
| h = *hp; |
| 76 |
3 |
▶ | if (!SvOBJECT(SvRV(h))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOBJECT(SvRV(h))) |
1 |
2 |
3 |
|
| 77 |
1 |
| (void)sv_bless(h, gv_stashpv("Fetch::Headers", GV_ADD)); |
| 78 |
3 |
| 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 |
2 |
| const char *ns = SvPV_const(name, nl); |
| 93 |
2 |
| SV **hp = hv_fetchs((HV *)SvRV(self), "headers", 0); |
| 94 |
2 |
| RETVAL = newSV(0); |
| 95 |
2 |
▶ | if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
2 |
0 |
2 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
2 |
0 |
2 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
2 |
0 |
2 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) { |
2 |
0 |
2 |
|
| 96 |
2 |
| AV *av = (AV *)SvRV(*hp); |
| 97 |
2 |
| SSize_t idx = ft_hdr_find(aTHX_ av, ns, nl); |
| 98 |
2 |
▶ | if (idx >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx >= 0) { |
2 |
0 |
2 |
|
| 99 |
2 |
| SV **v = av_fetch(av, idx + 1, 0); |
| 100 |
2 |
▶ | if (v && *v) { SvREFCNT_dec(RETVAL); RETVAL = newSVsv(*v); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (v && *v) { SvREFCNT_dec(RETVAL); RETVAL = newSVsv(*v); } |
2 |
0 |
2 |
| if (v && *v) { SvREFCNT_dec(RETVAL); RETVAL = newSVsv(*v); } |
2 |
0 |
2 |
|
| 101 |
|
| } |
| 102 |
|
| } |
| 103 |
|
| } |
| 104 |
|
| OUTPUT: |
| 105 |
|
| RETVAL |
| 106 |
|
| |
| 107 |
|
| # The response TRAILERS as a Fetch::Headers, or undef when the response |
| 108 |
|
| # carried none - which is every HTTP/1 response and most HTTP/2 ones. |
| 109 |
|
| # |
| 110 |
|
| # Kept separate from headers() rather than merged: a trailer arrived AFTER the |
| 111 |
|
| # body, and a consumer that cares about the difference has to be able to tell. |
| 112 |
|
| # The case that needs them is gRPC, where the call status lives in the |
| 113 |
|
| # trailers and nowhere else - a gRPC response is HTTP 200 whether it succeeded |
| 114 |
|
| # or failed, so a client that cannot read them can only report success. |
| 115 |
|
| SV * |
| 116 |
|
| trailers(self) |
| 117 |
|
| SV *self |
| 118 |
|
| CODE: |
| 119 |
|
| { |
| 120 |
0 |
| HV *hv = (HV *)SvRV(self); |
| 121 |
0 |
| SV **hp = hv_fetchs(hv, "trailers", 0); |
| 122 |
|
| SV *h; |
| 123 |
0 |
▶ | if (!(hp && *hp && SvROK(*hp))) XSRETURN_UNDEF; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(hp && *hp && SvROK(*hp))) XSRETURN_UNDEF; |
0 |
0 |
0 |
| if (!(hp && *hp && SvROK(*hp))) XSRETURN_UNDEF; |
0 |
0 |
0 |
| if (!(hp && *hp && SvROK(*hp))) XSRETURN_UNDEF; |
0 |
0 |
0 |
|
| 124 |
0 |
| h = *hp; |
| 125 |
0 |
▶ | if (!SvOBJECT(SvRV(h))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOBJECT(SvRV(h))) |
0 |
0 |
0 |
|
| 126 |
0 |
| (void)sv_bless(h, gv_stashpv("Fetch::Headers", GV_ADD)); |
| 127 |
0 |
| RETVAL = newSVsv(h); |
| 128 |
|
| } |
| 129 |
|
| OUTPUT: |
| 130 |
|
| RETVAL |