Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/h2o/lib/common/http1client.c
Line
Count
Source
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 <arpa/inet.h>
23
#include <fcntl.h>
24
#include <netdb.h>
25
#include <netinet/in.h>
26
#include <sys/socket.h>
27
#include <sys/types.h>
28
#include <sys/un.h>
29
#include "picohttpparser.h"
30
#include "h2o/httpclient.h"
31
#include "h2o/token.h"
32
33
#if !H2O_USE_LIBUV && defined(__linux__)
34
#define USE_PIPE_READER 1
35
#else
36
#define USE_PIPE_READER 0
37
#endif
38
39
enum enum_h2o_http1client_stream_state {
40
    STREAM_STATE_HEAD,
41
    STREAM_STATE_BODY,
42
    STREAM_STATE_CLOSED,
43
};
44
45
struct st_h2o_http1client_t {
46
    h2o_httpclient_t super;
47
    h2o_socket_t *sock;
48
    struct {
49
        enum enum_h2o_http1client_stream_state req;
50
        enum enum_h2o_http1client_stream_state res;
51
    } state;
52
    h2o_url_t *_origin;
53
    int _method_is_head;
54
    int _do_keepalive;
55
    union {
56
        struct {
57
            size_t bytesleft;
58
        } content_length;
59
        struct {
60
            struct phr_chunked_decoder decoder;
61
            size_t bytes_decoded_in_buf;
62
        } chunked;
63
    } _body_decoder;
64
    h2o_socket_cb reader;
65
    h2o_httpclient_proceed_req_cb proceed_req;
66
    /**
67
     * buffer used to hold chunk headers of a request body; the size is SIZE_MAX in hex + CRLF + '\0'
68
     */
69
    char _chunk_len_str[(sizeof(H2O_UINT64_LONGEST_HEX_STR) - 1) + 2 + 1];
70
    /**
71
     * buffer used to retain request body that is inflight
72
     */
73
    struct {
74
        h2o_buffer_t *buf;
75
        int is_end_stream;
76
    } body_buf;
77
    /**
78
     * `on_body_piped` is non-NULL iff used
79
     */
80
    h2o_httpclient_pipe_reader_t pipe_reader;
81
    /**
82
     * maintain the number of bytes being already processed on the associated socket
83
     */
84
    uint64_t _socket_bytes_processed;
85
    unsigned _num_informational;
86
    unsigned _is_chunked : 1;
87
    unsigned _seen_at_least_one_chunk : 1;
88
    unsigned _delay_free : 1;
89
    unsigned _app_prefers_pipe_reader : 1;
90
    unsigned _send_own_expect : 1;
91
};
92
93
static void on_body_to_pipe(h2o_socket_t *_sock, const char *err);
94
95
static void req_body_send(struct st_h2o_http1client_t *client);
96
static void update_read_state(struct st_h2o_http1client_t *client);
97
98
static void close_client(struct st_h2o_http1client_t *client)
99
3.68k
{
100
3.68k
    if (client->sock != NULL) {
101
3.68k
        if (client->super.connpool != NULL && client->_do_keepalive && client->super.connpool->socketpool->timeout > 0) {
102
            /* we do not send pipelined requests, and thus can trash all the received input at the end of the request */
103
0
            h2o_buffer_consume(&client->sock->input, client->sock->input->size);
104
0
            h2o_socketpool_return(client->super.connpool->socketpool, client->sock);
105
3.68k
        } else {
106
3.68k
            h2o_socket_close(client->sock);
107
3.68k
        }
108
3.68k
    }
109
3.68k
    if (h2o_timer_is_linked(&client->super._timeout))
110
198
        h2o_timer_unlink(&client->super._timeout);
111
3.68k
    if (client->body_buf.buf != NULL)
112
1.15k
        h2o_buffer_dispose(&client->body_buf.buf);
113
3.68k
    if (!client->_delay_free)
114
3.68k
        free(client);
115
3.68k
}
116
117
static void close_response(struct st_h2o_http1client_t *client)
118
3.36k
{
119
3.36k
    assert(client->state.res == STREAM_STATE_CLOSED);
120
3.36k
    if (client->state.req == STREAM_STATE_CLOSED) {
121
2.63k
        close_client(client);
122
2.63k
    } else {
123
734
        h2o_socket_read_stop(client->sock);
124
734
    }
125
3.36k
}
126
127
static h2o_httpclient_body_cb call_on_head(struct st_h2o_http1client_t *client, const char *errstr, h2o_httpclient_on_head_t *args)
128
3.51k
{
129
3.51k
    assert(!client->_delay_free);
130
3.51k
    client->_delay_free = 1;
131
3.51k
    h2o_httpclient_body_cb cb = client->super._cb.on_head(&client->super, errstr, args);
132
3.51k
    client->_delay_free = 0;
133
3.51k
    return cb;
134
3.51k
}
135
136
static int call_on_body(struct st_h2o_http1client_t *client, const char *errstr)
137
5.42k
{
138
5.42k
    assert(!client->_delay_free);
139
5.42k
    client->_delay_free = 1;
140
5.42k
    int ret = (client->reader == on_body_to_pipe ? client->pipe_reader.on_body_piped : client->super._cb.on_body)(&client->super,
141
5.42k
                                                                                                                  errstr, NULL, 0);
142
5.42k
    client->_delay_free = 0;
143
5.42k
    return ret;
144
5.42k
}
145
146
static void call_proceed_req(struct st_h2o_http1client_t *client, const char *errstr)
147
1.86k
{
148
1.86k
    assert(!client->_delay_free);
149
1.86k
    client->_delay_free = 1;
150
1.86k
    client->proceed_req(&client->super, errstr);
151
1.86k
    client->_delay_free = 0;
152
1.86k
}
153
154
static void on_error(struct st_h2o_http1client_t *client, const char *errstr)
155
511
{
156
511
    switch (client->state.res) {
157
93
    case STREAM_STATE_HEAD:
158
93
        call_on_head(client, errstr, NULL);
159
93
        break;
160
31
    case STREAM_STATE_BODY:
161
31
        call_on_body(client, errstr);
162
31
        break;
163
387
    case STREAM_STATE_CLOSED:
164
387
        if (client->proceed_req != NULL)
165
387
            call_proceed_req(client, errstr);
166
387
        break;
167
511
    }
168
511
    close_client(client);
169
511
}
170
171
static void on_body_timeout(h2o_timer_t *entry)
172
0
{
173
0
    struct st_h2o_http1client_t *client = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1client_t, super._timeout, entry);
