Apache2
h2.h
Go to the documentation of this file.
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements. See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __mod_h2__h2__
18 #define __mod_h2__h2__
19 
20 #include <apr_version.h>
21 #include <ap_mmn.h>
22 
23 #include <nghttp2/nghttp2ver.h>
24 
25 struct h2_session;
26 struct h2_stream;
27 
28 /*
29  * When apr pollsets can poll file descriptors (e.g. pipes),
30  * we use it for polling stream input/output.
31  */
32 #ifdef H2_NO_PIPES
33 #define H2_USE_PIPES 0
34 #else
35 #define H2_USE_PIPES (APR_FILES_AS_SOCKETS && APR_VERSION_AT_LEAST(1,6,0))
36 #endif
37 
38 #if AP_MODULE_MAGIC_AT_LEAST(20211221, 15)
39 #define H2_USE_POLLFD_FROM_CONN 1
40 #else
41 #define H2_USE_POLLFD_FROM_CONN 0
42 #endif
43 
44 /* WebSockets support requires apr 1.7.0 for apr_encode.h, plus the
45  * WebSockets features of nghttp2 1.34.0 and later. */
46 #if H2_USE_PIPES && defined(NGHTTP2_VERSION_NUM) && NGHTTP2_VERSION_NUM >= 0x012200 && APR_VERSION_AT_LEAST(1,7,0)
47 #define H2_USE_WEBSOCKETS 1
48 #else
49 #define H2_USE_WEBSOCKETS 0
50 #endif
51 
56 extern const char *H2_MAGIC_TOKEN;
57 
58 #define H2_ERR_NO_ERROR (0x00)
59 #define H2_ERR_PROTOCOL_ERROR (0x01)
60 #define H2_ERR_INTERNAL_ERROR (0x02)
61 #define H2_ERR_FLOW_CONTROL_ERROR (0x03)
62 #define H2_ERR_SETTINGS_TIMEOUT (0x04)
63 #define H2_ERR_STREAM_CLOSED (0x05)
64 #define H2_ERR_FRAME_SIZE_ERROR (0x06)
65 #define H2_ERR_REFUSED_STREAM (0x07)
66 #define H2_ERR_CANCEL (0x08)
67 #define H2_ERR_COMPRESSION_ERROR (0x09)
68 #define H2_ERR_CONNECT_ERROR (0x0a)
69 #define H2_ERR_ENHANCE_YOUR_CALM (0x0b)
70 #define H2_ERR_INADEQUATE_SECURITY (0x0c)
71 #define H2_ERR_HTTP_1_1_REQUIRED (0x0d)
72 
73 #define H2_HEADER_METHOD ":method"
74 #define H2_HEADER_METHOD_LEN 7
75 #define H2_HEADER_SCHEME ":scheme"
76 #define H2_HEADER_SCHEME_LEN 7
77 #define H2_HEADER_AUTH ":authority"
78 #define H2_HEADER_AUTH_LEN 10
79 #define H2_HEADER_PATH ":path"
80 #define H2_HEADER_PATH_LEN 5
81 #define H2_HEADER_PROTO ":protocol"
82 #define H2_HEADER_PROTO_LEN 9
83 #define H2_CRLF "\r\n"
84 
85 /* Size of the frame header itself in HTTP/2 */
86 #define H2_FRAME_HDR_LEN 9
87 
88 /* Max data size to write so it fits inside a TLS record */
89 #define H2_DATA_CHUNK_SIZE ((16*1024) - 100 - H2_FRAME_HDR_LEN)
90 
91 /* Maximum number of padding bytes in a frame, rfc7540 */
92 #define H2_MAX_PADLEN 256
93 /* Initial default window size, RFC 7540 ch. 6.5.2 */
94 #define H2_INITIAL_WINDOW_SIZE ((64*1024)-1)
95 
96 #define H2_STREAM_CLIENT_INITIATED(id) (id&0x01)
97 
98 #define H2_ALEN(a) (sizeof(a)/sizeof((a)[0]))
99 
100 #define H2MAX(x,y) ((x) > (y) ? (x) : (y))
101 #define H2MIN(x,y) ((x) < (y) ? (x) : (y))
102 
103 typedef enum {
107 } h2_dependency;
108 
109 typedef struct h2_priority {
111  int weight;
113 
114 typedef enum {
120 
121 typedef enum {
122  H2_SESSION_ST_INIT, /* send initial SETTINGS, etc. */
123  H2_SESSION_ST_DONE, /* finished, connection close */
124  H2_SESSION_ST_IDLE, /* nothing to write, expecting data inc */
125  H2_SESSION_ST_BUSY, /* read/write without stop */
126  H2_SESSION_ST_WAIT, /* waiting for c1 incoming + c2s output */
127  H2_SESSION_ST_CLEANUP, /* pool is being cleaned up */
129 
130 typedef struct h2_session_props {
131  int accepted_max; /* the highest remote stream id was/will be handled */
132  int completed_max; /* the highest remote stream completed */
133  int emitted_count; /* the number of local streams sent */
134  int emitted_max; /* the highest local stream id sent */
135  int error; /* the last session error encountered */
136  const char *error_msg; /* the short message given on the error */
137  unsigned int accepting : 1; /* if the session is accepting new streams */
138  unsigned int shutdown : 1; /* if the final GOAWAY has been sent */
140 
141 typedef enum h2_stream_state_t {
150  H2_SS_MAX
152 
153 typedef enum {
162 
163 
164 /* h2_request is the transformer of HTTP2 streams into HTTP/1.1 internal
165  * format that will be fed to various httpd input filters to finally
166  * become a request_rec to be handled by soemone.
167  */
168 typedef struct h2_request h2_request;
169 struct h2_request {
170  const char *method; /* pseudo header values, see ch. 8.1.2.3 */
171  const char *scheme;
172  const char *authority;
173  const char *path;
174  const char *protocol;
176 
178  apr_off_t raw_bytes; /* RAW network bytes that generated this request - if known. */
179  int http_status; /* Store a possible HTTP status code that gets
180  * defined before creating the dummy HTTP/1.1
181  * request e.g. due to an error already
182  * detected.
183  */
184 };
185 
186 /*
187  * A possible HTTP status code is not defined yet. See the http_status field
188  * in struct h2_request above for further explanation.
189  */
190 #define H2_HTTP_STATUS_UNSET (0)
191 
192 typedef apr_status_t h2_io_data_cb(void *ctx, const char *data, apr_off_t len);
193 
194 typedef int h2_stream_pri_cmp_fn(int stream_id1, int stream_id2, void *session);
195 typedef struct h2_stream *h2_stream_get_fn(struct h2_session *session, int stream_id);
196 
197 /* Note key to attach stream id to conn_rec/request_rec instances */
198 #define H2_HDR_CONFORMANCE "http2-hdr-conformance"
199 #define H2_HDR_CONFORMANCE_UNSAFE "unsafe"
200 #define H2_PUSH_MODE_NOTE "http2-push-mode"
201 
202 
203 #if AP_MODULE_MAGIC_AT_LEAST(20211221, 6)
204 #define AP_HAS_RESPONSE_BUCKETS 1
205 
206 #else /* AP_MODULE_MAGIC_AT_LEAST(20211221, 6) */
207 #define AP_HAS_RESPONSE_BUCKETS 0
208 
209 #endif /* else AP_MODULE_MAGIC_AT_LEAST(20211221, 6) */
210 
211 #endif /* defined(__mod_h2__h2__) */
Module Magic Number.
APR Versioning Interface.
int apr_status_t
Definition: apr_errno.h:44
off_t apr_off_t
Definition: apr.h:396
struct apr_table_t apr_table_t
Definition: apr_tables.h:56
apr_int64_t apr_time_t
Definition: apr_time.h:45
h2_stream_state_t
Definition: h2.h:141
@ H2_SS_RSVD_L
Definition: h2.h:144
@ H2_SS_CLOSED
Definition: h2.h:148
@ H2_SS_CLOSED_R
Definition: h2.h:146
@ H2_SS_CLOSED_L
Definition: h2.h:147
@ H2_SS_IDLE
Definition: h2.h:142
@ H2_SS_MAX
Definition: h2.h:150
@ H2_SS_OPEN
Definition: h2.h:145
@ H2_SS_RSVD_R
Definition: h2.h:143
@ H2_SS_CLEANUP
Definition: h2.h:149
h2_push_policy
Definition: h2.h:114
@ H2_PUSH_FAST_LOAD
Definition: h2.h:118
@ H2_PUSH_NONE
Definition: h2.h:115
@ H2_PUSH_HEAD
Definition: h2.h:117
@ H2_PUSH_DEFAULT
Definition: h2.h:116
struct h2_priority h2_priority
h2_dependency
Definition: h2.h:103
@ H2_DEPENDANT_AFTER
Definition: h2.h:104
@ H2_DEPENDANT_INTERLEAVED
Definition: h2.h:105
@ H2_DEPENDANT_BEFORE
Definition: h2.h:106
apr_status_t h2_io_data_cb(void *ctx, const char *data, apr_off_t len)
Definition: h2.h:192
h2_stream_event_t
Definition: h2.h:153
@ H2_SEV_IN_ERROR
Definition: h2.h:158
@ H2_SEV_CLOSED_L
Definition: h2.h:154
@ H2_SEV_EOS_SENT
Definition: h2.h:157
@ H2_SEV_CLOSED_R
Definition: h2.h:155
@ H2_SEV_IN_DATA_PENDING
Definition: h2.h:159
@ H2_SEV_OUT_C1_BLOCK
Definition: h2.h:160
@ H2_SEV_CANCELLED
Definition: h2.h:156
struct h2_stream * h2_stream_get_fn(struct h2_session *session, int stream_id)
Definition: h2.h:195
int h2_stream_pri_cmp_fn(int stream_id1, int stream_id2, void *session)
Definition: h2.h:194
h2_session_state
Definition: h2.h:121
@ H2_SESSION_ST_INIT
Definition: h2.h:122
@ H2_SESSION_ST_IDLE
Definition: h2.h:124
@ H2_SESSION_ST_CLEANUP
Definition: h2.h:127
@ H2_SESSION_ST_DONE
Definition: h2.h:123
@ H2_SESSION_ST_WAIT
Definition: h2.h:126
@ H2_SESSION_ST_BUSY
Definition: h2.h:125
const char * H2_MAGIC_TOKEN
struct h2_session_props h2_session_props
Definition: h2.h:109
int weight
Definition: h2.h:111
h2_dependency dependency
Definition: h2.h:110
Definition: h2.h:169
const char * method
Definition: h2.h:170
const char * authority
Definition: h2.h:172
const char * scheme
Definition: h2.h:171
int http_status
Definition: h2.h:179
const char * path
Definition: h2.h:173
apr_table_t * headers
Definition: h2.h:175
apr_time_t request_time
Definition: h2.h:177
apr_off_t raw_bytes
Definition: h2.h:178
const char * protocol
Definition: h2.h:174
Definition: h2.h:130
unsigned int shutdown
Definition: h2.h:138
unsigned int accepting
Definition: h2.h:137
int error
Definition: h2.h:135
int emitted_max
Definition: h2.h:134
const char * error_msg
Definition: h2.h:136
int completed_max
Definition: h2.h:132
int emitted_count
Definition: h2.h:133
int accepted_max
Definition: h2.h:131
Definition: h2_session.h:64
Definition: h2_stream.h:78
struct h2_session * session
Definition: h2_stream.h:85