Coverage Report

Created: 2025-07-11 06:26

/src/h2o/lib/http2/stream.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2014 DeNA Co., Ltd.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
#include "h2o.h"
23
#include "h2o/http2.h"
24
#include "h2o/http2_internal.h"
25
#include "h2o/absprio.h"
26
#include "../probes_.h"
27
28
static void finalostream_send(h2o_ostream_t *self, h2o_req_t *req, h2o_sendvec_t *bufs, size_t bufcnt, h2o_send_state_t state);
29
static void finalostream_send_informational(h2o_ostream_t *_self, h2o_req_t *req);
30
31
static size_t sz_min(size_t x, size_t y)
32
18.8k
{
33
18.8k
    return x < y ? x : y;
34
18.8k
}
35
36
h2o_http2_stream_t *h2o_http2_stream_open(h2o_http2_conn_t *conn, uint32_t stream_id, h2o_req_t *src_req,
37
                                          const h2o_http2_priority_t *received_priority)
38
43.9k
{
39
43.9k
    h2o_http2_stream_t *stream = h2o_mem_alloc(sizeof(*stream));
40
41
    /* init properties (other than req) */
42
43.9k
    memset(stream, 0, offsetof(h2o_http2_stream_t, req));
43
43.9k
    stream->stream_id = stream_id;
44
43.9k
    stream->_ostr_final.do_send = finalostream_send;
45
43.9k
    stream->_ostr_final.send_informational =
46
43.9k
        conn->super.ctx->globalconf->send_informational_mode == H2O_SEND_INFORMATIONAL_MODE_NONE ? NULL
47
43.9k
                                                                                                 : finalostream_send_informational;
48
43.9k
    stream->state = H2O_HTTP2_STREAM_STATE_IDLE;
49
43.9k
    h2o_http2_window_init(&stream->output_window, conn->peer_settings.initial_window_size);
50
43.9k
    h2o_http2_window_init(&stream->input_window.window, H2O_HTTP2_SETTINGS_HOST_STREAM_INITIAL_WINDOW_SIZE);
51
43.9k
    stream->received_priority = *received_priority;
52
53
    /* init request */
54
43.9k
    h2o_init_request(&stream->req, &conn->super, src_req);
55
43.9k
    stream->req.version = 0x200;
56
43.9k
    if (src_req != NULL)
57
159
        memset(&stream->req.upgrade, 0, sizeof(stream->req.upgrade));
58
43.9k
    stream->req._ostr_top = &stream->_ostr_final;
59
60
43.9k
    h2o_http2_conn_register_stream(conn, stream);
61
62
43.9k
    ++conn->num_streams.priority.open;
63
43.9k
    stream->_num_streams_slot = &conn->num_streams.priority;
64
65
43.9k
    return stream;
66
43.9k
}
67
68
void h2o_http2_stream_close(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream)
69
43.9k
{
70
43.9k
    h2o_http2_conn_unregister_stream(conn, stream);
71
43.9k
    if (stream->cache_digests != NULL)
72
317
        h2o_cache_digests_destroy(stream->cache_digests);
73
43.9k
    if (stream->req_body.buf != NULL)
74
7.09k
        h2o_buffer_dispose(&stream->req_body.buf);
75
43.9k
    h2o_dispose_request(&stream->req);
76
43.9k
    if (stream->stream_id == 1 && conn->_http1_req_input != NULL)
77
159
        h2o_buffer_dispose(&conn->_http1_req_input);
78
43.9k
    free(stream);
79
43.9k
}
80
81
void h2o_http2_stream_reset(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream)
82
19.7k
{
83
19.7k
    switch (stream->state) {
84
14.2k
    case H2O_HTTP2_STREAM_STATE_IDLE:
85
19.5k
    case H2O_HTTP2_STREAM_STATE_RECV_HEADERS:
86
19.6k
    case H2O_HTTP2_STREAM_STATE_RECV_BODY:
87
19.6k
    case H2O_HTTP2_STREAM_STATE_REQ_PENDING:
88
19.6k
        h2o_http2_stream_close(conn, stream);
89
19.6k
        break;
90
8
    case H2O_HTTP2_STREAM_STATE_SEND_HEADERS:
91
28
    case H2O_HTTP2_STREAM_STATE_SEND_BODY:
92
90
    case H2O_HTTP2_STREAM_STATE_SEND_BODY_IS_FINAL:
93
90
        h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_END_STREAM);
94
    /* continues */
95
121
    case H2O_HTTP2_STREAM_STATE_END_STREAM:
96
        /* clear all the queued bufs, and close the connection in the callback */
97
121
        stream->_data.size = 0;
98
121
        if (h2o_linklist_is_linked(&stream->_link)) {
99
            /* will be closed in the callback */
100
72
        } else {
101
72
            h2o_http2_stream_close(conn, stream);
102
72
        }
103
121
        break;
104
19.7k
    }