174
0
    on_error(client, h2o_httpclient_error_io_timeout);
175
0
}
176
177
static void on_body_until_close(h2o_socket_t *sock, const char *err)
178
5.39k
{
179
5.39k
    struct st_h2o_http1client_t *client = sock->data;
180
181
5.39k
    h2o_timer_unlink(&client->super._timeout);
182
183
5.39k
    if (err != NULL) {
184
2.67k
        client->state.res = STREAM_STATE_CLOSED;
185
2.67k
        client->super.timings.response_end_at = h2o_gettimeofday(client->super.ctx->loop);
186
2.67k
        call_on_body(client, h2o_httpclient_error_is_eos);
187
2.67k
        close_response(client);
188
2.67k
        return;
189
2.67k
    }
190
2.72k
    uint64_t size = sock->bytes_read - client->_socket_bytes_processed;
191
2.72k
    client->_socket_bytes_processed = sock->bytes_read;
192
193
2.72k
    client->super.bytes_read.body += size;
194
2.72k
    client->super.bytes_read.total += size;
195
196
2.72k
    if (size != 0) {
197
2.72k
        if (call_on_body(client, NULL) != 0) {
198
0
            close_client(client);
199
0
            return;
200
0
        }
201
2.72k
        update_read_state(client);
202
2.72k
    }
203
2.72k
}
204
205
static void on_body_content_length(h2o_socket_t *sock, const char *err)
206
0
{
207
0
    struct st_h2o_http1client_t *client = sock->data;
208
209
0
    h2o_timer_unlink(&client->super._timeout);
210
211
0
    if (err != NULL) {
212
0
        on_error(client, h2o_httpclient_error_io);
213
0
        return;
214
0
    }
215
0
    uint64_t size = sock->bytes_read - client->_socket_bytes_processed;
216
0
    client->_socket_bytes_processed = sock->bytes_read;
217
218
0
    client->super.bytes_read.body += size;
219
0
    client->super.bytes_read.total += size;
220
221
0
    if (size != 0 || client->_body_decoder.content_length.bytesleft == 0) {
222
0
        int ret;
223
0
        if (client->_body_decoder.content_length.bytesleft <= size) {
224
0
            if (client->_body_decoder.content_length.bytesleft < size) {
225
                /* remove the trailing garbage from buf, and disable keepalive */
226
0
                client->sock->input->size -= size - client->_body_decoder.content_length.bytesleft;
227
0
                client->_do_keepalive = 0;
228
0
            }
229
0
            client->_body_decoder.content_length.bytesleft = 0;
230
0
            client->state.res = STREAM_STATE_CLOSED;
231
0
            client->super.timings.response_end_at = h2o_gettimeofday(client->super.ctx->loop);
232
0
        } else {
233
0
            client->_body_decoder.content_length.bytesleft -= size;
234
0
        }
235
0
        ret = call_on_body(client, client->state.res == STREAM_STATE_CLOSED ? h2o_httpclient_error_is_eos : NULL);
236
0
        if (client->state.res == STREAM_STATE_CLOSED) {
237
0
            close_response(client);
238
0
            return;
239
0
        } else if (ret != 0) {
240
0
            client->_do_keepalive = 0;
241
0
            close_client(client);
242
0
            return;
243
0
        }
244
0
    }
245
246
0
#if USE_PIPE_READER
247
0
    if (client->pipe_reader.on_body_piped != NULL) {
248
0
        h2o_socket_dont_read(client->sock, 1);
249
0
        client->reader = on_body_to_pipe;
250
0
    }
251
0
#endif
252
0
    update_read_state(client);
253
0
}
254
255
void on_body_to_pipe(h2o_socket_t *_sock, const char *err)
256
0
{
257
0
#if USE_PIPE_READER
258
0
    struct st_h2o_http1client_t *client = _sock->data;
259
260
0
    h2o_timer_unlink(&client->super._timeout);
261
0
    h2o_socket_read_stop(client->sock);
262
263
0
    if (err != NULL) {
264
0
        on_error(client, h2o_httpclient_error_io);
265
0
        return;
266
0
    }
267
268
0
    size_t bytes_to_splice = client->_body_decoder.content_length.bytesleft < H2O_PULL_SENDVEC_MAX_SIZE
269
0
                                 ? client->_body_decoder.content_length.bytesleft
270
0
                                 : H2O_PULL_SENDVEC_MAX_SIZE;
271
272
0
    ssize_t bytes_read;
273
0
    while ((bytes_read = splice(h2o_socket_get_fd(client->sock), NULL, client->pipe_reader.fd, NULL, bytes_to_splice,
274
0
                                SPLICE_F_NONBLOCK)) == -1 &&
275
0
           errno == EINTR)
276
0
        ;
277
0
    if (bytes_read == -1 && errno == EAGAIN) {
278
0
        update_read_state(client);
279
0
        return;
280
0
    }
281
0
    if (bytes_read <= 0) {
282
0
        on_error(client, h2o_httpclient_error_io);
283
0
        return;
284
0
    }
285
286
0
    client->_socket_bytes_processed += bytes_read;
287
0
    client->sock->bytes_read += bytes_read;
288
0
    client->super.bytes_read.body += bytes_read;
289
0
    client->super.bytes_read.total += bytes_read;
290
291
0
    client->_body_decoder.content_length.bytesleft -= bytes_read;
292
0
    if (client->_body_decoder.content_length.bytesleft == 0) {
293
0
        client->state.res = STREAM_STATE_CLOSED;
294
0
        client->super.timings.response_end_at = h2o_gettimeofday(client->super.ctx->loop);
295
0
        h2o_socket_dont_read(client->sock, 0);
296
0
    }
297
298
0
    int ret = call_on_body(client, client->state.res == STREAM_STATE_CLOSED ? h2o_httpclient_error_is_eos : NULL);
299
300
0
    if (client->state.res == STREAM_STATE_CLOSED) {
301
0
        close_response(client);
302
0
    } else if (ret != 0) {
303
0
        client->_do_keepalive = 0;
304
0
        close_client(client);
305
0
    }
306
#else
307
    h2o_fatal("%s cannot be called", __FUNCTION__);
308
#endif
309
0
}
310
311
static void on_body_chunked(h2o_socket_t *sock, const char *err)
312
0
{
313
0
    struct st_h2o_http1client_t *client = sock->data;
314
0
    h2o_buffer_t *inbuf;
315
316
0
    h2o_timer_unlink(&client->super._timeout);
317
318
0
    if (err != NULL) {
319
0
        if (err == h2o_socket_error_closed && !phr_decode_chunked_is_in_data(&client->_body_decoder.chunked.decoder) &&
320
0
            client->_seen_at_least_one_chunk) {
321
            /*
322
             * if the peer closed after a full chunk, treat this
323
             * as if the transfer had complete, browsers appear to ignore
324
             * a missing 0\r\n chunk
325
             */
326
0
            client->_do_keepalive = 0;
327
0
            client->state.res = STREAM_STATE_CLOSED;
328
0
            client->super.timings.response_end_at = h2o_gettimeofday(client->super.ctx->loop);
329
0
            call_on_body(client, h2o_httpclient_error_is_eos);
330
0
            close_response(client);
331
0
        } else {
332
0
            on_error(client, h2o_httpclient_error_io);
333
0
        }
334
0
        return;
335
0
    }
336
0
    uint64_t size = sock->bytes_read - client->_socket_bytes_processed;
337
0
    client->_socket_bytes_processed = sock->bytes_read;
338
339
0
    client->super.bytes_read.body += size;
340
0
    client->super.bytes_read.total += size;
341
342
0
    inbuf = client->sock->input;
343
0
    if (size != 0) {
344
0
        const char *errstr;
345
0
        int cb_ret;
346
0
        size_t newsz = size;
347
348
0
        switch (phr_decode_chunked(&client->_body_decoder.chunked.decoder, inbuf->bytes + inbuf->size - newsz, &newsz)) {
349
0
        case -1: /* error */
350
0
            newsz = 0;
351
0
            client->_do_keepalive = 0;
352
0
            errstr = h2o_httpclient_error_http1_parse_failed;
353
0
            break;
354
0
        case -2: /* incomplete */
355
0
            errstr = NULL;
356
0
            break;
357
0
        default: /* complete, with garbage on tail; should disable keepalive */
358
0
            client->_do_keepalive = 0;
359
        /* fallthru */
360
0
        case 0: /* complete */
361
0
            client->state.res = STREAM_STATE_CLOSED;
362
0
            errstr = h2o_httpclient_error_is_eos;
363
0
            client->super.timings.response_end_at = h2o_gettimeofday(client->super.ctx->loop);
364
0
            break;
365
0
        }
366
0
        inbuf->size -= size - newsz;
367
0
        if (inbuf->size > 0)
368
0
            client->_seen_at_least_one_chunk = 1;
369
0
        cb_ret = call_on_body(client, errstr);
370
0
        if (client->state.res == STREAM_STATE_CLOSED) {
371
0
            close_response(client);
372
0
            return;
373
0
        } else if (errstr != NULL) {
374
0
            close_client(client);
375
0
            return;
376
0
        } else if (cb_ret != 0) {
377
0
            client->_do_keepalive = 0;
378
0
            close_client(client);
379
0
            return;
380
0
        }
381
0
        update_read_state(client);
382
0
    }
383
0
}
384
385
static void on_head_timeout(h2o_timer_t *entry)
386
0
{
387
0
    struct st_h2o_http1client_t *client = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1client_t, super._timeout, entry);
