Apache HTTPD
h2_switch.c
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#include <assert.h>
18
19#include <apr_strings.h>
20#include <apr_optional.h>
21#include <apr_optional_hooks.h>
22
23#include <httpd.h>
24#include <http_core.h>
25#include <http_config.h>
26#include <http_connection.h>
27#include <http_protocol.h>
28#include <http_ssl.h>
29#include <http_log.h>
30
31#include "h2_private.h"
32#include "h2.h"
33
34#include "h2_config.h"
35#include "h2_conn_ctx.h"
36#include "h2_c1.h"
37#include "h2_c2.h"
38#include "h2_protocol.h"
39#include "h2_switch.h"
40
41/*******************************************************************************
42 * Once per lifetime init, retrieve optional functions
43 */
45{
46 (void)pool;
47 ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, s, "h2_switch init");
48
49 return APR_SUCCESS;
50}
51
56{
57 int proposed = 0;
58 int is_tls = ap_ssl_conn_is_ssl(c);
59 const char **protos = is_tls? h2_protocol_ids_tls : h2_protocol_ids_clear;
60
61 if (!h2_mpm_supported()) {
62 return DECLINED;
63 }
64
66 /* We do not know how to switch from anything else but http/1.1.
67 */
69 "protocol switch: current proto != http/1.1, declined");
70 return DECLINED;
71 }
72
75 "protocol propose: connection requirements not met");
76 return DECLINED;
77 }
78
79 if (r) {
80 /* So far, this indicates an HTTP/1 Upgrade header initiated
81 * protocol switch. For that, the HTTP2-Settings header needs
82 * to be present and valid for the connection.
83 */
84 const char *p;
85
86 if (!h2_c1_can_upgrade(r)) {
87 return DECLINED;
88 }
89
90 p = apr_table_get(r->headers_in, "HTTP2-Settings");
91 if (!p) {
93 "upgrade without HTTP2-Settings declined");
94 return DECLINED;
95 }
96
97 p = apr_table_get(r->headers_in, "Connection");
98 if (!ap_find_token(r->pool, p, "http2-settings")) {
100 "upgrade without HTTP2-Settings declined");
101 return DECLINED;
102 }
103
104 /* We also allow switching only for requests that have no body.
105 */
106 p = apr_table_get(r->headers_in, "Content-Length");
107 if ((p && strcmp(p, "0"))
108 || (!p && apr_table_get(r->headers_in, "Transfer-Encoding"))) {
110 "upgrade with body declined");
111 return DECLINED;
112 }
113 }
114
115 while (*protos) {
116 /* Add all protocols we know (tls or clear) and that
117 * are part of the offerings (if there have been any).
118 */
119 if (!offers || ap_array_str_contains(offers, *protos)) {
121 "proposing protocol '%s'", *protos);
122 APR_ARRAY_PUSH(proposals, const char*) = *protos;
123 proposed = 1;
124 }
125 ++protos;
126 }
127 return proposed? DECLINED : OK;
128}
129
130#if AP_HAS_RESPONSE_BUCKETS
132{
134
135 while (f && f->frec->ftype < ftype) {
136 fnext = f->next;
138 f = fnext;
139 }
140}
141
143{
145
146 while (f && f->frec->ftype < ftype) {
147 fnext = f->next;
149 f = fnext;
150 }
151}
152#endif
153
155 const char *protocol)
156{
157 int found = 0;
159 const char **p = protos;
160
161 (void)s;
162 if (!h2_mpm_supported()) {
163 return DECLINED;
164 }
165
166 while (*p) {
167 if (!strcmp(*p, protocol)) {
168 found = 1;
169 break;
170 }
171 p++;
172 }
173
174 if (found) {
176 "switching protocol to '%s'", protocol);
178
179 if (r != NULL) {
181#if AP_HAS_RESPONSE_BUCKETS
182 /* Switching in the middle of a request means that
183 * we have to send out the response to this one in h2
184 * format. So we need to take over the connection
185 * and remove all old filters with type up to the
186 * CONNEDCTION/NETWORK ones.
187 */
190#else
191 /* Switching in the middle of a request means that
192 * we have to send out the response to this one in h2
193 * format. So we need to take over the connection
194 * right away.
195 */
198#endif
199 /* Ok, start an h2_conn on this one. */
200 status = h2_c1_setup(c, r, s);
201
202 if (status != APR_SUCCESS) {
204 "session setup");
206 return !OK;
207 }
208
209 h2_c1_run(c);
210 }
211 return OK;
212 }
213
214 return DECLINED;
215}
216
217static const char *h2_protocol_get(const conn_rec *c)
218{
220
221 if (c->master) {
222 c = c->master;
223 }
225 return ctx? ctx->protocol : NULL;
226}
227
APR-UTIL registration of functions exported by modules.
Apache optional hook functions.
APR Strings library.
return found
Definition core.c:2840
request_rec * r
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
void ap_remove_input_filter(ap_filter_t *f)
ap_filter_type
apr_status_t ap_remove_input_filter_byhandle(ap_filter_t *next, const char *handle)
apr_status_t ap_remove_output_filter_byhandle(ap_filter_t *next, const char *handle)
void ap_remove_output_filter(ap_filter_t *f)
@ AP_FTYPE_CONNECTION
#define APLOGNO(n)
Definition http_log.h:117
#define ap_log_rerror
Definition http_log.h:454
#define ap_log_error
Definition http_log.h:370
#define ap_log_cerror
Definition http_log.h:498
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_TRACE1
Definition http_log.h:72
#define APLOG_DEBUG
Definition http_log.h:71
void ap_hook_protocol_get(ap_HOOK_protocol_get_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition protocol.c:2605
void ap_hook_protocol_propose(ap_HOOK_protocol_propose_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition protocol.c:2599
const char * ap_get_protocol(conn_rec *c)
Definition protocol.c:2397
void ap_hook_protocol_switch(ap_HOOK_protocol_switch_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition protocol.c:2603
#define AP_PROTOCOL_HTTP1
int ap_ssl_conn_is_ssl(conn_rec *c)
Definition ssl.c:90
apr_file_t * f
apr_brigade_flush void * ctx
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
int ap_find_token(apr_pool_t *p, const char *line, const char *tok)
Definition util.c:1726
int ap_array_str_contains(const apr_array_header_t *array, const char *s)
Definition util.c:3446
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_vformatter_buff_t * c
Definition apr_lib.h:175
int int int protocol
const char * s
Definition apr_strings.h:95
#define APR_ARRAY_PUSH(ary, type)
Definition apr_tables.h:150
int int status
apr_status_t h2_c1_run(conn_rec *c)
Definition h2_c1.c:116
apr_status_t h2_c1_setup(conn_rec *c, request_rec *r, server_rec *s)
Definition h2_c1.c:87
int h2_c1_can_upgrade(request_rec *r)
Definition h2_c1.c:201
int h2_mpm_supported(void)
Definition h2_c2.c:125
h2_conn_ctx_t * h2_conn_ctx_create_for_c1(conn_rec *c1, server_rec *s, const char *protocol)
Definition h2_conn_ctx.c:54
void h2_conn_ctx_detach(conn_rec *c)
Definition h2_conn_ctx.c:37
#define h2_conn_ctx_get(c)
Definition h2_conn_ctx.h:78
const char * h2_protocol_ids_tls[]
Definition h2_protocol.c:48
int h2_protocol_is_acceptable_c1(conn_rec *c, request_rec *r, int require_all)
const char * h2_protocol_ids_clear[]
Definition h2_protocol.c:52
void h2_switch_register_hooks(void)
Definition h2_switch.c:228
static int h2_protocol_switch(conn_rec *c, request_rec *r, server_rec *s, const char *protocol)
Definition h2_switch.c:154
static int h2_protocol_propose(conn_rec *c, request_rec *r, server_rec *s, const apr_array_header_t *offers, apr_array_header_t *proposals)
Definition h2_switch.c:52
static const char * h2_protocol_get(const conn_rec *c)
Definition h2_switch.c:217
apr_status_t h2_switch_init(apr_pool_t *pool, server_rec *s)
Definition h2_switch.c:44
Apache Configuration.
Apache connection library.
CORE HTTP Daemon.
Apache Logging library.
HTTP protocol handling.
SSL protocol handling.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
The representation of a filter chain.
Structure to store things which are per connection.
Definition httpd.h:1152
A structure that represents the current request.
Definition httpd.h:845
struct ap_filter_t * output_filters
Definition httpd.h:1070
apr_pool_t * pool
Definition httpd.h:847
struct ap_filter_t * input_filters
Definition httpd.h:1072
apr_table_t * headers_in
Definition httpd.h:976
A structure to store information for each virtual server.
Definition httpd.h:1322