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.
16 #ifndef __mod_h2__h2_session__
17 #define __mod_h2__h2_session__
19 #include "h2_conn_io.h"
22 * A HTTP/2 connection, a session with a specific client.
24 * h2_session sits on top of a httpd conn_rec* instance and takes complete
25 * control of the connection data. It receives protocol frames from the
26 * client. For new HTTP/2 streams it creates h2_task(s) that are sent
27 * via callback to a dispatcher (see h2_conn.c).
28 * h2_session keeps h2_io's for each ongoing stream which buffer the
29 * payload for that stream.
31 * New incoming HEADER frames are converted into a h2_stream+h2_task instance
32 * that both represent a HTTP/2 stream, but may have separate lifetimes. This
33 * allows h2_task to be scheduled in other threads without semaphores
34 * all over the place. It allows task memory to be freed independant of
35 * session lifetime and sessions may close down while tasks are still running.
40 struct apr_thread_mutext_t;
41 struct apr_thread_cond_t;
50 struct nghttp2_session;
52 typedef struct h2_session h2_session;
55 long id; /* identifier of this session, unique
56 * inside a httpd process */
57 conn_rec *c; /* the connection this session serves */
58 request_rec *r; /* the request that started this in case
59 * of 'h2c', NULL otherwise */
60 int aborted; /* this session is being aborted */
61 apr_size_t frames_received; /* number of http/2 frames received */
62 apr_size_t max_stream_count; /* max number of open streams */
63 apr_size_t max_stream_mem; /* max buffer memory for a single stream */
65 apr_pool_t *pool; /* pool to use in session handling */
66 apr_bucket_brigade *bbtmp; /* brigade for keeping temporary data */
67 struct apr_thread_cond_t *iowait; /* our cond when trywaiting for data */
69 h2_conn_io io; /* io on httpd conn filters */
70 struct h2_mplx *mplx; /* multiplexer for stream data */
72 struct h2_stream_set *streams; /* streams handled by this session */
74 struct nghttp2_session *ngh2; /* the nghttp2 session (internal use) */
75 struct h2_workers *workers; /* for executing stream tasks */
79 /* Create a new h2_session for the given connection (mode 'h2').
80 * The session will apply the configured parameter.
82 h2_session *h2_session_create(conn_rec *c, struct h2_config *cfg,
83 struct h2_workers *workers);
85 /* Create a new h2_session for the given request (mode 'h2c').
86 * The session will apply the configured parameter.
88 h2_session *h2_session_rcreate(request_rec *r, struct h2_config *cfg,
89 struct h2_workers *workers);
91 /* Destroy the session and all object it still contains. This will not
92 * destroy h2_task instances that not finished yet. */
93 void h2_session_destroy(h2_session *session);
95 /* Called once at start of session. Performs initial client thingies. */
96 apr_status_t h2_session_start(h2_session *session, int *rv);
98 /* Return != 0 iff session is finished and connection can be closed.
100 int h2_session_is_done(h2_session *session);
102 /* Called when the session will shutdown after all open streams
103 * are handled. New streams will no longer be accepted.
104 * Call with reason APR_SUCCESS to initiate a graceful shutdown. */
105 apr_status_t h2_session_goaway(h2_session *session, apr_status_t reason);
107 /* Called when an error occured and the session needs to shut down.
108 * Status indicates the reason of the error. */
109 apr_status_t h2_session_abort(h2_session *session, apr_status_t reason, int rv);
111 /* Called before a session gets destroyed, might flush output etc. */
112 apr_status_t h2_session_close(h2_session *session);
114 /* Read more data from the client connection. Used normally with blocking
115 * APR_NONBLOCK_READ, which will return APR_EAGAIN when no data is available.
116 * Use with APR_BLOCK_READ only when certain that no data needs to be written
118 apr_status_t h2_session_read(h2_session *session, apr_read_type_e block);
120 /* Write data out to the client, if there is any. Otherwise, wait for
121 * a maximum of timeout micro-seconds and return to the caller. If timeout
122 * occurred, APR_TIMEUP will be returned.
124 apr_status_t h2_session_write(h2_session *session,
125 apr_interval_time_t timeout);
127 /* Start submitting the response to a stream request. This is possible
128 * once we have all the response headers. */
129 apr_status_t h2_session_handle_response(h2_session *session,
130 struct h2_stream *stream);
132 /* Get the h2_stream for the given stream idenrtifier. */
133 struct h2_stream *h2_session_get_stream(h2_session *session, int stream_id);
135 void h2_session_log_stats(h2_session *session);
137 #endif /* defined(__mod_h2__h2_session__) */