388
0
    on_error(client, h2o_httpclient_error_io_timeout);
389
0
}
390
391
static void on_head(h2o_socket_t *sock, const char *err)
392
3.41k
{
393
3.41k
    struct st_h2o_http1client_t *client = sock->data;
394
3.41k
    int minor_version, version, http_status, rlen;
395
3.41k
    const char *msg;
396
3.41k
#define MAX_HEADERS 100
397
3.41k
    h2o_header_t *headers;
398
3.41k
    h2o_iovec_t *header_names;
399
3.41k
    size_t msg_len, num_headers, i;
400
3.41k
    h2o_socket_cb reader;
401
402
3.41k
    h2o_timer_unlink(&client->super._timeout);
403
404
3.41k
    if (err != NULL) {
405
0
        on_error(client, h2o_httpclient_error_io);
406
0
        return;
407
0
    }
408
409
    /* revert max read size to 1MB now that we have received the first chunk, presumably carrying all the response headers */
410
3.41k
#if USE_PIPE_READER
411
3.41k
    if (client->_app_prefers_pipe_reader)
412
2.42k
        h2o_evloop_socket_set_max_read_size(client->sock, h2o_evloop_socket_max_read_size);
413
3.41k
#endif
414
415
3.41k
    client->super._timeout.cb = on_head_timeout;
416
417
3.41k
    headers = h2o_mem_alloc_pool(client->super.pool, *headers, MAX_HEADERS);
418
3.41k
    header_names = h2o_mem_alloc_pool(client->super.pool, *header_names, MAX_HEADERS);
419
420
    /* continue parsing the responses until we see a final one */
421
3.41k
    while (1) {
422
        /* parse response */
423
3.41k
        size_t inresplen = sock->input->size < H2O_MAX_RESPLEN ? sock->input->size : H2O_MAX_RESPLEN;
424
3.41k
        struct phr_header src_headers[MAX_HEADERS];
425
3.41k
        num_headers = MAX_HEADERS;
426
3.41k
        rlen = phr_parse_response(sock->input->bytes, inresplen, &minor_version, &http_status, &msg, &msg_len, src_headers,
427
3.41k
                                  &num_headers, 0);
428
3.41k
        switch (rlen) {
429
0
        case -1: /* error */
430
0
            on_error(client, h2o_httpclient_error_http1_parse_failed);
431
0
            return;
432
0
        case -2: /* incomplete */
433
0
            if (inresplen == H2O_MAX_RESPLEN) {
434
0
                on_error(client, h2o_httpclient_error_http1_headers_too_long);
435
0
                return;
436
0
            }
437
0
            h2o_timer_link(client->super.ctx->loop, client->super.ctx->io_timeout, &client->super._timeout);
438
0
            return;
439
3.41k
        }
440
441
3.41k
        client->super.bytes_read.header += rlen;
442
3.41k
        client->super.bytes_read.total += rlen;
443
444
3.41k
        version = 0x100 | (minor_version != 0);
445
446
        /* fill-in the headers */
447
6.83k
        for (i = 0; i != num_headers; ++i) {
448
3.41k
            if (src_headers[i].name_len == 0) {
449
                /* reject multiline header */
450
0
                on_error(client, h2o_httpclient_error_http1_line_folding);
451
0
                return;
452
0
            }
453
3.41k
            const h2o_token_t *token;
454
3.41k
            char *orig_name = h2o_strdup(client->super.pool, src_headers[i].name, src_headers[i].name_len).base;
455
3.41k
            h2o_strtolower((char *)src_headers[i].name, src_headers[i].name_len);
456
3.41k
            token = h2o_lookup_token(src_headers[i].name, src_headers[i].name_len);
457
3.41k
            if (token != NULL) {
458
3.41k
                headers[i].name = (h2o_iovec_t *)&token->buf;
459
3.41k
            } else {
460
0
                header_names[i] = h2o_iovec_init(src_headers[i].name, src_headers[i].name_len);
461
0
                headers[i].name = &header_names[i];
462
0
            }
463
3.41k
            headers[i].value = h2o_iovec_init(src_headers[i].value, src_headers[i].value_len);
464
3.41k
            headers[i].orig_name = orig_name;
465
3.41k
            headers[i].flags = (h2o_header_flags_t){0};
466
3.41k
        }
467
468
3.41k
        if (http_status == 101) {
469
0
            if (client->super.upgrade_to == NULL) {
470
                /* 101 is expected only when trying to establish a tunnel */
471
0
                on_error(client, h2o_httpclient_error_unexpected_101);
472
0
                return;
473
0
            }
474
0
            break;
475
0
        }
476
3.41k
        if (http_status <= 199 && ++client->_num_informational > H2O_HTTPCLIENT_MAX_INFORMATIONAL_RESPONSES) {
477
0
            on_error(client, h2o_httpclient_error_too_many_informational);
478
0
            return;
479
0
        }
480
3.41k
        if (http_status == 100 || http_status >= 200) {
481
            /* When request body has been withheld and a 100 or a final response has been received, start sending the request body,
482
             * see: https://github.com/h2o/h2o/pull/3316#discussion_r1456859634. */
483
3.41k
            if (client->_send_own_expect) {
484
0
                client->_send_own_expect = 0;
485
0
                req_body_send(client);
486
0
            }
487
3.41k
            if (http_status >= 200)
488
3.41k
                break;
489
3.41k
        }
490
3.41k
        assert(http_status <= 199);
491
0
        if (client->super.informational_cb != NULL &&
492
0
            client->super.informational_cb(&client->super, version, http_status, h2o_iovec_init(msg, msg_len), headers,
493
0
                                           num_headers) != 0) {
494
0
            close_client(client);
495
0
            return;
496
0
        }
497
498
0
        h2o_buffer_consume(&client->sock->input, rlen);
499
0
        if (client->sock->input->size == 0) {
500
0
            if (!h2o_timer_is_linked(&client->super._timeout)) {
501
0
                h2o_timer_link(client->super.ctx->loop, client->super.ctx->io_timeout, &client->super._timeout);
502
0
            }
503
0
            return;
504
0
        }
505
0
    }
506
507
    /* recognize hop-by-hop response headers */
508
3.41k
    reader = on_body_until_close;
509
3.41k
    if (!h2o_httpclient__tunnel_is_ready(&client->super, http_status, version)) {
510
3.41k
        client->_do_keepalive = minor_version >= 1;
511
6.83k
        for (i = 0; i != num_headers; ++i) {
512
3.41k
            if (headers[i].name == &H2O_TOKEN_CONNECTION->buf) {
513
3.41k
                if (h2o_contains_token(headers[i].value.base, headers[i].value.len, H2O_STRLIT("keep-alive"), ',')) {
514
0
                    client->_do_keepalive = 1;
515
3.41k
                } else {
516
3.41k
                    client->_do_keepalive = 0;
517
3.41k
                }
518
3.41k
            } else if (headers[i].name == &H2O_TOKEN_TRANSFER_ENCODING->buf) {
519
0
                if (h2o_memis(headers[i].value.base, headers[i].value.len, H2O_STRLIT("chunked"))) {
520
                    /* precond: _body_decoder.chunked is zero-filled */
521
0
                    client->_body_decoder.chunked.decoder.consume_trailer = 1;
522
0
                    reader = on_body_chunked;
523
0
                } else if (h2o_memis(headers[i].value.base, headers[i].value.len, H2O_STRLIT("identity"))) {
524
                    /* continue */
525
0
                } else {
526
0
                    on_error(client, h2o_httpclient_error_http1_unexpected_transfer_encoding);
527
0
                    return;
528
0
                }
529
0
            } else if (headers[i].name == &H2O_TOKEN_CONTENT_LENGTH->buf) {
530
0
                if ((client->_body_decoder.content_length.bytesleft = h2o_strtosize(headers[i].value.base, headers[i].value.len)) ==
531
0
                    SIZE_MAX) {
532
0
                    on_error(client, h2o_httpclient_error_invalid_content_length);
533
0
                    return;
534
0
                }
535
0
                if (reader != on_body_chunked)
536
0
                    reader = on_body_content_length;
537
0
            }
538
3.41k
        }
539
        /* Never pool a rejected CONNECT connection; it might contain optimistically forwarded tunnel data. */
540
3.41k
        if (client->super.upgrade_to == h2o_httpclient_upgrade_to_connect)
541
0
            client->_do_keepalive = 0;
542
3.41k
    }
543
544
3.41k
    client->state.res = STREAM_STATE_BODY;
545
3.41k
    client->super.timings.response_start_at = h2o_gettimeofday(client->super.ctx->loop);
546
547
    /* RFC 2616 4.4 */
548
3.41k
    if (client->_method_is_head || http_status == 204 || http_status == 304) {
549
693
        client->state.res = STREAM_STATE_CLOSED;
550
693
        client->super.timings.response_end_at = h2o_gettimeofday(client->super.ctx->loop);
551
2.72k
    } else {
552
        /* close the connection if impossible to determine the end of the response (RFC 7230 3.3.3) */
553
2.72k
        if (reader == on_body_until_close)
554
2.72k
            client->_do_keepalive = 0;
555
2.72k
    }
556
557
3.41k
    h2o_httpclient_on_head_t on_head = {
558
3.41k
        .version = version,
559
3.41k
        .status = http_status,
560
3.41k
        .msg = h2o_iovec_init(msg, msg_len),
561
3.41k
        .headers = headers,
562
3.41k
        .num_headers = num_headers,
563
3.41k
        .header_requires_dup = 1,
564
3.41k
    };
565
3.41k
#if USE_PIPE_READER
566
    /* If there is no less than 64KB of data to be read from the socket, offer the application the opportunity to use pipe for
567
     * transferring the content zero-copy. As switching to pipe involves the cost of creating a pipe (and disposing it when the
568
     * request is complete), we adopt this margin of 64KB, which offers clear improvement (5%) on 9th-gen Intel Core. */
569
3.41k
    if (client->_app_prefers_pipe_reader && reader == on_body_content_length &&
570
0
        client->sock->input->size + 65536 <= client->_body_decoder.content_length.bytesleft && client->sock->ssl == NULL)
571
0
        on_head.pipe_reader = &client->pipe_reader;
572
3.41k
#endif
573
574
    /* call the callback */
575
3.41k
    client->super._cb.on_body =
576
3.41k
        call_on_head(client, client->state.res == STREAM_STATE_CLOSED ? h2o_httpclient_error_is_eos : NULL, &on_head);
577
578
3.41k
    if (client->state.res == STREAM_STATE_CLOSED) {
579
693
        close_response(client);
580
693
        return;
581
2.72k
    } else if (client->super._cb.on_body == NULL) {
582
0
        client->_do_keepalive = 0;
583
0
        close_client(client);
584
0
        return;
585
0
    }
586
587
2.72k
    h2o_buffer_consume(&sock->input, rlen);
588
2.72k
    client->_socket_bytes_processed = client->sock->bytes_read - client->sock->input->size;
589
590
2.72k
    client->super._timeout.cb = on_body_timeout;
591
2.72k
    h2o_socket_read_start(sock, reader);
592
2.72k
    reader(client->sock, 0);
593
594
2.72k
#undef MAX_HEADERS
595
2.72k
}
596
597
static void on_head_first_byte_timeout(h2o_timer_t *entry)
598
0
{
599
0
    struct st_h2o_http1client_t *client = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1client_t, super._timeout, entry);
