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.
19 #include <http_core.h>
21 #include <http_connection.h>
22 #include <scoreboard.h>
24 #include "h2_private.h"
25 #include "h2_conn_io.h"
30 #include "h2_stream.h"
31 #include "h2_stream_set.h"
32 #include "h2_request.h"
33 #include "h2_response.h"
34 #include "h2_session.h"
36 #include "h2_version.h"
38 #include "h2_filter.h"
41 #define H2MIN(x,y) ((x) < (y) ? (x) : (y))
43 static apr_status_t consume_brigade(h2_filter_cin *cin,
44 apr_bucket_brigade *bb,
45 apr_read_type_e block)
47 apr_status_t status = APR_SUCCESS;
48 apr_size_t readlen = 0;
50 while (status == APR_SUCCESS && !APR_BRIGADE_EMPTY(bb)) {
52 apr_bucket* bucket = APR_BRIGADE_FIRST(bb);
53 if (APR_BUCKET_IS_METADATA(bucket)) {
54 /* we do nothing regarding any meta here */
57 const char *bucket_data = NULL;
58 apr_size_t bucket_length = 0;
59 status = apr_bucket_read(bucket, &bucket_data,
60 &bucket_length, block);
62 if (status == APR_SUCCESS && bucket_length > 0) {
63 apr_size_t consumed = 0;
65 status = cin->cb(cin->cb_ctx, bucket_data, bucket_length, &consumed);
66 if (status == APR_SUCCESS && bucket_length > consumed) {
67 /* We have data left in the bucket. Split it. */
68 status = apr_bucket_split(bucket, consumed);
71 cin->start_read = apr_time_now();
74 apr_bucket_delete(bucket);
77 if (readlen == 0 && status == APR_SUCCESS && block == APR_NONBLOCK_READ) {
83 h2_filter_cin *h2_filter_cin_create(apr_pool_t *p, h2_filter_cin_cb *cb, void *ctx)
87 cin = apr_pcalloc(p, sizeof(*cin));
91 cin->start_read = UNSET;
95 void h2_filter_cin_timeout_set(h2_filter_cin *cin, int timeout_secs)
97 cin->timeout_secs = timeout_secs;
100 apr_status_t h2_filter_core_input(ap_filter_t* f,
101 apr_bucket_brigade* brigade,
102 ap_input_mode_t mode,
103 apr_read_type_e block,
106 h2_filter_cin *cin = f->ctx;
107 apr_status_t status = APR_SUCCESS;
108 apr_time_t saved_timeout = UNSET;
110 ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, f->c,
111 "core_input(%ld): read, %s, mode=%d, readbytes=%ld, timeout=%d",
112 (long)f->c->id, (block == APR_BLOCK_READ)? "BLOCK_READ" : "NONBLOCK_READ",
113 mode, (long)readbytes, cin->timeout_secs);
115 if (mode == AP_MODE_INIT || mode == AP_MODE_SPECULATIVE) {
116 return ap_get_brigade(f->next, brigade, mode, block, readbytes);
119 if (mode != AP_MODE_READBYTES) {
120 return (block == APR_BLOCK_READ)? APR_SUCCESS : APR_EAGAIN;
124 cin->bb = apr_brigade_create(cin->pool, f->c->bucket_alloc);
128 cin->socket = ap_get_conn_socket(f->c);
131 cin->start_read = apr_time_now();
132 if (APR_BRIGADE_EMPTY(cin->bb)) {
133 /* We only do a blocking read when we have no streams to process. So,
134 * in httpd scoreboard lingo, we are in a KEEPALIVE connection state.
135 * When reading non-blocking, we do have streams to process and update
136 * child with NULL request. That way, any current request information
137 * in the scoreboard is preserved.
139 if (block == APR_BLOCK_READ) {
140 if (cin->timeout_secs > 0) {
141 apr_time_t t = apr_time_from_sec(cin->timeout_secs);
142 apr_socket_timeout_get(cin->socket, &saved_timeout);
143 apr_socket_timeout_set(cin->socket, t);
146 status = ap_get_brigade(f->next, cin->bb, AP_MODE_READBYTES,
148 if (saved_timeout != UNSET) {
149 apr_socket_timeout_set(cin->socket, saved_timeout);
151 ap_log_cerror(APLOG_MARK, APLOG_TRACE1, status, f->c,
152 "core_input(%ld): got_brigade", (long)f->c->id);
157 status = consume_brigade(cin, cin->bb, block);
164 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, status, f->c,
165 "h2_conn_io: error reading");
171 /*******************************************************************************
172 * http2 connection status handler + stream out source
173 ******************************************************************************/
175 static const char *H2_SOS_H2_STATUS = "http2-status";
177 int h2_filter_h2_status_handler(request_rec *r)
179 h2_ctx *ctx = h2_ctx_rget(r);
182 if (strcmp(r->handler, "http2-status")) {
185 if (r->method_number != M_GET) {
189 task = ctx? h2_ctx_get_task(ctx) : NULL;
191 /* We need to handle the actual output on the main thread, as
192 * we need to access h2_session information. */
193 apr_table_setn(r->notes, H2_RESP_SOS_NOTE, H2_SOS_H2_STATUS);
194 apr_table_setn(r->headers_out, "Content-Type", "application/json");
201 #define bbout(...) apr_brigade_printf(bb, NULL, NULL, __VA_ARGS__)
202 static apr_status_t h2_sos_h2_status_buffer(h2_sos *sos, apr_bucket_brigade *bb)
204 h2_stream *stream = sos->stream;
205 h2_session *session = stream->session;
206 h2_mplx *mplx = session->mplx;
207 h2_push_diary *diary;
211 bb = apr_brigade_create(stream->pool, session->c->bucket_alloc);
215 bbout(" \"HTTP2\": \"on\",\n");
216 bbout(" \"H2PUSH\": \"%s\",\n", h2_session_push_enabled(session)? "on" : "off");
217 bbout(" \"mod_http2_version\": \"%s\",\n", MOD_HTTP2_VERSION);
218 bbout(" \"session_id\": %ld,\n", (long)session->id);
219 bbout(" \"streams_max\": %d,\n", (int)session->max_stream_count);
220 bbout(" \"this_stream\": %d,\n", stream->id);
221 bbout(" \"streams_open\": %d,\n", (int)h2_stream_set_size(session->streams));
222 bbout(" \"max_stream_started\": %d,\n", mplx->max_stream_started);
223 bbout(" \"requests_received\": %d,\n", session->requests_received);
224 bbout(" \"responses_submitted\": %d,\n", session->responses_submitted);
225 bbout(" \"streams_reset\": %d, \n", session->streams_reset);
226 bbout(" \"pushes_promised\": %d,\n", session->pushes_promised);
227 bbout(" \"pushes_submitted\": %d,\n", session->pushes_submitted);
228 bbout(" \"pushes_reset\": %d,\n", session->pushes_reset);
230 diary = session->push_diary;
233 const char *base64_digest;
236 status = h2_push_diary_digest_get(diary, stream->pool, 256,
237 stream->request->authority, &data, &len);
238 if (status == APR_SUCCESS) {
239 base64_digest = h2_util_base64url_encode(data, len, stream->pool);
240 bbout(" \"cache_digest\": \"%s\",\n", base64_digest);
243 /* try the reverse for testing purposes */
244 status = h2_push_diary_digest_set(diary, stream->request->authority, data, len);
245 if (status == APR_SUCCESS) {
246 status = h2_push_diary_digest_get(diary, stream->pool, 256,
247 stream->request->authority, &data, &len);
248 if (status == APR_SUCCESS) {
249 base64_digest = h2_util_base64url_encode(data, len, stream->pool);
250 bbout(" \"cache_digest^2\": \"%s\",\n", base64_digest);
254 bbout(" \"frames_received\": %ld,\n", (long)session->frames_received);
255 bbout(" \"frames_sent\": %ld,\n", (long)session->frames_sent);
256 bbout(" \"bytes_received\": %"APR_UINT64_T_FMT",\n", session->io.bytes_read);
257 bbout(" \"bytes_sent\": %"APR_UINT64_T_FMT"\n", session->io.bytes_written);
260 return sos->prev->buffer(sos->prev, bb);
263 static apr_status_t h2_sos_h2_status_read_to(h2_sos *sos, apr_bucket_brigade *bb,
264 apr_off_t *plen, int *peos)
266 return sos->prev->read_to(sos->prev, bb, plen, peos);
269 static apr_status_t h2_sos_h2_status_prep_read(h2_sos *sos, apr_off_t *plen, int *peos)
271 return sos->prev->prep_read(sos->prev, plen, peos);
274 static apr_status_t h2_sos_h2_status_readx(h2_sos *sos, h2_io_data_cb *cb, void *ctx,
275 apr_off_t *plen, int *peos)
277 return sos->prev->readx(sos->prev, cb, ctx, plen, peos);
280 static apr_table_t *h2_sos_h2_status_get_trailers(h2_sos *sos)
282 return sos->prev->get_trailers(sos->prev);
285 static h2_sos *h2_sos_h2_status_create(h2_sos *prev)
288 h2_response *response = prev->response;
290 apr_table_unset(response->headers, "Content-Length");
291 response->content_length = -1;
293 sos = apr_pcalloc(prev->stream->pool, sizeof(*sos));
295 sos->response = response;
296 sos->stream = prev->stream;
297 sos->buffer = h2_sos_h2_status_buffer;
298 sos->prep_read = h2_sos_h2_status_prep_read;
299 sos->readx = h2_sos_h2_status_readx;
300 sos->read_to = h2_sos_h2_status_read_to;
301 sos->get_trailers = h2_sos_h2_status_get_trailers;
306 h2_sos *h2_filter_sos_create(const char *name, struct h2_sos *prev)
308 if (!strcmp(H2_SOS_H2_STATUS, name)) {
309 return h2_sos_h2_status_create(prev);