13 static void ngx_http_wait_request_handler(
ngx_event_t *ev);
14 static void ngx_http_process_request_line(
ngx_event_t *rev);
15 static void ngx_http_process_request_headers(
ngx_event_t *rev);
41 static void ngx_http_request_handler(
ngx_event_t *ev);
50 static void ngx_http_keepalive_handler(
ngx_event_t *ev);
52 static void ngx_http_lingering_close_handler(
ngx_event_t *ev);
57 static u_char *ngx_http_log_error(
ngx_log_t *log, u_char *buf,
size_t len);
62 static void ngx_http_ssl_handshake(
ngx_event_t *rev);
67 static char *ngx_http_client_errors[] = {
70 "client sent invalid method",
73 "client sent invalid request",
76 "client sent invalid method in HTTP/0.9 request"
82 ngx_http_process_host },
85 ngx_http_process_connection },
89 ngx_http_process_unique_header_line },
93 ngx_http_process_unique_header_line },
97 ngx_http_process_unique_header_line },
101 ngx_http_process_unique_header_line },
104 ngx_http_process_user_agent },
107 ngx_http_process_header_line },
111 ngx_http_process_unique_header_line },
115 ngx_http_process_header_line },
118 ngx_http_process_header_line },
122 ngx_http_process_unique_header_line },
126 ngx_http_process_header_line },
130 ngx_http_process_unique_header_line },
134 ngx_http_process_header_line },
139 ngx_http_process_header_line },
142 ngx_http_process_header_line },
147 ngx_http_process_unique_header_line },
150 ngx_http_process_header_line },
152 #if (NGX_HTTP_X_FORWARDED_FOR)
155 ngx_http_process_multi_header_lines },
158 #if (NGX_HTTP_REALIP)
161 ngx_http_process_header_line },
164 #if (NGX_HTTP_HEADERS)
166 ngx_http_process_header_line },
170 ngx_http_process_header_line },
175 ngx_http_process_header_line },
178 ngx_http_process_header_line },
181 ngx_http_process_header_line },
184 ngx_http_process_header_line },
188 ngx_http_process_multi_header_lines },
199 struct sockaddr_in *sin;
205 struct sockaddr_in6 *sin6;
206 ngx_http_in6_addr_t *addr6;
244 for (i = 0; i < port->
naddrs - 1; i++) {
245 if (
ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
262 for (i = 0; i < port->
naddrs - 1; i++) {
263 if (addr[i].addr == sin->sin_addr.s_addr) {
312 rev->
handler = ngx_http_wait_request_handler;
333 "no \"ssl_certificate\" is defined "
334 "in server listening on SSL port");
341 rev->
handler = ngx_http_ssl_handshake;
409 }
else if (b->
start == NULL) {
412 if (b->
start == NULL) {
426 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
434 "client timed out in deferred accept");
468 "client closed connection");
475 c->
log->
action =
"reading client request line";
480 if (c->
data == NULL) {
485 rev->
handler = ngx_http_process_request_line;
486 ngx_http_process_request_line(rev);
546 if (r->
ctx == NULL) {
591 (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
593 (void) ngx_atomic_fetch_add(ngx_stat_requests, 1);
616 "http check ssl handshake");
629 n = recv(c->
fd, (
char *) buf, 1, MSG_PEEK);
638 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
641 "client timed out in deferred accept");
666 if (buf[0] & 0x80 || buf[0] == 0x16 ) {
668 "https ssl handshake: 0x%02Xd", buf[0]);
691 c->ssl->handler = ngx_http_ssl_handshake_handler;
695 ngx_http_ssl_handshake_handler(c);
704 rev->
handler = ngx_http_wait_request_handler;
705 ngx_http_wait_request_handler(rev);
718 if (c->ssl->handshaked) {
728 c->ssl->no_wait_shutdown = 1;
730 #if (NGX_HTTP_SPDY && defined TLSEXT_TYPE_next_proto_neg)
733 const unsigned char *data;
736 SSL_get0_next_proto_negotiated(c->ssl->connection, &data, &len);
752 ngx_http_wait_request_handler(c->
read);
764 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
767 ngx_http_ssl_servername(
ngx_ssl_conn_t *ssl_conn,
int *ad,
void *arg)
770 const char *servername;
777 servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
779 if (servername == NULL) {
780 return SSL_TLSEXT_ERR_NOACK;
786 "SSL server name: \"%s\"", servername);
791 return SSL_TLSEXT_ERR_NOACK;
794 host.
data = (u_char *) servername;
796 if (ngx_http_validate_host(&host, c->
pool, 1) !=
NGX_OK) {
797 return SSL_TLSEXT_ERR_NOACK;
806 return SSL_TLSEXT_ERR_NOACK;
810 if (hc->ssl_servername == NULL) {
811 return SSL_TLSEXT_ERR_NOACK;
814 *hc->ssl_servername = host;
825 SSL_set_SSL_CTX(ssl_conn, sscf->
ssl.
ctx);
832 SSL_set_verify(ssl_conn, SSL_CTX_get_verify_mode(sscf->
ssl.
ctx),
833 SSL_CTX_get_verify_callback(sscf->
ssl.
ctx));
835 SSL_set_verify_depth(ssl_conn, SSL_CTX_get_verify_depth(sscf->
ssl.
ctx));
837 #ifdef SSL_CTRL_CLEAR_OPTIONS
839 SSL_clear_options(ssl_conn, SSL_get_options(ssl_conn) &
840 ~SSL_CTX_get_options(sscf->
ssl.
ctx));
843 SSL_set_options(ssl_conn, SSL_CTX_get_options(sscf->
ssl.
ctx));
846 return SSL_TLSEXT_ERR_OK;
867 "http process request line");
881 n = ngx_http_read_request_header(r);
917 rc = ngx_http_validate_host(&host, r->
pool, 0);
921 "client sent invalid host in request line");
931 if (ngx_http_set_virtual_server(r, &host) ==
NGX_ERROR) {
960 c->
log->
action =
"reading client request headers";
962 rev->
handler = ngx_http_process_request_headers;
963 ngx_http_process_request_headers(rev);
982 rv = ngx_http_alloc_large_header_buffer(r, 1);
994 "client sent too long URI");
1026 "client sent invalid request");
1071 if (p < last && *p ==
'$') {
1073 "client sent unsafe win32 URI");
1106 "http uri: \"%V\"", &r->
uri);
1109 "http args: \"%V\"", &r->
args);
1112 "http exten: \"%V\"", &r->
exten);
1119 ngx_http_process_request_headers(
ngx_event_t *rev)
1136 "http process request header line");
1155 rv = ngx_http_alloc_large_header_buffer(r, 0);
1169 "client sent too large request");
1179 p[len++] =
'.'; p[len++] =
'.'; p[len++] =
'.';
1183 "client sent too long header line: \"%*s\"",
1192 n = ngx_http_read_request_header(r);
1214 "client sent invalid header line: \"%*s\"",
1259 "http header: \"%V: %V\"",
1270 "http header done");
1297 "client sent invalid header line: \"%*s\\r...\"",
1346 "client prematurely closed connection");
1351 c->
log->
action =
"reading client request headers";
1373 "http alloc large header buffer");
1375 if (request_line && r->
state == 0) {
1402 "http large header free: %p %uz",
1407 if (hc->
busy == NULL) {
1410 if (hc->
busy == NULL) {
1422 "http large header alloc: %p %uz",
1431 if (r->
state == 0) {
1444 "http large header copy: %d", r->
header_in->
pos - old);
1537 "client sent duplicate header line: \"%V: %V\", "
1538 "previous value: \"%V: %V\"",
1539 &h->
key, &h->
value, &(*ph)->key, &(*ph)->value);
1560 rc = ngx_http_validate_host(&host, r->
pool, 0);
1564 "client sent invalid host header");
1578 if (ngx_http_set_virtual_server(r, &host) ==
NGX_ERROR) {
1607 u_char *user_agent, *msie;
1621 if (msie && msie + 7 < user_agent + h->
value.
len) {
1625 if (msie[6] ==
'.') {
1633 if (
ngx_strstrn(msie + 8,
"SV1", 3 - 1) == NULL) {
1643 c->ssl->no_send_shutdown = 1;
1659 }
else if (
ngx_strstrn(user_agent,
"Chrome/", 7 - 1)) {
1662 }
else if (
ngx_strstrn(user_agent,
"Safari/", 7 - 1)
1667 }
else if (
ngx_strstrn(user_agent,
"Konqueror", 9 - 1)) {
1685 if (headers->
elts == NULL) {
1717 "client sent HTTP/1.1 request without \"Host\" header");
1729 "client sent invalid \"Content-Length\" header");
1737 "client sent TRACE method");
1745 (u_char *)
"chunked", 7) == 0)
1753 (u_char *)
"identity", 8) != 0)
1756 "client sent unknown \"Transfer-Encoding\": \"%V\"",
1789 if (c->ssl == NULL) {
1791 "client sent plain HTTP request to HTTPS port");
1799 rc = SSL_get_verify_result(c->ssl->connection);
1805 "client SSL certificate verify error: (%l:%s)",
1806 rc, X509_verify_cert_error_string(rc));
1809 (SSL_get0_session(c->ssl->connection)));
1816 cert = SSL_get_peer_certificate(c->ssl->connection);
1820 "client sent no required SSL certificate");
1823 (SSL_get0_session(c->ssl->connection)));
1841 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
1842 r->stat_reading = 0;
1843 (void) ngx_atomic_fetch_add(ngx_stat_writing, 1);
1844 r->stat_writing = 1;
1861 size_t i, dot_pos, host_len;
1869 dot_pos = host->
len;
1870 host_len = host->
len;
1876 for (i = 0; i < host->
len; i++) {
1882 if (dot_pos == i - 1) {
1889 if (state == sw_usual) {
1902 if (state == sw_literal) {
1917 if (ch >=
'A' && ch <=
'Z') {
1925 if (dot_pos == host_len - 1) {
1929 if (host_len == 0) {
1935 if (host->
data == NULL) {
1942 host->
len = host_len;
1958 #if (NGX_HTTP_SSL && defined SSL_CTRL_SET_TLSEXT_HOSTNAME)
1960 if (hc->ssl_servername) {
1961 if (hc->ssl_servername->len == host->
len
1966 if (hc->ssl_servername_regex
1967 && ngx_http_regex_exec(r, hc->ssl_servername_regex,
1968 hc->ssl_servername) !=
NGX_OK)
1980 rc = ngx_http_find_virtual_server(r->
connection,
1989 #if (NGX_HTTP_SSL && defined SSL_CTRL_SET_TLSEXT_HOSTNAME)
1991 if (hc->ssl_servername) {
2003 "client attempted to request the server name "
2004 "different from that one was negotiated");
2034 if (virtual_names == NULL) {
2049 if (host->
len && virtual_names->
nregex) {
2054 sn = virtual_names->
regex;
2056 #if (NGX_HTTP_SSL && defined SSL_CTRL_SET_TLSEXT_HOSTNAME)
2061 for (i = 0; i < virtual_names->
nregex; i++) {
2071 hc->ssl_servername_regex = sn[
i].regex;
2079 "on \"%V\" using \"%V\"",
2080 n, host, &sn[i].regex->
name);
2090 for (i = 0; i < virtual_names->
nregex; i++) {
2092 n = ngx_http_regex_exec(r, sn[i].regex, host);
2127 "http run request: \"%V?%V\"", &r->
uri, &r->
args);
2168 "http posted request: \"%V?%V\"", &r->
uri, &r->
args);
2208 "http finalize request: %d, \"%V?%V\" a:%d, c:%d",
2212 ngx_http_finalize_connection(r);
2236 if (ngx_http_post_action(r) ==
NGX_OK) {
2244 ngx_http_terminate_request(r, rc);
2253 ngx_http_terminate_request(r, rc);
2278 if (ngx_http_set_write_handler(r) !=
NGX_OK) {
2279 ngx_http_terminate_request(r, 0);
2297 ngx_http_log_request(r);
2304 "subrequest: \"%V?%V\" logged again",
2319 "http finalize non-active request: \"%V?%V\"",
2331 ngx_http_terminate_request(r, 0);
2336 "http wake parent request: \"%V?%V\"",
2344 if (ngx_http_set_write_handler(r) !=
NGX_OK) {
2345 ngx_http_terminate_request(r, 0);
2353 "http finalize non-active request: \"%V?%V\"",
2365 if (ngx_http_post_action(r) ==
NGX_OK) {
2379 ngx_http_close_request(r, 0);
2383 ngx_http_finalize_connection(r);
2397 "http terminate request count:%d", mr->
count);
2415 "http terminate cleanup count:%d blk:%d",
2431 ngx_http_close_request(mr, rc);
2439 "http terminate handler count:%d", r->
count);
2443 ngx_http_close_request(r, 0);
2453 if (r->spdy_stream) {
2454 ngx_http_close_request(r, 0);
2473 ngx_http_close_request(r, 0);
2482 ngx_http_set_keepalive(r);
2492 ngx_http_set_lingering_close(r);
2496 ngx_http_close_request(r, 0);
2514 if (r->spdy_stream) {
2531 ngx_http_close_request(r, 0);
2551 "http writer handler: \"%V?%V\"", &r->
uri, &r->
args);
2558 "client timed out");
2572 ngx_http_close_request(r, 0);
2582 "http writer delayed");
2585 ngx_http_close_request(r, 0);
2594 "http writer output filter: %d, \"%V?%V\"",
2609 ngx_http_close_request(r, 0);
2616 "http writer done: \"%V?%V\"", &r->
uri, &r->
args);
2628 "http finalizer done: \"%V?%V\"", &r->
uri, &r->
args);
2638 "http reading blocked");
2646 ngx_http_close_request(r, 0);
2668 if (r->spdy_stream) {
2679 #if (NGX_HAVE_KQUEUE)
2689 err = rev->kq_errno;
2696 n = recv(c->
fd, buf, 1, MSG_PEEK);
2705 }
else if (n == -1) {
2721 ngx_http_close_request(r, 0);
2734 "client prematurely closed connection");
2787 if (hc->
free == NULL) {
2791 if (hc->
free == NULL) {
2792 ngx_http_close_request(r, 0);
2797 for (i = 0; i < hc->
nbusy - 1; i++) {
2828 c->
log->
action =
"reading client pipelined request line";
2847 rev->
handler = ngx_http_process_request_line;
2879 for (i = 0; i < hc->
nfree; i++) {
2891 for (i = 0; i < hc->
nbusy; i++) {
2905 rev->
handler = ngx_http_keepalive_handler;
2936 if (setsockopt(c->
fd, IPPROTO_TCP, TCP_NODELAY,
2937 (
const void *) &tcp_nodelay,
sizeof(
int))
2946 "setsockopt(TCP_NODELAY) failed");
2989 #if (NGX_HAVE_KQUEUE)
2995 "kevent() reported that client %V closed "
2999 c->ssl->no_send_shutdown = 1;
3012 if (b->
pos == NULL) {
3021 if (b->
pos == NULL) {
3074 "client %V closed keepalive connection", &c->
addr_text);
3082 c->
log->
action =
"reading client request line";
3088 if (c->
data == NULL) {
3098 rev->
handler = ngx_http_process_request_line;
3099 ngx_http_process_request_line(rev);
3115 rev->
handler = ngx_http_lingering_close_handler;
3121 ngx_http_close_request(r, 0);
3130 ngx_http_close_request(r, 0);
3138 ngx_http_close_request(r, 0);
3143 ngx_http_lingering_close_handler(rev);
3149 ngx_http_lingering_close_handler(
ngx_event_t *rev)
3162 "http lingering close handler");
3165 ngx_http_close_request(r, 0);
3171 ngx_http_close_request(r, 0);
3181 ngx_http_close_request(r, 0);
3185 }
while (rev->
ready);
3188 ngx_http_close_request(r, 0);
3217 "http request empty handler");
3304 if (r->
count == 0) {
3315 if (r->spdy_stream) {
3331 struct linger linger;
3340 if (r->
pool == NULL) {
3347 cln->handler(cln->data);
3353 if (r->stat_reading) {
3354 (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
3357 if (r->stat_writing) {
3358 (void) ngx_atomic_fetch_add(ngx_stat_writing, -1);
3367 log->
action =
"logging request";
3369 ngx_http_log_request(r);
3371 log->
action =
"closing request";
3378 linger.l_linger = 0;
3380 if (setsockopt(r->
connection->
fd, SOL_SOCKET, SO_LINGER,
3381 (
const void *) &linger,
sizeof(
struct linger)) == -1)
3384 "setsockopt(SO_LINGER) failed");
3421 for (i = 0; i < n; i++) {
3433 "close http connection: %d", c->
fd);
3447 (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
3461 ngx_http_log_error(
ngx_log_t *log, u_char *buf,
size_t len)
3494 u_char *buf,
size_t len)
3496 char *uri_separator;
3509 if (*p ==
CR || *p ==
LF) {
3536 #if (NGX_HAVE_UNIX_DOMAIN)
3538 uri_separator =
":";
3544 uri_separator, &u->
uri);