Apache HTTPD
http_core.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 "apr_strings.h"
18#include "apr_thread_proc.h" /* for RLIMIT stuff */
19
20#define APR_WANT_STRFUNC
21#include "apr_want.h"
22
23#include "httpd.h"
24#include "http_config.h"
25#include "http_connection.h"
26#include "http_core.h"
27#include "http_protocol.h" /* For index_of_response(). Grump. */
28#include "http_request.h"
29
30#include "util_filter.h"
31#include "util_ebcdic.h"
32#include "ap_mpm.h"
33#include "scoreboard.h"
34
35#include "mod_core.h"
36
37/* Handles for core filters */
43
45
46/* If we are using an MPM That Supports Async Connections,
47 * use a different processing function
48 */
49static int async_mpm = 0;
50
51static const char *set_keep_alive_timeout(cmd_parms *cmd, void *dummy,
52 const char *arg)
53{
56 if (err != NULL) {
57 return err;
58 }
59
60 /* Stolen from mod_proxy.c */
62 return "KeepAliveTimeout has wrong format";
63 cmd->server->keep_alive_timeout = timeout;
64
65 /* We don't want to take into account whether or not KeepAliveTimeout is
66 * set for the main server, because if no http_module directive is used
67 * for a vhost, it will inherit the http_srv_cfg from the main server.
68 * However keep_alive_timeout_set helps determine whether the vhost should
69 * use its own configured timeout or the one from the vhost declared first
70 * on the same IP:port (ie. c->base_server, and the legacy behaviour).
71 */
72 if (cmd->server->is_virtual) {
73 cmd->server->keep_alive_timeout_set = 1;
74 }
75 return NULL;
76}
77
78static const char *set_keep_alive(cmd_parms *cmd, void *dummy,
79 int arg)
80{
82 if (err != NULL) {
83 return err;
84 }
85
86 cmd->server->keep_alive = arg;
87 return NULL;
88}
89
90static const char *set_keep_alive_max(cmd_parms *cmd, void *dummy,
91 const char *arg)
92{
94 if (err != NULL) {
95 return err;
96 }
97
98 cmd->server->keep_alive_max = atoi(arg);
99 return NULL;
100}
101
102static const command_rec http_cmds[] = {
104 "Keep-Alive timeout duration (sec)"),
105 AP_INIT_TAKE1("MaxKeepAliveRequests", set_keep_alive_max, NULL, RSRC_CONF,
106 "Maximum number of Keep-Alive requests per connection, "
107 "or 0 for infinite"),
109 "Whether persistent connections should be On or Off"),
110 { NULL }
111};
112
113static const char *http_scheme(const request_rec *r)
114{
115 /*
116 * The http module shouldn't return anything other than
117 * "http" (the default) or "https".
118 */
119 if (r->server->server_scheme &&
120 (strcmp(r->server->server_scheme, "https") == 0))
121 return "https";
122
123 return "http";
124}
125
127{
128 if (r->server->server_scheme &&
129 (strcmp(r->server->server_scheme, "https") == 0))
130 return DEFAULT_HTTPS_PORT;
131
132 return DEFAULT_HTTP_PORT;
133}
134
136{
137 request_rec *r = NULL;
138 conn_state_t *cs = c->cs;
139
140 AP_DEBUG_ASSERT(cs != NULL);
142
145 if (ap_extended_status) {
146 ap_set_conn_count(c->sbh, r, c->keepalives);
147 }
148 if ((r = ap_read_request(c))) {
149 if (r->status == HTTP_OK) {
151 if (ap_extended_status) {
152 ap_set_conn_count(c->sbh, r, c->keepalives + 1);
153 }
156 /* After the call to ap_process_request, the
157 * request pool may have been deleted. We set
158 * r=NULL here to ensure that any dereference
159 * of r that might be added later in this function
160 * will result in a segfault immediately instead
161 * of nondeterministic failures later.
162 */
163 r = NULL;
164 }
165
168 /* Something went wrong; close the connection */
170 }
171 }
172 else { /* ap_read_request failed - client may have closed */
174 }
175 }
176
177 return OK;
178}
179
181{
182 request_rec *r;
183 conn_state_t *cs = c->cs;
185 int mpm_state = 0;
186
187 /*
188 * Read and process each request found on our connection
189 * until no requests are left or we decide to close.
190 */
191
193 while ((r = ap_read_request(c)) != NULL) {
194 apr_interval_time_t keep_alive_timeout = r->server->keep_alive_timeout;
195
196 /* To preserve legacy behaviour, use the keepalive timeout from the
197 * base server (first on this IP:port) when none is explicitly
198 * configured on this server.
199 */
201 keep_alive_timeout = c->base_server->keep_alive_timeout;
202 }
203
204 if (r->status == HTTP_OK) {
205 if (cs)
209 /* After the call to ap_process_request, the
210 * request pool will have been deleted. We set
211 * r=NULL here to ensure that any dereference
212 * of r that might be added later in this function
213 * will result in a segfault immediately instead
214 * of nondeterministic failures later.
215 */
216 r = NULL;
217 }
218
219 if (c->keepalive != AP_CONN_KEEPALIVE || c->aborted)
220 break;
221
223
225 break;
226 }
227
229 break;
230 }
231
232 if (!csd) {
234 }
236 apr_socket_timeout_set(csd, keep_alive_timeout);
237 /* Go straight to select() to wait for the next request */
238 }
239
240 return OK;
241}
242
244{
245 if (async_mpm && !c->clogging_input_filters) {
247 }
248 else {
250 }
251}
252
268
270{
271 if ((r->method_number == M_OPTIONS) && r->uri && (r->uri[0] == '*') &&
272 (r->uri[1] == '\0')) {
273 return DONE; /* Send HTTP pong, without Allow header */
274 }
275 return DECLINED;
276}
277
289
317
320 NULL, /* create per-directory config structure */
321 NULL, /* merge per-directory config structures */
322 NULL, /* create per-server config structure */
323 NULL, /* merge per-server config structures */
324 http_cmds, /* command apr_table_t */
325 register_hooks /* register hooks */
326};
#define AP_DECLARE_DATA
Definition ap_config.h:89
Apache Multi-Processing Module library.
APR Strings library.
APR Thread and Process Library.
APR Standard Headers Support.
void ap_hook_process_connection(ap_HOOK_process_connection_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition connection.c:42
#define AP_INIT_TAKE1(directive, func, mconfig, where, help)
void ap_hook_post_config(ap_HOOK_post_config_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition config.c:105
#define AP_DECLARE_MODULE(foo)
#define AP_INIT_FLAG(directive, func, mconfig, where, help)
request_rec * r
void * csd
#define DEFAULT_HTTPS_PORT
Definition httpd.h:280
#define DECLINED
Definition httpd.h:457
#define OK
Definition httpd.h:456
#define DEFAULT_HTTP_PORT
Definition httpd.h:278
#define DONE
Definition httpd.h:458
ap_filter_rec_t * ap_register_output_filter(const char *name, ap_out_filter_func filter_func, ap_init_filter_func filter_init, ap_filter_type ftype)
apr_status_t ap_filter_rec_t * ap_register_input_filter(const char *name, ap_in_filter_func filter_func, ap_init_filter_func filter_init, ap_filter_type ftype)
ap_filter_t * ap_add_output_filter_handle(ap_filter_rec_t *f, void *ctx, request_rec *r, conn_rec *c)
@ AP_FTYPE_TRANSCODE
@ AP_FTYPE_PROTOCOL
ap_filter_rec_t * ap_content_length_filter_handle
Definition core.c:133
apr_socket_t * ap_get_conn_socket(conn_rec *c)
Definition core.c:5202
void ap_method_registry_init(apr_pool_t *p)
void ap_hook_http_scheme(ap_HOOK_http_scheme_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition protocol.c:2589
apr_status_t ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b)
request_rec * ap_read_request(conn_rec *c)
Definition protocol.c:1423
apr_status_t ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b)
void ap_hook_default_port(ap_HOOK_default_port_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition protocol.c:2591
void ap_process_request(request_rec *r)
void ap_hook_map_to_storage(ap_HOOK_map_to_storage_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition request.c:83
void ap_hook_create_request(ap_HOOK_create_request_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition request.c:98
void ap_process_async_request(request_rec *r)
void * dummy
Definition http_vhost.h:62
void const char * arg
Definition http_vhost.h:63
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define APR_HOOK_REALLY_LAST
Definition apr_hooks.h:307
#define RSRC_CONF
#define HTTP_OK
Definition httpd.h:490
apr_status_t ap_http_chunk_filter(ap_filter_t *f, apr_bucket_brigade *b)
ap_filter_rec_t * ap_chunk_filter_handle
Definition http_core.c:40
int ap_send_http_trace(request_rec *r)
const char * ap_multipart_boundary
Definition http_core.c:44
ap_filter_rec_t * ap_http_outerror_filter_handle
Definition http_core.c:41
ap_filter_rec_t * ap_http_header_filter_handle
Definition http_core.c:39
ap_filter_rec_t * ap_http_input_filter_handle
Definition http_core.c:38
ap_filter_rec_t * ap_byterange_filter_handle
Definition http_core.c:42
apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes)
apr_status_t ap_http_outerror_filter(ap_filter_t *f, apr_bucket_brigade *b)
#define M_OPTIONS
Definition httpd.h:597
#define STANDARD20_MODULE_STUFF
void ap_random_insecure_bytes(void *buf, apr_size_t size)
Definition core.c:5455
#define AP_DEBUG_ASSERT(exp)
Definition httpd.h:2283
apr_status_t ap_timeout_parameter_parse(const char *timeout_parameter, apr_interval_time_t *timeout, const char *default_time_unit)
Definition util.c:2622
@ AP_CONN_KEEPALIVE
Definition httpd.h:1146
@ CONN_STATE_LINGER
Definition httpd.h:1264
@ CONN_STATE_READ_REQUEST_LINE
Definition httpd.h:1260
@ CONN_STATE_WRITE_COMPLETION
Definition httpd.h:1262
@ CONN_STATE_SUSPENDED
Definition httpd.h:1263
@ CONN_STATE_HANDLER
Definition httpd.h:1261
#define NOT_IN_DIR_CONTEXT
const char * ap_check_cmd_context(cmd_parms *cmd, unsigned forbidden)
Definition core.c:1301
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
#define APR_SUCCESS
Definition apr_errno.h:225
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_uint16_t apr_port_t
#define APR_INCOMPLETE_READ
const char * s
Definition apr_strings.h:95
apr_int32_t apr_int32_t apr_int32_t err
apr_cmdtype_e cmd
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
apr_status_t ap_mpm_query(int query_code, int *result)
Definition mpm_common.c:421
#define AP_MPMQ_MPM_STATE
Definition ap_mpm.h:174
#define AP_MPMQ_IS_ASYNC
Definition ap_mpm.h:176
#define AP_MPMQ_STOPPING
Definition ap_mpm.h:142
Apache Configuration.
Apache connection library.
static int ap_process_http_async_connection(conn_rec *c)
Definition http_core.c:135
static int http_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
Definition http_core.c:278
static const char * set_keep_alive_timeout(cmd_parms *cmd, void *dummy, const char *arg)
Definition http_core.c:51
static const char * set_keep_alive(cmd_parms *cmd, void *dummy, int arg)
Definition http_core.c:78
static const char * set_keep_alive_max(cmd_parms *cmd, void *dummy, const char *arg)
Definition http_core.c:90
static int ap_process_http_sync_connection(conn_rec *c)
Definition http_core.c:180
static const char * http_scheme(const request_rec *r)
Definition http_core.c:113
static int ap_process_http_connection(conn_rec *c)
Definition http_core.c:243
static int http_create_request(request_rec *r)
Definition http_core.c:253
static void register_hooks(apr_pool_t *p)
Definition http_core.c:290
static const command_rec http_cmds[]
Definition http_core.c:102
static int async_mpm
Definition http_core.c:49
static int http_send_options(request_rec *r)
Definition http_core.c:269
static apr_port_t http_port(const request_rec *r)
Definition http_core.c:126
CORE HTTP Daemon.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
mod_core private header file
return NULL
Definition mod_so.c:359
static int mpm_state
Apache scoreboard library.
void ap_set_conn_count(ap_sb_handle_t *sb, request_rec *r, unsigned short conn_count)
Definition scoreboard.c:367
#define SERVER_BUSY_WRITE
Definition scoreboard.h:60
#define SERVER_BUSY_READ
Definition scoreboard.h:59
int ap_extended_status
Definition scoreboard.c:61
int ap_update_child_status(ap_sb_handle_t *sbh, int status, request_rec *r)
Definition scoreboard.c:590
#define SERVER_BUSY_KEEPALIVE
Definition scoreboard.h:61
int ap_update_child_status_from_conn(ap_sb_handle_t *sbh, int status, conn_rec *c)
Definition scoreboard.c:603
This structure is used for recording information about the registered filters. It associates a name w...
Structure to store things which are per connection.
Definition httpd.h:1152
A structure to contain connection state information.
Definition httpd.h:1280
conn_state_e state
Definition httpd.h:1282
A structure that represents the current request.
Definition httpd.h:845
int status
Definition httpd.h:891
char * uri
Definition httpd.h:1016
request_rec * prev
Definition httpd.h:856
int method_number
Definition httpd.h:898
conn_rec * connection
Definition httpd.h:849
request_rec * main
Definition httpd.h:860
server_rec * server
Definition httpd.h:851
A structure to store information for each virtual server.
Definition httpd.h:1322
apr_interval_time_t keep_alive_timeout
Definition httpd.h:1374
const char * server_scheme
Definition httpd.h:1358
unsigned int keep_alive_timeout_set
Definition httpd.h:1403
apr_status_t apr_socket_opt_set(apr_socket_t *sock, apr_int32_t opt, apr_int32_t on)
Definition sockopt.c:113
apr_status_t apr_socket_timeout_set(apr_socket_t *sock, apr_interval_time_t t)
Definition sockopt.c:75
Utilities for EBCDIC conversion.
Apache filter library.
IN ULONG IN INT timeout