Coverage Report

Created: 2025-07-11 06:24

/src/h2o/lib/handler/self_trace.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021 Fastly, Kazuho Oku
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 <inttypes.h>
23
#include "h2o.h"
24
25
0
#define SEND_WAIT 500        /* how frequent new loglines should be pushed out; in milliseconds */
26
0
#define BUFFER_LIMIT 8388608 /* do not buffer more than 8MB */
27
28
struct h2o_self_trace_generator {
29
    h2o_generator_t super;
30
    h2o_req_t *req;
31
    h2o_buffer_t *buf;
32
    h2o_doublebuffer_t inflight;
33
    h2o_timer_t send_timer;
34
    unsigned should_send_buffered : 1;
35
};
36
37
static void on_generator_dispose(void *_self)
38
0
{
39
0
    struct h2o_self_trace_generator *self = _self;
40
41
0
    quicly_tracer_t *tracer = self->req->conn->callbacks->get_tracer(self->req->conn);
42
0
    tracer->cb = NULL;
43
0
    tracer->ctx = NULL;
44
45
0
    h2o_buffer_dispose(&self->buf);
46
0
    h2o_doublebuffer_dispose(&self->inflight);
47
0
    h2o_timer_unlink(&self->send_timer);
48
0
}
49
50
static void do_send(struct h2o_self_trace_generator *self)
51
0
{
52
0
    h2o_iovec_t vec = h2o_doublebuffer_prepare(&self->inflight, &self->buf, SIZE_MAX);
53
0
    h2o_send(self->req, &vec, 1, H2O_SEND_STATE_IN_PROGRESS);
54
0
}
55
56
static void on_send_timeout(h2o_timer_t *_timer)
57
0
{
58
0
    struct h2o_self_trace_generator *self = H2O_STRUCT_FROM_MEMBER(struct h2o_self_trace_generator, send_timer, _timer);
59
60
0
    assert(!self->inflight.inflight);
61
0
    assert(self->should_send_buffered);
62
63
0
    self->should_send_buffered = 0;
64
0
    do_send(self);
65
0
}
66
67
static void adjust_send_timer(struct h2o_self_trace_generator *self)
68
0
{
69
0
    if (!self->inflight.inflight && self->should_send_buffered && !h2o_timer_is_linked(&self->send_timer))
70
0
        h2o_timer_link(self->req->conn->ctx->loop, SEND_WAIT, &self->send_timer);
71
0
}
72
73
static void log_trace(void *_self, const char *fmt, ...)
74
0
{
75
0
    struct h2o_self_trace_generator *self = _self;
76
77
    /* append provided input to the buffer */
78
0
    if (self->buf->size < BUFFER_LIMIT) {
79
0
        va_list args;
80
0
        va_start(args, fmt);
81
82
0
        h2o_iovec_t buf = h2o_buffer_reserve(&self->buf, 1024);
83
0
        int len = vsnprintf(buf.base, buf.len, fmt, args);
84
0
        if (len >= buf.len) {
85
0
            buf = h2o_buffer_reserve(&self->buf, len + 1);
86
0
            len = vsnprintf(buf.base, buf.len, fmt, args);
87
0
            assert(len < buf.len);
88
0
        }
89
0
        self->buf->size += len;
90
91
0
        va_end(args);
92
0
    }
93
94
    /* Log is sent only when there's another request inflight. Otherwise, it is buffered until another request becomes inflight. */
95
0
    if (!self->should_send_buffered && self->req->conn->callbacks->num_reqs_inflight(self->req->conn) > 1)
96
0
        self->should_send_buffered = 1;
97
0
    adjust_send_timer(self);
98
0
}
99
100
static void do_proceed(h2o_generator_t *_self, h2o_req_t *_req)
101
0
{
102
0
    struct h2o_self_trace_generator *self = (void *)_self;
103
104
0
    assert(self->inflight.inflight);
105
0
    h2o_doublebuffer_consume(&self->inflight);
106
107
0
    adjust_send_timer(self);
108
0
}
109
110
static int on_req(h2o_handler_t *handler, h2o_req_t *req)
111
0
{
112
0
    if (req->conn->callbacks->get_tracer == NULL) {
113
0
        h2o_send_error_403(req, "Forbidden", "not available", 0);
114
0
        return 0;
115
0
    }
116
117
0
    quicly_tracer_t *tracer = req->conn->callbacks->get_tracer(req->conn);
118
0
    if (tracer->cb != NULL) {
119
0
        h2o_send_error_403(req, "Forbidden", "conn-state handler is already attached", 0);
120
0
        return 0;
121
0
    }
122
123
    /* instantiate the generator */
124
0
    struct h2o_self_trace_generator *self = h2o_mem_alloc_shared(&req->pool, sizeof(*self), on_generator_dispose);
125
0
    self->super.proceed = do_proceed;
126
0
    self->super.stop = NULL;
127
0
    self->req = req;
128
0
    h2o_buffer_init(&self->buf, &h2o_socket_buffer_prototype);
129
0
    h2o_doublebuffer_init(&self->inflight, &h2o_socket_buffer_prototype);
130
0
    self->send_timer = (h2o_timer_t){.cb = on_send_timeout};
131
0
    self->should_send_buffered = 0;
132
133
    /* register */
134
0
    tracer->cb = log_trace;
135
0
    tracer->ctx = self;
136
137
    /* build response headers */
138
0
    req->res.status = 200;
139
0
    h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("text/plain; charset=utf-8"));
140
0
    h2o_add_header(&req->pool, &req->res.headers, H2O_TOKEN_CACHE_CONTROL, NULL, H2O_STRLIT("no-cache, no-store"));
141
0
    h2o_buffer_append(&self->buf, "\n", 1); /* add some data for simplicity */
142
143
0
    h2o_start_response(self->req, &self->super);
144
0
    do_send(self);
145
146
0
    return 0;
147
0
}
148
149
void h2o_self_trace_register(h2o_pathconf_t *conf)
150
0
{
151
0
    h2o_handler_t *self = h2o_create_handler(conf, sizeof(*self));
152
0
    self->on_req = on_req;
153
0
}