600
0
    on_error(client, h2o_httpclient_error_first_byte_timeout);
601
0
}
602
603
static void on_whole_request_sent(h2o_socket_t *sock, const char *err)
604
3.25k
{
605
3.25k
    struct st_h2o_http1client_t *client = sock->data;
606
607
3.25k
    h2o_timer_unlink(&client->super._timeout);
608
609
3.25k
    if (err != NULL) {
610
511
        on_error(client, h2o_httpclient_error_io);
611
511
        return;
612
511
    }
613
614
2.74k
    client->state.req = STREAM_STATE_CLOSED;
615
2.74k
    client->super.timings.request_end_at = h2o_gettimeofday(client->super.ctx->loop);
616
617
2.74k
    if (client->super.upgrade_to != NULL) {
618
        /* TODO use shutdown(2) to signal the peer that our send side has been closed, but continue reading on the receive side. */
619
0
        on_error(client, client->state.res < STREAM_STATE_BODY ? h2o_httpclient_error_io : h2o_httpclient_error_is_eos);
620
2.74k
    } else {
621
2.74k
        switch (client->state.res) {
622
2.74k
        case STREAM_STATE_HEAD:
623
2.74k
            client->super._timeout.cb = on_head_first_byte_timeout;
624
2.74k
            h2o_timer_link(client->super.ctx->loop, client->super.ctx->first_byte_timeout, &client->super._timeout);
625
2.74k
            break;
626
0
        case STREAM_STATE_BODY:
627
            /* the timer was used for sending the request; use it for receiving the response unless reading is paused */
628
0
            if (h2o_socket_is_reading(client->sock))
629
0
                h2o_timer_link(client->super.ctx->loop, client->super.ctx->io_timeout, &client->super._timeout);
630
0
            break;
631
1
        case STREAM_STATE_CLOSED:
632
1
            close_client(client);
633
1
            break;
634
2.74k
        }
635
2.74k
    }
636
2.74k
}
637
638
static void on_header_sent_wait_100(h2o_socket_t *sock, const char *err)
639
0
{
640
0
    struct st_h2o_http1client_t *client = sock->data;
641
642
0
    h2o_timer_unlink(&client->super._timeout);
643
644
0
    if (err != NULL) {
645
0
        on_error(client, h2o_httpclient_error_io);
646
0
        return;
647
0
    }
648
649
0
    if (client->state.res == STREAM_STATE_HEAD) {
650
0
        client->super._timeout.cb = on_head_first_byte_timeout;
651
0
        h2o_timer_link(client->super.ctx->loop, client->super.ctx->first_byte_timeout, &client->super._timeout);
652
0
    }
653
0
}
654
655
static void req_body_send_complete(h2o_socket_t *sock, const char *err)
656
1.98k
{
657
1.98k
    struct st_h2o_http1client_t *client = sock->data;
658
659
1.98k
    h2o_buffer_consume(&client->body_buf.buf, client->body_buf.buf->size);
660
661
1.98k
    if (err != NULL) {
662
511
        on_whole_request_sent(client->sock, err);
663
511
        return;
664
511
    }
665
666
1.47k
    int is_end_stream = client->body_buf.is_end_stream;
667
668
1.47k
    if (client->proceed_req != NULL) {
669
1.47k
        call_proceed_req(client, NULL);
670
1.47k
    }
671
672
1.47k
    if (is_end_stream)
673
217
        on_whole_request_sent(client->sock, NULL);
674
1.47k
}
675
676
/**
677
 * Encodes data. `bufs` must have at least 4 elements of space.
678
 */