105
19.7k
}
106
107
static size_t calc_max_payload_size(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream)
108
6.45k
{
109
6.45k
    ssize_t conn_max, stream_max;
110
111
6.45k
    if ((conn_max = h2o_http2_conn_get_buffer_window(conn)) <= 0)
112
0
        return 0;
113
6.45k
    if ((stream_max = h2o_http2_window_get_avail(&stream->output_window)) <= 0)
114
0
        return 0;
115
6.45k
    return sz_min(sz_min(conn_max, stream_max), conn->peer_settings.max_frame_size);
116
6.45k
}
117
118
static void commit_data_header(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream, h2o_buffer_t **outbuf, size_t length,
119
                               h2o_send_state_t send_state)
120
6.45k
{
121
6.45k
    assert(outbuf != NULL);
122
    /* send a DATA frame if there's data or the END_STREAM flag to send */
123
6.45k
    int is_end_stream = send_state == H2O_SEND_STATE_FINAL && !stream->req.send_server_timing && stream->req.res.trailers.size == 0;
124
6.45k
    if (length != 0 || is_end_stream) {
125
6.44k
        h2o_http2_encode_frame_header((void *)((*outbuf)->bytes + (*outbuf)->size), length, H2O_HTTP2_FRAME_TYPE_DATA,
126
6.44k
                                      is_end_stream ? H2O_HTTP2_FRAME_FLAG_END_STREAM : 0, stream->stream_id);
127
6.44k
        h2o_http2_window_consume_window(&conn->_write.window, length);
128
6.44k
        h2o_http2_window_consume_window(&stream->output_window, length);
129
6.44k
        (*outbuf)->size += length + H2O_HTTP2_FRAME_HEADER_SIZE;
130
6.44k
        stream->req.bytes_sent += length;
131
6.44k
    }
132
    /* send a RST_STREAM if there's an error */
133
6.45k
    if (send_state == H2O_SEND_STATE_ERROR) {
134
9
        h2o_http2_encode_rst_stream_frame(
135
9
            outbuf, stream->stream_id, -(stream->req.upstream_refused ? H2O_HTTP2_ERROR_REFUSED_STREAM : H2O_HTTP2_ERROR_PROTOCOL));
136
9
    }
137
6.45k
}
138
139
static h2o_sendvec_t *send_data(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream, h2o_sendvec_t *bufs, size_t bufcnt,
140
                                h2o_send_state_t send_state)
141
6.45k
{
142
6.45k
    h2o_iovec_t dst;
143
6.45k
    size_t max_payload_size;
144
145
6.45k
    if ((max_payload_size = calc_max_payload_size(conn, stream)) == 0)
146
0
        goto Exit;
147
148
    /* reserve buffer and point dst to the payload */
149
6.45k
    dst.base =
150
6.45k
        h2o_buffer_reserve(&conn->_write.buf, H2O_HTTP2_FRAME_HEADER_SIZE + max_payload_size).base + H2O_HTTP2_FRAME_HEADER_SIZE;
151
6.45k
    dst.len = max_payload_size;
152
153
    /* emit data */
154
6.45k
    while (bufcnt != 0) {
155
5.94k
        size_t fill_size = sz_min(dst.len, bufs->len);
156
5.94k
        if (!(*bufs->callbacks->read_)(bufs, dst.base, fill_size)) {
157
0
            h2o_http2_encode_rst_stream_frame(&conn->_write.buf, stream->stream_id, -H2O_HTTP2_ERROR_INTERNAL);
158
0
            return NULL;
159
0
        }
160
5.94k
        dst.base += fill_size;
161
5.94k
        dst.len -= fill_size;
162
5.94k
        if (bufs->len == 0) {
163
5.22k
            ++bufs;
164
5.22k
            --bufcnt;
165
5.22k
            if (bufcnt == 0)
166
5.22k
                break;
167
5.22k
        }
168
723
        if (dst.len == 0)
169
723
            break;
170
723
    }
171
172
    /* commit the DATA frame if we have actually emitted payload */
173
6.45k
    if (dst.len != max_payload_size || !h2o_send_state_is_in_progress(send_state)) {
174
6.45k
        size_t payload_len = max_payload_size - dst.len;
175
6.45k
        if (bufcnt != 0) {
176
723
            send_state = H2O_SEND_STATE_IN_PROGRESS;
177
723
        }
178
6.45k
        commit_data_header(conn, stream, &conn->_write.buf, payload_len, send_state);
179
6.45k
    }
180
181
6.45k
Exit:
182
6.45k
    return bufs;
183
6.45k
}
184
185
static int is_blocking_asset(h2o_req_t *req)
186
0
{
187
0
    if (req->res.mime_attr == NULL)
188
0
        h2o_req_fill_mime_attributes(req);
189
0
    return req->res.mime_attr->priority == H2O_MIME_ATTRIBUTE_PRIORITY_HIGHEST;
190
0
}
191
192
static void request_write_and_close(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream)
193
7
{
194
7
    h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_END_STREAM);
