| 1 |
|
| /* punk_afterres.h - work that runs after the client has the response. |
| 2 |
|
| * |
| 3 |
|
| * hook after_response => sub { my ($c, $resp) = @_; ... }; |
| 4 |
|
| * $c->after_response(sub { my ($c, $resp) = @_; ... }); |
| 5 |
|
| * |
| 6 |
|
| * `after_dispatch` sees the finalized triplet and may replace it, which means |
| 7 |
|
| * it necessarily runs BEFORE the response is written. There was no phase after |
| 8 |
|
| * it, and on an event loop that is the natural home for the work a request |
| 9 |
|
| * generates but the client is not waiting for: an audit row, a cache warm, a |
| 10 |
|
| * counter, an enqueue. Without one an application either pays for that work |
| 11 |
|
| * inside the response or reaches for a job queue for something that never |
| 12 |
|
| * needed durability. |
| 13 |
|
| * |
| 14 |
|
| * WHEN "after" is: |
| 15 |
|
| * |
| 16 |
|
| * - `psgix.cleanup` server (the PSGI extension): pushed onto |
| 17 |
|
| * psgix.cleanup.handlers, which the server runs once the response is |
| 18 |
|
| * complete. This is the exact promise, and it is the server's to keep. |
| 19 |
|
| * - a Hyperman worker: a zero-delay loop timer, so the callbacks run on the |
| 20 |
|
| * next pass of the loop - after the response has been handed to the |
| 21 |
|
| * kernel, and off the request's own path. |
| 22 |
|
| * - anywhere else: inline, immediately after the triplet is final. There is |
| 23 |
|
| * no loop to hand the work to and no server extension to take it, so the |
| 24 |
|
| * phase still runs in the right ORDER; only the promise that the client is |
| 25 |
|
| * no longer waiting is one the environment cannot make. |
| 26 |
|
| * |
| 27 |
|
| * A callback that dies is logged through the application's logger and the rest |
| 28 |
|
| * still run: there is no response left to turn into a 500, and a die that |
| 29 |
|
| * vanished silently here would be the worst of both. |
| 30 |
|
| * |
| 31 |
|
| * Must be included after hm_abi.h (the loop timer), punk_static.h |
| 32 |
|
| * (punk_closure) and punk_context.h (which forward-declares pk_after_res). |
| 33 |
|
| */ |
| 34 |
|
| |
| 35 |
|
| #ifndef PUNK_AFTERRES_H |
| 36 |
|
| #define PUNK_AFTERRES_H |
| 37 |
|
| |
| 38 |
|
| /* Run the callbacks. Each gets ($c, $resp); the response is already gone, so |
| 39 |
|
| * a return value has nowhere to go and is discarded. */ |
| 40 |
9 |
| static void pk_ar_run(pTHX_ SV *c, SV *resp, AV *cbs) { |
| 41 |
9 |
▶ | SSize_t i, n = cbs ? av_len(cbs) + 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| SSize_t i, n = cbs ? av_len(cbs) + 1 : 0; |
9 |
0 |
9 |
|
| 42 |
26 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
17 |
9 |
26 |
|
| 43 |
17 |
| SV **cp = av_fetch(cbs, i, 0); |
| 44 |
17 |
| int died = 0; |
| 45 |
|
| SV *r; |
| 46 |
17 |
▶ | if (!cp || !*cp || !SvOK(*cp)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cp || !*cp || !SvOK(*cp)) continue; |
17 |
0 |
17 |
| if (!cp || !*cp || !SvOK(*cp)) continue; |
17 |
0 |
17 |
| if (!cp || !*cp || !SvOK(*cp)) continue; |
0 |
17 |
17 |
|
| 47 |
17 |
| r = pcx_call2(aTHX_ *cp, c, resp, &died); |
| 48 |
17 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
17 |
0 |
17 |
|
| 49 |
17 |
▶ | if (died) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) { |
1 |
16 |
17 |
|
| 50 |
|
| /* The logger, not warn: an application that configured logging |
| 51 |
|
| * said where its diagnostics go, and this is a diagnostic. Its |
| 52 |
|
| * own failure is swallowed - there is nothing above to tell. */ |
| 53 |
1 |
▶ | SV *err = sv_2mortal(newSVsv(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *err = sv_2mortal(newSVsv(ERRSV)); |
1 |
0 |
1 |
|
| 54 |
1 |
| SV *msg = sv_2mortal(newSVpvs("after_response hook died: ")); |
| 55 |
1 |
| SV *log = NULL; |
| 56 |
1 |
| sv_catsv(msg, err); |
| 57 |
|
| { /* $c->log, trapped: everything from here down is a |
| 58 |
|
| * diagnostic, and a diagnostic that dies must not take the |
| 59 |
|
| * remaining callbacks with it */ |
| 60 |
1 |
| dSP; int count; |
| 61 |
1 |
| ENTER; SAVETMPS; |
| 62 |
1 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
1 |
1 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
1 |
1 |
|
| 63 |
1 |
| count = call_method("log", G_SCALAR | G_EVAL); |
| 64 |
1 |
| SPAGAIN; |
| 65 |
1 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
1 |
0 |
1 |
|
| 66 |
1 |
| SV *l = POPs; |
| 67 |
1 |
▶ | if (!SvTRUE(ERRSV) && SvOK(l)) log = newSVsv(l); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && SvOK(l)) log = newSVsv(l); |
1 |
0 |
1 |
| if (!SvTRUE(ERRSV) && SvOK(l)) log = newSVsv(l); |
1 |
0 |
1 |
| if (!SvTRUE(ERRSV) && SvOK(l)) log = newSVsv(l); |
1 |
0 |
1 |
|
| 68 |
|
| } |
| 69 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 70 |
|
| } |
| 71 |
1 |
▶ | if (log) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (log) { |
1 |
0 |
1 |
|
| 72 |
1 |
| dSP; |
| 73 |
1 |
| ENTER; SAVETMPS; |
| 74 |
1 |
▶ | PUSHMARK(SP); EXTEND(SP, 2); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
1 |
1 |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
1 |
1 |
|
| 75 |
1 |
| PUSHs(sv_2mortal(log)); |
| 76 |
1 |
| PUSHs(msg); |
| 77 |
1 |
| PUTBACK; |
| 78 |
1 |
| (void)call_method("error", G_VOID | G_EVAL | G_DISCARD); |
| 79 |
1 |
| SPAGAIN; |
| 80 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 81 |
|
| } |
| 82 |
|
| } |
| 83 |
|
| } |
| 84 |
9 |
| } |
| 85 |
|
| |
| 86 |
|
| /* the job, for the two deferred paths */ |
| 87 |
|
| typedef struct { |
| 88 |
|
| SV *c; |
| 89 |
|
| SV *resp; |
| 90 |
|
| AV *cbs; |
| 91 |
|
| } pk_ar_job; |
| 92 |
|
| |
| 93 |
2 |
| static pk_ar_job *pk_ar_job_new(pTHX_ SV *c, SV *resp, AV *cbs) { |
| 94 |
|
| pk_ar_job *j; |
| 95 |
2 |
| Newxz(j, 1, pk_ar_job); |
| 96 |
2 |
| j->c = SvREFCNT_inc_simple_NN(c); |
| 97 |
2 |
| j->resp = SvREFCNT_inc_simple_NN(resp); |
| 98 |
2 |
| j->cbs = cbs; /* takes the +1 */ |
| 99 |
2 |
| return j; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
2 |
| static void pk_ar_job_free(pTHX_ pk_ar_job *j) { |
| 103 |
2 |
| SvREFCNT_dec(j->c); |
| 104 |
2 |
| SvREFCNT_dec(j->resp); |
| 105 |
2 |
| SvREFCNT_dec((SV *)j->cbs); |
| 106 |
2 |
| Safefree(j); |
| 107 |
2 |
| } |
| 108 |
|
| |
| 109 |
2 |
| static void pk_ar_timer_cb_in(pTHX_ void *ud) { |
| 110 |
2 |
| pk_ar_job *j = (pk_ar_job *)ud; |
| 111 |
2 |
| pk_ar_run(aTHX_ j->c, j->resp, j->cbs); |
| 112 |
2 |
| pk_ar_job_free(aTHX_ j); |
| 113 |
2 |
| } |
| 114 |
|
| |
| 115 |
2 |
| static void pk_ar_timer_cb(pTHX_ void *ud) { |
| 116 |
2 |
| PK_LOOP_ENTER; |
| 117 |
2 |
| pk_ar_timer_cb_in(aTHX_ ud); |
| 118 |
2 |
▶ | PK_LOOP_LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PK_LOOP_LEAVE; |
1 |
1 |
2 |
|
| 119 |
2 |
| } |
| 120 |
|
| |
| 121 |
|
| /* The psgix.cleanup handler: the server calls it with the env, which this |
| 122 |
|
| * does not need - everything it runs on is in the capture. */ |
| 123 |
|
| XS_INTERNAL(pk_ar_cleanup_cb); |
| 124 |
1 |
| XS_INTERNAL(pk_ar_cleanup_cb) { |
| 125 |
1 |
| dXSARGS; |
| 126 |
1 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 127 |
1 |
▶ | SV **c = cap ? av_fetch(cap, 0, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **c = cap ? av_fetch(cap, 0, 0) : NULL; |
1 |
0 |
1 |
|
| 128 |
1 |
▶ | SV **rp = cap ? av_fetch(cap, 1, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **rp = cap ? av_fetch(cap, 1, 0) : NULL; |
1 |
0 |
1 |
|
| 129 |
1 |
▶ | SV **cbs = cap ? av_fetch(cap, 2, 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **cbs = cap ? av_fetch(cap, 2, 0) : NULL; |
1 |
0 |
1 |
|
| 130 |
|
| PERL_UNUSED_VAR(items); |
| 131 |
1 |
▶ | if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) |
Branch: 7 branches
| Branch | True | False | Total |
| if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) |
1 |
0 |
1 |
| if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) |
1 |
0 |
1 |
| if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) |
1 |
0 |
1 |
| if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) |
1 |
0 |
1 |
| if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) |
1 |
0 |
1 |
| if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) |
1 |
0 |
1 |
| if (c && *c && rp && *rp && cbs && *cbs && SvROK(*cbs)) |
1 |
0 |
1 |
|
| 132 |
1 |
| pk_ar_run(aTHX_ *c, *rp, (AV *)SvRV(*cbs)); |
| 133 |
1 |
| XSRETURN_EMPTY; |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
|
| /* The callbacks for this response: the application's own hooks first, then |
| 137 |
|
| * anything the request queued, in the order they were registered. Returns a |
| 138 |
|
| * new AV (+1), or NULL when there is nothing to run - which is the answer for |
| 139 |
|
| * every request in an application that uses none of this. */ |
| 140 |
218973 |
| static AV *pk_ar_collect(pTHX_ SV *c) { |
| 141 |
218973 |
| AV *av = pcx_av(aTHX_ c); |
| 142 |
218973 |
| SV *appsv = pcx_get(aTHX_ av, PCX_APP); |
| 143 |
218973 |
| SV *q = pcx_get(aTHX_ av, PCX_AFTER_RES); |
| 144 |
218973 |
| AV *out = NULL; |
| 145 |
|
| SSize_t i, n; |
| 146 |
|
| |
| 147 |
218973 |
▶ | if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { |
218973 |
0 |
218973 |
| if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { |
218973 |
0 |
218973 |
| if (appsv && SvROK(appsv) && SvTYPE(SvRV(appsv)) == SVt_PVHV) { |
218973 |
0 |
218973 |
|
| 148 |
218973 |
| SV **cp = hv_fetchs((HV *)SvRV(appsv), K_AFTER_RES_C, 0); |
| 149 |
218973 |
▶ | if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { |
9 |
218964 |
218973 |
| if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { |
9 |
0 |
9 |
| if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { |
9 |
0 |
9 |
| if (cp && *cp && SvROK(*cp) && SvTYPE(SvRV(*cp)) == SVt_PVAV) { |
9 |
0 |
9 |
|
| 150 |
9 |
| AV *chain = (AV *)SvRV(*cp); |
| 151 |
9 |
| n = av_len(chain) + 1; |
| 152 |
18 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
9 |
9 |
18 |
|
| 153 |
9 |
| SV **e = av_fetch(chain, i, 0); |
| 154 |
9 |
▶ | if (!e || !*e) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !*e) continue; |
9 |
0 |
9 |
| if (!e || !*e) continue; |
0 |
9 |
9 |
|
| 155 |
9 |
▶ | if (!out) out = newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) out = newAV(); |
9 |
0 |
9 |
|
| 156 |
9 |
| av_push(out, newSVsv(*e)); |
| 157 |
|
| } |
| 158 |
|
| } |
| 159 |
|
| } |
| 160 |
218973 |
▶ | if (q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV) { |
6 |
218967 |
218973 |
| if (q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV) { |
6 |
0 |
6 |
| if (q && SvROK(q) && SvTYPE(SvRV(q)) == SVt_PVAV) { |
6 |
0 |
6 |
|
| 161 |
6 |
| AV *queue = (AV *)SvRV(q); |
| 162 |
6 |
| n = av_len(queue) + 1; |
| 163 |
14 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
8 |
6 |
14 |
|
| 164 |
8 |
| SV **e = av_fetch(queue, i, 0); |
| 165 |
8 |
▶ | if (!e || !*e) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!e || !*e) continue; |
8 |
0 |
8 |
| if (!e || !*e) continue; |
0 |
8 |
8 |
|
| 166 |
8 |
▶ | if (!out) out = newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) out = newAV(); |
0 |
8 |
8 |
|
| 167 |
8 |
| av_push(out, newSVsv(*e)); |
| 168 |
|
| } |
| 169 |
|
| } |
| 170 |
218973 |
| return out; |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
|
| /* Called from punk_deliver, once, with the response that is about to be |
| 174 |
|
| * returned to the server. */ |
| 175 |
218973 |
| static void pk_after_res(pTHX_ SV *c, SV *resp) { |
| 176 |
218973 |
| AV *cbs = pk_ar_collect(aTHX_ c); |
| 177 |
|
| AV *av; |
| 178 |
|
| SV *envsv; |
| 179 |
|
| |
| 180 |
218973 |
▶ | if (!cbs) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cbs) return; |
218964 |
9 |
218973 |
|
| 181 |
9 |
| av = pcx_av(aTHX_ c); |
| 182 |
9 |
| envsv = pcx_get(aTHX_ av, PCX_ENV); |
| 183 |
|
| |
| 184 |
|
| /* 1. the server's own post-response phase */ |
| 185 |
9 |
▶ | if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { |
9 |
0 |
9 |
| if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { |
9 |
0 |
9 |
| if (envsv && SvROK(envsv) && SvTYPE(SvRV(envsv)) == SVt_PVHV) { |
9 |
0 |
9 |
|
| 186 |
9 |
| HV *env = (HV *)SvRV(envsv); |
| 187 |
9 |
| SV **cl = hv_fetchs(env, "psgix.cleanup", 0); |
| 188 |
9 |
▶ | if (cl && *cl && SvTRUE(*cl)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (cl && *cl && SvTRUE(*cl)) { |
1 |
8 |
9 |
| if (cl && *cl && SvTRUE(*cl)) { |
1 |
0 |
1 |
| if (cl && *cl && SvTRUE(*cl)) { |
1 |
0 |
1 |
|
| 189 |
1 |
| SV **hp = hv_fetchs(env, "psgix.cleanup.handlers", 0); |
| 190 |
1 |
| AV *handlers = NULL; |
| 191 |
1 |
▶ | 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) |
0 |
1 |
1 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) |
0 |
0 |
0 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) |
0 |
0 |
0 |
| if (hp && *hp && SvROK(*hp) && SvTYPE(SvRV(*hp)) == SVt_PVAV) |
0 |
0 |
0 |
|
| 192 |
0 |
| handlers = (AV *)SvRV(*hp); |
| 193 |
|
| else { |
| 194 |
1 |
| handlers = newAV(); |
| 195 |
1 |
| (void)hv_stores(env, "psgix.cleanup.handlers", |
| 196 |
|
| newRV_noinc((SV *)handlers)); |
| 197 |
|
| } |
| 198 |
|
| { |
| 199 |
1 |
| AV *cap = newAV(); |
| 200 |
1 |
| av_push(cap, newSVsv(c)); |
| 201 |
1 |
| av_push(cap, newSVsv(resp)); |
| 202 |
1 |
| av_push(cap, newRV_noinc((SV *)cbs)); /* takes the +1 */ |
| 203 |
1 |
| av_push(handlers, punk_closure(aTHX_ pk_ar_cleanup_cb, cap)); |
| 204 |
|
| } |
| 205 |
1 |
| return; |
| 206 |
|
| } |
| 207 |
|
| } |
| 208 |
|
| |
| 209 |
|
| /* 2. the worker's loop */ |
| 210 |
|
| { |
| 211 |
8 |
| const hm_abi *A = punk_hm(aTHX); |
| 212 |
8 |
▶ | void *loop = (A && A->cur_loop && A->timer) ? A->cur_loop(aTHX) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| void *loop = (A && A->cur_loop && A->timer) ? A->cur_loop(aTHX) : NULL; |
8 |
0 |
8 |
| void *loop = (A && A->cur_loop && A->timer) ? A->cur_loop(aTHX) : NULL; |
8 |
0 |
8 |
| void *loop = (A && A->cur_loop && A->timer) ? A->cur_loop(aTHX) : NULL; |
8 |
0 |
8 |
|
| 213 |
8 |
▶ | if (loop) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop) { |
2 |
6 |
8 |
|
| 214 |
2 |
| (void)A->timer(aTHX_ loop, 0.0, pk_ar_timer_cb, |
| 215 |
2 |
| pk_ar_job_new(aTHX_ c, resp, cbs)); |
| 216 |
2 |
| return; |
| 217 |
|
| } |
| 218 |
|
| } |
| 219 |
|
| |
| 220 |
|
| /* 3. here, now */ |
| 221 |
6 |
| pk_ar_run(aTHX_ c, resp, cbs); |
| 222 |
6 |
| SvREFCNT_dec((SV *)cbs); |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
|
| #endif /* PUNK_AFTERRES_H */ |