679
static size_t req_body_send_prepare(struct st_h2o_http1client_t *client, h2o_iovec_t *bufs, size_t *bytes)
680
2.01k
{
681
2.01k
    size_t bufcnt = 0;
682
2.01k
    *bytes = 0;
683
684
2.01k
    if (client->_is_chunked) {
685
826
        if (client->body_buf.buf->size != 0) {
686
            /* build chunk header */
687
805
            bufs[bufcnt].base = client->_chunk_len_str;
688
805
            bufs[bufcnt].len =
689
805
                snprintf(client->_chunk_len_str, sizeof(client->_chunk_len_str), "%zx\r\n", client->body_buf.buf->size);
690
805
            *bytes += bufs[bufcnt].len;
691
805
            ++bufcnt;
692
            /* append chunk body */
693
805
            bufs[bufcnt++] = h2o_iovec_init(client->body_buf.buf->bytes, client->body_buf.buf->size);
694
805
            *bytes += client->body_buf.buf->size;
695
            /* append CRLF */
696
805
            bufs[bufcnt++] = h2o_iovec_init("\r\n", 2);
697
805
            *bytes += 2;
698
805
        }
699
826
        if (client->body_buf.is_end_stream) {
700
117
            static const h2o_iovec_t terminator = {H2O_STRLIT("0\r\n\r\n")};
701
117
            bufs[bufcnt++] = terminator;
702
117
            *bytes += terminator.len;
703
117
        }
704
1.18k
    } else if (client->body_buf.buf->size != 0) {
705
1.18k
        bufs[bufcnt++] = h2o_iovec_init(client->body_buf.buf->bytes, client->body_buf.buf->size);
706
1.18k
        *bytes += client->body_buf.buf->size;
707
1.18k
    }
708
709
2.01k
    return bufcnt;
710
2.01k
}
711
712
static void req_body_send(struct st_h2o_http1client_t *client)
713
859
{
714
859
    h2o_iovec_t bufs[4];
715
859
    size_t bytes, bufcnt = req_body_send_prepare(client, bufs, &bytes);
716
717
859
    h2o_timer_unlink(&client->super._timeout);
718
719
859
    h2o_socket_write(client->sock, bufs, bufcnt, req_body_send_complete);
720
859
    client->super.bytes_written.body += bytes;
721
859
    client->super.bytes_written.total += bytes;
722
723
859
    h2o_timer_link(client->super.ctx->loop, client->super.ctx->io_timeout, &client->super._timeout);
724
859
}
725
726
static int do_write_req(h2o_httpclient_t *_client, h2o_iovec_t chunk, int is_end_stream)
727
859
{
728
859
    struct st_h2o_http1client_t *client = (struct st_h2o_http1client_t *)_client;
729
730
859
    assert(chunk.len != 0 || is_end_stream);
731
859
    assert(!h2o_socket_is_writing(client->sock));
732
859
    assert(client->body_buf.buf->size == 0);
733
734
    /* store given content to buffer */
735
859
    if (chunk.len != 0) {
736
838
        if (!h2o_buffer_try_append(&client->body_buf.buf, chunk.base, chunk.len))
737
0
            return -1;
738
838
    }
739
859
    client->body_buf.is_end_stream = is_end_stream;
740
741
    /* check if the connection has to be closed for correct framing */
742
859
    if (client->state.res == STREAM_STATE_CLOSED)
743
394
        client->_do_keepalive = 0;
744
745
859
    req_body_send(client);
746
747
859
    return 0;
748
859
}
749
750
static void on_send_timeout(h2o_timer_t *entry)
751
0
{
752
0
    struct st_h2o_http1client_t *client = H2O_STRUCT_FROM_MEMBER(struct st_h2o_http1client_t, super._timeout, entry);
753
0
    on_error(client, h2o_httpclient_error_io_timeout);
754
0
}
755
756
static h2o_iovec_t build_request(struct st_h2o_http1client_t *client, h2o_iovec_t method, const h2o_url_t *url,
757
                                 h2o_iovec_t connection, const h2o_header_t *headers, size_t num_headers)
