| 1 |
|
| #ifndef HM_STREAM_H |
| 2 |
|
| #define HM_STREAM_H |
| 3 |
|
| |
| 4 |
|
| /* ABI v6 stream handles: one transport-neutral way to write a response body |
| 5 |
|
| * a piece at a time. |
| 6 |
|
| * |
| 7 |
|
| * hm_detach hands over a file descriptor, and that is why it refuses HTTP/2 |
| 8 |
|
| * and TLS. Those refusals are right, not gaps: an h2 stream is one of many on |
| 9 |
|
| * a shared connection, so no fd means "this stream", and a TLS session's |
| 10 |
|
| * state belongs to the server rather than to the socket. The seam that works |
| 11 |
|
| * on every transport is therefore a HANDLE, and this file is it - one |
| 12 |
|
| * registry with a transport branch inside, so a caller never tests the |
| 13 |
|
| * protocol. |
| 14 |
|
| * |
| 15 |
|
| * Included from hm_core.h after hm_http2.h, because it is built on both |
| 16 |
|
| * halves: hm_start_stream / hm_stream_write / hm_stream_close for HTTP/1.1 |
| 17 |
|
| * and the nghttp2 data provider for HTTP/2. |
| 18 |
|
| * |
| 19 |
|
| * The handle belongs to whoever opened it, from stream_open until |
| 20 |
|
| * stream_close. Nothing else ever frees one - a connection that goes away |
| 21 |
|
| * only MARKS its handles dead and fires their abort callbacks, so a write |
| 22 |
|
| * arriving afterwards is an error return and not a read of freed memory. |
| 23 |
|
| * Every entry point validates the pointer by finding it on the live list |
| 24 |
|
| * first, which is also what keeps a recycled address from being mistaken for |
| 25 |
|
| * the handle that used to live there. */ |
| 26 |
|
| |
| 27 |
|
| #define HM_STREAM_H1 0 |
| 28 |
|
| #define HM_STREAM_H2 1 |
| 29 |
|
| |
| 30 |
|
| typedef struct hm_stream { |
| 31 |
|
| UV serial; /* names the handle in diagnostics; the list |
| 32 |
|
| * membership is what proves it is live */ |
| 33 |
|
| hm_loop *loop; |
| 34 |
|
| int fd; |
| 35 |
|
| UV cid; /* connection generation (the detach ticket) */ |
| 36 |
|
| int64_t sid; /* h2 stream id, or -1 for HTTP/1.1 */ |
| 37 |
|
| unsigned char kind; /* HM_STREAM_H1 / HM_STREAM_H2 */ |
| 38 |
|
| unsigned char dead; /* the stream is gone */ |
| 39 |
|
| unsigned char paused; /* over the high-water mark */ |
| 40 |
|
| unsigned char aborted; /* the abort callback has already fired */ |
| 41 |
|
| hm_abi_stream_cb drain_cb; void *drain_ud; |
| 42 |
|
| hm_abi_stream_cb abort_cb; void *abort_ud; |
| 43 |
|
| hm_abi_stream_data_cb data_cb; void *data_ud; /* v8, the read half */ |
| 44 |
|
| struct hm_stream *next; |
| 45 |
|
| } hm_stream; |
| 46 |
|
| |
| 47 |
|
| static hm_stream *hm_streams = NULL; |
| 48 |
|
| static UV hm_stream_serials = 0; |
| 49 |
|
| |
| 50 |
|
| /* Live iff it is on the list. A pointer that is not is either already |
| 51 |
|
| * closed or was never a handle, and either way must not be dereferenced. */ |
| 52 |
2138 |
| static hm_stream *hm_stream_of(void *h) { |
| 53 |
|
| hm_stream *s; |
| 54 |
2138 |
▶ | if (!h) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return NULL; |
2 |
2136 |
2138 |
|
| 55 |
2136 |
▶ | for (s = hm_streams; s; s = s->next) if ((void *)s == h) return s; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| for (s = hm_streams; s; s = s->next) if ((void *)s == h) return s; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2131 |
0 |
2131 |
| for (s = hm_streams; s; s = s->next) if ((void *)s == h) return s; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2131 |
5 |
2136 |
|
| 56 |
5 |
| return NULL; |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| /* The connection this handle names, or NULL if it has been closed or the fd |
| 60 |
|
| * was reused - the generation check is the whole point of carrying cid. */ |
| 61 |
4175 |
| static hm_conn *hm_stream_conn(hm_stream *s) { |
| 62 |
|
| hm_conn *c; |
| 63 |
|
| /* |
| 64 |
|
| # uncoverable branch true class:ignore_covered_err |
| 65 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 66 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 67 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 68 |
4175 |
▶ | if (!s->loop || s->fd < 0 || s->fd >= HM_MAXFD) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!s->loop || s->fd < 0 || s->fd >= HM_MAXFD) return NULL; |
4175 |
0 |
4175 |
| if (!s->loop || s->fd < 0 || s->fd >= HM_MAXFD) return NULL; |
4175 |
0 |
4175 |
| if (!s->loop || s->fd < 0 || s->fd >= HM_MAXFD) return NULL; |
0 |
4175 |
4175 |
|
| 69 |
4175 |
| c = s->loop->conns[s->fd]; |
| 70 |
|
| /* |
| 71 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 72 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 73 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 74 |
4175 |
▶ | return (c && c->id == s->cid && !c->detached) ? c : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (c && c->id == s->cid && !c->detached) ? c : NULL; |
4175 |
0 |
4175 |
| return (c && c->id == s->cid && !c->detached) ? c : NULL; |
4175 |
0 |
4175 |
| return (c && c->id == s->cid && !c->detached) ? c : NULL; |
4175 |
0 |
4175 |
|
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
1 |
| static void hm_stream_mark_dead(hm_stream *s) { |
| 78 |
1 |
▶ | if (s->dead) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->dead) return; |
0 |
1 |
1 |
|
| 79 |
1 |
| s->dead = 1; |
| 80 |
1 |
▶ | if (s->paused) { s->paused = 0; hm_stream_paused_n--; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->paused) { s->paused = 0; hm_stream_paused_n--; } |
0 |
1 |
1 |
|
| 81 |
|
| } |
| 82 |
|
| |
| 83 |
|
| /* Fire every pending abort callback. Each one may close its own handle, or |
| 84 |
|
| * anyone else's, so the list is re-scanned from the top after every call |
| 85 |
|
| * rather than walked with a saved next pointer. */ |
| 86 |
12 |
| static void hm_stream_fire_aborts(pTHX) { |
| 87 |
12 |
| int again = 1; |
| 88 |
37 |
▶ | while (again) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (again) { |
13 |
12 |
25 |
|
| 89 |
|
| hm_stream *s; |
| 90 |
13 |
| again = 0; |
| 91 |
25 |
▶ | for (s = hm_streams; s; s = s->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = hm_streams; s; s = s->next) { |
13 |
12 |
25 |
|
| 92 |
13 |
▶ | if (s->dead && !s->aborted && s->abort_cb) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (s->dead && !s->aborted && s->abort_cb) { |
3 |
10 |
13 |
| if (s->dead && !s->aborted && s->abort_cb) { |
1 |
2 |
3 |
| if (s->dead && !s->aborted && s->abort_cb) { |
1 |
0 |
1 |
|
| 93 |
1 |
| hm_abi_stream_cb cb = s->abort_cb; |
| 94 |
1 |
| void *ud = s->abort_ud; |
| 95 |
1 |
| s->aborted = 1; |
| 96 |
1 |
| cb(aTHX_ (void *)s, ud); |
| 97 |
1 |
| again = 1; |
| 98 |
1 |
| break; |
| 99 |
|
| } |
| 100 |
|
| } |
| 101 |
|
| } |
| 102 |
12 |
| } |
| 103 |
|
| |
| 104 |
|
| /* A whole connection went away (close, or a detach handing the fd on). */ |
| 105 |
12 |
| static void hm_stream_conn_gone(pTHX_ int fd, UV id) { |
| 106 |
|
| hm_stream *s; |
| 107 |
|
| /* |
| 108 |
|
| # uncoverable branch true |
| 109 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 110 |
12 |
▶ | if (!hm_stream_live_n) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hm_stream_live_n) return; |
0 |
12 |
12 |
|
| 111 |
24 |
▶ | for (s = hm_streams; s; s = s->next) |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = hm_streams; s; s = s->next) |
12 |
12 |
24 |
|
| 112 |
12 |
▶ | if (s->fd == fd && s->cid == id) hm_stream_mark_dead(s); |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->fd == fd && s->cid == id) hm_stream_mark_dead(s); |
2 |
10 |
12 |
| if (s->fd == fd && s->cid == id) hm_stream_mark_dead(s); |
1 |
1 |
2 |
|
| 113 |
12 |
| hm_stream_fire_aborts(aTHX); |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
|
| /* One h2 stream out of many went away - the peer reset it, or it finished. */ |
| 117 |
0 |
| static void hm_stream_h2_gone(pTHX_ int fd, UV id, int64_t sid) { |
| 118 |
|
| hm_stream *s; |
| 119 |
0 |
▶ | if (!hm_stream_live_n) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hm_stream_live_n) return; |
0 |
0 |
0 |
|
| 120 |
0 |
▶ | for (s = hm_streams; s; s = s->next) |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = hm_streams; s; s = s->next) |
0 |
0 |
0 |
|
| 121 |
|
| /* |
| 122 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 123 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 124 |
0 |
▶ | if (s->fd == fd && s->cid == id && s->sid == sid) hm_stream_mark_dead(s); |
Branch: 3 branches
| Branch | True | False | Total |
| if (s->fd == fd && s->cid == id && s->sid == sid) hm_stream_mark_dead(s); |
0 |
0 |
0 |
| if (s->fd == fd && s->cid == id && s->sid == sid) hm_stream_mark_dead(s); |
0 |
0 |
0 |
| if (s->fd == fd && s->cid == id && s->sid == sid) hm_stream_mark_dead(s); |
0 |
0 |
0 |
|
| 125 |
0 |
| hm_stream_fire_aborts(aTHX); |
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
|
| /* What is still waiting to leave on behalf of this handle. |
| 129 |
|
| * |
| 130 |
|
| * For HTTP/1.1 that is the connection's unwritten output. For HTTP/2 it is |
| 131 |
|
| * that PLUS what the stream itself is holding: nghttp2 frames a body only as |
| 132 |
|
| * fast as the peer's flow-control window allows, and everything it has not |
| 133 |
|
| * been allowed to frame yet sits in the stream's own buffer, which the |
| 134 |
|
| * connection's does not count. Measuring the connection alone, a write was |
| 135 |
|
| * never answered "full" over HTTP/2 and a producer never paused: the whole |
| 136 |
|
| * body was held for a client that had not asked for it yet. */ |
| 137 |
|
| static size_t hm_stream_h2_held(hm_conn *c, int64_t sid); |
| 138 |
|
| |
| 139 |
2074 |
| static size_t hm_stream_held(hm_stream *s, hm_conn *c) { |
| 140 |
2074 |
| size_t held = c->wlen - c->woff; |
| 141 |
2074 |
▶ | if (s->kind == HM_STREAM_H2) held += hm_stream_h2_held(c, s->sid); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->kind == HM_STREAM_H2) held += hm_stream_h2_held(c, s->sid); |
0 |
2074 |
2074 |
|
| 142 |
2074 |
| return held; |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| /* Below this a paused HTTP/2 producer is restarted. Half the mark it was |
| 146 |
|
| * stopped at, so it is not woken to write one frame and stopped again. */ |
| 147 |
|
| #define HM_STREAM_LOWAT (HM_ABI_STREAM_HIWAT / 2) |
| 148 |
|
| |
| 149 |
|
| /* Output that was over the high-water mark has gone out. Same re-scan rule |
| 150 |
|
| * as the aborts: a drain callback's whole job is to write more. */ |
| 151 |
1 |
| static void hm_stream_drained(pTHX_ hm_conn *c) { |
| 152 |
1 |
| int again = 1; |
| 153 |
1 |
| hm_loop *loop = c->loop; |
| 154 |
1 |
| int fd = c->fd; |
| 155 |
1 |
| UV id = c->id; |
| 156 |
995 |
▶ | while (again) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (again) { |
993 |
1 |
994 |
|
| 157 |
|
| hm_stream *s; |
| 158 |
993 |
| again = 0; |
| 159 |
|
| /* |
| 160 |
|
| # uncoverable branch true class:ignore_covered_err |
| 161 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 162 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 163 |
993 |
▶ | if (loop->conns[fd] != c || c->id != id) return; /* a callback closed it */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (loop->conns[fd] != c || c->id != id) return; /* a callback closed it */ |
993 |
0 |
993 |
| if (loop->conns[fd] != c || c->id != id) return; /* a callback closed it */ |
0 |
993 |
993 |
|
| 164 |
993 |
▶ | for (s = hm_streams; s; s = s->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = hm_streams; s; s = s->next) { |
992 |
1 |
993 |
|
| 165 |
|
| /* |
| 166 |
|
| # uncoverable branch false class:ignore_covered_err |
| 167 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 168 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 169 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 170 |
992 |
▶ | if (s->paused && s->fd == fd && s->cid == id) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (s->paused && s->fd == fd && s->cid == id) { |
992 |
0 |
992 |
| if (s->paused && s->fd == fd && s->cid == id) { |
992 |
0 |
992 |
| if (s->paused && s->fd == fd && s->cid == id) { |
992 |
0 |
992 |
|
| 171 |
|
| hm_abi_stream_cb cb; |
| 172 |
|
| void *ud; |
| 173 |
|
| /* an HTTP/2 stream is ready when ITS output has gone, which |
| 174 |
|
| * the connection's buffer emptying does not say */ |
| 175 |
992 |
▶ | if (s->kind == HM_STREAM_H2 |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->kind == HM_STREAM_H2 |
0 |
992 |
992 |
|
| 176 |
0 |
▶ | && hm_stream_held(s, c) > (size_t)HM_STREAM_LOWAT) |
Branch: 1 branch
| Branch | True | False | Total |
| && hm_stream_held(s, c) > (size_t)HM_STREAM_LOWAT) |
0 |
0 |
0 |
|
| 177 |
0 |
| continue; |
| 178 |
992 |
| cb = s->drain_cb; |
| 179 |
992 |
| ud = s->drain_ud; |
| 180 |
992 |
| s->paused = 0; |
| 181 |
992 |
| hm_stream_paused_n--; |
| 182 |
992 |
| again = 1; |
| 183 |
|
| /* |
| 184 |
|
| # uncoverable branch false class:ignore_covered_err |
| 185 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 186 |
992 |
▶ | if (cb) cb(aTHX_ (void *)s, ud); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb) cb(aTHX_ (void *)s, ud); |
992 |
0 |
992 |
|
| 187 |
992 |
| break; |
| 188 |
|
| } |
| 189 |
|
| } |
| 190 |
|
| } |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
|
| /* Take a handle off the registry without freeing it. Ending a body can close |
| 194 |
|
| * the connection underneath us - EOF-delimited HTTP/1.1 always does - and a |
| 195 |
|
| * handle that is deliberately finishing must not then be told its stream went |
| 196 |
|
| * away. So stream_close unlinks first, ends the body, and frees afterwards. */ |
| 197 |
19 |
| static void hm_stream_unlink(hm_stream *s) { |
| 198 |
19 |
| hm_stream **pp = &hm_streams; |
| 199 |
|
| /* |
| 200 |
|
| # uncoverable branch false class:ignore_covered_err |
| 201 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 202 |
19 |
▶ | while (*pp) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*pp) { |
19 |
0 |
19 |
|
| 203 |
|
| /* |
| 204 |
|
| # uncoverable branch false |
| 205 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 206 |
19 |
▶ | if (*pp == s) { *pp = s->next; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*pp == s) { *pp = s->next; break; } |
19 |
0 |
19 |
|
| 207 |
0 |
| pp = &(*pp)->next; /* # uncoverable statement */ |
| 208 |
|
| } |
| 209 |
19 |
| s->next = NULL; |
| 210 |
19 |
▶ | if (s->paused) { s->paused = 0; hm_stream_paused_n--; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->paused) { s->paused = 0; hm_stream_paused_n--; } |
1 |
18 |
19 |
|
| 211 |
19 |
| hm_stream_live_n--; |
| 212 |
19 |
| } |
| 213 |
|
| |
| 214 |
|
| /* ---- the HTTP/2 half ---------------------------------------------------- */ |
| 215 |
|
| |
| 216 |
|
| #ifdef HM_HAVE_NGHTTP2 |
| 217 |
|
| |
| 218 |
|
| static hm_h2_stream *hm_stream_h2_st(hm_conn *c, int64_t sid) { |
| 219 |
|
| hm_h2_sess *s; |
| 220 |
|
| /* |
| 221 |
|
| # uncoverable branch true class:ignore_covered_err |
| 222 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 223 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 224 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 225 |
|
| if (!c->h2 || sid < 0 || sid > 0x7fffffff) return NULL; |
| 226 |
|
| s = (hm_h2_sess *)c->h2; |
| 227 |
|
| return (hm_h2_stream *)nghttp2_session_get_stream_user_data(s->session, |
| 228 |
|
| (int32_t)sid); |
| 229 |
|
| } |
| 230 |
|
| |
| 231 |
|
| /* The body bytes this stream has been given and nghttp2 has not yet framed. */ |
| 232 |
|
| static size_t hm_stream_h2_held(hm_conn *c, int64_t sid) { |
| 233 |
|
| hm_h2_stream *st = hm_stream_h2_st(c, sid); |
| 234 |
|
| STRLEN blen; |
| 235 |
|
| /* |
| 236 |
|
| # uncoverable branch true class:ignore_covered_err |
| 237 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 238 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 239 |
|
| if (!st || !st->resp_body) return 0; |
| 240 |
|
| blen = SvCUR(st->resp_body); |
| 241 |
|
| return blen > st->resp_off ? (size_t)(blen - st->resp_off) : 0; |
| 242 |
|
| } |
| 243 |
|
| |
| 244 |
|
| /* Submit status + headers now, with a data provider that defers until there |
| 245 |
|
| * are bytes. hm_h2_submit_response already drops hop-by-hop names and hands |
| 246 |
|
| * the rest to nghttp2 as nghttp2_nv[], which is why headers travel as a list |
| 247 |
|
| * all the way down here instead of as a byte string. */ |
| 248 |
|
| static int hm_stream_h2_begin(pTHX_ hm_conn *c, int64_t sid, int status, |
| 249 |
|
| SV *headers) { |
| 250 |
|
| hm_h2_sess *s = (hm_h2_sess *)c->h2; |
| 251 |
|
| hm_h2_stream *st = hm_stream_h2_st(c, sid); |
| 252 |
|
| AV *hav = (headers && SvROK(headers) && SvTYPE(SvRV(headers)) == SVt_PVAV) |
| 253 |
|
| ? (AV *)SvRV(headers) : NULL; |
| 254 |
|
| /* |
| 255 |
|
| # uncoverable branch true |
| 256 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 257 |
|
| if (!st) return 0; |
| 258 |
|
| /* |
| 259 |
|
| # uncoverable branch true |
| 260 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 261 |
|
| if (st->status) return 0; /* a response has already been submitted */ |
| 262 |
|
| /* |
| 263 |
|
| # uncoverable branch true class:ignore_covered_err |
| 264 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 265 |
|
| if (st->resp_body) { SvREFCNT_dec(st->resp_body); st->resp_body = NULL; } |
| 266 |
|
| hm_h2_src_free(aTHX_ st); |
| 267 |
|
| st->resp_body = newSVpvs(""); |
| 268 |
|
| st->blen = 0; |
| 269 |
|
| st->producing = 1; |
| 270 |
|
| st->prod_done = 0; |
| 271 |
|
| hm_h2_submit_response(aTHX_ s, st, status, hav); |
| 272 |
|
| hm_h2_flush_send(aTHX_ s); |
| 273 |
|
| return 1; |
| 274 |
|
| } |
| 275 |
|
| |
| 276 |
|
| static int hm_stream_h2_feed(pTHX_ hm_conn *c, int64_t sid, |
| 277 |
|
| const char *buf, STRLEN len, int done) { |
| 278 |
|
| hm_h2_sess *s = (hm_h2_sess *)c->h2; |
| 279 |
|
| hm_h2_stream *st = hm_stream_h2_st(c, sid); |
| 280 |
|
| /* |
| 281 |
|
| # uncoverable branch true |
| 282 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 283 |
|
| if (!st) return 0; |
| 284 |
|
| if (len) { |
| 285 |
|
| sv_catpvn(st->resp_body, buf, len); |
| 286 |
|
| st->blen += len; |
| 287 |
|
| c->loop->bytes_out += len; |
| 288 |
|
| } |
| 289 |
|
| if (done) st->prod_done = 1; |
| 290 |
|
| nghttp2_session_resume_data(s->session, (int32_t)sid); |
| 291 |
|
| hm_h2_flush_send(aTHX_ s); |
| 292 |
|
| return 1; |
| 293 |
|
| } |
| 294 |
|
| |
| 295 |
|
| /* Kill one stream and leave the rest of the connection alone. INTERNAL_ERROR |
| 296 |
|
| * is the code for "the server failed", which is what an aborted producer is; |
| 297 |
|
| * a client that has already had the headers reports the response as |
| 298 |
|
| * incomplete rather than as a short success. */ |
| 299 |
|
| static int hm_stream_h2_reset(pTHX_ hm_conn *c, int64_t sid) { |
| 300 |
|
| hm_h2_sess *s = (hm_h2_sess *)c->h2; |
| 301 |
|
| hm_h2_stream *st = hm_stream_h2_st(c, sid); |
| 302 |
|
| /* |
| 303 |
|
| # uncoverable branch true |
| 304 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 305 |
|
| if (!st) return 0; |
| 306 |
|
| st->producing = 0; /* the data provider must stop deferring */ |
| 307 |
|
| st->prod_done = 1; |
| 308 |
|
| nghttp2_submit_rst_stream(s->session, NGHTTP2_FLAG_NONE, (int32_t)sid, |
| 309 |
|
| NGHTTP2_INTERNAL_ERROR); |
| 310 |
|
| hm_h2_flush_send(aTHX_ s); |
| 311 |
|
| return 1; |
| 312 |
|
| } |
| 313 |
|
| |
| 314 |
|
| /* What an Extended CONNECT stream was sent before its read half existed. |
| 315 |
|
| * Requests are dispatched after nghttp2_session_mem_recv returns, so a DATA |
| 316 |
|
| * frame read in the same burst as the CONNECT headers is parsed first, and |
| 317 |
|
| * with no handle to take it, it was buffered as the request body. Handed |
| 318 |
|
| * over, owned by the caller, and the stream forgets it. Not while the |
| 319 |
|
| * stream's handler is still running: it has not wired its events yet, and |
| 320 |
|
| * hm_h2_dispatch hands the bytes over when it returns. */ |
| 321 |
|
| static SV *hm_stream_h2_take_early(pTHX_ hm_conn *c, int64_t sid) { |
| 322 |
|
| hm_h2_stream *st = hm_stream_h2_st(c, sid); |
| 323 |
|
| SV *b; |
| 324 |
|
| PERL_UNUSED_CONTEXT; |
| 325 |
|
| /* |
| 326 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 327 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 328 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 329 |
|
| # uncoverable condition left count:5 class:ignore_covered_err */ |
| 330 |
|
| if (!st || !st->tunnel || st->in_app |
| 331 |
|
| || !st->body || !SvCUR(st->body)) return NULL; |
| 332 |
|
| b = st->body; |
| 333 |
|
| st->body = NULL; |
| 334 |
|
| return b; |
| 335 |
|
| } |
| 336 |
|
| |
| 337 |
|
| #else /* !HM_HAVE_NGHTTP2: there is no h2 transport to branch to */ |
| 338 |
|
| |
| 339 |
0 |
| static int hm_stream_h2_begin(pTHX_ hm_conn *c, int64_t sid, int status, |
| 340 |
|
| SV *headers) { |
| 341 |
|
| PERL_UNUSED_CONTEXT; |
| 342 |
|
| (void)c; (void)sid; (void)status; (void)headers; |
| 343 |
0 |
| return 0; |
| 344 |
|
| } |
| 345 |
0 |
| static int hm_stream_h2_feed(pTHX_ hm_conn *c, int64_t sid, |
| 346 |
|
| const char *buf, STRLEN len, int done) { |
| 347 |
|
| PERL_UNUSED_CONTEXT; |
| 348 |
|
| (void)c; (void)sid; (void)buf; (void)len; (void)done; |
| 349 |
0 |
| return 0; |
| 350 |
|
| } |
| 351 |
0 |
| static int hm_stream_h2_reset(pTHX_ hm_conn *c, int64_t sid) { |
| 352 |
|
| PERL_UNUSED_CONTEXT; |
| 353 |
|
| (void)c; (void)sid; |
| 354 |
0 |
| return 0; |
| 355 |
|
| } |
| 356 |
0 |
| static SV *hm_stream_h2_take_early(pTHX_ hm_conn *c, int64_t sid) { |
| 357 |
|
| PERL_UNUSED_CONTEXT; |
| 358 |
|
| (void)c; (void)sid; |
| 359 |
0 |
| return NULL; |
| 360 |
|
| } |
| 361 |
0 |
| static size_t hm_stream_h2_held(hm_conn *c, int64_t sid) { |
| 362 |
|
| (void)c; (void)sid; |
| 363 |
0 |
| return 0; |
| 364 |
|
| } |
| 365 |
|
| |
| 366 |
|
| #endif /* HM_HAVE_NGHTTP2 */ |
| 367 |
|
| |
| 368 |
|
| /* psgix.hyperman.stream -> (fd, generation, stream id). 1 if the env carried |
| 369 |
|
| * a well-formed ticket. Shared by the two doors so they cannot drift. */ |
| 370 |
22 |
| static int hm_stream_ticket(pTHX_ SV *env, int *fd, UV *id, int64_t *sid) { |
| 371 |
|
| HV *ehv; |
| 372 |
|
| AV *tick; |
| 373 |
|
| SV **e, **fsv, **isv, **ssv; |
| 374 |
|
| /* |
| 375 |
|
| # uncoverable branch true class:ignore_covered_err |
| 376 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 377 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 378 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 379 |
22 |
▶ | if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return 0; |
22 |
0 |
22 |
| if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return 0; |
22 |
0 |
22 |
| if (!(env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)) return 0; |
0 |
22 |
22 |
|
| 380 |
22 |
| ehv = (HV *)SvRV(env); |
| 381 |
22 |
| e = hv_fetchs(ehv, "psgix.hyperman.stream", 0); |
| 382 |
|
| /* |
| 383 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 384 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 385 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 386 |
22 |
▶ | if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; |
21 |
1 |
22 |
| if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; |
21 |
0 |
21 |
| if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; |
21 |
0 |
21 |
| if (!(e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV)) return 0; |
0 |
21 |
21 |
|
| 387 |
21 |
| tick = (AV *)SvRV(*e); |
| 388 |
21 |
| fsv = av_fetch(tick, 0, 0); |
| 389 |
21 |
| isv = av_fetch(tick, 1, 0); |
| 390 |
21 |
| ssv = av_fetch(tick, 2, 0); |
| 391 |
|
| /* |
| 392 |
|
| # uncoverable branch true class:ignore_covered_err |
| 393 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 394 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 395 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 396 |
|
| # uncoverable condition left count:4 class:ignore_covered_err */ |
| 397 |
21 |
▶ | if (!(fsv && *fsv && isv && *isv)) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(fsv && *fsv && isv && *isv)) return 0; |
21 |
0 |
21 |
| if (!(fsv && *fsv && isv && *isv)) return 0; |
21 |
0 |
21 |
| if (!(fsv && *fsv && isv && *isv)) return 0; |
21 |
0 |
21 |
| if (!(fsv && *fsv && isv && *isv)) return 0; |
0 |
21 |
21 |
|
| 398 |
21 |
| *fd = (int)SvIV(*fsv); |
| 399 |
21 |
| *id = SvUV(*isv); |
| 400 |
|
| /* |
| 401 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 402 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 403 |
21 |
▶ | *sid = (ssv && *ssv) ? (int64_t)SvIV(*ssv) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| *sid = (ssv && *ssv) ? (int64_t)SvIV(*ssv) : -1; |
21 |
0 |
21 |
| *sid = (ssv && *ssv) ? (int64_t)SvIV(*ssv) : -1; |
21 |
0 |
21 |
|
| 404 |
21 |
| return 1; |
| 405 |
|
| } |
| 406 |
|
| |
| 407 |
|
| /* ---- the entry points behind the ABI table ------------------------------ */ |
| 408 |
|
| |
| 409 |
22 |
| static void *hm_stream_open(pTHX_ void *vl, int fd, UV id, int64_t sid, |
| 410 |
|
| int status, SV *headers) { |
| 411 |
22 |
▶ | hm_loop *loop = vl ? (hm_loop *)vl : hm_cur_loop; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| hm_loop *loop = vl ? (hm_loop *)vl : hm_cur_loop; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
22 |
0 |
22 |
|
| 412 |
|
| /* |
| 413 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 414 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 415 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 416 |
22 |
▶ | hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; |
22 |
0 |
22 |
| hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; |
22 |
0 |
22 |
| hm_conn *c = (loop && fd >= 0 && fd < HM_MAXFD) ? loop->conns[fd] : NULL; |
22 |
0 |
22 |
|
| 417 |
|
| hm_stream *s; |
| 418 |
|
| /* |
| 419 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 420 |
|
| # uncoverable condition left count:3 class:ignore_covered_err */ |
| 421 |
22 |
▶ | if (!(c && c->id == id) || c->detached) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(c && c->id == id) || c->detached) return NULL; |
20 |
2 |
22 |
| if (!(c && c->id == id) || c->detached) return NULL; |
20 |
0 |
20 |
| if (!(c && c->id == id) || c->detached) return NULL; |
0 |
20 |
20 |
|
| 422 |
20 |
▶ | if (sid >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sid >= 0) { |
0 |
20 |
20 |
|
| 423 |
|
| /* |
| 424 |
|
| # uncoverable branch true |
| 425 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 426 |
0 |
▶ | if (!c->h2) return NULL; /* not a multiplexed conn */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->h2) return NULL; /* not a multiplexed conn */ |
0 |
0 |
0 |
|
| 427 |
|
| /* |
| 428 |
|
| # uncoverable branch true |
| 429 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 430 |
0 |
▶ | if (!hm_stream_h2_begin(aTHX_ c, sid, status, headers)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hm_stream_h2_begin(aTHX_ c, sid, status, headers)) return NULL; |
0 |
0 |
0 |
|
| 431 |
|
| } else { |
| 432 |
|
| /* |
| 433 |
|
| # uncoverable branch true |
| 434 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 435 |
20 |
▶ | if (c->h2) return NULL; /* h2 needs a stream id */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->h2) return NULL; /* h2 needs a stream id */ |
0 |
20 |
20 |
|
| 436 |
|
| /* The response has to be deferred already - a psgi.streaming |
| 437 |
|
| * coderef, or a handler parked on a Future. A synchronous handler's |
| 438 |
|
| * return value is still coming, and it would be serialised on top of |
| 439 |
|
| * the body being streamed here; refusing is the only honest answer, |
| 440 |
|
| * and it is one the caller can act on. |
| 441 |
|
| * |
| 442 |
|
| * Except for a 101. An upgrade has no body for a return value to |
| 443 |
|
| * land on top of: the connection becomes a tunnel (c->tunnel), |
| 444 |
|
| * hm_process drops what the handler returns exactly as it drops the |
| 445 |
|
| * sentinel a detach leaves, and what the peer sends from here on |
| 446 |
|
| * goes to the handle's read half. That is what lets a WebSocket run |
| 447 |
|
| * over TLS, which conn_detach refuses. */ |
| 448 |
20 |
▶ | if (!c->awaiting && status != 101) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!c->awaiting && status != 101) return NULL; |
7 |
13 |
20 |
| if (!c->awaiting && status != 101) return NULL; |
1 |
6 |
7 |
|
| 449 |
|
| /* |
| 450 |
|
| # uncoverable branch true |
| 451 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 452 |
19 |
▶ | if (c->stream_open) return NULL; /* one response per request */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->stream_open) return NULL; /* one response per request */ |
0 |
19 |
19 |
|
| 453 |
19 |
▶ | if (status == 101) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (status == 101) { |
9 |
10 |
19 |
|
| 454 |
|
| /* Parked before the headers go out: keepalive is off, and a |
| 455 |
|
| * flush on a connection that is neither keeping alive nor |
| 456 |
|
| * parked closes it as soon as it drains. 2 marks a tunnel |
| 457 |
|
| * opened inside the handler, whose request is still on the |
| 458 |
|
| * parser's plate; hm_process finishes with it and makes it 1. */ |
| 459 |
9 |
▶ | c->tunnel = c->awaiting ? 1 : 2; |
Branch: 1 branch
| Branch | True | False | Total |
| c->tunnel = c->awaiting ? 1 : 2; |
3 |
6 |
9 |
|
| 460 |
9 |
| c->awaiting = 1; |
| 461 |
|
| } |
| 462 |
|
| /* |
| 463 |
|
| # uncoverable branch true |
| 464 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 465 |
19 |
▶ | if (hm_start_stream(aTHX_ fd, id, status, headers) <= 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_start_stream(aTHX_ fd, id, status, headers) <= 0) return NULL; |
0 |
19 |
19 |
|
| 466 |
|
| } |
| 467 |
|
| /* |
| 468 |
|
| # uncoverable branch true |
| 469 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 470 |
19 |
▶ | if (loop->conns[fd] != c) return NULL; /* the flush closed it */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop->conns[fd] != c) return NULL; /* the flush closed it */ |
0 |
19 |
19 |
|
| 471 |
19 |
| s = (hm_stream *)hm_xcalloc(1, sizeof(hm_stream)); |
| 472 |
19 |
| s->serial = ++hm_stream_serials; |
| 473 |
19 |
| s->loop = loop; |
| 474 |
19 |
| s->fd = fd; |
| 475 |
19 |
| s->cid = id; |
| 476 |
19 |
| s->sid = sid; |
| 477 |
19 |
| s->kind = sid >= 0 ? HM_STREAM_H2 : HM_STREAM_H1; |
| 478 |
19 |
| s->next = hm_streams; |
| 479 |
19 |
| hm_streams = s; |
| 480 |
19 |
| hm_stream_live_n++; |
| 481 |
19 |
| return (void *)s; |
| 482 |
|
| } |
| 483 |
|
| |
| 484 |
|
| /* Backpressure is measured on the connection's unwritten output, which is |
| 485 |
|
| * the same quantity on both transports: for h2 nghttp2 has already framed |
| 486 |
|
| * what it could and the rest is sitting in the same wbuf. */ |
| 487 |
2074 |
| static int hm_stream_pressure(hm_stream *s, hm_conn *c) { |
| 488 |
2074 |
▶ | if (hm_stream_held(s, c) <= (size_t)HM_ABI_STREAM_HIWAT) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_stream_held(s, c) <= (size_t)HM_ABI_STREAM_HIWAT) |
1081 |
993 |
2074 |
|
| 489 |
1081 |
| return HM_ABI_STREAM_OK; |
| 490 |
|
| /* |
| 491 |
|
| # uncoverable branch false class:ignore_covered_err |
| 492 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 493 |
993 |
▶ | if (!s->paused) { s->paused = 1; hm_stream_paused_n++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->paused) { s->paused = 1; hm_stream_paused_n++; } |
993 |
0 |
993 |
|
| 494 |
993 |
| return HM_ABI_STREAM_FULL; |
| 495 |
|
| } |
| 496 |
|
| |
| 497 |
2077 |
| static int hm_stream_write_h(pTHX_ void *h, const char *buf, STRLEN len) { |
| 498 |
2077 |
| hm_stream *s = hm_stream_of(h); |
| 499 |
|
| hm_conn *c; |
| 500 |
2077 |
▶ | if (!s) return HM_ABI_STREAM_STALE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return HM_ABI_STREAM_STALE; |
2 |
2075 |
2077 |
|
| 501 |
|
| /* |
| 502 |
|
| # uncoverable branch true class:ignore_covered_err |
| 503 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 504 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 505 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 506 |
2075 |
▶ | if (!buf && len) return HM_ABI_STREAM_ARG; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!buf && len) return HM_ABI_STREAM_ARG; |
0 |
2075 |
2075 |
| if (!buf && len) return HM_ABI_STREAM_ARG; |
0 |
0 |
0 |
|
| 507 |
2075 |
▶ | if (s->dead) return HM_ABI_STREAM_GONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->dead) return HM_ABI_STREAM_GONE; |
1 |
2074 |
2075 |
|
| 508 |
2074 |
| c = hm_stream_conn(s); |
| 509 |
|
| /* |
| 510 |
|
| # uncoverable branch true |
| 511 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 512 |
2074 |
▶ | if (!c) { hm_stream_mark_dead(s); return HM_ABI_STREAM_GONE; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) { hm_stream_mark_dead(s); return HM_ABI_STREAM_GONE; } |
0 |
2074 |
2074 |
|
| 513 |
2074 |
▶ | if (s->kind == HM_STREAM_H2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->kind == HM_STREAM_H2) { |
0 |
2074 |
2074 |
|
| 514 |
|
| /* |
| 515 |
|
| # uncoverable branch true |
| 516 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 517 |
0 |
▶ | if (!hm_stream_h2_feed(aTHX_ c, s->sid, buf, len, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hm_stream_h2_feed(aTHX_ c, s->sid, buf, len, 0)) { |
0 |
0 |
0 |
|
| 518 |
0 |
| hm_stream_mark_dead(s); /* # uncoverable statement */ |
| 519 |
0 |
| return HM_ABI_STREAM_GONE; /* # uncoverable statement */ |
| 520 |
|
| } |
| 521 |
|
| /* |
| 522 |
|
| # uncoverable branch false class:ignore_covered_err |
| 523 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 524 |
2074 |
▶ | } else if (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (len) { |
2074 |
0 |
2074 |
|
| 525 |
|
| /* The same framing the Writer gets (chunked, counted or raw): one |
| 526 |
|
| * function decides, so a handle and a writer on the same connection |
| 527 |
|
| * cannot disagree. */ |
| 528 |
|
| /* |
| 529 |
|
| # uncoverable branch false class:ignore_covered_err |
| 530 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 531 |
2074 |
▶ | if (hm_stream_body_put(aTHX_ c, buf, len)) hm_flush(aTHX_ c); |
Branch: 1 branch
| Branch | True | False | Total |
| if (hm_stream_body_put(aTHX_ c, buf, len)) hm_flush(aTHX_ c); |
2074 |
0 |
2074 |
|
| 532 |
|
| } |
| 533 |
|
| /* Either flush can close the connection; re-read it rather than trusting |
| 534 |
|
| * the pointer we came in with. */ |
| 535 |
2074 |
| c = hm_stream_conn(s); |
| 536 |
|
| /* |
| 537 |
|
| # uncoverable branch true |
| 538 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 539 |
2074 |
▶ | if (!c) { hm_stream_mark_dead(s); return HM_ABI_STREAM_GONE; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) { hm_stream_mark_dead(s); return HM_ABI_STREAM_GONE; } |
0 |
2074 |
2074 |
|
| 540 |
2074 |
| return hm_stream_pressure(s, c); |
| 541 |
|
| } |
| 542 |
|
| |
| 543 |
18 |
| static int hm_stream_close_h(pTHX_ void *h) { |
| 544 |
18 |
| hm_stream *s = hm_stream_of(h); |
| 545 |
|
| hm_conn *c; |
| 546 |
18 |
| int r = HM_ABI_STREAM_OK; |
| 547 |
18 |
▶ | if (!s) return HM_ABI_STREAM_STALE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return HM_ABI_STREAM_STALE; |
1 |
17 |
18 |
|
| 548 |
17 |
▶ | c = s->dead ? NULL : hm_stream_conn(s); |
Branch: 1 branch
| Branch | True | False | Total |
| c = s->dead ? NULL : hm_stream_conn(s); |
16 |
1 |
17 |
|
| 549 |
17 |
| hm_stream_unlink(s); |
| 550 |
17 |
▶ | if (!c) r = HM_ABI_STREAM_GONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) r = HM_ABI_STREAM_GONE; |
1 |
16 |
17 |
|
| 551 |
16 |
▶ | else if (s->kind == HM_STREAM_H2) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (s->kind == HM_STREAM_H2) { |
0 |
16 |
16 |
|
| 552 |
|
| /* |
| 553 |
|
| # uncoverable branch true class:ignore_covered_err |
| 554 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 555 |
0 |
▶ | if (!hm_stream_h2_feed(aTHX_ c, s->sid, NULL, 0, 1)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hm_stream_h2_feed(aTHX_ c, s->sid, NULL, 0, 1)) |
0 |
0 |
0 |
|
| 556 |
0 |
| r = HM_ABI_STREAM_GONE; /* # uncoverable statement */ |
| 557 |
|
| } else { |
| 558 |
16 |
| hm_stream_close(aTHX_ s->fd, s->cid, 0); /* unparks, then drains */ |
| 559 |
|
| } |
| 560 |
17 |
| free(s); |
| 561 |
17 |
| return r; |
| 562 |
|
| } |
| 563 |
|
| |
| 564 |
|
| /* The other ending: stop the body in a way the peer can tell apart from |
| 565 |
|
| * having finished. See stream_abort in hm_abi.h for why a clean close is the |
| 566 |
|
| * wrong answer for a producer that failed. |
| 567 |
|
| * |
| 568 |
|
| * Unlinked before anything is sent, for the same reason stream_close is: |
| 569 |
|
| * resetting fires the machinery that reports a stream as gone, and a handle |
| 570 |
|
| * deliberately ending must not be told its stream went away. */ |
| 571 |
4 |
| static int hm_stream_abort_h(pTHX_ void *h) { |
| 572 |
4 |
| hm_stream *s = hm_stream_of(h); |
| 573 |
|
| hm_conn *c; |
| 574 |
4 |
| int r = HM_ABI_STREAM_OK; |
| 575 |
4 |
▶ | if (!s) return HM_ABI_STREAM_STALE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return HM_ABI_STREAM_STALE; |
2 |
2 |
4 |
|
| 576 |
2 |
▶ | c = s->dead ? NULL : hm_stream_conn(s); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| c = s->dead ? NULL : hm_stream_conn(s); /* # uncoverable condition right count:1 class:ignore_covered_err */ |
2 |
0 |
2 |
|
| 577 |
2 |
| hm_stream_unlink(s); |
| 578 |
|
| /* |
| 579 |
|
| # uncoverable branch true |
| 580 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 581 |
2 |
▶ | if (!c) r = HM_ABI_STREAM_GONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) r = HM_ABI_STREAM_GONE; |
0 |
2 |
2 |
|
| 582 |
2 |
▶ | else if (s->kind == HM_STREAM_H2) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (s->kind == HM_STREAM_H2) { |
0 |
2 |
2 |
|
| 583 |
|
| /* |
| 584 |
|
| # uncoverable branch true class:ignore_covered_err |
| 585 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 586 |
0 |
▶ | if (!hm_stream_h2_reset(aTHX_ c, s->sid)) r = HM_ABI_STREAM_GONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hm_stream_h2_reset(aTHX_ c, s->sid)) r = HM_ABI_STREAM_GONE; |
0 |
0 |
0 |
|
| 587 |
|
| } |
| 588 |
|
| else { |
| 589 |
|
| /* An EOF-delimited body ends at the close, so a graceful close is |
| 590 |
|
| * indistinguishable from success and a reset is the only signal |
| 591 |
|
| * left. SO_LINGER with a zero timeout is what turns close(2) into |
| 592 |
|
| * an RST; queued output goes with it, which is the cost of being |
| 593 |
|
| * able to say the response is not to be trusted. Where the host's |
| 594 |
|
| * TCP ignores a zero linger (Cygwin, GNU/Hurd) the peer gets a FIN, |
| 595 |
|
| * and there is nothing else to send: t/41 probes the host before |
| 596 |
|
| * asserting the reset. */ |
| 597 |
|
| struct linger lg; |
| 598 |
2 |
| lg.l_onoff = 1; |
| 599 |
2 |
| lg.l_linger = 0; |
| 600 |
2 |
| (void)hm_os_setsockopt(s->fd, SOL_SOCKET, SO_LINGER, &lg, |
| 601 |
|
| (socklen_t)sizeof lg); |
| 602 |
2 |
| c->keepalive = 0; |
| 603 |
2 |
| c->awaiting = 0; /* it is not waiting for a response any more */ |
| 604 |
2 |
| hm_close(aTHX_ s->loop, c); |
| 605 |
|
| } |
| 606 |
2 |
| free(s); |
| 607 |
2 |
| return r; |
| 608 |
|
| } |
| 609 |
|
| |
| 610 |
15 |
| static int hm_stream_on_drain(pTHX_ void *h, hm_abi_stream_cb cb, void *ud) { |
| 611 |
15 |
| hm_stream *s = hm_stream_of(h); |
| 612 |
|
| PERL_UNUSED_CONTEXT; |
| 613 |
15 |
▶ | if (!s) return HM_ABI_STREAM_STALE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return HM_ABI_STREAM_STALE; |
1 |
14 |
15 |
|
| 614 |
14 |
| s->drain_cb = cb; |
| 615 |
14 |
| s->drain_ud = ud; |
| 616 |
14 |
| return HM_ABI_STREAM_OK; |
| 617 |
|
| } |
| 618 |
|
| |
| 619 |
9 |
| static int hm_stream_on_data(pTHX_ void *h, hm_abi_stream_data_cb cb, void *ud) { |
| 620 |
9 |
| hm_stream *s = hm_stream_of(h); |
| 621 |
|
| /* |
| 622 |
|
| # uncoverable branch true |
| 623 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 624 |
9 |
▶ | if (!s) return HM_ABI_STREAM_STALE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return HM_ABI_STREAM_STALE; |
0 |
9 |
9 |
|
| 625 |
9 |
| s->data_cb = cb; |
| 626 |
9 |
| s->data_ud = ud; |
| 627 |
|
| /* A settled HTTP/1.1 tunnel (tunnel == 1) may already hold bytes the |
| 628 |
|
| * peer sent before the read half was registered - which is the norm on |
| 629 |
|
| * the deferred path, where the request line is already consumed and only |
| 630 |
|
| * the post-upgrade payload is left in rbuf. Hand them over now rather |
| 631 |
|
| * than wait for the peer to send again. NOT tunnel == 2: there the |
| 632 |
|
| * request headers are still in rbuf, and hm_process delivers the |
| 633 |
|
| * leftover once it has stripped them. Cleared first - the callback may |
| 634 |
|
| * close the stream, and with it the connection. */ |
| 635 |
|
| /* |
| 636 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 637 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 638 |
18 |
▶ | if (cb && s->kind == HM_STREAM_H1 && !s->dead) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (cb && s->kind == HM_STREAM_H1 && !s->dead) { |
9 |
0 |
9 |
| if (cb && s->kind == HM_STREAM_H1 && !s->dead) { |
9 |
0 |
9 |
| if (cb && s->kind == HM_STREAM_H1 && !s->dead) { |
9 |
0 |
9 |
|
| 639 |
9 |
| hm_conn *c = hm_stream_conn(s); |
| 640 |
|
| /* |
| 641 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 642 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 643 |
9 |
▶ | if (c && c->tunnel == 1 && c->rlen) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c && c->tunnel == 1 && c->rlen) { |
9 |
0 |
9 |
| if (c && c->tunnel == 1 && c->rlen) { |
3 |
6 |
9 |
| if (c && c->tunnel == 1 && c->rlen) { |
3 |
0 |
3 |
|
| 644 |
3 |
| size_t n = c->rlen; |
| 645 |
3 |
| c->rlen = 0; |
| 646 |
3 |
| cb(aTHX_ (void *)s, c->rbuf, n, 0, ud); |
| 647 |
|
| } |
| 648 |
|
| } |
| 649 |
|
| /* The h2 counterpart of tunnel == 1: a handle opened after the handler |
| 650 |
|
| * returned (it parked on a Future) takes what the stream was sent |
| 651 |
|
| * before it. A handle opened inside the handler gets its bytes from |
| 652 |
|
| * hm_h2_dispatch once the handler has returned. */ |
| 653 |
|
| /* |
| 654 |
|
| # uncoverable branch false class:ignore_covered_err |
| 655 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 656 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 657 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 658 |
0 |
▶ | else if (cb && s->kind == HM_STREAM_H2 && !s->dead) { |
Branch: 3 branches
| Branch | True | False | Total |
| else if (cb && s->kind == HM_STREAM_H2 && !s->dead) { |
0 |
0 |
0 |
| else if (cb && s->kind == HM_STREAM_H2 && !s->dead) { |
0 |
0 |
0 |
| else if (cb && s->kind == HM_STREAM_H2 && !s->dead) { |
0 |
0 |
0 |
|
| 659 |
0 |
| hm_conn *c = hm_stream_conn(s); |
| 660 |
0 |
▶ | SV *b = c ? hm_stream_h2_take_early(aTHX_ c, s->sid) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *b = c ? hm_stream_h2_take_early(aTHX_ c, s->sid) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
0 |
0 |
0 |
|
| 661 |
0 |
▶ | if (b) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (b) { |
0 |
0 |
0 |
|
| 662 |
0 |
| cb(aTHX_ (void *)s, SvPVX(b), SvCUR(b), 0, ud); |
| 663 |
0 |
| SvREFCNT_dec(b); |
| 664 |
|
| } |
| 665 |
|
| } |
| 666 |
9 |
| return HM_ABI_STREAM_OK; |
| 667 |
|
| } |
| 668 |
|
| |
| 669 |
|
| /* Request bytes for a stream, if a handle on it wants them. 1 when one took |
| 670 |
|
| * them, so the transport knows not to buffer into psgi.input as well - a |
| 671 |
|
| * tunnelled stream never ends, and buffering it would grow forever. |
| 672 |
|
| * |
| 673 |
|
| * Looked up by (fd, generation, stream id) rather than held on the h2/h3 |
| 674 |
|
| * stream, so the transports need to know nothing about handles. */ |
| 675 |
12 |
| static int hm_stream_deliver(pTHX_ int fd, UV cid, int64_t sid, |
| 676 |
|
| const char *buf, STRLEN len, int fin) { |
| 677 |
|
| hm_stream *s; |
| 678 |
12 |
▶ | if (!hm_stream_live_n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hm_stream_live_n) return 0; |
0 |
12 |
12 |
|
| 679 |
12 |
▶ | for (s = hm_streams; s; s = s->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = hm_streams; s; s = s->next) { |
12 |
0 |
12 |
|
| 680 |
|
| /* |
| 681 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 682 |
|
| # uncoverable condition right count:3 class:ignore_covered_err |
| 683 |
|
| # uncoverable condition right count:4 class:ignore_covered_err */ |
| 684 |
12 |
▶ | if (s->fd == fd && s->cid == cid && s->sid == sid && s->data_cb) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (s->fd == fd && s->cid == cid && s->sid == sid && s->data_cb) { |
12 |
0 |
12 |
| if (s->fd == fd && s->cid == cid && s->sid == sid && s->data_cb) { |
12 |
0 |
12 |
| if (s->fd == fd && s->cid == cid && s->sid == sid && s->data_cb) { |
12 |
0 |
12 |
| if (s->fd == fd && s->cid == cid && s->sid == sid && s->data_cb) { |
12 |
0 |
12 |
|
| 685 |
12 |
| hm_abi_stream_data_cb cb = s->data_cb; |
| 686 |
12 |
| void *ud = s->data_ud; |
| 687 |
12 |
| cb(aTHX_ (void *)s, buf, len, fin, ud); |
| 688 |
12 |
| return 1; |
| 689 |
|
| } |
| 690 |
|
| } |
| 691 |
0 |
| return 0; |
| 692 |
|
| } |
| 693 |
|
| |
| 694 |
15 |
| static int hm_stream_on_abort(pTHX_ void *h, hm_abi_stream_cb cb, void *ud) { |
| 695 |
15 |
| hm_stream *s = hm_stream_of(h); |
| 696 |
15 |
▶ | if (!s) return HM_ABI_STREAM_STALE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return HM_ABI_STREAM_STALE; |
1 |
14 |
15 |
|
| 697 |
14 |
| s->abort_cb = cb; |
| 698 |
14 |
| s->abort_ud = ud; |
| 699 |
|
| /* Already dead: fire now rather than never. Same contract as |
| 700 |
|
| * future_on_ready on a settled future - registering after the event is |
| 701 |
|
| * not a way to miss it. */ |
| 702 |
|
| /* |
| 703 |
|
| # uncoverable branch true class:ignore_covered_err |
| 704 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 705 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 706 |
|
| # uncoverable condition left count:2 class:ignore_covered_err |
| 707 |
|
| # uncoverable condition left count:3 class:ignore_covered_err |
| 708 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 709 |
14 |
▶ | if (cb && s->dead && !s->aborted) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (cb && s->dead && !s->aborted) { |
14 |
0 |
14 |
| if (cb && s->dead && !s->aborted) { |
0 |
14 |
14 |
| if (cb && s->dead && !s->aborted) { |
0 |
0 |
0 |
|
| 710 |
0 |
| s->aborted = 1; /* # uncoverable statement */ |
| 711 |
0 |
| cb(aTHX_ (void *)s, ud); /* # uncoverable statement */ |
| 712 |
|
| } |
| 713 |
14 |
| return HM_ABI_STREAM_OK; |
| 714 |
|
| } |
| 715 |
|
| |
| 716 |
|
| #endif /* HM_STREAM_H */ |