195
7
    h2o_http2_scheduler_deactivate(&stream->_scheduler);
196
7
    if (!h2o_linklist_is_linked(&stream->_link))
197
7
        h2o_linklist_insert(&conn->_write.streams_to_proceed, &stream->_link);
198
7
    h2o_http2_conn_request_write(conn);
199
7
}
200
201
static void send_refused_stream(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream)
202
0
{
203
0
    h2o_http2_encode_rst_stream_frame(&conn->_write.buf, stream->stream_id, -H2O_HTTP2_ERROR_REFUSED_STREAM);
204
0
    request_write_and_close(conn, stream);
205
0
}
206
207
static int send_headers(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream, int is_end_stream)
208
9.02k
{
209
9.02k
    stream->req.timestamps.response_start_at = h2o_gettimeofday(conn->super.ctx->loop);
210
211
    /* cancel push with an error response */
212
9.02k
    if (h2o_http2_stream_is_push(stream->stream_id)) {
213
0
        if (400 <= stream->req.res.status)
214
0
            goto CancelPush;
215
0
        if (stream->cache_digests != NULL) {
216
0
            ssize_t etag_index = h2o_find_header(&stream->req.headers, H2O_TOKEN_ETAG, -1);
217
0
            if (etag_index != -1) {
218
0
                h2o_iovec_t url = h2o_concat(&stream->req.pool, stream->req.input.scheme->name, h2o_iovec_init(H2O_STRLIT("://")),
219
0
                                             stream->req.input.authority, stream->req.input.path);
220
0
                h2o_iovec_t *etag = &stream->req.headers.entries[etag_index].value;
221
0
                if (h2o_cache_digests_lookup_by_url_and_etag(stream->cache_digests, url.base, url.len, etag->base, etag->len) ==
222
0
                    H2O_CACHE_DIGESTS_STATE_FRESH)
223
0
                    goto CancelPush;
224
0
            }
225
0
        }
226
0
    }
227
228
    /* reset casper cookie in case cache-digests exist */
229
9.02k
    if (stream->cache_digests != NULL && stream->req.hostconf->http2.casper.capacity_bits != 0) {
230
0
        h2o_add_header(&stream->req.pool, &stream->req.res.headers, H2O_TOKEN_SET_COOKIE, NULL,
231
0
                       H2O_STRLIT("h2o_casper=; Path=/; Expires=Sat, 01 Jan 2000 00:00:00 GMT"));
232
0
    }
233
234
    /* CASPER */
235
9.02k
    if (conn->casper != NULL) {
236
        /* update casper if necessary */
237
0
        if (stream->req.hostconf->http2.casper.track_all_types || is_blocking_asset(&stream->req)) {
238
0
            if (h2o_http2_casper_lookup(conn->casper, stream->req.path.base, stream->req.path.len, 1)) {
239
                /* cancel if the pushed resource is already marked as cached */
240
0
                if (h2o_http2_stream_is_push(stream->stream_id))
241
0
                    goto CancelPush;
242
0
            }
243
0
        }
244
0
        if (stream->cache_digests != NULL)
245
0
            goto SkipCookie;
246
        /* browsers might ignore push responses, or they may process the responses in a different order than they were pushed.
247
         * Therefore H2O tries to include casper cookie only in the last stream that may be received by the client, or when the
248
         * value become stable; see also: https://github.com/h2o/h2o/issues/421
249
         */
250
0
        if (h2o_http2_stream_is_push(stream->stream_id)) {
251
0
            if (!(conn->num_streams.pull.open == 0 && (conn->num_streams.push.half_closed - conn->num_streams.push.send_body) == 1))
252
0
                goto SkipCookie;
253
0
        } else {
254
0
            if (conn->num_streams.push.half_closed - conn->num_streams.push.send_body != 0)
255
0
                goto SkipCookie;
256
0
        }
257
0
        h2o_iovec_t cookie = h2o_http2_casper_get_cookie(conn->casper);
258
0
        h2o_add_header(&stream->req.pool, &stream->req.res.headers, H2O_TOKEN_SET_COOKIE, NULL, cookie.base, cookie.len);
259
0
    SkipCookie:;
260
0
    }
261
262
9.02k
    if (h2o_http2_stream_is_push(stream->stream_id)) {
263
        /* for push, send the push promise */
264
0
        if (!stream->push.promise_sent)
265
0
            h2o_http2_stream_send_push_promise(conn, stream);
266
        /* send ASAP if it is a blocking asset (even in case of Firefox we can't wait 1RTT for it to reprioritize the asset) */
267
0
        if (is_blocking_asset(&stream->req))
268
0
            h2o_http2_scheduler_rebind(&stream->_scheduler, &conn->scheduler, 257, 0);
269
9.02k
    } else {
270
        /* Handle absolute priority header */
271
9.02k
        ssize_t absprio_cursor = h2o_find_header(&stream->req.res.headers, H2O_TOKEN_PRIORITY, -1);
272
9.02k
        if (absprio_cursor != -1 && conn->is_chromium_dependency_tree) {
273
            /* Found absolute priority header in the response header */
274
0
            h2o_absprio_t prio = h2o_absprio_default;
275
0
            h2o_iovec_t *header_value = &stream->req.res.headers.entries[absprio_cursor].value;
276
0
            h2o_absprio_parse_priority(header_value->base, header_value->len, &prio);
277
0
            uint16_t new_weight = h2o_absprio_urgency_to_chromium_weight(prio.urgency);
278
0
            h2o_http2_scheduler_node_t *new_parent = h2o_http2_scheduler_find_parent_by_weight(&conn->scheduler, new_weight);
279
0
            if (new_parent == &stream->_scheduler.node) {
280
                /* find_new_parent might return `stream` itself. In this case re-specify the current
281
                 * parent as a new parent */
282
0
                new_parent = h2o_http2_scheduler_get_parent(&stream->_scheduler);
283
0
            }
284
0
            if (new_parent != h2o_http2_scheduler_get_parent(&stream->_scheduler) ||
285
0
                new_weight != h2o_http2_scheduler_get_weight(&stream->_scheduler)) {
286
                /* Reprioritize the stream based on priority header information */
287
288
                /* First, preserve the current (client-given) priority information so that subsequent
289
                 * streams from the client can correctly refer to the original priority. */
290
0
                h2o_http2_conn_preserve_stream_scheduler(conn, stream);
291
                /* Open a new scheduler for the modified priority information for this stream */
292
0
                h2o_http2_scheduler_open(&stream->_scheduler, new_parent, new_weight, 1);
293
0
            }
294
9.02k
        } else if (conn->num_streams.priority.open == 0 && stream->req.hostconf->http2.reprioritize_blocking_assets &&
295
9.02k
                   h2o_http2_scheduler_get_parent(&stream->_scheduler) == &conn->scheduler && is_blocking_asset(&stream->req)) {
296
            /* raise the priority of asset files that block rendering to highest if the user-agent is _not_ using dependency-based
297
             * prioritization (e.g. that of Firefox)
298
             */
299
0
            h2o_http2_scheduler_rebind(&stream->_scheduler, &conn->scheduler, 257, 0);
300
0
        }
301
9.02k
    }
302
303
    /* send HEADERS, as well as start sending body */
304
9.02k
    if (h2o_http2_stream_is_push(stream->stream_id))
305
0
        h2o_add_header_by_str(&stream->req.pool, &stream->req.res.headers, H2O_STRLIT("x-http2-push"), 0, NULL,
306
0
                              H2O_STRLIT("pushed"));
307
9.02k
    if (stream->req.send_server_timing)
308
0
        h2o_add_server_timing_header(&stream->req, 1);
309
9.02k
    stream->req.header_bytes_sent += h2o_hpack_flatten_response(
310
9.02k
        &conn->_write.buf, &conn->_output_header_table, conn->peer_settings.header_table_size, stream->stream_id,
311
9.02k
        conn->peer_settings.max_frame_size, stream->req.res.status, stream->req.res.headers.entries, stream->req.res.headers.size,
312
9.02k
        &conn->super.ctx->globalconf->server_name, stream->req.res.content_length, is_end_stream);
313
9.02k
    h2o_http2_conn_request_write(conn);
314
9.02k
    h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_SEND_BODY);