758
3.68k
{
759
3.68k
    h2o_iovec_t buf;
760
3.68k
    size_t offset = 0;
761
762
3.68k
    buf.len = method.len + url->path.len + url->authority.len + 512;
763
3.68k
    buf.base = h2o_mem_alloc_pool(client->super.pool, char, buf.len);
764
765
3.68k
#define RESERVE(sz)                                                                                                                \
766
39.2k
    do {                                                                                                                           \
767
39.2k
        size_t required = offset + sz + 4 /* for "\r\n\r\n" */;                                                                    \
768
39.2k
        if (required > buf.len) {                                                                                                  \
769
8.26k
            do {                                                                                                                   \
770
8.26k
                buf.len *= 2;                                                                                                      \
771
8.26k
            } while (required > buf.len);                                                                                          \
772
3.13k
            char *newp = h2o_mem_alloc_pool(client->super.pool, char, buf.len);                                                    \
773
3.13k
            memcpy(newp, buf.base, offset);                                                                                        \
774
3.13k
            buf.base = newp;                                                                                                       \
775
3.13k
        }                                                                                                                          \
776
39.2k
    } while (0)
777
3.68k
#define APPEND(s, l)                                                                                                               \
778
96.9k
    do {                                                                                                                           \
779
96.9k
        h2o_memcpy(buf.base + offset, (s), (l));                                                                                   \
780
96.9k
        offset += (l);                                                                                                             \
781
96.9k
    } while (0)
782
7.36k
#define APPEND_STRLIT(lit) APPEND((lit), sizeof(lit) - 1)
783
3.68k
#define APPEND_HEADER(h)                                                                                                           \
784
39.2k
    do {                                                                                                                           \
785
39.2k
        RESERVE((h)->name->len + (h)->value.len + 4);                                                                              \
786
39.2k
        APPEND((h)->orig_name ? (h)->orig_name : (h)->name->base, (h)->name->len);                                                 \
787
39.2k
        buf.base[offset++] = ':';                                                                                                  \
788
39.2k
        buf.base[offset++] = ' ';                                                                                                  \
789
39.2k
        APPEND((h)->value.base, (h)->value.len);                                                                                   \
790
39.2k
        buf.base[offset++] = '\r';                                                                                                 \
791
39.2k
        buf.base[offset++] = '\n';                                                                                                 \
792
39.2k
    } while (0)
793
794
3.68k
    APPEND(method.base, method.len);
795
3.68k
    buf.base[offset++] = ' ';
796
3.68k
    if (client->super.upgrade_to == h2o_httpclient_upgrade_to_connect) {
797
0
        if (h2o_memis(method.base, method.len, H2O_STRLIT("CONNECT-UDP"))) {
798
0
            APPEND_STRLIT("masque://");
799
0
            APPEND(url->authority.base, url->authority.len);
800
0
            APPEND_STRLIT("/");
801
0
        } else {
802
0
            APPEND(url->authority.base, url->authority.len);
803
0
        }
804
3.68k
    } else {
805
3.68k
        APPEND(url->path.base, url->path.len);
806
3.68k
    }
807
3.68k
    APPEND_STRLIT(" HTTP/1.1\r\nhost: ");
808
3.68k
    APPEND(url->authority.base, url->authority.len);
809
3.68k
    buf.base[offset++] = '\r';
810
3.68k
    buf.base[offset++] = '\n';
811
3.68k
    assert(offset <= buf.len);
812
813
    /* append supplied connection header, or "connection: upgrade" and upgrade header when request an upgrade */
814
3.68k
    if (client->super.upgrade_to != NULL && client->super.upgrade_to != h2o_httpclient_upgrade_to_connect) {
815
0
        h2o_header_t c = {&H2O_TOKEN_CONNECTION->buf, NULL, h2o_iovec_init(H2O_STRLIT("upgrade"))},
816
0
                     u = {&H2O_TOKEN_UPGRADE->buf, NULL,
817
0
                          h2o_iovec_init(client->super.upgrade_to, strlen(client->super.upgrade_to))};
818
0
        APPEND_HEADER(&c);
819
0
        APPEND_HEADER(&u);
820
3.68k
    } else if (connection.base != NULL) {
821
3.68k
        h2o_header_t h = {&H2O_TOKEN_CONNECTION->buf, NULL, connection};
822
3.68k
        APPEND_HEADER(&h);
823
3.68k
    }
824
825
3.68k
    if (client->_send_own_expect) {
826
0
        h2o_header_t h = {&H2O_TOKEN_EXPECT->buf, NULL, h2o_iovec_init(H2O_STRLIT("100-continue"))};
827
0
        APPEND_HEADER(&h);
828
0
    }
829
830
3.68k
    if (num_headers != 0) {
831
39.2k
        for (const h2o_header_t *h = headers, *h_end = h + num_headers; h != h_end; ++h)
832
35.6k
            APPEND_HEADER(h);
833
3.68k
    }
834
835
3.68k
    APPEND_STRLIT("\r\n");
836
837
    /* set the length */
838
3.68k
    assert(offset <= buf.len);
839
3.68k
    buf.len = offset;
840
841
3.68k
    return buf;
842
843
3.68k
#undef RESERVE
844
3.68k
#undef APPEND
845
3.68k
#undef APPEND_STRLIT
846
3.68k
}
847
848
static void start_request(struct st_h2o_http1client_t *client, h2o_iovec_t method, const h2o_url_t *url,
849
                          const h2o_header_t *headers, size_t num_headers, h2o_iovec_t body,
850
                          const h2o_httpclient_properties_t *props)
