1e18a033bSKonstantin Ananyev
2e18a033bSKonstantin Ananyev/*
3e18a033bSKonstantin Ananyev * Copyright (C) Igor Sysoev
4e18a033bSKonstantin Ananyev * Copyright (C) Nginx, Inc.
5e18a033bSKonstantin Ananyev */
6e18a033bSKonstantin Ananyev
7e18a033bSKonstantin Ananyev
8e18a033bSKonstantin Ananyev#include <ngx_config.h>
9e18a033bSKonstantin Ananyev#include <ngx_core.h>
10e18a033bSKonstantin Ananyev#include <ngx_http.h>
11e18a033bSKonstantin Ananyev
12e18a033bSKonstantin Ananyev
13e18a033bSKonstantin Ananyevstatic void ngx_http_wait_request_handler(ngx_event_t *ev);
14e18a033bSKonstantin Ananyevstatic void ngx_http_process_request_line(ngx_event_t *rev);
15e18a033bSKonstantin Ananyevstatic void ngx_http_process_request_headers(ngx_event_t *rev);
16e18a033bSKonstantin Ananyevstatic ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
17e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
18e18a033bSKonstantin Ananyev    ngx_uint_t request_line);
19e18a033bSKonstantin Ananyev
20e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_process_header_line(ngx_http_request_t *r,
21e18a033bSKonstantin Ananyev    ngx_table_elt_t *h, ngx_uint_t offset);
22e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_process_unique_header_line(ngx_http_request_t *r,
23e18a033bSKonstantin Ananyev    ngx_table_elt_t *h, ngx_uint_t offset);
24e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_process_multi_header_lines(ngx_http_request_t *r,
25e18a033bSKonstantin Ananyev    ngx_table_elt_t *h, ngx_uint_t offset);
26e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_process_host(ngx_http_request_t *r,
27e18a033bSKonstantin Ananyev    ngx_table_elt_t *h, ngx_uint_t offset);
28e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
29e18a033bSKonstantin Ananyev    ngx_table_elt_t *h, ngx_uint_t offset);
30e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r,
31e18a033bSKonstantin Ananyev    ngx_table_elt_t *h, ngx_uint_t offset);
32e18a033bSKonstantin Ananyev
33e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool,
34e18a033bSKonstantin Ananyev    ngx_uint_t alloc);
35e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_set_virtual_server(ngx_http_request_t *r,
36e18a033bSKonstantin Ananyev    ngx_str_t *host);
37e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_find_virtual_server(ngx_connection_t *c,
38e18a033bSKonstantin Ananyev    ngx_http_virtual_names_t *virtual_names, ngx_str_t *host,
39e18a033bSKonstantin Ananyev    ngx_http_request_t *r, ngx_http_core_srv_conf_t **cscfp);
40e18a033bSKonstantin Ananyev
41e18a033bSKonstantin Ananyevstatic void ngx_http_request_handler(ngx_event_t *ev);
42e18a033bSKonstantin Ananyevstatic void ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc);
43e18a033bSKonstantin Ananyevstatic void ngx_http_terminate_handler(ngx_http_request_t *r);
44e18a033bSKonstantin Ananyevstatic void ngx_http_finalize_connection(ngx_http_request_t *r);
45e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
46e18a033bSKonstantin Ananyevstatic void ngx_http_writer(ngx_http_request_t *r);
47e18a033bSKonstantin Ananyevstatic void ngx_http_request_finalizer(ngx_http_request_t *r);
48e18a033bSKonstantin Ananyev
49e18a033bSKonstantin Ananyevstatic void ngx_http_set_keepalive(ngx_http_request_t *r);
50e18a033bSKonstantin Ananyevstatic void ngx_http_keepalive_handler(ngx_event_t *ev);
51e18a033bSKonstantin Ananyevstatic void ngx_http_set_lingering_close(ngx_http_request_t *r);
52e18a033bSKonstantin Ananyevstatic void ngx_http_lingering_close_handler(ngx_event_t *ev);
53e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
54e18a033bSKonstantin Ananyevstatic void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
55e18a033bSKonstantin Ananyevstatic void ngx_http_log_request(ngx_http_request_t *r);
56e18a033bSKonstantin Ananyev
57e18a033bSKonstantin Ananyevstatic u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
58e18a033bSKonstantin Ananyevstatic u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
59e18a033bSKonstantin Ananyev    ngx_http_request_t *sr, u_char *buf, size_t len);
60e18a033bSKonstantin Ananyev
61e18a033bSKonstantin Ananyev#if (NGX_HTTP_SSL)
62e18a033bSKonstantin Ananyevstatic void ngx_http_ssl_handshake(ngx_event_t *rev);
63e18a033bSKonstantin Ananyevstatic void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
64e18a033bSKonstantin Ananyev#endif
65e18a033bSKonstantin Ananyev
66e18a033bSKonstantin Ananyev
67e18a033bSKonstantin Ananyevstatic char *ngx_http_client_errors[] = {
68e18a033bSKonstantin Ananyev
69e18a033bSKonstantin Ananyev    /* NGX_HTTP_PARSE_INVALID_METHOD */
70e18a033bSKonstantin Ananyev    "client sent invalid method",
71e18a033bSKonstantin Ananyev
72e18a033bSKonstantin Ananyev    /* NGX_HTTP_PARSE_INVALID_REQUEST */
73e18a033bSKonstantin Ananyev    "client sent invalid request",
74e18a033bSKonstantin Ananyev
75e18a033bSKonstantin Ananyev    /* NGX_HTTP_PARSE_INVALID_09_METHOD */
76e18a033bSKonstantin Ananyev    "client sent invalid method in HTTP/0.9 request"
77e18a033bSKonstantin Ananyev};
78e18a033bSKonstantin Ananyev
79e18a033bSKonstantin Ananyev
80e18a033bSKonstantin Ananyevngx_http_header_t  ngx_http_headers_in[] = {
81e18a033bSKonstantin Ananyev    { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
82e18a033bSKonstantin Ananyev                 ngx_http_process_host },
83e18a033bSKonstantin Ananyev
84e18a033bSKonstantin Ananyev    { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
85e18a033bSKonstantin Ananyev                 ngx_http_process_connection },
86e18a033bSKonstantin Ananyev
87e18a033bSKonstantin Ananyev    { ngx_string("If-Modified-Since"),
88e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, if_modified_since),
89e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
90e18a033bSKonstantin Ananyev
91e18a033bSKonstantin Ananyev    { ngx_string("If-Unmodified-Since"),
92e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, if_unmodified_since),
93e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
94e18a033bSKonstantin Ananyev
95e18a033bSKonstantin Ananyev    { ngx_string("If-Match"),
96e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, if_match),
97e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
98e18a033bSKonstantin Ananyev
99e18a033bSKonstantin Ananyev    { ngx_string("If-None-Match"),
100e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, if_none_match),
101e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
102e18a033bSKonstantin Ananyev
103e18a033bSKonstantin Ananyev    { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
104e18a033bSKonstantin Ananyev                 ngx_http_process_user_agent },
105e18a033bSKonstantin Ananyev
106e18a033bSKonstantin Ananyev    { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
107e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
108e18a033bSKonstantin Ananyev
109e18a033bSKonstantin Ananyev    { ngx_string("Content-Length"),
110e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, content_length),
111e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
112e18a033bSKonstantin Ananyev
113e18a033bSKonstantin Ananyev    { ngx_string("Content-Range"),
114e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, content_range),
115e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
116e18a033bSKonstantin Ananyev
117e18a033bSKonstantin Ananyev    { ngx_string("Content-Type"),
118e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, content_type),
119e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
120e18a033bSKonstantin Ananyev
121e18a033bSKonstantin Ananyev    { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
122e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
123e18a033bSKonstantin Ananyev
124e18a033bSKonstantin Ananyev    { ngx_string("If-Range"),
125e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, if_range),
126e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
127e18a033bSKonstantin Ananyev
128e18a033bSKonstantin Ananyev    { ngx_string("Transfer-Encoding"),
129e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, transfer_encoding),
130e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
131e18a033bSKonstantin Ananyev
132e18a033bSKonstantin Ananyev    { ngx_string("Expect"),
133e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, expect),
134e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
135e18a033bSKonstantin Ananyev
136e18a033bSKonstantin Ananyev    { ngx_string("Upgrade"),
137e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, upgrade),
138e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
139e18a033bSKonstantin Ananyev
140e18a033bSKonstantin Ananyev#if (NGX_HTTP_GZIP)
141e18a033bSKonstantin Ananyev    { ngx_string("Accept-Encoding"),
142e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, accept_encoding),
143e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
144e18a033bSKonstantin Ananyev
145e18a033bSKonstantin Ananyev    { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
146e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
147e18a033bSKonstantin Ananyev#endif
148e18a033bSKonstantin Ananyev
149e18a033bSKonstantin Ananyev    { ngx_string("Authorization"),
150e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, authorization),
151e18a033bSKonstantin Ananyev                 ngx_http_process_unique_header_line },
152e18a033bSKonstantin Ananyev
153e18a033bSKonstantin Ananyev    { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
154e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
155e18a033bSKonstantin Ananyev
156e18a033bSKonstantin Ananyev#if (NGX_HTTP_X_FORWARDED_FOR)
157e18a033bSKonstantin Ananyev    { ngx_string("X-Forwarded-For"),
158e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, x_forwarded_for),
159e18a033bSKonstantin Ananyev                 ngx_http_process_multi_header_lines },
160e18a033bSKonstantin Ananyev#endif
161e18a033bSKonstantin Ananyev
162e18a033bSKonstantin Ananyev#if (NGX_HTTP_REALIP)
163e18a033bSKonstantin Ananyev    { ngx_string("X-Real-IP"),
164e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, x_real_ip),
165e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
166e18a033bSKonstantin Ananyev#endif
167e18a033bSKonstantin Ananyev
168e18a033bSKonstantin Ananyev#if (NGX_HTTP_HEADERS)
169e18a033bSKonstantin Ananyev    { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
170e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
171e18a033bSKonstantin Ananyev
172e18a033bSKonstantin Ananyev    { ngx_string("Accept-Language"),
173e18a033bSKonstantin Ananyev                 offsetof(ngx_http_headers_in_t, accept_language),
174e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
175e18a033bSKonstantin Ananyev#endif
176e18a033bSKonstantin Ananyev
177e18a033bSKonstantin Ananyev#if (NGX_HTTP_DAV)
178e18a033bSKonstantin Ananyev    { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
179e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
180e18a033bSKonstantin Ananyev
181e18a033bSKonstantin Ananyev    { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
182e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
183e18a033bSKonstantin Ananyev
184e18a033bSKonstantin Ananyev    { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
185e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
186e18a033bSKonstantin Ananyev
187e18a033bSKonstantin Ananyev    { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
188e18a033bSKonstantin Ananyev                 ngx_http_process_header_line },
189e18a033bSKonstantin Ananyev#endif
190e18a033bSKonstantin Ananyev
191e18a033bSKonstantin Ananyev    { ngx_string("Cookie"), offsetof(ngx_http_headers_in_t, cookies),
192e18a033bSKonstantin Ananyev                 ngx_http_process_multi_header_lines },
193e18a033bSKonstantin Ananyev
194e18a033bSKonstantin Ananyev    { ngx_null_string, 0, NULL }
195e18a033bSKonstantin Ananyev};
196e18a033bSKonstantin Ananyev
197e18a033bSKonstantin Ananyev
198e18a033bSKonstantin Ananyevvoid
199e18a033bSKonstantin Ananyevngx_http_init_connection(ngx_connection_t *c)
200e18a033bSKonstantin Ananyev{
201e18a033bSKonstantin Ananyev    ngx_uint_t              i;
202e18a033bSKonstantin Ananyev    ngx_event_t            *rev;
203e18a033bSKonstantin Ananyev    struct sockaddr_in     *sin;
204e18a033bSKonstantin Ananyev    ngx_http_port_t        *port;
205e18a033bSKonstantin Ananyev    ngx_http_in_addr_t     *addr;
206e18a033bSKonstantin Ananyev    ngx_http_log_ctx_t     *ctx;
207e18a033bSKonstantin Ananyev    ngx_http_connection_t  *hc;
208e18a033bSKonstantin Ananyev#if (NGX_HAVE_INET6)
209e18a033bSKonstantin Ananyev    struct sockaddr_in6    *sin6;
210e18a033bSKonstantin Ananyev    ngx_http_in6_addr_t    *addr6;
211e18a033bSKonstantin Ananyev#endif
212e18a033bSKonstantin Ananyev
213e18a033bSKonstantin Ananyev    hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
214e18a033bSKonstantin Ananyev    if (hc == NULL) {
215e18a033bSKonstantin Ananyev        ngx_http_close_connection(c);
216e18a033bSKonstantin Ananyev        return;
217e18a033bSKonstantin Ananyev    }
218e18a033bSKonstantin Ananyev
219e18a033bSKonstantin Ananyev    c->data = hc;
220e18a033bSKonstantin Ananyev
221e18a033bSKonstantin Ananyev    /* find the server configuration for the address:port */
222e18a033bSKonstantin Ananyev
223e18a033bSKonstantin Ananyev    port = c->listening->servers;
224e18a033bSKonstantin Ananyev
225e18a033bSKonstantin Ananyev    if (port->naddrs > 1) {
226e18a033bSKonstantin Ananyev
227e18a033bSKonstantin Ananyev        /*
228e18a033bSKonstantin Ananyev         * there are several addresses on this port and one of them
229e18a033bSKonstantin Ananyev         * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
230e18a033bSKonstantin Ananyev         * is required to determine a server address
231e18a033bSKonstantin Ananyev         */
232e18a033bSKonstantin Ananyev
233e18a033bSKonstantin Ananyev        if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
234e18a033bSKonstantin Ananyev            ngx_http_close_connection(c);
235e18a033bSKonstantin Ananyev            return;
236e18a033bSKonstantin Ananyev        }
237e18a033bSKonstantin Ananyev
238e18a033bSKonstantin Ananyev        switch (c->local_sockaddr->sa_family) {
239e18a033bSKonstantin Ananyev
240e18a033bSKonstantin Ananyev#if (NGX_HAVE_INET6)
241e18a033bSKonstantin Ananyev        case AF_INET6:
242e18a033bSKonstantin Ananyev            sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
243e18a033bSKonstantin Ananyev
244e18a033bSKonstantin Ananyev            addr6 = port->addrs;
245e18a033bSKonstantin Ananyev
246e18a033bSKonstantin Ananyev            /* the last address is "*" */
247e18a033bSKonstantin Ananyev
248e18a033bSKonstantin Ananyev            for (i = 0; i < port->naddrs - 1; i++) {
249e18a033bSKonstantin Ananyev                if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
250e18a033bSKonstantin Ananyev                    break;
251e18a033bSKonstantin Ananyev                }
252e18a033bSKonstantin Ananyev            }
253e18a033bSKonstantin Ananyev
254e18a033bSKonstantin Ananyev            hc->addr_conf = &addr6[i].conf;
255e18a033bSKonstantin Ananyev
256e18a033bSKonstantin Ananyev            break;
257e18a033bSKonstantin Ananyev#endif
258e18a033bSKonstantin Ananyev
259e18a033bSKonstantin Ananyev        default: /* AF_INET */
260e18a033bSKonstantin Ananyev            sin = (struct sockaddr_in *) c->local_sockaddr;
261e18a033bSKonstantin Ananyev
262e18a033bSKonstantin Ananyev            addr = port->addrs;
263e18a033bSKonstantin Ananyev
264e18a033bSKonstantin Ananyev            /* the last address is "*" */
265e18a033bSKonstantin Ananyev
266e18a033bSKonstantin Ananyev            for (i = 0; i < port->naddrs - 1; i++) {
267e18a033bSKonstantin Ananyev                if (addr[i].addr == sin->sin_addr.s_addr) {
268e18a033bSKonstantin Ananyev                    break;
269e18a033bSKonstantin Ananyev                }
270e18a033bSKonstantin Ananyev            }
271e18a033bSKonstantin Ananyev
272e18a033bSKonstantin Ananyev            hc->addr_conf = &addr[i].conf;
273e18a033bSKonstantin Ananyev
274e18a033bSKonstantin Ananyev            break;
275e18a033bSKonstantin Ananyev        }
276e18a033bSKonstantin Ananyev
277e18a033bSKonstantin Ananyev    } else {
278e18a033bSKonstantin Ananyev
279e18a033bSKonstantin Ananyev        switch (c->local_sockaddr->sa_family) {
280e18a033bSKonstantin Ananyev
281e18a033bSKonstantin Ananyev#if (NGX_HAVE_INET6)
282e18a033bSKonstantin Ananyev        case AF_INET6:
283e18a033bSKonstantin Ananyev            addr6 = port->addrs;
284e18a033bSKonstantin Ananyev            hc->addr_conf = &addr6[0].conf;
285e18a033bSKonstantin Ananyev            break;
286e18a033bSKonstantin Ananyev#endif
287e18a033bSKonstantin Ananyev
288e18a033bSKonstantin Ananyev        default: /* AF_INET */
289e18a033bSKonstantin Ananyev            addr = port->addrs;
290e18a033bSKonstantin Ananyev            hc->addr_conf = &addr[0].conf;
291e18a033bSKonstantin Ananyev            break;
292e18a033bSKonstantin Ananyev        }
293e18a033bSKonstantin Ananyev    }
294e18a033bSKonstantin Ananyev
295e18a033bSKonstantin Ananyev    /* the default server configuration for the address:port */
296e18a033bSKonstantin Ananyev    hc->conf_ctx = hc->addr_conf->default_server->ctx;
297e18a033bSKonstantin Ananyev
298e18a033bSKonstantin Ananyev    ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
299e18a033bSKonstantin Ananyev    if (ctx == NULL) {
300e18a033bSKonstantin Ananyev        ngx_http_close_connection(c);
301e18a033bSKonstantin Ananyev        return;
302e18a033bSKonstantin Ananyev    }
303e18a033bSKonstantin Ananyev
304e18a033bSKonstantin Ananyev    ctx->connection = c;
305e18a033bSKonstantin Ananyev    ctx->request = NULL;
306e18a033bSKonstantin Ananyev    ctx->current_request = NULL;
307e18a033bSKonstantin Ananyev
308e18a033bSKonstantin Ananyev    c->log->connection = c->number;
309e18a033bSKonstantin Ananyev    c->log->handler = ngx_http_log_error;
310e18a033bSKonstantin Ananyev    c->log->data = ctx;
311e18a033bSKonstantin Ananyev    c->log->action = "waiting for request";
312e18a033bSKonstantin Ananyev
313e18a033bSKonstantin Ananyev    c->log_error = NGX_ERROR_INFO;
314e18a033bSKonstantin Ananyev
315e18a033bSKonstantin Ananyev    rev = c->read;
316e18a033bSKonstantin Ananyev    rev->handler = ngx_http_wait_request_handler;
317e18a033bSKonstantin Ananyev    c->write->handler = ngx_http_empty_handler;
318e18a033bSKonstantin Ananyev
319e18a033bSKonstantin Ananyev#if (NGX_HTTP_V2)
320e18a033bSKonstantin Ananyev    if (hc->addr_conf->http2) {
321e18a033bSKonstantin Ananyev        rev->handler = ngx_http_v2_init;
322e18a033bSKonstantin Ananyev    }
323e18a033bSKonstantin Ananyev#endif
324e18a033bSKonstantin Ananyev
325e18a033bSKonstantin Ananyev#if (NGX_HTTP_SSL)
326e18a033bSKonstantin Ananyev    {
327e18a033bSKonstantin Ananyev    ngx_http_ssl_srv_conf_t  *sscf;
328e18a033bSKonstantin Ananyev
329e18a033bSKonstantin Ananyev    sscf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_ssl_module);
330e18a033bSKonstantin Ananyev
331e18a033bSKonstantin Ananyev    if (sscf->enable || hc->addr_conf->ssl) {
332e18a033bSKonstantin Ananyev
333e18a033bSKonstantin Ananyev        c->log->action = "SSL handshaking";
334e18a033bSKonstantin Ananyev
335e18a033bSKonstantin Ananyev        if (hc->addr_conf->ssl && sscf->ssl.ctx == NULL) {
336e18a033bSKonstantin Ananyev            ngx_log_error(NGX_LOG_ERR, c->log, 0,
337e18a033bSKonstantin Ananyev                          "no \"ssl_certificate\" is defined "
338e18a033bSKonstantin Ananyev                          "in server listening on SSL port");
339e18a033bSKonstantin Ananyev            ngx_http_close_connection(c);
340e18a033bSKonstantin Ananyev            return;
341e18a033bSKonstantin Ananyev        }
342e18a033bSKonstantin Ananyev
343e18a033bSKonstantin Ananyev        hc->ssl = 1;
344e18a033bSKonstantin Ananyev
345e18a033bSKonstantin Ananyev        rev->handler = ngx_http_ssl_handshake;
346e18a033bSKonstantin Ananyev    }
347e18a033bSKonstantin Ananyev    }
348e18a033bSKonstantin Ananyev#endif
349e18a033bSKonstantin Ananyev
350e18a033bSKonstantin Ananyev    if (hc->addr_conf->proxy_protocol) {
351e18a033bSKonstantin Ananyev        hc->proxy_protocol = 1;
352e18a033bSKonstantin Ananyev        c->log->action = "reading PROXY protocol";
353e18a033bSKonstantin Ananyev    }
354e18a033bSKonstantin Ananyev
355e18a033bSKonstantin Ananyev    if (rev->ready) {
356e18a033bSKonstantin Ananyev        /* the deferred accept(), iocp */
357e18a033bSKonstantin Ananyev
358e18a033bSKonstantin Ananyev        if (ngx_use_accept_mutex) {
359e18a033bSKonstantin Ananyev            ngx_post_event(rev, &ngx_posted_events);
360e18a033bSKonstantin Ananyev            return;
361e18a033bSKonstantin Ananyev        }
362e18a033bSKonstantin Ananyev
363e18a033bSKonstantin Ananyev        rev->handler(rev);
364e18a033bSKonstantin Ananyev        return;
365e18a033bSKonstantin Ananyev    }
366e18a033bSKonstantin Ananyev
367e18a033bSKonstantin Ananyev    ngx_add_timer(rev, c->listening->post_accept_timeout);
368e18a033bSKonstantin Ananyev    ngx_reusable_connection(c, 1);
369e18a033bSKonstantin Ananyev
370e18a033bSKonstantin Ananyev    if (ngx_handle_read_event(rev, 0) != NGX_OK) {
371e18a033bSKonstantin Ananyev        ngx_http_close_connection(c);
372e18a033bSKonstantin Ananyev        return;
373e18a033bSKonstantin Ananyev    }
374e18a033bSKonstantin Ananyev}
375e18a033bSKonstantin Ananyev
376e18a033bSKonstantin Ananyev
377e18a033bSKonstantin Ananyevstatic void
378e18a033bSKonstantin Ananyevngx_http_wait_request_handler(ngx_event_t *rev)
379e18a033bSKonstantin Ananyev{
380e18a033bSKonstantin Ananyev    u_char                    *p;
381e18a033bSKonstantin Ananyev    size_t                     size;
382e18a033bSKonstantin Ananyev    ssize_t                    n;
383e18a033bSKonstantin Ananyev    ngx_buf_t                 *b;
384e18a033bSKonstantin Ananyev    ngx_connection_t          *c;
385e18a033bSKonstantin Ananyev    ngx_http_connection_t     *hc;
386e18a033bSKonstantin Ananyev    ngx_http_core_srv_conf_t  *cscf;
387e18a033bSKonstantin Ananyev
388e18a033bSKonstantin Ananyev    c = rev->data;
389e18a033bSKonstantin Ananyev
390e18a033bSKonstantin Ananyev    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http wait request handler");
391e18a033bSKonstantin Ananyev
392e18a033bSKonstantin Ananyev    if (rev->timedout) {
393e18a033bSKonstantin Ananyev        ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
394e18a033bSKonstantin Ananyev        ngx_http_close_connection(c);
395e18a033bSKonstantin Ananyev        return;
396e18a033bSKonstantin Ananyev    }
397e18a033bSKonstantin Ananyev
398e18a033bSKonstantin Ananyev    if (c->close) {
399e18a033bSKonstantin Ananyev        ngx_http_close_connection(c);
400e18a033bSKonstantin Ananyev        return;
401e18a033bSKonstantin Ananyev    }
402e18a033bSKonstantin Ananyev
403e18a033bSKonstantin Ananyev    hc = c->data;
404e18a033bSKonstantin Ananyev    cscf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_core_module);
405e18a033bSKonstantin Ananyev
406e18a033bSKonstantin Ananyev    size = cscf->client_header_buffer_size;
407e18a033bSKonstantin Ananyev
408e18a033bSKonstantin Ananyev    b = c->buffer;
409e18a033bSKonstantin Ananyev
410e18a033bSKonstantin Ananyev    if (b == NULL) {
411e18a033bSKonstantin Ananyev        b = ngx_create_temp_buf(c->pool, size);
412e18a033bSKonstantin Ananyev        if (b == NULL) {
413e18a033bSKonstantin Ananyev            ngx_http_close_connection(c);
414e18a033bSKonstantin Ananyev            return;
415e18a033bSKonstantin Ananyev        }
416e18a033bSKonstantin Ananyev
417e18a033bSKonstantin Ananyev        c->buffer</