315
316
9.02k
    return 0;
317
318
0
CancelPush:
319
0
    h2o_add_header_by_str(&stream->req.pool, &stream->req.res.headers, H2O_STRLIT("x-http2-push"), 0, NULL,
320
0
                          H2O_STRLIT("cancelled"));
321
0
    h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_END_STREAM);
322
0
    h2o_linklist_insert(&conn->_write.streams_to_proceed, &stream->_link);
323
0
    if (stream->push.promise_sent) {
324
0
        h2o_http2_encode_rst_stream_frame(&conn->_write.buf, stream->stream_id, -H2O_HTTP2_ERROR_INTERNAL);
325
0
        h2o_http2_conn_request_write(conn);
326
0
    }
327
0
    return -1;
328
9.02k
}
329
330
void finalostream_send(h2o_ostream_t *self, h2o_req_t *req, h2o_sendvec_t *bufs, size_t bufcnt, h2o_send_state_t state)
331
9.60k
{
332
9.60k
    h2o_http2_stream_t *stream = H2O_STRUCT_FROM_MEMBER(h2o_http2_stream_t, _ostr_final, self);
333
9.60k
    h2o_http2_conn_t *conn = (h2o_http2_conn_t *)req->conn;
334
335
9.60k
    assert(h2o_send_state_is_in_progress(stream->send_state));
336
9.60k
    assert(stream->_data.size == 0);
337
338
9.60k
    if (stream->blocked_by_server)
339
5.94k
        h2o_http2_stream_set_blocked_by_server(conn, stream, 0);
340
341
9.60k
    if (stream->req.res.status == 425 && stream->req.reprocess_if_too_early) {
342
0
        assert(stream->state <= H2O_HTTP2_STREAM_STATE_SEND_HEADERS);
343
0
        h2o_http2_conn_register_for_replay(conn, stream);
344
0
        return;
345
0
    }
346
347
9.60k
    stream->send_state = state;
348
9.60k
    int is_end_stream = state == H2O_SEND_STATE_FINAL && bufcnt == 0,
349
9.60k
        empty_payload_allowed = stream->state < H2O_HTTP2_STREAM_STATE_SEND_BODY || state != H2O_SEND_STATE_IN_PROGRESS;
350
351
    /* send headers */
352
9.60k
    switch (stream->state) {
353
243
    case H2O_HTTP2_STREAM_STATE_RECV_BODY:
354
243
        h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_REQ_PENDING);
355
    /* fallthru */
356
243
    case H2O_HTTP2_STREAM_STATE_REQ_PENDING:
357
243
        h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_SEND_HEADERS);