851
3.68k
{
852
3.68k
    h2o_iovec_t reqbufs[6]; /* 6 should be the maximum possible elements used */
853
3.68k
    size_t reqbufcnt = 0;
854
3.68k
    if (props->proxy_protocol->base != NULL)
855
0
        reqbufs[reqbufcnt++] = *props->proxy_protocol;
856
857
3.68k
    if (props->send_own_expect && (client->proceed_req != NULL || body.len != 0) && client->super.upgrade_to == NULL)
858
0
        client->_send_own_expect = 1; /* this must be set before calling build_request */
859
860
3.68k
    h2o_iovec_t header = build_request(client, method, url, *props->connection_header, headers, num_headers);
861
3.68k
    reqbufs[reqbufcnt++] = header;
862
3.68k
    client->super.bytes_written.header = header.len;
863
864
3.68k
    client->_is_chunked = *props->chunked;
865
3.68k
    client->_method_is_head = h2o_memis(method.base, method.len, H2O_STRLIT("HEAD"));
866
867
3.68k
    assert(PTLS_ELEMENTSOF(reqbufs) - reqbufcnt >= 4); /* req_body_send_prepare could write to 4 additional elements */
868
3.68k
    if (client->proceed_req != NULL) {
869
1.15k
        h2o_buffer_init(&client->body_buf.buf, &h2o_socket_buffer_prototype);
870
1.15k
        if (body.len != 0 && !h2o_buffer_try_append(&client->body_buf.buf, body.base, body.len)) {
871
0
            on_whole_request_sent(client->sock, h2o_httpclient_error_internal);
872
0
            return;
873
0
        }
874
1.15k
        if (client->_send_own_expect) {
875
0
            h2o_socket_write(client->sock, reqbufs, reqbufcnt, on_header_sent_wait_100);
876
1.15k
        } else {
877
1.15k
            size_t bytes_written;
878
1.15k
            reqbufcnt += req_body_send_prepare(client, reqbufs + reqbufcnt, &bytes_written);
879
1.15k
            client->super.bytes_written.body = bytes_written;
880
1.15k
            h2o_socket_write(client->sock, reqbufs, reqbufcnt, req_body_send_complete);
881
1.15k
        }
882
2.52k
    } else if (body.len != 0) {
883
136
        assert(!client->_is_chunked);
884
136
        if (client->_send_own_expect) {
885
0
            h2o_buffer_init(&client->body_buf.buf, &h2o_socket_buffer_prototype);
886
0
            client->body_buf.is_end_stream = 1;
887
0
            if (!h2o_buffer_try_append(&client->body_buf.buf, body.base, body.len)) {
888
0
                on_whole_request_sent(client->sock, h2o_httpclient_error_internal);
889
0
                return;
890
0
            }
891
0
            h2o_socket_write(client->sock, reqbufs, reqbufcnt, on_header_sent_wait_100);
892
136
        } else {
893
136
            reqbufs[reqbufcnt++] = body;
894
136
            client->super.bytes_written.body = body.len;
895
136
            h2o_socket_write(client->sock, reqbufs, reqbufcnt, on_whole_request_sent);
896
136
        }
897
2.39k
    } else {
898
2.39k
        assert(!client->_is_chunked);
899
2.39k
        h2o_socket_write(client->sock, reqbufs, reqbufcnt, on_whole_request_sent);
900
2.39k
    }
901
3.68k
    client->super.bytes_written.total = client->sock->bytes_written;
902
903
    /* Even all data highly likely has been written into TCP sendbuf, it is our practice to assume the socket write operation is
904
     * asynchronous and link the timer. */
905
3.68k
    client->super._timeout.cb = on_send_timeout;
906
3.68k
    h2o_timer_link(client->super.ctx->loop, client->super.ctx->io_timeout, &client->super._timeout);
907
908
3.68k
    client->state.req = STREAM_STATE_BODY;
909
3.68k
    client->super.timings.request_begin_at = h2o_gettimeofday(client->super.ctx->loop);
910
911
    /* If there's possibility of using a pipe for forwarding the content, reduce maximum read size before fetching headers. The
912
     * intent here is to not do a full-sized read of 1MB. 16KB has been chosen so that all HTTP response headers would be available,
913
     * and that an almost full-sized HTTP/2 frame / TLS record can be generated for the first chunk of data that we pass through
914
     * memory. */
915
3.68k
#if USE_PIPE_READER
916
3.68k
    if (client->_app_prefers_pipe_reader && h2o_evloop_socket_max_read_size > 16384)
917
2.50k
        h2o_evloop_socket_set_max_read_size(client->sock, 16384);
918
3.68k
#endif
919
920
3.68k
    h2o_socket_read_start(client->sock, on_head);
921
3.68k
}
922
923
static void on_connection_ready(struct st_h2o_http1client_t *client)
924
3.68k
{
925
3.68k
    h2o_iovec_t proxy_protocol = h2o_iovec_init(NULL, 0);
926
3.68k
    int chunked = 0;
927
3.68k
    h2o_iovec_t connection_header = h2o_iovec_init(NULL, 0);
928
3.68k
    h2o_httpclient_properties_t props = {
929
3.68k
        &proxy_protocol,
930
3.68k
        &chunked,
931
3.68k
        &connection_header,
932
3.68k
    };
933
3.68k
    h2o_iovec_t method;
934
3.68k
    h2o_url_t url;
935
3.68k
    h2o_header_t *headers;
936
3.68k
    size_t num_headers;
937
3.68k
    h2o_iovec_t body;
938
939
3.68k
    client->super._cb.on_head = client->super._cb.on_connect(&client->super, NULL, &method, &url, (const h2o_header_t **)&headers,
940
3.68k
                                                             &num_headers, &body, &client->proceed_req, &props, client->_origin);
941
3.68k
    client->_app_prefers_pipe_reader = props.prefer_pipe_reader;
942
943
3.68k
    if (client->super._cb.on_head == NULL) {
944
0
        close_client(client);
945
0
        return;
946
0
    }
947
948
3.68k
    start_request(client, method, &url, headers, num_headers, body, &props);
949
3.68k
}
950
951
static void do_cancel(h2o_httpclient_t *_client)
952
538
{
953
538
    struct st_h2o_http1client_t *client = (struct st_h2o_http1client_t *)_client;
954
538
    client->_do_keepalive = 0;
955
538
    close_client(client);
956
538
}
957
958
void update_read_state(struct st_h2o_http1client_t *client)
959
5.03k
{
960
    /* If pipe used, `client->reader` would have switched to `on_body_pipe` by the time this function is called for the first time.
961
     */
962
5.03k
    assert((client->pipe_reader.on_body_piped != NULL) == (client->reader == on_body_to_pipe));
963
964
5.03k
    if (client->reader == on_body_to_pipe) {
965
        /* When pipe is being used, resume read when consumption is notified from user. `h2o_socket_read_start` is invoked without
966
         * checking if we are already reading; this is because we want to make sure that the read callback replaced to the current
967
         * one. */
968
0
        h2o_socket_read_start(client->sock, client->reader);
969
5.03k
    } else {
970
        /* When buffer is used, start / stop reading based on the amount of data being buffered. */
971
5.03k
        if ((*client->super.buf)->size >= client->super.ctx->max_buffer_size) {
972
0
            if (h2o_socket_is_reading(client->sock)) {
973
0
                client->reader = client->sock->_cb.read;
974
0
                h2o_socket_read_stop(client->sock);
975
0
            }
976
5.03k
        } else {
977
5.03k
            if (!h2o_socket_is_reading(client->sock))
978
0
                h2o_socket_read_start(client->sock, client->reader);
979
5.03k
        }
980
5.03k
    }
981
982
    /* arm or unarm i/o timeout depending on if we are reading */
983
5.03k
    if (h2o_socket_is_reading(client->sock)) {
984
5.03k
        if (h2o_timer_is_linked(&client->super._timeout))
985
2.31k
            h2o_timer_unlink(&client->super._timeout);
986
5.03k
        h2o_timer_link(client->super.ctx->loop, client->super.ctx->io_timeout, &client->super._timeout);
987
5.03k
    } else {
988
0
        if (h2o_timer_is_linked(&client->super._timeout))
989
0
            h2o_timer_unlink(&client->super._timeout);
990
0
    }
991
5.03k
}
992
993
static void do_update_window(struct st_h2o_httpclient_t *_client)
994
2.31k
{
995
2.31k
    struct st_h2o_http1client_t *client = (void *)_client;
996
997
    /* When we are splicing to pipe, read synchronously. For prioritization logic to work correctly, it is important to provide
998
     * additional data synchronously in response to the invocation of `h2o_proceed_response`. When memory buffers are used,
999
     * lib/core/proxy.c uses a double buffering to prepare next chunk of data while a chunk of data is being fed to the HTTP
1000
     * handlers via `h2o_sendvec`. But when using splice, the pipe is the only one buffer available. */
1001
2.31k
    if (client->reader == on_body_to_pipe) {
1002
0
        on_body_to_pipe(client->sock, NULL);
1003
0
        return;
1004
0
    }
1005
1006
2.31k
    update_read_state(client);
1007
2.31k
}
1008
1009
static void do_get_conn_properties(h2o_httpclient_t *_client, h2o_httpclient_conn_properties_t *properties)
1010
3.68k
{
1011
3.68k
    struct st_h2o_http1client_t *client = (void *)_client;
1012
3.68k
    h2o_httpclient_set_conn_properties_of_socket(client->sock, properties);
1013
3.68k
}
1014
1015
static void setup_client(struct st_h2o_http1client_t *client, h2o_socket_t *sock, h2o_url_t *origin)
1016
3.68k
{
1017
3.68k
    memset(&client->sock, 0, sizeof(*client) - offsetof(struct st_h2o_http1client_t, sock));
1018
3.68k
    client->super.cancel = do_cancel;
1019
3.68k
    client->super.get_conn_properties = do_get_conn_properties;
1020
3.68k
    client->super.update_window = do_update_window;
1021
3.68k
    client->super.write_req = do_write_req;
1022
3.68k
    client->super.buf = &sock->input;
1023
3.68k
    client->sock = sock;
1024
3.68k
    sock->data = client;
1025
3.68k
    client->_origin = origin;
1026
3.68k
}
1027
1028
void h2o_httpclient__h1_on_connect(h2o_httpclient_t *_client, h2o_socket_t *sock, h2o_url_t *origin)
1029
3.68k
{
1030
3.68k
    struct st_h2o_http1client_t *client = (void *)_client;
1031
1032
3.68k
    assert(!h2o_timer_is_linked(&client->super._timeout));
1033
1034
3.68k
    setup_client(client, sock, origin);
1035
3.68k
    on_connection_ready(client);
1036
3.68k
}
1037
1038
const size_t h2o_httpclient__h1_size = sizeof(struct st_h2o_http1client_t);