1 /* Copyright 2015 greenbytes GmbH (https://www.greenbytes.de)
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <apr_strings.h>
20 #include <http_core.h>
21 #include <http_protocol.h>
23 #include <http_connection.h>
24 #include <scoreboard.h>
26 #include "h2_private.h"
28 #include "h2_config.h"
29 #include "h2_conn_io.h"
34 #include "h2_stream.h"
35 #include "h2_request.h"
36 #include "h2_headers.h"
37 #include "h2_stream.h"
38 #include "h2_session.h"
40 #include "h2_version.h"
42 #include "h2_filter.h"
45 #define H2MIN(x,y) ((x) < (y) ? (x) : (y))
47 static apr_status_t recv_RAW_DATA(conn_rec *c, h2_filter_cin *cin,
48 apr_bucket *b, apr_read_type_e block)
50 h2_session *session = cin->session;
51 apr_status_t status = APR_SUCCESS;
56 status = apr_bucket_read(b, &data, &len, block);
58 while (status == APR_SUCCESS && len > 0) {
59 n = nghttp2_session_mem_recv(session->ngh2, (const uint8_t *)data, len);
61 ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c,
62 H2_SSSN_MSG(session, "fed %ld bytes to nghttp2, %ld read"),
65 if (nghttp2_is_fatal((int)n)) {
66 h2_session_event(session, H2_SESSION_EV_PROTO_ERROR,
67 (int)n, nghttp2_strerror((int)n));
68 status = APR_EGENERAL;
72 session->io.bytes_read += n;
84 static apr_status_t recv_RAW_brigade(conn_rec *c, h2_filter_cin *cin,
85 apr_bucket_brigade *bb,
86 apr_read_type_e block)
88 apr_status_t status = APR_SUCCESS;
92 h2_util_bb_log(c, c->id, APLOG_TRACE2, "RAW_in", bb);
93 while (status == APR_SUCCESS && !APR_BRIGADE_EMPTY(bb)) {
94 b = APR_BRIGADE_FIRST(bb);
96 if (APR_BUCKET_IS_METADATA(b)) {
100 status = recv_RAW_DATA(c, cin, b, block);
103 apr_bucket_delete(b);
106 if (!consumed && status == APR_SUCCESS && block == APR_NONBLOCK_READ) {
112 h2_filter_cin *h2_filter_cin_create(h2_session *session)
116 cin = apr_pcalloc(session->pool, sizeof(*cin));
120 cin->session = session;
124 void h2_filter_cin_timeout_set(h2_filter_cin *cin, apr_interval_time_t timeout)
126 cin->timeout = timeout;
129 apr_status_t h2_filter_core_input(ap_filter_t* f,
130 apr_bucket_brigade* brigade,
131 ap_input_mode_t mode,
132 apr_read_type_e block,
135 h2_filter_cin *cin = f->ctx;
136 apr_status_t status = APR_SUCCESS;
137 apr_interval_time_t saved_timeout = UNSET;
138 const int trace1 = APLOGctrace1(f->c);
141 ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, f->c,
142 "h2_session(%ld): read, %s, mode=%d, readbytes=%ld",
143 (long)f->c->id, (block == APR_BLOCK_READ)?
144 "BLOCK_READ" : "NONBLOCK_READ", mode, (long)readbytes);
147 if (mode == AP_MODE_INIT || mode == AP_MODE_SPECULATIVE) {
148 return ap_get_brigade(f->next, brigade, mode, block, readbytes);
151 if (mode != AP_MODE_READBYTES) {
152 return (block == APR_BLOCK_READ)? APR_SUCCESS : APR_EAGAIN;
156 cin->bb = apr_brigade_create(cin->session->pool, f->c->bucket_alloc);
160 cin->socket = ap_get_conn_socket(f->c);
163 if (APR_BRIGADE_EMPTY(cin->bb)) {
164 /* We only do a blocking read when we have no streams to process. So,
165 * in httpd scoreboard lingo, we are in a KEEPALIVE connection state.
167 if (block == APR_BLOCK_READ) {
168 if (cin->timeout > 0) {
169 apr_socket_timeout_get(cin->socket, &saved_timeout);
170 apr_socket_timeout_set(cin->socket, cin->timeout);
173 status = ap_get_brigade(f->next, cin->bb, AP_MODE_READBYTES,
175 if (saved_timeout != UNSET) {
176 apr_socket_timeout_set(cin->socket, saved_timeout);
182 status = recv_RAW_brigade(f->c, cin, cin->bb, block);
188 ap_log_cerror(APLOG_MARK, APLOG_TRACE1, status, f->c,
189 "h2_session(%ld): read", f->c->id);
193 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, status, f->c, APLOGNO(03046)
194 "h2_session(%ld): error reading", f->c->id);
200 /*******************************************************************************
201 * http2 connection status handler + stream out source
202 ******************************************************************************/
205 apr_bucket_refcount refcount;
206 h2_bucket_event_cb *cb;
208 } h2_bucket_observer;
210 static apr_status_t bucket_read(apr_bucket *b, const char **str,
211 apr_size_t *len, apr_read_type_e block)
220 static void bucket_destroy(void *data)
222 h2_bucket_observer *h = data;
223 if (apr_bucket_shared_destroy(h)) {
225 h->cb(h->ctx, H2_BUCKET_EV_BEFORE_DESTROY, NULL);
231 apr_bucket * h2_bucket_observer_make(apr_bucket *b, h2_bucket_event_cb *cb,
234 h2_bucket_observer *br;
236 br = apr_bucket_alloc(sizeof(*br), b->list);
240 b = apr_bucket_shared_make(b, br, 0, 0);
241 b->type = &h2_bucket_type_observer;
245 apr_bucket * h2_bucket_observer_create(apr_bucket_alloc_t *list,
246 h2_bucket_event_cb *cb, void *ctx)
248 apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
251 b->free = apr_bucket_free;
253 b = h2_bucket_observer_make(b, cb, ctx);
257 apr_status_t h2_bucket_observer_fire(apr_bucket *b, h2_bucket_event event)
259 if (H2_BUCKET_IS_OBSERVER(b)) {
260 h2_bucket_observer *l = (h2_bucket_observer *)b->data;
261 return l->cb(l->ctx, event, b);
266 const apr_bucket_type_t h2_bucket_type_observer = {
267 "H2OBS", 5, APR_BUCKET_METADATA,
270 apr_bucket_setaside_noop,
271 apr_bucket_split_notimpl,
272 apr_bucket_shared_copy
275 apr_bucket *h2_bucket_observer_beam(struct h2_bucket_beam *beam,
276 apr_bucket_brigade *dest,
277 const apr_bucket *src)
279 if (H2_BUCKET_IS_OBSERVER(src)) {
280 h2_bucket_observer *l = (h2_bucket_observer *)src->data;
281 apr_bucket *b = h2_bucket_observer_create(dest->bucket_alloc,
283 APR_BRIGADE_INSERT_TAIL(dest, b);
286 h2_bucket_observer_fire(b, H2_BUCKET_EV_BEFORE_MASTER_SEND);
292 static apr_status_t bbout(apr_bucket_brigade *bb, const char *fmt, ...)
298 rv = apr_brigade_vprintf(bb, NULL, NULL, fmt, args);
304 static void add_settings(apr_bucket_brigade *bb, h2_session *s, int last)
306 h2_mplx *m = s->mplx;
308 bbout(bb, " \"settings\": {\n");
309 bbout(bb, " \"SETTINGS_MAX_CONCURRENT_STREAMS\": %d,\n", m->max_streams);
310 bbout(bb, " \"SETTINGS_MAX_FRAME_SIZE\": %d,\n", 16*1024);
311 bbout(bb, " \"SETTINGS_INITIAL_WINDOW_SIZE\": %d,\n",
312 h2_config_geti(s->config, H2_CONF_WIN_SIZE));
313 bbout(bb, " \"SETTINGS_ENABLE_PUSH\": %d\n", h2_session_push_enabled(s));
314 bbout(bb, " }%s\n", last? "" : ",");
317 static void add_peer_settings(apr_bucket_brigade *bb, h2_session *s, int last)
319 bbout(bb, " \"peerSettings\": {\n");
320 bbout(bb, " \"SETTINGS_MAX_CONCURRENT_STREAMS\": %d,\n",
321 nghttp2_session_get_remote_settings(s->ngh2, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS));
322 bbout(bb, " \"SETTINGS_MAX_FRAME_SIZE\": %d,\n",
323 nghttp2_session_get_remote_settings(s->ngh2, NGHTTP2_SETTINGS_MAX_FRAME_SIZE));
324 bbout(bb, " \"SETTINGS_INITIAL_WINDOW_SIZE\": %d,\n",
325 nghttp2_session_get_remote_settings(s->ngh2, NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE));
326 bbout(bb, " \"SETTINGS_ENABLE_PUSH\": %d,\n",
327 nghttp2_session_get_remote_settings(s->ngh2, NGHTTP2_SETTINGS_ENABLE_PUSH));
328 bbout(bb, " \"SETTINGS_HEADER_TABLE_SIZE\": %d,\n",
329 nghttp2_session_get_remote_settings(s->ngh2, NGHTTP2_SETTINGS_HEADER_TABLE_SIZE));
330 bbout(bb, " \"SETTINGS_MAX_HEADER_LIST_SIZE\": %d\n",
331 nghttp2_session_get_remote_settings(s->ngh2, NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE));
332 bbout(bb, " }%s\n", last? "" : ",");
336 apr_bucket_brigade *bb;
341 static int add_stream(h2_stream *stream, void *ctx)
343 stream_ctx_t *x = ctx;
344 int32_t flowIn, flowOut;
346 flowIn = nghttp2_session_get_stream_effective_local_window_size(x->s->ngh2, stream->id);
347 flowOut = nghttp2_session_get_stream_remote_window_size(x->s->ngh2, stream->id);
348 bbout(x->bb, "%s\n \"%d\": {\n", (x->idx? "," : ""), stream->id);
349 bbout(x->bb, " \"state\": \"%s\",\n", h2_stream_state_str(stream));
350 bbout(x->bb, " \"created\": %f,\n", ((double)stream->created)/APR_USEC_PER_SEC);
351 bbout(x->bb, " \"flowIn\": %d,\n", flowIn);
352 bbout(x->bb, " \"flowOut\": %d,\n", flowOut);
353 bbout(x->bb, " \"dataIn\": %"APR_UINT64_T_FMT",\n", stream->in_data_octets);
354 bbout(x->bb, " \"dataOut\": %"APR_UINT64_T_FMT"\n", stream->out_data_octets);
361 static void add_streams(apr_bucket_brigade *bb, h2_session *s, int last)
368 bbout(bb, " \"streams\": {");
369 h2_mplx_stream_do(s->mplx, add_stream, &x);
370 bbout(bb, "\n }%s\n", last? "" : ",");
373 static void add_push(apr_bucket_brigade *bb, h2_session *s,
374 h2_stream *stream, int last)
376 h2_push_diary *diary;
379 bbout(bb, " \"push\": {\n");
380 diary = s->push_diary;
383 const char *base64_digest;
386 status = h2_push_diary_digest_get(diary, bb->p, 256,
387 stream->request->authority,
389 if (status == APR_SUCCESS) {
390 base64_digest = h2_util_base64url_encode(data, len, bb->p);
391 bbout(bb, " \"cacheDigest\": \"%s\",\n", base64_digest);
394 bbout(bb, " \"promises\": %d,\n", s->pushes_promised);
395 bbout(bb, " \"submits\": %d,\n", s->pushes_submitted);
396 bbout(bb, " \"resets\": %d\n", s->pushes_reset);
397 bbout(bb, " }%s\n", last? "" : ",");
400 static void add_in(apr_bucket_brigade *bb, h2_session *s, int last)
402 bbout(bb, " \"in\": {\n");
403 bbout(bb, " \"requests\": %d,\n", s->remote.emitted_count);
404 bbout(bb, " \"resets\": %d, \n", s->streams_reset);
405 bbout(bb, " \"frames\": %ld,\n", (long)s->frames_received);
406 bbout(bb, " \"octets\": %"APR_UINT64_T_FMT"\n", s->io.bytes_read);
407 bbout(bb, " }%s\n", last? "" : ",");
410 static void add_out(apr_bucket_brigade *bb, h2_session *s, int last)
412 bbout(bb, " \"out\": {\n");
413 bbout(bb, " \"responses\": %d,\n", s->responses_submitted);
414 bbout(bb, " \"frames\": %ld,\n", (long)s->frames_sent);
415 bbout(bb, " \"octets\": %"APR_UINT64_T_FMT"\n", s->io.bytes_written);
416 bbout(bb, " }%s\n", last? "" : ",");
419 static void add_stats(apr_bucket_brigade *bb, h2_session *s,
420 h2_stream *stream, int last)
422 bbout(bb, " \"stats\": {\n");
425 add_push(bb, s, stream, 1);
426 bbout(bb, " }%s\n", last? "" : ",");
429 static apr_status_t h2_status_insert(h2_task *task, apr_bucket *b)
431 h2_mplx *m = task->mplx;
432 h2_stream *stream = h2_mplx_stream_get(m, task->stream_id);
436 apr_bucket_brigade *bb;
438 int32_t connFlowIn, connFlowOut;
441 /* stream already done */
447 bb = apr_brigade_create(stream->pool, c->bucket_alloc);
449 connFlowIn = nghttp2_session_get_effective_local_window_size(s->ngh2);
450 connFlowOut = nghttp2_session_get_remote_window_size(s->ngh2);
453 bbout(bb, " \"version\": \"draft-01\",\n");
454 add_settings(bb, s, 0);
455 add_peer_settings(bb, s, 0);
456 bbout(bb, " \"connFlowIn\": %d,\n", connFlowIn);
457 bbout(bb, " \"connFlowOut\": %d,\n", connFlowOut);
458 bbout(bb, " \"sentGoAway\": %d,\n", s->local.shutdown);
460 add_streams(bb, s, 0);
462 add_stats(bb, s, stream, 1);
465 while ((e = APR_BRIGADE_FIRST(bb)) != APR_BRIGADE_SENTINEL(bb)) {
466 APR_BUCKET_REMOVE(e);
467 APR_BUCKET_INSERT_AFTER(b, e);
470 apr_brigade_destroy(bb);
475 static apr_status_t status_event(void *ctx, h2_bucket_event event,
480 ap_log_cerror(APLOG_MARK, APLOG_TRACE3, 0, task->c->master,
481 "status_event(%s): %d", task->id, event);
483 case H2_BUCKET_EV_BEFORE_MASTER_SEND:
484 h2_status_insert(task, b);
492 int h2_filter_h2_status_handler(request_rec *r)
494 h2_ctx *ctx = h2_ctx_rget(r);
495 conn_rec *c = r->connection;
497 apr_bucket_brigade *bb;
501 if (strcmp(r->handler, "http2-status")) {
504 if (r->method_number != M_GET && r->method_number != M_POST) {
508 task = ctx? h2_ctx_get_task(ctx) : NULL;
511 if ((status = ap_discard_request_body(r)) != OK) {
515 /* We need to handle the actual output on the main thread, as
516 * we need to access h2_session information. */
520 apr_table_unset(r->headers_out, "Content-Length");
521 ap_set_content_type(r, "application/json");
522 apr_table_setn(r->notes, H2_FILTER_DEBUG_NOTE, "on");
524 bb = apr_brigade_create(r->pool, c->bucket_alloc);
525 b = h2_bucket_observer_create(c->bucket_alloc, status_event, task);
526 APR_BRIGADE_INSERT_TAIL(bb, b);
527 b = apr_bucket_eos_create(c->bucket_alloc);
528 APR_BRIGADE_INSERT_TAIL(bb, b);
530 ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r,
531 "status_handler(%s): checking for incoming trailers",
533 if (r->trailers_in && !apr_is_empty_table(r->trailers_in)) {
534 ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, r,
535 "status_handler(%s): seeing incoming trailers",
537 apr_table_setn(r->trailers_out, "h2-trailers-in",
538 apr_itoa(r->pool, 1));
541 status = ap_pass_brigade(r->output_filters, bb);
542 if (status == APR_SUCCESS
543 || r->status != HTTP_OK
548 /* no way to know what type of error occurred */
549 ap_log_rerror(APLOG_MARK, APLOG_TRACE1, status, r,
550 "status_handler(%s): ap_pass_brigade failed",
552 return AP_FILTER_ERROR;