358
    /* fallthru */
359
9.02k
    case H2O_HTTP2_STREAM_STATE_SEND_HEADERS:
360
9.02k
        if (stream->req.upstream_refused) {
361
0
            send_refused_stream(conn, stream);
362
0
            return;
363
0
        }
364
9.02k
        h2o_probe_log_response(&stream->req, stream->stream_id);
365
9.02k
        if (send_headers(conn, stream, is_end_stream) != 0)
366
0
            return;
367
9.02k
        if (is_end_stream) {
368
7
            request_write_and_close(conn, stream);
369
7
            return;
370
7
        }
371
    /* fallthru */
372
9.57k
    case H2O_HTTP2_STREAM_STATE_SEND_BODY:
373
9.57k
        if (state != H2O_SEND_STATE_IN_PROGRESS) {
374
8.93k
            h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_SEND_BODY_IS_FINAL);
375
8.93k
        }
376
9.57k
        break;
377
19
    case H2O_HTTP2_STREAM_STATE_END_STREAM:
378
        /* might get set by h2o_http2_stream_reset */
379
19
        return;
380
0
    default:
381
0
        assert(!"cannot be in a receiving state");
382
9.60k
    }
383
384
    /* save the contents in queue */
385
9.57k
    h2o_vector_reserve(&req->pool, &stream->_data, bufcnt);
386
18.5k
    for (size_t i = 0; i < bufcnt; ++i) {
387
9.01k
        if (bufs[i].len == 0)
388
0
            continue;
389
9.01k
        stream->_data.entries[stream->_data.size++] = bufs[i];
390
9.01k
    }
391
9.57k
    assert(empty_payload_allowed || stream->_data.size != 0 || !"h2o_data must only be called when there is progress");
392
393
9.57k
    h2o_http2_conn_register_for_proceed_callback(conn, stream);
394
9.57k
}
395
396
static void finalostream_send_informational(h2o_ostream_t *self, h2o_req_t *req)
397
0
{
398
0
    h2o_http2_stream_t *stream = H2O_STRUCT_FROM_MEMBER(h2o_http2_stream_t, _ostr_final, self);
399
0
    h2o_http2_conn_t *conn = (h2o_http2_conn_t *)req->conn;
400
401
0
    stream->req.header_bytes_sent += h2o_hpack_flatten_response(
402
0
        &conn->_write.buf, &conn->_output_header_table, conn->peer_settings.header_table_size, stream->stream_id,
403
0
        conn->peer_settings.max_frame_size, req->res.status, req->res.headers.entries, req->res.headers.size, NULL, SIZE_MAX, 0);
404
0
    h2o_http2_conn_request_write(conn);
405
0
}
406
407
void h2o_http2_stream_send_pending_data(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream)
408
6.84k
{
409
6.84k
    if (h2o_http2_window_get_avail(&stream->output_window) <= 0)
410
385
        return;
411
412
6.45k
    h2o_sendvec_t *nextbuf = send_data(conn, stream, stream->_data.entries, stream->_data.size, stream->send_state);
413
6.45k
    if (nextbuf == NULL && stream->_data.entries != NULL) {
414
        /* error */
415
0
        stream->_data.size = 0;
416
0
        stream->send_state = H2O_SEND_STATE_ERROR;
417
0
        h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_END_STREAM);
418
6.45k
    } else if (nextbuf == stream->_data.entries + stream->_data.size) {
419
        /* sent all data */
420
5.73k
        stream->_data.size = 0;
421
5.73k
        if (stream->state == H2O_HTTP2_STREAM_STATE_SEND_BODY_IS_FINAL)
422
5.15k
            h2o_http2_stream_set_state(conn, stream, H2O_HTTP2_STREAM_STATE_END_STREAM);
423
5.73k
    } else if (nextbuf != stream->_data.entries) {
424
        /* adjust the buffer */
425
0
        size_t newsize = stream->_data.size - (nextbuf - stream->_data.entries);
426
0
        memmove(stream->_data.entries, nextbuf, sizeof(stream->_data.entries[0]) * newsize);
427
0
        stream->_data.size = newsize;
428
0
    }
429
430
    /* if the stream entered error state, suppress sending trailers */
431
6.45k
    if (stream->send_state == H2O_SEND_STATE_ERROR)
432
9
        stream->req.send_server_timing = 0;
433
6.45k
}
434
435
void h2o_http2_stream_proceed(h2o_http2_conn_t *conn, h2o_http2_stream_t *stream)
436
5.76k
{
437
5.76k
    if (stream->state == H2O_HTTP2_STREAM_STATE_END_STREAM) {
438
5.19k
        switch (stream->req_body.state) {
439
4.77k
        case H2O_HTTP2_REQ_BODY_NONE:
440
5.00k
        case H2O_HTTP2_REQ_BODY_CLOSE_DELIVERED:
441
5.00k
            h2o_http2_stream_close(conn, stream);
442
5.00k
            break;
443
190
        default:
444
190
            break; /* the stream will be closed when the read side is done */
445
5.19k
        }
446
5.19k
    } else {
447
574
        if (!stream->blocked_by_server)
448
546
            h2o_http2_stream_set_blocked_by_server(conn, stream, 1);
449
574
        h2o_proceed_response(&stream->req);
450
574
    }
451
5.76k
}