Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 8 | #include <ngx_core.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 9 | #include <ngx_http.h> |
| 10 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 11 | |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 12 | static void ngx_http_init_request(ngx_event_t *ev); |
| 13 | static void ngx_http_process_request_line(ngx_event_t *rev); |
| 14 | static void ngx_http_process_request_headers(ngx_event_t *rev); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 15 | static ssize_t ngx_http_read_request_header(ngx_http_request_t *r); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 16 | static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r, |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 17 | ngx_uint_t request_line); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 18 | |
| 19 | static ngx_int_t ngx_http_process_header_line(ngx_http_request_t *r, |
| 20 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 21 | static ngx_int_t ngx_http_process_unique_header_line(ngx_http_request_t *r, |
| 22 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 23 | static ngx_int_t ngx_http_process_host(ngx_http_request_t *r, |
| 24 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | ec67b19 | 2007-09-09 18:28:49 +0000 | [diff] [blame] | 25 | static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r, |
| 26 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | bc5fb29 | 2008-05-13 09:18:58 +0000 | [diff] [blame] | 27 | static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r, |
| 28 | ngx_table_elt_t *h, ngx_uint_t offset); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 29 | static ngx_int_t ngx_http_process_cookie(ngx_http_request_t *r, |
| 30 | ngx_table_elt_t *h, ngx_uint_t offset); |
| 31 | |
Igor Sysoev | 2f65722 | 2004-06-16 15:32:11 +0000 | [diff] [blame] | 32 | static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r); |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 33 | static void ngx_http_process_request(ngx_http_request_t *r); |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 34 | static ssize_t ngx_http_validate_host(u_char *host, size_t len); |
| 35 | static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r, |
| 36 | u_char *host, size_t len); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 37 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 38 | static void ngx_http_request_handler(ngx_event_t *ev); |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 39 | static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 40 | static void ngx_http_writer(ngx_http_request_t *r); |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 41 | static void ngx_http_request_finalizer(ngx_http_request_t *r); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 42 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 43 | static void ngx_http_set_keepalive(ngx_http_request_t *r); |
| 44 | static void ngx_http_keepalive_handler(ngx_event_t *ev); |
| 45 | static void ngx_http_set_lingering_close(ngx_http_request_t *r); |
| 46 | static void ngx_http_lingering_close_handler(ngx_event_t *ev); |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 47 | static ngx_int_t ngx_http_post_action(ngx_http_request_t *r); |
Igor Sysoev | 90c0814 | 2005-07-25 09:41:38 +0000 | [diff] [blame] | 48 | static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error); |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 49 | static void ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error); |
Igor Sysoev | 2eec1e1 | 2008-09-27 15:08:02 +0000 | [diff] [blame] | 50 | static void ngx_http_log_request(ngx_http_request_t *r); |
Igor Sysoev | 90c0814 | 2005-07-25 09:41:38 +0000 | [diff] [blame] | 51 | static void ngx_http_close_connection(ngx_connection_t *c); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 52 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 53 | static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len); |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 54 | static u_char *ngx_http_log_error_handler(ngx_http_request_t *r, |
| 55 | ngx_http_request_t *sr, u_char *buf, size_t len); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 56 | |
Igor Sysoev | ceb9929 | 2005-09-06 16:09:32 +0000 | [diff] [blame] | 57 | #if (NGX_HTTP_SSL) |
| 58 | static void ngx_http_ssl_handshake(ngx_event_t *rev); |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 59 | static void ngx_http_ssl_handshake_handler(ngx_connection_t *c); |
Igor Sysoev | ceb9929 | 2005-09-06 16:09:32 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 62 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 63 | static char *ngx_http_client_errors[] = { |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 64 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 65 | /* NGX_HTTP_PARSE_INVALID_METHOD */ |
| 66 | "client sent invalid method", |
Igor Sysoev | 6a644c6 | 2003-03-04 06:33:48 +0000 | [diff] [blame] | 67 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 68 | /* NGX_HTTP_PARSE_INVALID_REQUEST */ |
| 69 | "client sent invalid request", |
| 70 | |
| 71 | /* NGX_HTTP_PARSE_INVALID_09_METHOD */ |
| 72 | "client sent invalid method in HTTP/0.9 request" |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 76 | ngx_http_header_t ngx_http_headers_in[] = { |
Igor Sysoev | cbc7c61 | 2008-08-19 19:40:45 +0000 | [diff] [blame] | 77 | { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host), |
| 78 | ngx_http_process_host }, |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 79 | |
Igor Sysoev | cbc7c61 | 2008-08-19 19:40:45 +0000 | [diff] [blame] | 80 | { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection), |
| 81 | ngx_http_process_connection }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 82 | |
| 83 | { ngx_string("If-Modified-Since"), |
| 84 | offsetof(ngx_http_headers_in_t, if_modified_since), |
Igor Sysoev | 4f62b73 | 2007-08-29 15:32:49 +0000 | [diff] [blame] | 85 | ngx_http_process_unique_header_line }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 86 | |
Igor Sysoev | cbc7c61 | 2008-08-19 19:40:45 +0000 | [diff] [blame] | 87 | { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent), |
| 88 | ngx_http_process_user_agent }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 89 | |
| 90 | { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer), |
| 91 | ngx_http_process_header_line }, |
| 92 | |
| 93 | { ngx_string("Content-Length"), |
| 94 | offsetof(ngx_http_headers_in_t, content_length), |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 95 | ngx_http_process_unique_header_line }, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 96 | |
| 97 | { ngx_string("Content-Type"), |
| 98 | offsetof(ngx_http_headers_in_t, content_type), |
| 99 | ngx_http_process_header_line }, |
| 100 | |
| 101 | { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range), |
| 102 | ngx_http_process_header_line }, |
| 103 | |
Igor Sysoev | 6c2f052 | 2007-11-09 15:41:35 +0000 | [diff] [blame] | 104 | { ngx_string("If-Range"), |
| 105 | offsetof(ngx_http_headers_in_t, if_range), |
| 106 | ngx_http_process_unique_header_line }, |
| 107 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 108 | { ngx_string("Transfer-Encoding"), |
| 109 | offsetof(ngx_http_headers_in_t, transfer_encoding), |
| 110 | ngx_http_process_header_line }, |
| 111 | |
Igor Sysoev | 1e1f4c8 | 2008-06-26 13:00:39 +0000 | [diff] [blame] | 112 | { ngx_string("Expect"), |
| 113 | offsetof(ngx_http_headers_in_t, expect), |
| 114 | ngx_http_process_unique_header_line }, |
| 115 | |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 116 | #if (NGX_HTTP_GZIP) |
| 117 | { ngx_string("Accept-Encoding"), |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 118 | offsetof(ngx_http_headers_in_t, accept_encoding), |
| 119 | ngx_http_process_header_line }, |
| 120 | |
| 121 | { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via), |
| 122 | ngx_http_process_header_line }, |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
| 125 | { ngx_string("Authorization"), |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 126 | offsetof(ngx_http_headers_in_t, authorization), |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 127 | ngx_http_process_unique_header_line }, |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 128 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 129 | { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive), |
| 130 | ngx_http_process_header_line }, |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 131 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 132 | #if (NGX_HTTP_PROXY || NGX_HTTP_REALIP) |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 133 | { ngx_string("X-Forwarded-For"), |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 134 | offsetof(ngx_http_headers_in_t, x_forwarded_for), |
| 135 | ngx_http_process_header_line }, |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 136 | #endif |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 137 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 138 | #if (NGX_HTTP_REALIP) |
| 139 | { ngx_string("X-Real-IP"), |
| 140 | offsetof(ngx_http_headers_in_t, x_real_ip), |
| 141 | ngx_http_process_header_line }, |
| 142 | #endif |
| 143 | |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 144 | #if (NGX_HTTP_HEADERS) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 145 | { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept), |
| 146 | ngx_http_process_header_line }, |
| 147 | |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 148 | { ngx_string("Accept-Language"), |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 149 | offsetof(ngx_http_headers_in_t, accept_language), |
| 150 | ngx_http_process_header_line }, |
Igor Sysoev | 1ebfead | 2005-02-16 13:40:36 +0000 | [diff] [blame] | 151 | #endif |
| 152 | |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 153 | #if (NGX_HTTP_DAV) |
| 154 | { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth), |
| 155 | ngx_http_process_header_line }, |
| 156 | |
| 157 | { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination), |
| 158 | ngx_http_process_header_line }, |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 159 | |
Igor Sysoev | 56331ba | 2007-01-18 21:11:23 +0000 | [diff] [blame] | 160 | { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite), |
| 161 | ngx_http_process_header_line }, |
| 162 | |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 163 | { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date), |
| 164 | ngx_http_process_header_line }, |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 167 | { ngx_string("Cookie"), 0, ngx_http_process_cookie }, |
| 168 | |
| 169 | { ngx_null_string, 0, NULL } |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 173 | void |
| 174 | ngx_http_init_connection(ngx_connection_t *c) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 175 | { |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 176 | ngx_event_t *rev; |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 177 | ngx_http_log_ctx_t *ctx; |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 178 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 179 | ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t)); |
| 180 | if (ctx == NULL) { |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 181 | ngx_http_close_connection(c); |
| 182 | return; |
| 183 | } |
| 184 | |
Igor Sysoev | cc59567 | 2007-12-30 08:01:50 +0000 | [diff] [blame] | 185 | ctx->connection = c; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 186 | ctx->request = NULL; |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 187 | ctx->current_request = NULL; |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 188 | |
| 189 | c->log->connection = c->number; |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 190 | c->log->handler = ngx_http_log_error; |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 191 | c->log->data = ctx; |
| 192 | c->log->action = "reading client request line"; |
| 193 | |
Igor Sysoev | c7a2f68 | 2004-02-10 16:23:38 +0000 | [diff] [blame] | 194 | c->log_error = NGX_ERROR_INFO; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 195 | |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 196 | rev = c->read; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 197 | rev->handler = ngx_http_init_request; |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 198 | c->write->handler = ngx_http_empty_handler; |
Igor Sysoev | 7af6b16 | 2004-02-09 07:46:43 +0000 | [diff] [blame] | 199 | |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 200 | #if (NGX_STAT_STUB) |
| 201 | ngx_atomic_fetch_add(ngx_stat_reading, 1); |
| 202 | #endif |
| 203 | |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 204 | if (rev->ready) { |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 205 | /* the deferred accept(), rtsig, aio, iocp */ |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 206 | |
Igor Sysoev | ffe7144 | 2006-02-08 15:33:12 +0000 | [diff] [blame] | 207 | if (ngx_use_accept_mutex) { |
Igor Sysoev | c2068d0 | 2005-10-19 12:33:58 +0000 | [diff] [blame] | 208 | ngx_post_event(rev, &ngx_posted_events); |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 209 | return; |
| 210 | } |
| 211 | |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 212 | ngx_http_init_request(rev); |
| 213 | return; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 214 | } |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 215 | |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 216 | ngx_add_timer(rev, c->listening->post_accept_timeout); |
Igor Sysoev | 9b25d69 | 2003-01-26 21:08:14 +0000 | [diff] [blame] | 217 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 218 | if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 219 | #if (NGX_STAT_STUB) |
| 220 | ngx_atomic_fetch_add(ngx_stat_reading, -1); |
| 221 | #endif |
Igor Sysoev | b3e73d8 | 2003-10-10 15:10:50 +0000 | [diff] [blame] | 222 | ngx_http_close_connection(c); |
| 223 | return; |
| 224 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 227 | |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 228 | static void |
| 229 | ngx_http_init_request(ngx_event_t *rev) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 230 | { |
Igor Sysoev | b4ccb9f | 2007-04-21 07:50:19 +0000 | [diff] [blame] | 231 | ngx_time_t *tp; |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 232 | ngx_uint_t i; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 233 | ngx_connection_t *c; |
| 234 | ngx_http_request_t *r; |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 235 | struct sockaddr_in *sin; |
| 236 | ngx_http_port_t *port; |
| 237 | ngx_http_in_addr_t *addr; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 238 | ngx_http_log_ctx_t *ctx; |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 239 | ngx_http_addr_conf_t *addr_conf; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 240 | ngx_http_connection_t *hc; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 241 | ngx_http_core_srv_conf_t *cscf; |
| 242 | ngx_http_core_loc_conf_t *clcf; |
| 243 | ngx_http_core_main_conf_t *cmcf; |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 244 | #if (NGX_HAVE_INET6) |
| 245 | struct sockaddr_in6 *sin6; |
| 246 | ngx_http_in6_addr_t *addr6; |
| 247 | #endif |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 248 | |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 249 | #if (NGX_STAT_STUB) |
| 250 | ngx_atomic_fetch_add(ngx_stat_reading, -1); |
| 251 | #endif |
| 252 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 253 | c = rev->data; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 254 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 255 | if (rev->timedout) { |
| 256 | ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame] | 257 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 258 | ngx_http_close_connection(c); |
| 259 | return; |
| 260 | } |
| 261 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 262 | hc = c->data; |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 263 | |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 264 | if (hc == NULL) { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 265 | hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t)); |
| 266 | if (hc == NULL) { |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 267 | ngx_http_close_connection(c); |
| 268 | return; |
| 269 | } |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 270 | } |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 271 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 272 | r = hc->request; |
| 273 | |
| 274 | if (r) { |
| 275 | ngx_memzero(r, sizeof(ngx_http_request_t)); |
| 276 | |
| 277 | r->pipeline = hc->pipeline; |
| 278 | |
| 279 | if (hc->nbusy) { |
| 280 | r->header_in = hc->busy[0]; |
| 281 | } |
| 282 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 283 | } else { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 284 | r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t)); |
| 285 | if (r == NULL) { |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 286 | ngx_http_close_connection(c); |
| 287 | return; |
| 288 | } |
Igor Sysoev | 59f3aa3 | 2004-06-24 16:07:04 +0000 | [diff] [blame] | 289 | |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 290 | hc->request = r; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 293 | c->data = r; |
| 294 | r->http_connection = hc; |
| 295 | |
Igor Sysoev | 59f3aa3 | 2004-06-24 16:07:04 +0000 | [diff] [blame] | 296 | c->sent = 0; |
Igor Sysoev | c83f683 | 2004-06-24 07:53:37 +0000 | [diff] [blame] | 297 | r->signature = NGX_HTTP_MODULE; |
Igor Sysoev | 3b30a90 | 2003-12-25 20:26:58 +0000 | [diff] [blame] | 298 | |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 299 | /* find the server configuration for the address:port */ |
| 300 | |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 301 | port = c->listening->servers; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 302 | |
Igor Sysoev | c7a9b7a | 2007-12-29 16:00:34 +0000 | [diff] [blame] | 303 | r->connection = c; |
| 304 | |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 305 | if (port->naddrs > 1) { |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 306 | |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 307 | /* |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 308 | * there are several addresses on this port and one of them |
| 309 | * is an "*:port" wildcard so getsockname() in ngx_http_server_addr() |
| 310 | * is required to determine a server address |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 311 | */ |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 312 | |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 313 | c->local_sockaddr = NULL; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 314 | |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 315 | if (ngx_http_server_addr(r, NULL) != NGX_OK) { |
| 316 | ngx_http_close_connection(c); |
| 317 | return; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 320 | switch (c->local_sockaddr->sa_family) { |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 321 | |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 322 | #if (NGX_HAVE_INET6) |
| 323 | case AF_INET6: |
| 324 | sin6 = (struct sockaddr_in6 *) c->local_sockaddr; |
| 325 | |
| 326 | addr6 = (ngx_http_in6_addr_t *) port->addrs; |
| 327 | |
| 328 | /* the last address is "*" */ |
| 329 | |
| 330 | for (i = 0; i < port->naddrs - 1; i++) { |
| 331 | if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) { |
| 332 | break; |
| 333 | } |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 334 | } |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 335 | |
| 336 | addr_conf = &addr6[i].conf; |
| 337 | |
| 338 | break; |
| 339 | #endif |
| 340 | |
| 341 | default: /* AF_INET */ |
| 342 | sin = (struct sockaddr_in *) c->local_sockaddr; |
| 343 | |
| 344 | addr = port->addrs; |
| 345 | |
| 346 | /* the last address is "*" */ |
| 347 | |
| 348 | for (i = 0; i < port->naddrs - 1; i++) { |
| 349 | if (addr[i].addr == sin->sin_addr.s_addr) { |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | addr_conf = &addr[i].conf; |
| 355 | |
| 356 | break; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | } else { |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 360 | |
| 361 | switch (c->local_sockaddr->sa_family) { |
| 362 | |
| 363 | #if (NGX_HAVE_INET6) |
| 364 | case AF_INET6: |
| 365 | addr6 = (ngx_http_in6_addr_t *) port->addrs; |
| 366 | addr_conf = &addr6[0].conf; |
| 367 | break; |
| 368 | #endif |
| 369 | |
| 370 | default: /* AF_INET */ |
| 371 | addr = port->addrs; |
| 372 | addr_conf = &addr[0].conf; |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 373 | break; |
| 374 | } |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 377 | r->virtual_names = addr_conf->virtual_names; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 378 | |
| 379 | /* the default server configuration for the address:port */ |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 380 | cscf = addr_conf->core_srv_conf; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 381 | |
| 382 | r->main_conf = cscf->ctx->main_conf; |
| 383 | r->srv_conf = cscf->ctx->srv_conf; |
| 384 | r->loc_conf = cscf->ctx->loc_conf; |
| 385 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 386 | rev->handler = ngx_http_process_request_line; |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 387 | |
Igor Sysoev | 1c3567e | 2004-07-15 16:35:51 +0000 | [diff] [blame] | 388 | #if (NGX_HTTP_SSL) |
| 389 | |
Igor Sysoev | 86ef6aa | 2007-12-10 12:09:51 +0000 | [diff] [blame] | 390 | { |
| 391 | ngx_http_ssl_srv_conf_t *sscf; |
| 392 | |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 393 | sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module); |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 394 | if (sscf->enable || addr_conf->ssl) { |
Igor Sysoev | 74a5ddb | 2004-07-18 19:11:20 +0000 | [diff] [blame] | 395 | |
| 396 | if (c->ssl == NULL) { |
Igor Sysoev | f100c78 | 2008-09-01 14:19:01 +0000 | [diff] [blame] | 397 | |
| 398 | c->log->action = "SSL handshaking"; |
| 399 | |
Igor Sysoev | a35eacc | 2009-02-21 07:02:02 +0000 | [diff] [blame] | 400 | if (addr_conf->ssl && sscf->ssl.ctx == NULL) { |
Igor Sysoev | f100c78 | 2008-09-01 14:19:01 +0000 | [diff] [blame] | 401 | ngx_log_error(NGX_LOG_ERR, c->log, 0, |
| 402 | "no \"ssl_certificate\" is defined " |
| 403 | "in server listening on SSL port"); |
| 404 | ngx_http_close_connection(c); |
| 405 | return; |
| 406 | } |
| 407 | |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 408 | if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER) |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 409 | == NGX_ERROR) |
Igor Sysoev | 74a5ddb | 2004-07-18 19:11:20 +0000 | [diff] [blame] | 410 | { |
| 411 | ngx_http_close_connection(c); |
| 412 | return; |
| 413 | } |
| 414 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 415 | rev->handler = ngx_http_ssl_handshake; |
Igor Sysoev | 1c3567e | 2004-07-15 16:35:51 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Igor Sysoev | d52477f | 2005-05-16 13:53:20 +0000 | [diff] [blame] | 418 | r->main_filter_need_in_memory = 1; |
Igor Sysoev | 1c3567e | 2004-07-15 16:35:51 +0000 | [diff] [blame] | 419 | } |
Igor Sysoev | 86ef6aa | 2007-12-10 12:09:51 +0000 | [diff] [blame] | 420 | } |
Igor Sysoev | 1c3567e | 2004-07-15 16:35:51 +0000 | [diff] [blame] | 421 | |
Igor Sysoev | 4aa8888 | 2004-07-14 20:07:58 +0000 | [diff] [blame] | 422 | #endif |
| 423 | |
Igor Sysoev | 890fc96 | 2003-07-20 21:15:59 +0000 | [diff] [blame] | 424 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 425 | c->log->file = clcf->err_log->file; |
Igor Sysoev | fff3232 | 2004-04-08 15:58:25 +0000 | [diff] [blame] | 426 | if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) { |
| 427 | c->log->log_level = clcf->err_log->log_level; |
| 428 | } |
Igor Sysoev | 890fc96 | 2003-07-20 21:15:59 +0000 | [diff] [blame] | 429 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 430 | if (c->buffer == NULL) { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 431 | c->buffer = ngx_create_temp_buf(c->pool, |
| 432 | cscf->client_header_buffer_size); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 433 | if (c->buffer == NULL) { |
| 434 | ngx_http_close_connection(c); |
| 435 | return; |
| 436 | } |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 439 | if (r->header_in == NULL) { |
| 440 | r->header_in = c->buffer; |
| 441 | } |
| 442 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 443 | r->pool = ngx_create_pool(cscf->request_pool_size, c->log); |
| 444 | if (r->pool == NULL) { |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 445 | ngx_http_close_connection(c); |
| 446 | return; |
| 447 | } |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 448 | |
Igor Sysoev | 89690bf | 2004-03-23 06:01:52 +0000 | [diff] [blame] | 449 | |
Igor Sysoev | aab4d8c | 2004-09-06 18:45:00 +0000 | [diff] [blame] | 450 | if (ngx_list_init(&r->headers_out.headers, r->pool, 20, |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 451 | sizeof(ngx_table_elt_t)) |
| 452 | == NGX_ERROR) |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 453 | { |
| 454 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | 980a924 | 2004-09-05 19:54:02 +0000 | [diff] [blame] | 455 | return; |
| 456 | } |
| 457 | |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 458 | r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module); |
| 459 | if (r->ctx == NULL) { |
| 460 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 461 | return; |
| 462 | } |
Igor Sysoev | e0268b9 | 2002-09-11 15:18:33 +0000 | [diff] [blame] | 463 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 464 | cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); |
| 465 | |
| 466 | r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts |
| 467 | * sizeof(ngx_http_variable_value_t)); |
| 468 | if (r->variables == NULL) { |
| 469 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 470 | return; |
| 471 | } |
| 472 | |
Igor Sysoev | c78c41c | 2004-07-07 06:15:04 +0000 | [diff] [blame] | 473 | c->single_connection = 1; |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 474 | c->destroyed = 0; |
| 475 | |
Igor Sysoev | f6e1fe3 | 2005-10-04 10:38:53 +0000 | [diff] [blame] | 476 | r->main = r; |
| 477 | |
Igor Sysoev | b4ccb9f | 2007-04-21 07:50:19 +0000 | [diff] [blame] | 478 | tp = ngx_timeofday(); |
| 479 | r->start_sec = tp->sec; |
| 480 | r->start_msec = tp->msec; |
Igor Sysoev | 11d7532 | 2005-03-01 15:20:36 +0000 | [diff] [blame] | 481 | |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 482 | r->method = NGX_HTTP_UNKNOWN; |
| 483 | |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 484 | r->headers_in.content_length_n = -1; |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 485 | r->headers_in.keep_alive_n = -1; |
| 486 | r->headers_out.content_length_n = -1; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 487 | r->headers_out.last_modified_time = -1; |
| 488 | |
Igor Sysoev | ef31643 | 2006-08-16 13:09:33 +0000 | [diff] [blame] | 489 | r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1; |
| 490 | r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1; |
| 491 | |
Igor Sysoev | 59f3aa3 | 2004-06-24 16:07:04 +0000 | [diff] [blame] | 492 | r->http_state = NGX_HTTP_READING_REQUEST_STATE; |
| 493 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 494 | ctx = c->log->data; |
| 495 | ctx->request = r; |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 496 | ctx->current_request = r; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 497 | r->log_handler = ngx_http_log_error_handler; |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 498 | |
Igor Sysoev | dc3b2a7 | 2004-09-14 19:39:54 +0000 | [diff] [blame] | 499 | #if (NGX_STAT_STUB) |
Igor Sysoev | 7845223 | 2005-10-12 13:50:36 +0000 | [diff] [blame] | 500 | ngx_atomic_fetch_add(ngx_stat_reading, 1); |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 501 | r->stat_reading = 1; |
Igor Sysoev | 7845223 | 2005-10-12 13:50:36 +0000 | [diff] [blame] | 502 | ngx_atomic_fetch_add(ngx_stat_requests, 1); |
Igor Sysoev | dc3b2a7 | 2004-09-14 19:39:54 +0000 | [diff] [blame] | 503 | #endif |
| 504 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 505 | rev->handler(rev); |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
| 509 | #if (NGX_HTTP_SSL) |
| 510 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 511 | static void |
| 512 | ngx_http_ssl_handshake(ngx_event_t *rev) |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 513 | { |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 514 | u_char buf[1]; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 515 | ssize_t n; |
| 516 | ngx_int_t rc; |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 517 | ngx_connection_t *c; |
| 518 | ngx_http_request_t *r; |
| 519 | |
| 520 | c = rev->data; |
| 521 | r = c->data; |
| 522 | |
| 523 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, |
| 524 | "http check ssl handshake"); |
| 525 | |
| 526 | if (rev->timedout) { |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 527 | ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); |
| 528 | c->timedout = 1; |
| 529 | ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT); |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 533 | n = recv(c->fd, (char *) buf, 1, MSG_PEEK); |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 534 | |
| 535 | if (n == -1 && ngx_socket_errno == NGX_EAGAIN) { |
Igor Sysoev | bd68d51 | 2008-01-08 20:51:06 +0000 | [diff] [blame] | 536 | |
| 537 | if (!rev->timer_set) { |
| 538 | ngx_add_timer(rev, c->listening->post_accept_timeout); |
| 539 | } |
| 540 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 541 | if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
Igor Sysoev | 57b088d | 2008-01-09 08:21:57 +0000 | [diff] [blame] | 542 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 543 | } |
| 544 | |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | |
| 548 | if (n == 1) { |
| 549 | if (buf[0] == 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) { |
| 550 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 551 | "https ssl handshake: 0x%02Xd", buf[0]); |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 552 | |
Igor Sysoev | 74a5ddb | 2004-07-18 19:11:20 +0000 | [diff] [blame] | 553 | rc = ngx_ssl_handshake(c); |
| 554 | |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 555 | if (rc == NGX_AGAIN) { |
Igor Sysoev | 7b8ed42 | 2008-01-04 09:32:12 +0000 | [diff] [blame] | 556 | |
| 557 | if (!rev->timer_set) { |
| 558 | ngx_add_timer(rev, c->listening->post_accept_timeout); |
| 559 | } |
| 560 | |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 561 | c->ssl->handler = ngx_http_ssl_handshake_handler; |
Igor Sysoev | 74a5ddb | 2004-07-18 19:11:20 +0000 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 565 | ngx_http_ssl_handshake_handler(c); |
| 566 | |
| 567 | return; |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 568 | |
| 569 | } else { |
| 570 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, |
| 571 | "plain http"); |
| 572 | |
| 573 | r->plain_http = 1; |
| 574 | } |
| 575 | } |
| 576 | |
Igor Sysoev | f100c78 | 2008-09-01 14:19:01 +0000 | [diff] [blame] | 577 | c->log->action = "reading client request line"; |
| 578 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 579 | rev->handler = ngx_http_process_request_line; |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 580 | ngx_http_process_request_line(rev); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 583 | |
| 584 | static void |
| 585 | ngx_http_ssl_handshake_handler(ngx_connection_t *c) |
| 586 | { |
| 587 | ngx_http_request_t *r; |
| 588 | |
| 589 | if (c->ssl->handshaked) { |
| 590 | |
| 591 | /* |
| 592 | * The majority of browsers do not send the "close notify" alert. |
| 593 | * Among them are MSIE, old Mozilla, Netscape 4, Konqueror, |
| 594 | * and Links. And what is more, MSIE ignores the server's alert. |
| 595 | * |
| 596 | * Opera and recent Mozilla send the alert. |
| 597 | */ |
| 598 | |
| 599 | c->ssl->no_wait_shutdown = 1; |
| 600 | |
| 601 | c->read->handler = ngx_http_process_request_line; |
| 602 | /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler; |
| 603 | |
| 604 | ngx_http_process_request_line(c->read); |
| 605 | |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | r = c->data; |
| 610 | |
| 611 | ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST); |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 612 | |
| 613 | return; |
| 614 | } |
| 615 | |
Igor Sysoev | e60303c | 2007-05-29 15:21:09 +0000 | [diff] [blame] | 616 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 617 | |
| 618 | int |
| 619 | ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg) |
| 620 | { |
Igor Sysoev | b29426d | 2008-08-21 12:56:10 +0000 | [diff] [blame] | 621 | size_t len; |
Igor Sysoev | e60303c | 2007-05-29 15:21:09 +0000 | [diff] [blame] | 622 | const char *servername; |
| 623 | ngx_connection_t *c; |
| 624 | ngx_http_request_t *r; |
| 625 | ngx_http_ssl_srv_conf_t *sscf; |
| 626 | |
| 627 | servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name); |
| 628 | |
| 629 | if (servername == NULL) { |
| 630 | return SSL_TLSEXT_ERR_NOACK; |
| 631 | } |
| 632 | |
| 633 | c = ngx_ssl_get_connection(ssl_conn); |
| 634 | |
| 635 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 636 | "SSL server name: \"%s\"", servername); |
| 637 | |
Igor Sysoev | b29426d | 2008-08-21 12:56:10 +0000 | [diff] [blame] | 638 | len = ngx_strlen(servername); |
| 639 | |
| 640 | if (len == 0) { |
| 641 | return SSL_TLSEXT_ERR_NOACK; |
| 642 | } |
| 643 | |
Igor Sysoev | e60303c | 2007-05-29 15:21:09 +0000 | [diff] [blame] | 644 | r = c->data; |
| 645 | |
Igor Sysoev | b29426d | 2008-08-21 12:56:10 +0000 | [diff] [blame] | 646 | if (ngx_http_find_virtual_server(r, (u_char *) servername, len) != NGX_OK) { |
Igor Sysoev | e60303c | 2007-05-29 15:21:09 +0000 | [diff] [blame] | 647 | return SSL_TLSEXT_ERR_NOACK; |
| 648 | } |
| 649 | |
Igor Sysoev | e60303c | 2007-05-29 15:21:09 +0000 | [diff] [blame] | 650 | sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module); |
| 651 | |
| 652 | SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx); |
| 653 | |
| 654 | return SSL_TLSEXT_ERR_OK; |
| 655 | } |
| 656 | |
| 657 | #endif |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 658 | |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 659 | #endif |
| 660 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 661 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 662 | static void |
| 663 | ngx_http_process_request_line(ngx_event_t *rev) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 664 | { |
Igor Sysoev | 8decab3 | 2007-10-18 11:36:58 +0000 | [diff] [blame] | 665 | ssize_t n; |
| 666 | ngx_int_t rc, rv; |
| 667 | ngx_connection_t *c; |
| 668 | ngx_http_request_t *r; |
| 669 | ngx_http_core_srv_conf_t *cscf; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 670 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 671 | c = rev->data; |
| 672 | r = c->data; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 673 | |
Igor Sysoev | 2b58fbf | 2003-12-09 15:08:11 +0000 | [diff] [blame] | 674 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, |
| 675 | "http process request line"); |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 676 | |
| 677 | if (rev->timedout) { |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 678 | ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); |
| 679 | c->timedout = 1; |
| 680 | ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 681 | return; |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 682 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 683 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 684 | rc = NGX_AGAIN; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 685 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 686 | for ( ;; ) { |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 687 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 688 | if (rc == NGX_AGAIN) { |
| 689 | n = ngx_http_read_request_header(r); |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 690 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 691 | if (n == NGX_AGAIN || n == NGX_ERROR) { |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 692 | return; |
| 693 | } |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 696 | rc = ngx_http_parse_request_line(r, r->header_in); |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 697 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 698 | if (rc == NGX_OK) { |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 699 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 700 | /* the request line has been parsed successfully */ |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 701 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 702 | r->request_line.len = r->request_end - r->request_start; |
| 703 | r->request_line.data = r->request_start; |
Igor Sysoev | 42511c0 | 2008-08-04 11:10:52 +0000 | [diff] [blame] | 704 | *r->request_end = '\0'; |
Igor Sysoev | bb6ec8c | 2003-11-20 07:05:50 +0000 | [diff] [blame] | 705 | |
Igor Sysoev | bb6ec8c | 2003-11-20 07:05:50 +0000 | [diff] [blame] | 706 | |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 707 | if (r->args_start) { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 708 | r->uri.len = r->args_start - 1 - r->uri_start; |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 709 | } else { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 710 | r->uri.len = r->uri_end - r->uri_start; |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 713 | |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 714 | if (r->complex_uri || r->quoted_uri) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 715 | |
Igor Sysoev | 7f6b2ff | 2008-06-17 15:00:30 +0000 | [diff] [blame] | 716 | r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1); |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 717 | if (r->uri.data == NULL) { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 718 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 719 | return; |
| 720 | } |
| 721 | |
Igor Sysoev | 8decab3 | 2007-10-18 11:36:58 +0000 | [diff] [blame] | 722 | cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
| 723 | |
| 724 | rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 725 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 726 | if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) { |
| 727 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 728 | "client sent invalid request"); |
| 729 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 730 | return; |
| 731 | } |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 732 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 733 | } else { |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 734 | r->uri.data = r->uri_start; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 737 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 738 | r->unparsed_uri.len = r->uri_end - r->uri_start; |
| 739 | r->unparsed_uri.data = r->uri_start; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 740 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 741 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 742 | r->method_name.len = r->method_end - r->request_start + 1; |
| 743 | r->method_name.data = r->request_line.data; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 744 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 745 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 746 | if (r->http_protocol.data) { |
| 747 | r->http_protocol.len = r->request_end - r->http_protocol.data; |
| 748 | } |
| 749 | |
| 750 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 751 | if (r->uri_ext) { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 752 | if (r->args_start) { |
| 753 | r->exten.len = r->args_start - 1 - r->uri_ext; |
| 754 | } else { |
| 755 | r->exten.len = r->uri_end - r->uri_ext; |
| 756 | } |
| 757 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 758 | r->exten.data = r->uri_ext; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 761 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 762 | if (r->args_start && r->uri_end > r->args_start) { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 763 | r->args.len = r->uri_end - r->args_start; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 764 | r->args.data = r->args_start; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 767 | |
| 768 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 769 | "http request line: \"%V\"", &r->request_line); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 770 | |
| 771 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 772 | "http uri: \"%V\"", &r->uri); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 773 | |
| 774 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 775 | "http args: \"%V\"", &r->args); |
| 776 | |
| 777 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 778 | "http exten: \"%V\"", &r->exten); |
| 779 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 780 | if (r->host_start && r->host_end) { |
| 781 | n = ngx_http_validate_host(r->host_start, |
| 782 | r->host_end - r->host_start); |
| 783 | |
| 784 | if (n <= 0) { |
| 785 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 786 | "client sent invalid host in request line"); |
| 787 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | r->headers_in.server.len = n; |
| 792 | r->headers_in.server.data = r->host_start; |
| 793 | } |
| 794 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 795 | if (r->http_version < NGX_HTTP_VERSION_10) { |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 796 | |
| 797 | if (ngx_http_find_virtual_server(r, r->headers_in.server.data, |
| 798 | r->headers_in.server.len) |
| 799 | == NGX_ERROR) |
| 800 | { |
| 801 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 802 | return; |
| 803 | } |
| 804 | |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 805 | ngx_http_process_request(r); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 806 | return; |
| 807 | } |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 808 | |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 809 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 810 | if (ngx_list_init(&r->headers_in.headers, r->pool, 20, |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 811 | sizeof(ngx_table_elt_t)) |
| 812 | == NGX_ERROR) |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 813 | { |
| 814 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 815 | return; |
Igor Sysoev | 3d58f8c | 2004-01-08 08:47:17 +0000 | [diff] [blame] | 816 | } |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 817 | |
| 818 | |
Igor Sysoev | 8184d1b | 2005-03-04 14:06:57 +0000 | [diff] [blame] | 819 | if (ngx_array_init(&r->headers_in.cookies, r->pool, 2, |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 820 | sizeof(ngx_table_elt_t *)) |
| 821 | == NGX_ERROR) |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 822 | { |
| 823 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 824 | return; |
| 825 | } |
| 826 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 827 | c->log->action = "reading client request headers"; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 828 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 829 | rev->handler = ngx_http_process_request_headers; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 830 | ngx_http_process_request_headers(rev); |
| 831 | |
| 832 | return; |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 833 | } |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 834 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 835 | if (rc != NGX_AGAIN) { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 836 | |
| 837 | /* there was error while a request line parsing */ |
| 838 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 839 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 840 | ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]); |
| 841 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 842 | return; |
Igor Sysoev | 3d58f8c | 2004-01-08 08:47:17 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 845 | /* NGX_AGAIN: a request line parsing is still incomplete */ |
Igor Sysoev | 3d58f8c | 2004-01-08 08:47:17 +0000 | [diff] [blame] | 846 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 847 | if (r->header_in->pos == r->header_in->end) { |
Igor Sysoev | a1c8a92 | 2004-01-19 18:09:14 +0000 | [diff] [blame] | 848 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 849 | rv = ngx_http_alloc_large_header_buffer(r, 1); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 850 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 851 | if (rv == NGX_ERROR) { |
| 852 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 853 | return; |
| 854 | } |
Igor Sysoev | 0dad629 | 2003-03-05 17:30:51 +0000 | [diff] [blame] | 855 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 856 | if (rv == NGX_DECLINED) { |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 857 | r->request_line.len = r->header_in->end - r->request_start; |
| 858 | r->request_line.data = r->request_start; |
| 859 | |
| 860 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 861 | "client sent too long URI"); |
| 862 | ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 863 | return; |
Igor Sysoev | 0dad629 | 2003-03-05 17:30:51 +0000 | [diff] [blame] | 864 | } |
Igor Sysoev | 0dad629 | 2003-03-05 17:30:51 +0000 | [diff] [blame] | 865 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 866 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 869 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 870 | static void |
| 871 | ngx_http_process_request_headers(ngx_event_t *rev) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 872 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 873 | ssize_t n; |
| 874 | ngx_int_t rc, rv; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 875 | ngx_str_t header; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 876 | ngx_table_elt_t *h; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 877 | ngx_connection_t *c; |
| 878 | ngx_http_header_t *hh; |
| 879 | ngx_http_request_t *r; |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame] | 880 | ngx_http_core_srv_conf_t *cscf; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 881 | ngx_http_core_main_conf_t *cmcf; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 882 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 883 | c = rev->data; |
| 884 | r = c->data; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 885 | |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 886 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, |
| 887 | "http process request header line"); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 888 | |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 889 | if (rev->timedout) { |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 890 | ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); |
| 891 | c->timedout = 1; |
| 892 | ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 896 | cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame] | 897 | cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 898 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 899 | rc = NGX_AGAIN; |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 900 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 901 | for ( ;; ) { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 902 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 903 | if (rc == NGX_AGAIN) { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 904 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 905 | if (r->header_in->pos == r->header_in->end) { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 906 | |
| 907 | rv = ngx_http_alloc_large_header_buffer(r, 0); |
| 908 | |
| 909 | if (rv == NGX_ERROR) { |
| 910 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | |
| 914 | if (rv == NGX_DECLINED) { |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 915 | header.len = r->header_in->end - r->header_name_start; |
| 916 | header.data = r->header_name_start; |
Igor Sysoev | 90c0814 | 2005-07-25 09:41:38 +0000 | [diff] [blame] | 917 | |
| 918 | if (header.len > NGX_MAX_ERROR_STR - 300) { |
| 919 | header.len = NGX_MAX_ERROR_STR - 300; |
| 920 | header.data[header.len++] = '.'; |
| 921 | header.data[header.len++] = '.'; |
| 922 | header.data[header.len++] = '.'; |
| 923 | } |
| 924 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 925 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 926 | "client sent too long header line: \"%V\"", |
| 927 | &header); |
Igor Sysoev | 543a6ae | 2007-08-27 20:52:40 +0000 | [diff] [blame] | 928 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 929 | return; |
| 930 | } |
| 931 | } |
| 932 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 933 | n = ngx_http_read_request_header(r); |
| 934 | |
| 935 | if (n == NGX_AGAIN || n == NGX_ERROR) { |
| 936 | return; |
| 937 | } |
| 938 | } |
| 939 | |
Igor Sysoev | 753792e | 2008-09-24 14:02:50 +0000 | [diff] [blame] | 940 | rc = ngx_http_parse_header_line(r, r->header_in, |
| 941 | cscf->underscores_in_headers); |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 942 | |
Igor Sysoev | 0dad629 | 2003-03-05 17:30:51 +0000 | [diff] [blame] | 943 | if (rc == NGX_OK) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 944 | |
Igor Sysoev | 3362b8d | 2005-05-14 18:42:03 +0000 | [diff] [blame] | 945 | if (r->invalid_header && cscf->ignore_invalid_headers) { |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 946 | |
| 947 | /* there was error while a header line parsing */ |
| 948 | |
| 949 | header.len = r->header_end - r->header_name_start; |
| 950 | header.data = r->header_name_start; |
| 951 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 952 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 953 | "client sent invalid header line: \"%V\"", |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 954 | &header); |
| 955 | continue; |
| 956 | } |
| 957 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 958 | /* a header line has been parsed successfully */ |
| 959 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 960 | h = ngx_list_push(&r->headers_in.headers); |
| 961 | if (h == NULL) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 962 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 963 | return; |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 966 | h->hash = r->header_hash; |
| 967 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 968 | h->key.len = r->header_name_end - r->header_name_start; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 969 | h->key.data = r->header_name_start; |
| 970 | h->key.data[h->key.len] = '\0'; |
| 971 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 972 | h->value.len = r->header_end - r->header_start; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 973 | h->value.data = r->header_start; |
| 974 | h->value.data[h->value.len] = '\0'; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 975 | |
Igor Sysoev | 7f6b2ff | 2008-06-17 15:00:30 +0000 | [diff] [blame] | 976 | h->lowcase_key = ngx_pnalloc(r->pool, h->key.len); |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 977 | if (h->lowcase_key == NULL) { |
| 978 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 979 | return; |
| 980 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 981 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 982 | if (h->key.len == r->lowcase_index) { |
| 983 | ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len); |
| 984 | |
| 985 | } else { |
Igor Sysoev | 777b019 | 2008-08-04 10:07:00 +0000 | [diff] [blame] | 986 | ngx_strlow(h->lowcase_key, h->key.data, h->key.len); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Igor Sysoev | 3338cfd | 2006-05-11 14:43:47 +0000 | [diff] [blame] | 989 | hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash, |
| 990 | h->lowcase_key, h->key.len); |
| 991 | |
| 992 | if (hh && hh->handler(r, h, hh->offset) != NGX_OK) { |
| 993 | return; |
| 994 | } |
| 995 | |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 996 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 997 | "http header: \"%V: %V\"", |
| 998 | &h->key, &h->value); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 999 | |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 1000 | continue; |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1001 | } |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 1002 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1003 | if (rc == NGX_HTTP_PARSE_HEADER_DONE) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1004 | |
| 1005 | /* a whole header has been parsed successfully */ |
| 1006 | |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 1007 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 1008 | "http header done"); |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 1009 | |
Igor Sysoev | 6a12fc9 | 2004-12-06 14:45:08 +0000 | [diff] [blame] | 1010 | r->request_length += r->header_in->pos - r->header_in->start; |
| 1011 | |
Igor Sysoev | 59f3aa3 | 2004-06-24 16:07:04 +0000 | [diff] [blame] | 1012 | r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE; |
| 1013 | |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1014 | rc = ngx_http_process_request_header(r); |
Igor Sysoev | a19a85e | 2003-01-28 15:56:37 +0000 | [diff] [blame] | 1015 | |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1016 | if (rc != NGX_OK) { |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1017 | return; |
Igor Sysoev | a09f08d | 2003-04-25 14:43:13 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1020 | ngx_http_process_request(r); |
Igor Sysoev | e503539 | 2005-08-30 10:55:07 +0000 | [diff] [blame] | 1021 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1022 | return; |
Igor Sysoev | 0dad629 | 2003-03-05 17:30:51 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1025 | if (rc == NGX_AGAIN) { |
Igor Sysoev | 0dad629 | 2003-03-05 17:30:51 +0000 | [diff] [blame] | 1026 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1027 | /* a header line parsing is still not complete */ |
| 1028 | |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
| 1032 | /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */ |
| 1033 | |
| 1034 | header.len = r->header_end - r->header_name_start; |
| 1035 | header.data = r->header_name_start; |
| 1036 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 1037 | "client sent invalid header line: \"%V\\r...\"", |
| 1038 | &header); |
Igor Sysoev | 543a6ae | 2007-08-27 20:52:40 +0000 | [diff] [blame] | 1039 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1040 | return; |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 1041 | } |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 1044 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 1045 | static ssize_t |
| 1046 | ngx_http_read_request_header(ngx_http_request_t *r) |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1047 | { |
Igor Sysoev | 187fcd8 | 2003-05-23 11:53:01 +0000 | [diff] [blame] | 1048 | ssize_t n; |
| 1049 | ngx_event_t *rev; |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1050 | ngx_connection_t *c; |
Igor Sysoev | 187fcd8 | 2003-05-23 11:53:01 +0000 | [diff] [blame] | 1051 | ngx_http_core_srv_conf_t *cscf; |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1052 | |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1053 | c = r->connection; |
| 1054 | rev = c->read; |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 1055 | |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1056 | n = r->header_in->last - r->header_in->pos; |
| 1057 | |
| 1058 | if (n > 0) { |
| 1059 | return n; |
| 1060 | } |
| 1061 | |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 1062 | if (rev->ready) { |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1063 | n = c->recv(c, r->header_in->last, |
| 1064 | r->header_in->end - r->header_in->last); |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 1065 | } else { |
| 1066 | n = NGX_AGAIN; |
Igor Sysoev | 222a2ad | 2003-11-18 16:49:00 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1069 | if (n == NGX_AGAIN) { |
Igor Sysoev | 1bc3a93 | 2008-01-08 20:55:27 +0000 | [diff] [blame] | 1070 | if (!rev->timer_set) { |
Igor Sysoev | f38e046 | 2004-07-16 17:11:43 +0000 | [diff] [blame] | 1071 | cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
Igor Sysoev | 187fcd8 | 2003-05-23 11:53:01 +0000 | [diff] [blame] | 1072 | ngx_add_timer(rev, cscf->client_header_timeout); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 1075 | if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
Igor Sysoev | b3e73d8 | 2003-10-10 15:10:50 +0000 | [diff] [blame] | 1076 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
Igor Sysoev | b3e73d8 | 2003-10-10 15:10:50 +0000 | [diff] [blame] | 1077 | return NGX_ERROR; |
| 1078 | } |
| 1079 | |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1080 | return NGX_AGAIN; |
| 1081 | } |
| 1082 | |
| 1083 | if (n == 0) { |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1084 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1085 | "client closed prematurely connection"); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1086 | } |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1087 | |
| 1088 | if (n == 0 || n == NGX_ERROR) { |
Igor Sysoev | 2316936 | 2007-01-08 18:22:06 +0000 | [diff] [blame] | 1089 | c->error = 1; |
Igor Sysoev | 1691aaf | 2007-01-08 18:23:38 +0000 | [diff] [blame] | 1090 | c->log->action = "reading client request headers"; |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1091 | |
Igor Sysoev | 2316936 | 2007-01-08 18:22:06 +0000 | [diff] [blame] | 1092 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 1093 | return NGX_ERROR; |
| 1094 | } |
| 1095 | |
| 1096 | r->header_in->last += n; |
| 1097 | |
| 1098 | return n; |
| 1099 | } |
| 1100 | |
| 1101 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 1102 | static ngx_int_t |
| 1103 | ngx_http_alloc_large_header_buffer(ngx_http_request_t *r, |
| 1104 | ngx_uint_t request_line) |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1105 | { |
| 1106 | u_char *old, *new; |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1107 | ngx_buf_t *b; |
| 1108 | ngx_http_connection_t *hc; |
| 1109 | ngx_http_core_srv_conf_t *cscf; |
| 1110 | |
| 1111 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 1112 | "http alloc large header buffer"); |
| 1113 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1114 | if (request_line && r->state == 0) { |
| 1115 | |
| 1116 | /* the client fills up the buffer with "\r\n" */ |
| 1117 | |
Igor Sysoev | 6a12fc9 | 2004-12-06 14:45:08 +0000 | [diff] [blame] | 1118 | r->request_length += r->header_in->end - r->header_in->start; |
| 1119 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1120 | r->header_in->pos = r->header_in->start; |
| 1121 | r->header_in->last = r->header_in->start; |
| 1122 | |
| 1123 | return NGX_OK; |
| 1124 | } |
| 1125 | |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1126 | old = request_line ? r->request_start : r->header_name_start; |
| 1127 | |
| 1128 | cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
| 1129 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1130 | if (r->state != 0 |
| 1131 | && (size_t) (r->header_in->pos - old) |
| 1132 | >= cscf->large_client_header_buffers.size) |
| 1133 | { |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1134 | return NGX_DECLINED; |
| 1135 | } |
| 1136 | |
| 1137 | hc = r->http_connection; |
| 1138 | |
| 1139 | if (hc->nfree) { |
| 1140 | b = hc->free[--hc->nfree]; |
| 1141 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 1142 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1143 | "http large header free: %p %uz", |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 1144 | b->pos, b->end - b->last); |
| 1145 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1146 | } else if (hc->nbusy < cscf->large_client_header_buffers.num) { |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1147 | |
| 1148 | if (hc->busy == NULL) { |
| 1149 | hc->busy = ngx_palloc(r->connection->pool, |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1150 | cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *)); |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1151 | if (hc->busy == NULL) { |
| 1152 | return NGX_ERROR; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | b = ngx_create_temp_buf(r->connection->pool, |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1157 | cscf->large_client_header_buffers.size); |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1158 | if (b == NULL) { |
| 1159 | return NGX_ERROR; |
| 1160 | } |
| 1161 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 1162 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 1163 | "http large header alloc: %p %uz", |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 1164 | b->pos, b->end - b->last); |
| 1165 | |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1166 | } else { |
| 1167 | return NGX_DECLINED; |
| 1168 | } |
| 1169 | |
| 1170 | hc->busy[hc->nbusy++] = b; |
| 1171 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1172 | if (r->state == 0) { |
| 1173 | /* |
| 1174 | * r->state == 0 means that a header line was parsed successfully |
| 1175 | * and we do not need to copy incomplete header line and |
| 1176 | * to relocate the parser header pointers |
| 1177 | */ |
| 1178 | |
Igor Sysoev | 6a12fc9 | 2004-12-06 14:45:08 +0000 | [diff] [blame] | 1179 | r->request_length += r->header_in->end - r->header_in->start; |
| 1180 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1181 | r->header_in = b; |
| 1182 | |
| 1183 | return NGX_OK; |
| 1184 | } |
| 1185 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 1186 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 1187 | "http large header copy: %d", r->header_in->pos - old); |
| 1188 | |
Igor Sysoev | 6a12fc9 | 2004-12-06 14:45:08 +0000 | [diff] [blame] | 1189 | r->request_length += old - r->header_in->start; |
| 1190 | |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1191 | new = b->start; |
| 1192 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 1193 | ngx_memcpy(new, old, r->header_in->pos - old); |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1194 | |
| 1195 | b->pos = new + (r->header_in->pos - old); |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 1196 | b->last = new + (r->header_in->pos - old); |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1197 | |
| 1198 | if (request_line) { |
| 1199 | r->request_start = new; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1200 | |
| 1201 | if (r->request_end) { |
| 1202 | r->request_end = new + (r->request_end - old); |
| 1203 | } |
| 1204 | |
| 1205 | r->method_end = new + (r->method_end - old); |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1206 | |
| 1207 | r->uri_start = new + (r->uri_start - old); |
| 1208 | r->uri_end = new + (r->uri_end - old); |
| 1209 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1210 | if (r->schema_start) { |
| 1211 | r->schema_start = new + (r->schema_start - old); |
| 1212 | r->schema_end = new + (r->schema_end - old); |
| 1213 | } |
| 1214 | |
| 1215 | if (r->host_start) { |
| 1216 | r->host_start = new + (r->host_start - old); |
Igor Sysoev | 03011fa | 2006-12-15 10:24:57 +0000 | [diff] [blame] | 1217 | if (r->host_end) { |
| 1218 | r->host_end = new + (r->host_end - old); |
| 1219 | } |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | if (r->port_start) { |
| 1223 | r->port_start = new + (r->port_start - old); |
| 1224 | r->port_end = new + (r->port_end - old); |
| 1225 | } |
| 1226 | |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1227 | if (r->uri_ext) { |
| 1228 | r->uri_ext = new + (r->uri_ext - old); |
| 1229 | } |
| 1230 | |
| 1231 | if (r->args_start) { |
| 1232 | r->args_start = new + (r->args_start - old); |
| 1233 | } |
| 1234 | |
Igor Sysoev | 58ea0c1 | 2007-12-01 12:22:48 +0000 | [diff] [blame] | 1235 | if (r->http_protocol.data) { |
| 1236 | r->http_protocol.data = new + (r->http_protocol.data - old); |
| 1237 | } |
| 1238 | |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1239 | } else { |
| 1240 | r->header_name_start = new; |
| 1241 | r->header_name_end = new + (r->header_name_end - old); |
| 1242 | r->header_start = new + (r->header_start - old); |
| 1243 | r->header_end = new + (r->header_end - old); |
| 1244 | } |
| 1245 | |
| 1246 | r->header_in = b; |
| 1247 | |
| 1248 | return NGX_OK; |
| 1249 | } |
| 1250 | |
Igor Sysoev | 85080d0 | 2004-09-22 16:18:21 +0000 | [diff] [blame] | 1251 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 1252 | static ngx_int_t |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1253 | ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 1254 | ngx_uint_t offset) |
| 1255 | { |
| 1256 | ngx_table_elt_t **ph; |
| 1257 | |
| 1258 | ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset); |
| 1259 | |
| 1260 | if (*ph == NULL) { |
| 1261 | *ph = h; |
| 1262 | } |
| 1263 | |
| 1264 | return NGX_OK; |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | static ngx_int_t |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 1269 | ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 1270 | ngx_uint_t offset) |
| 1271 | { |
| 1272 | ngx_table_elt_t **ph; |
| 1273 | |
| 1274 | ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset); |
| 1275 | |
| 1276 | if (*ph == NULL) { |
| 1277 | *ph = h; |
| 1278 | return NGX_OK; |
| 1279 | } |
| 1280 | |
| 1281 | ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1282 | "client sent duplicate header line: \"%V: %V\", " |
| 1283 | "previous value: \"%V: %V\"", |
| 1284 | &h->key, &h->value, &(*ph)->key, &(*ph)->value); |
Igor Sysoev | b145b06 | 2005-06-15 18:33:41 +0000 | [diff] [blame] | 1285 | |
| 1286 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
| 1287 | |
| 1288 | return NGX_ERROR; |
| 1289 | } |
| 1290 | |
| 1291 | |
| 1292 | static ngx_int_t |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1293 | ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 1294 | ngx_uint_t offset) |
| 1295 | { |
| 1296 | ssize_t len; |
| 1297 | |
| 1298 | if (r->headers_in.host == NULL) { |
| 1299 | r->headers_in.host = h; |
| 1300 | } |
| 1301 | |
| 1302 | len = ngx_http_validate_host(h->value.data, h->value.len); |
| 1303 | |
| 1304 | if (len <= 0) { |
| 1305 | ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
| 1306 | "client sent invalid host header"); |
| 1307 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
| 1308 | return NGX_ERROR; |
| 1309 | } |
| 1310 | |
| 1311 | if (r->headers_in.server.len) { |
| 1312 | return NGX_OK; |
| 1313 | } |
| 1314 | |
| 1315 | r->headers_in.server.len = len; |
| 1316 | r->headers_in.server.data = h->value.data; |
| 1317 | |
| 1318 | return NGX_OK; |
| 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | static ngx_int_t |
Igor Sysoev | ec67b19 | 2007-09-09 18:28:49 +0000 | [diff] [blame] | 1323 | ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 1324 | ngx_uint_t offset) |
| 1325 | { |
Igor Sysoev | f532941 | 2007-10-01 12:57:47 +0000 | [diff] [blame] | 1326 | if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) { |
Igor Sysoev | ec67b19 | 2007-09-09 18:28:49 +0000 | [diff] [blame] | 1327 | r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE; |
| 1328 | |
Igor Sysoev | f532941 | 2007-10-01 12:57:47 +0000 | [diff] [blame] | 1329 | } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) { |
Igor Sysoev | ec67b19 | 2007-09-09 18:28:49 +0000 | [diff] [blame] | 1330 | r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE; |
| 1331 | } |
| 1332 | |
| 1333 | return NGX_OK; |
| 1334 | } |
| 1335 | |
| 1336 | |
| 1337 | static ngx_int_t |
Igor Sysoev | bc5fb29 | 2008-05-13 09:18:58 +0000 | [diff] [blame] | 1338 | ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 1339 | ngx_uint_t offset) |
| 1340 | { |
Igor Sysoev | 54477e4 | 2008-08-26 15:09:28 +0000 | [diff] [blame] | 1341 | u_char *user_agent, *msie; |
Igor Sysoev | bc5fb29 | 2008-05-13 09:18:58 +0000 | [diff] [blame] | 1342 | |
| 1343 | if (r->headers_in.user_agent) { |
| 1344 | return NGX_OK; |
| 1345 | } |
| 1346 | |
| 1347 | r->headers_in.user_agent = h; |
| 1348 | |
| 1349 | /* check some widespread browsers while the header is in CPU cache */ |
| 1350 | |
| 1351 | user_agent = h->value.data; |
| 1352 | |
Igor Sysoev | 54477e4 | 2008-08-26 15:09:28 +0000 | [diff] [blame] | 1353 | msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1); |
Igor Sysoev | bc5fb29 | 2008-05-13 09:18:58 +0000 | [diff] [blame] | 1354 | |
Igor Sysoev | 54477e4 | 2008-08-26 15:09:28 +0000 | [diff] [blame] | 1355 | if (msie && msie + 7 < user_agent + h->value.len) { |
Igor Sysoev | bc5fb29 | 2008-05-13 09:18:58 +0000 | [diff] [blame] | 1356 | |
| 1357 | r->headers_in.msie = 1; |
| 1358 | |
Igor Sysoev | 54477e4 | 2008-08-26 15:09:28 +0000 | [diff] [blame] | 1359 | if (msie[6] == '.') { |
| 1360 | |
| 1361 | switch (msie[5]) { |
| 1362 | case '4': |
| 1363 | r->headers_in.msie4 = 1; |
| 1364 | /* fall through */ |
| 1365 | case '5': |
| 1366 | case '6': |
| 1367 | r->headers_in.msie6 = 1; |
| 1368 | } |
Igor Sysoev | bc5fb29 | 2008-05-13 09:18:58 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | #if 0 |
| 1372 | /* MSIE ignores the SSL "close notify" alert */ |
| 1373 | if (c->ssl) { |
| 1374 | c->ssl->no_send_shutdown = 1; |
| 1375 | } |
| 1376 | #endif |
| 1377 | } |
| 1378 | |
| 1379 | if (ngx_strstrn(user_agent, "Opera", 5 - 1)) { |
| 1380 | r->headers_in.opera = 1; |
| 1381 | r->headers_in.msie = 0; |
| 1382 | r->headers_in.msie4 = 0; |
Igor Sysoev | 54477e4 | 2008-08-26 15:09:28 +0000 | [diff] [blame] | 1383 | r->headers_in.msie6 = 0; |
Igor Sysoev | bc5fb29 | 2008-05-13 09:18:58 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | if (!r->headers_in.msie && !r->headers_in.opera) { |
| 1387 | |
| 1388 | if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) { |
| 1389 | r->headers_in.gecko = 1; |
| 1390 | |
| 1391 | } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) { |
| 1392 | r->headers_in.konqueror = 1; |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | return NGX_OK; |
| 1397 | } |
| 1398 | |
| 1399 | |
| 1400 | static ngx_int_t |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1401 | ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h, |
| 1402 | ngx_uint_t offset) |
| 1403 | { |
| 1404 | ngx_table_elt_t **cookie; |
| 1405 | |
| 1406 | cookie = ngx_array_push(&r->headers_in.cookies); |
| 1407 | if (cookie) { |
| 1408 | *cookie = h; |
| 1409 | return NGX_OK; |
| 1410 | } |
| 1411 | |
| 1412 | ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1413 | |
| 1414 | return NGX_ERROR; |
| 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | static ngx_int_t |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 1419 | ngx_http_process_request_header(ngx_http_request_t *r) |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1420 | { |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1421 | if (ngx_http_find_virtual_server(r, r->headers_in.server.data, |
| 1422 | r->headers_in.server.len) |
| 1423 | == NGX_ERROR) |
| 1424 | { |
| 1425 | ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
| 1426 | return NGX_ERROR; |
| 1427 | } |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1428 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1429 | if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) { |
| 1430 | ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
| 1431 | "client sent HTTP/1.1 request without \"Host\" header"); |
| 1432 | ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST); |
| 1433 | return NGX_ERROR; |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | if (r->headers_in.content_length) { |
| 1437 | r->headers_in.content_length_n = |
Igor Sysoev | 1765f47 | 2006-07-07 16:33:19 +0000 | [diff] [blame] | 1438 | ngx_atoof(r->headers_in.content_length->value.data, |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1439 | r->headers_in.content_length->value.len); |
| 1440 | |
| 1441 | if (r->headers_in.content_length_n == NGX_ERROR) { |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1442 | ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
| 1443 | "client sent invalid \"Content-Length\" header"); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1444 | ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED); |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1445 | return NGX_ERROR; |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | |
Igor Sysoev | 5bf2087 | 2008-12-08 14:04:31 +0000 | [diff] [blame] | 1449 | if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) { |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1450 | ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
Igor Sysoev | 0d68f27 | 2007-12-30 09:11:19 +0000 | [diff] [blame] | 1451 | "client sent %V method without \"Content-Length\" header", |
| 1452 | &r->method_name); |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1453 | ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED); |
| 1454 | return NGX_ERROR; |
| 1455 | } |
| 1456 | |
Igor Sysoev | 589fc74 | 2007-09-07 09:29:08 +0000 | [diff] [blame] | 1457 | if (r->method & NGX_HTTP_TRACE) { |
Igor Sysoev | 3e933d2 | 2007-03-30 18:59:26 +0000 | [diff] [blame] | 1458 | ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
| 1459 | "client sent TRACE method"); |
| 1460 | ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED); |
| 1461 | return NGX_ERROR; |
| 1462 | } |
| 1463 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1464 | if (r->headers_in.transfer_encoding |
Igor Sysoev | 0659720 | 2007-09-26 19:26:14 +0000 | [diff] [blame] | 1465 | && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data, |
Igor Sysoev | f532941 | 2007-10-01 12:57:47 +0000 | [diff] [blame] | 1466 | "chunked", 7 - 1)) |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 1467 | { |
| 1468 | ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
| 1469 | "client sent \"Transfer-Encoding: chunked\" header"); |
| 1470 | ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED); |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 1471 | return NGX_ERROR; |
Igor Sysoev | 6881bfb | 2004-03-30 06:27:36 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Igor Sysoev | ec67b19 | 2007-09-09 18:28:49 +0000 | [diff] [blame] | 1474 | if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) { |
| 1475 | if (r->headers_in.keep_alive) { |
| 1476 | r->headers_in.keep_alive_n = |
| 1477 | ngx_atotm(r->headers_in.keep_alive->value.data, |
| 1478 | r->headers_in.keep_alive->value.len); |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1482 | return NGX_OK; |
| 1483 | } |
| 1484 | |
| 1485 | |
| 1486 | static void |
| 1487 | ngx_http_process_request(ngx_http_request_t *r) |
| 1488 | { |
Igor Sysoev | 86ef6aa | 2007-12-10 12:09:51 +0000 | [diff] [blame] | 1489 | ngx_connection_t *c; |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1490 | |
| 1491 | c = r->connection; |
| 1492 | |
Igor Sysoev | 1d4e9b3 | 2007-12-01 11:46:04 +0000 | [diff] [blame] | 1493 | if (r->plain_http) { |
| 1494 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 1495 | "client sent plain HTTP request to HTTPS port"); |
| 1496 | ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS); |
| 1497 | return; |
| 1498 | } |
| 1499 | |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1500 | #if (NGX_HTTP_SSL) |
| 1501 | |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1502 | if (c->ssl) { |
Igor Sysoev | 86ef6aa | 2007-12-10 12:09:51 +0000 | [diff] [blame] | 1503 | long rc; |
Igor Sysoev | 439e288 | 2008-04-23 18:57:25 +0000 | [diff] [blame] | 1504 | X509 *cert; |
Igor Sysoev | 86ef6aa | 2007-12-10 12:09:51 +0000 | [diff] [blame] | 1505 | ngx_http_ssl_srv_conf_t *sscf; |
| 1506 | |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1507 | sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module); |
| 1508 | |
Igor Sysoev | 49ed6f3 | 2008-07-29 14:29:02 +0000 | [diff] [blame] | 1509 | if (sscf->verify == 1) { |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1510 | rc = SSL_get_verify_result(c->ssl->connection); |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1511 | |
| 1512 | if (rc != X509_V_OK) { |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1513 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
Igor Sysoev | c55a104 | 2006-08-09 19:59:45 +0000 | [diff] [blame] | 1514 | "client SSL certificate verify error: (%l:%s)", |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1515 | rc, X509_verify_cert_error_string(rc)); |
Igor Sysoev | 472233d | 2008-03-10 14:47:07 +0000 | [diff] [blame] | 1516 | |
| 1517 | ngx_ssl_remove_cached_session(sscf->ssl.ctx, |
| 1518 | (SSL_get0_session(c->ssl->connection))); |
| 1519 | |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1520 | ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR); |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1521 | return; |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Igor Sysoev | 439e288 | 2008-04-23 18:57:25 +0000 | [diff] [blame] | 1524 | cert = SSL_get_peer_certificate(c->ssl->connection); |
| 1525 | |
| 1526 | if (cert == NULL) { |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1527 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1528 | "client sent no required SSL certificate"); |
Igor Sysoev | 472233d | 2008-03-10 14:47:07 +0000 | [diff] [blame] | 1529 | |
| 1530 | ngx_ssl_remove_cached_session(sscf->ssl.ctx, |
| 1531 | (SSL_get0_session(c->ssl->connection))); |
| 1532 | |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1533 | ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT); |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1534 | return; |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1535 | } |
Igor Sysoev | 439e288 | 2008-04-23 18:57:25 +0000 | [diff] [blame] | 1536 | |
| 1537 | X509_free(cert); |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | #endif |
| 1542 | |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1543 | if (c->read->timer_set) { |
| 1544 | ngx_del_timer(c->read); |
| 1545 | } |
| 1546 | |
| 1547 | #if (NGX_STAT_STUB) |
| 1548 | ngx_atomic_fetch_add(ngx_stat_reading, -1); |
| 1549 | r->stat_reading = 0; |
| 1550 | ngx_atomic_fetch_add(ngx_stat_writing, 1); |
| 1551 | r->stat_writing = 1; |
| 1552 | #endif |
| 1553 | |
| 1554 | c->read->handler = ngx_http_request_handler; |
| 1555 | c->write->handler = ngx_http_request_handler; |
Igor Sysoev | 7ab4d9c | 2007-08-06 15:22:40 +0000 | [diff] [blame] | 1556 | r->read_event_handler = ngx_http_block_reading; |
Igor Sysoev | 62864d1 | 2007-06-04 20:40:03 +0000 | [diff] [blame] | 1557 | |
| 1558 | ngx_http_handler(r); |
| 1559 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1560 | ngx_http_run_posted_requests(c); |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1564 | static ssize_t |
| 1565 | ngx_http_validate_host(u_char *host, size_t len) |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 1566 | { |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1567 | u_char ch; |
| 1568 | size_t i, last; |
| 1569 | ngx_uint_t dot; |
| 1570 | |
| 1571 | last = len; |
| 1572 | dot = 0; |
| 1573 | |
| 1574 | for (i = 0; i < len; i++) { |
| 1575 | ch = host[i]; |
| 1576 | |
| 1577 | if (ch == '.') { |
| 1578 | if (dot) { |
| 1579 | return -1; |
| 1580 | } |
| 1581 | |
| 1582 | dot = 1; |
| 1583 | continue; |
| 1584 | } |
| 1585 | |
| 1586 | dot = 0; |
| 1587 | |
| 1588 | if (ch == ':') { |
| 1589 | last = i; |
| 1590 | continue; |
| 1591 | } |
| 1592 | |
| 1593 | if (ch == '/' || ch == '\0') { |
| 1594 | return -1; |
| 1595 | } |
| 1596 | |
| 1597 | #if (NGX_WIN32) |
| 1598 | if (ch == '\\') { |
| 1599 | return -1; |
| 1600 | } |
| 1601 | #endif |
| 1602 | } |
| 1603 | |
| 1604 | if (dot) { |
| 1605 | last--; |
| 1606 | } |
| 1607 | |
| 1608 | return last; |
| 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | static ngx_int_t |
| 1613 | ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len) |
| 1614 | { |
Igor Sysoev | 6a07833 | 2008-08-04 10:18:36 +0000 | [diff] [blame] | 1615 | u_char *server; |
| 1616 | ngx_uint_t hash; |
Igor Sysoev | 305a9d8 | 2005-12-26 17:07:48 +0000 | [diff] [blame] | 1617 | ngx_http_core_loc_conf_t *clcf; |
| 1618 | ngx_http_core_srv_conf_t *cscf; |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1619 | u_char buf[32]; |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 1620 | |
Igor Sysoev | b29426d | 2008-08-21 12:56:10 +0000 | [diff] [blame] | 1621 | if (r->virtual_names == NULL) { |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1622 | return NGX_DECLINED; |
| 1623 | } |
| 1624 | |
| 1625 | if (len <= 32) { |
| 1626 | server = buf; |
| 1627 | |
| 1628 | } else { |
Igor Sysoev | 7f6b2ff | 2008-06-17 15:00:30 +0000 | [diff] [blame] | 1629 | server = ngx_pnalloc(r->pool, len); |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1630 | if (server == NULL) { |
| 1631 | return NGX_ERROR; |
| 1632 | } |
| 1633 | } |
| 1634 | |
Igor Sysoev | 6a07833 | 2008-08-04 10:18:36 +0000 | [diff] [blame] | 1635 | hash = ngx_hash_strlow(server, host, len); |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1636 | |
| 1637 | cscf = ngx_hash_find_combined(&r->virtual_names->names, hash, server, len); |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 1638 | |
Igor Sysoev | 9d8a75c | 2007-06-11 19:49:22 +0000 | [diff] [blame] | 1639 | if (cscf) { |
| 1640 | goto found; |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Igor Sysoev | 43c3bf4 | 2007-08-12 19:48:12 +0000 | [diff] [blame] | 1643 | #if (NGX_PCRE) |
| 1644 | |
| 1645 | if (r->virtual_names->nregex) { |
Igor Sysoev | 86ef6aa | 2007-12-10 12:09:51 +0000 | [diff] [blame] | 1646 | ngx_int_t n; |
| 1647 | ngx_uint_t i; |
| 1648 | ngx_str_t name; |
| 1649 | ngx_http_server_name_t *sn; |
Igor Sysoev | 43c3bf4 | 2007-08-12 19:48:12 +0000 | [diff] [blame] | 1650 | |
| 1651 | name.len = len; |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1652 | name.data = server; |
Igor Sysoev | 43c3bf4 | 2007-08-12 19:48:12 +0000 | [diff] [blame] | 1653 | |
| 1654 | sn = r->virtual_names->regex; |
| 1655 | |
| 1656 | for (i = 0; i < r->virtual_names->nregex; i++) { |
| 1657 | |
| 1658 | n = ngx_regex_exec(sn[i].regex, &name, NULL, 0); |
| 1659 | |
| 1660 | if (n == NGX_REGEX_NO_MATCHED) { |
| 1661 | continue; |
| 1662 | } |
| 1663 | |
| 1664 | if (n < 0) { |
| 1665 | ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, |
| 1666 | ngx_regex_exec_n |
| 1667 | " failed: %d on \"%V\" using \"%V\"", |
| 1668 | n, &name, &sn[i].name); |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1669 | return NGX_ERROR; |
Igor Sysoev | 43c3bf4 | 2007-08-12 19:48:12 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* match */ |
| 1673 | |
| 1674 | cscf = sn[i].core_srv_conf; |
| 1675 | |
| 1676 | goto found; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | #endif |
| 1681 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1682 | return NGX_OK; |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 1683 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1684 | found: |
| 1685 | |
Igor Sysoev | 305a9d8 | 2005-12-26 17:07:48 +0000 | [diff] [blame] | 1686 | r->srv_conf = cscf->ctx->srv_conf; |
| 1687 | r->loc_conf = cscf->ctx->loc_conf; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1688 | |
| 1689 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 1690 | r->connection->log->file = clcf->err_log->file; |
| 1691 | |
| 1692 | if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) { |
| 1693 | r->connection->log->log_level = clcf->err_log->log_level; |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Igor Sysoev | 5fd0931 | 2008-05-15 14:44:47 +0000 | [diff] [blame] | 1696 | return NGX_OK; |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1700 | static void |
| 1701 | ngx_http_request_handler(ngx_event_t *ev) |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1702 | { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1703 | ngx_connection_t *c; |
| 1704 | ngx_http_request_t *r; |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 1705 | ngx_http_log_ctx_t *ctx; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1706 | |
| 1707 | c = ev->data; |
| 1708 | r = c->data; |
| 1709 | |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 1710 | ctx = c->log->data; |
| 1711 | ctx->current_request = r; |
| 1712 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1713 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1714 | "http run request: \"%V?%V\"", &r->uri, &r->args); |
| 1715 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1716 | if (ev->write) { |
| 1717 | r->write_event_handler(r); |
| 1718 | |
| 1719 | } else { |
| 1720 | r->read_event_handler(r); |
| 1721 | } |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1722 | |
| 1723 | ngx_http_run_posted_requests(c); |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | void |
| 1728 | ngx_http_run_posted_requests(ngx_connection_t *c) |
| 1729 | { |
| 1730 | ngx_http_request_t *r; |
| 1731 | ngx_http_log_ctx_t *ctx; |
| 1732 | ngx_http_posted_request_t *pr; |
| 1733 | |
| 1734 | for ( ;; ) { |
| 1735 | |
| 1736 | if (c->destroyed) { |
| 1737 | return; |
| 1738 | } |
| 1739 | |
| 1740 | r = c->data; |
| 1741 | pr = r->main->posted_requests; |
| 1742 | |
| 1743 | if (pr == NULL) { |
| 1744 | return; |
| 1745 | } |
| 1746 | |
| 1747 | r->main->posted_requests = pr->next; |
| 1748 | |
| 1749 | r = pr->request; |
| 1750 | |
| 1751 | ctx = c->log->data; |
| 1752 | ctx->current_request = r; |
| 1753 | |
| 1754 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1755 | "http posted request: \"%V?%V\"", &r->uri, &r->args); |
| 1756 | |
| 1757 | r->write_event_handler(r); |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | ngx_int_t |
| 1763 | ngx_http_post_request(ngx_http_request_t *r) |
| 1764 | { |
| 1765 | ngx_http_posted_request_t *pr, **p; |
| 1766 | |
| 1767 | pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t)); |
| 1768 | if (pr == NULL) { |
| 1769 | return NGX_ERROR; |
| 1770 | } |
| 1771 | |
| 1772 | pr->request = r; |
| 1773 | pr->next = NULL; |
| 1774 | |
| 1775 | for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ } |
| 1776 | |
| 1777 | *p = pr; |
| 1778 | |
| 1779 | return NGX_OK; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | |
| 1783 | void |
| 1784 | ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc) |
| 1785 | { |
Igor Sysoev | ae0347a | 2007-08-27 15:53:00 +0000 | [diff] [blame] | 1786 | ngx_connection_t *c; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1787 | ngx_http_request_t *pr; |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 1788 | ngx_http_core_loc_conf_t *clcf; |
| 1789 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1790 | if (rc == NGX_DONE) { |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 1791 | /* the request pool may be already destroyed */ |
Igor Sysoev | 1342d9c | 2003-10-09 07:00:45 +0000 | [diff] [blame] | 1792 | return; |
| 1793 | } |
| 1794 | |
Igor Sysoev | ae0347a | 2007-08-27 15:53:00 +0000 | [diff] [blame] | 1795 | c = r->connection; |
| 1796 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1797 | ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1798 | "http finalize request: %d, \"%V?%V\" %d", |
| 1799 | rc, &r->uri, &r->args, r == c->data); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1800 | |
Igor Sysoev | da173ab | 2006-08-30 10:39:17 +0000 | [diff] [blame] | 1801 | if (rc == NGX_DECLINED) { |
| 1802 | r->content_handler = NULL; |
| 1803 | r->write_event_handler = ngx_http_core_run_phases; |
| 1804 | ngx_http_core_run_phases(r); |
| 1805 | return; |
| 1806 | } |
| 1807 | |
Igor Sysoev | 960100e | 2006-10-13 15:20:10 +0000 | [diff] [blame] | 1808 | if (r != r->main && r->post_subrequest) { |
| 1809 | rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc); |
Igor Sysoev | 8f12558 | 2006-07-28 15:16:17 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Igor Sysoev | e87afba | 2007-12-27 13:13:34 +0000 | [diff] [blame] | 1812 | if (rc == NGX_ERROR |
| 1813 | || rc == NGX_HTTP_REQUEST_TIME_OUT |
| 1814 | || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST |
| 1815 | || c->error) |
| 1816 | { |
Igor Sysoev | 8b57df9 | 2007-01-29 11:53:23 +0000 | [diff] [blame] | 1817 | if (rc > 0 && r->headers_out.status == 0) { |
| 1818 | r->headers_out.status = rc; |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 1821 | if (ngx_http_post_action(r) == NGX_OK) { |
| 1822 | return; |
| 1823 | } |
| 1824 | |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 1825 | ngx_http_close_request(r, 0); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1826 | return; |
| 1827 | } |
| 1828 | |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1829 | if (rc >= NGX_HTTP_SPECIAL_RESPONSE |
| 1830 | || rc == NGX_HTTP_CREATED |
| 1831 | || rc == NGX_HTTP_NO_CONTENT) |
| 1832 | { |
Igor Sysoev | cdc4630 | 2005-12-07 14:51:31 +0000 | [diff] [blame] | 1833 | if (rc == NGX_HTTP_CLOSE) { |
| 1834 | ngx_http_close_request(r, rc); |
| 1835 | return; |
| 1836 | } |
| 1837 | |
Igor Sysoev | df3254a | 2006-01-11 15:26:57 +0000 | [diff] [blame] | 1838 | if (r == r->main) { |
Igor Sysoev | ae0347a | 2007-08-27 15:53:00 +0000 | [diff] [blame] | 1839 | if (c->read->timer_set) { |
| 1840 | ngx_del_timer(c->read); |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Igor Sysoev | ae0347a | 2007-08-27 15:53:00 +0000 | [diff] [blame] | 1843 | if (c->write->timer_set) { |
| 1844 | ngx_del_timer(c->write); |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 1845 | } |
| 1846 | } |
| 1847 | |
Igor Sysoev | 05a7656 | 2008-09-15 16:44:37 +0000 | [diff] [blame] | 1848 | c->read->handler = ngx_http_request_handler; |
| 1849 | c->write->handler = ngx_http_request_handler; |
| 1850 | |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 1851 | ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc)); |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 1852 | return; |
| 1853 | } |
| 1854 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1855 | if (r != r->main) { |
Igor Sysoev | eb62fa0 | 2008-09-29 13:18:41 +0000 | [diff] [blame] | 1856 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1857 | if (r->buffered || r->postponed) { |
Igor Sysoev | eb62fa0 | 2008-09-29 13:18:41 +0000 | [diff] [blame] | 1858 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1859 | if (ngx_http_set_write_handler(r) != NGX_OK) { |
| 1860 | ngx_http_close_request(r->main, 0); |
| 1861 | } |
Igor Sysoev | eb62fa0 | 2008-09-29 13:18:41 +0000 | [diff] [blame] | 1862 | |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 1863 | return; |
| 1864 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1865 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1866 | #if (NGX_DEBUG) |
| 1867 | if (r != c->data) { |
| 1868 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1869 | "http finalize non-active request: \"%V?%V\"", |
| 1870 | &r->uri, &r->args); |
| 1871 | } |
| 1872 | #endif |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1873 | |
| 1874 | pr = r->parent; |
| 1875 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1876 | if (r == c->data) { |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 1877 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1878 | if (!r->logged) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1879 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1880 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 1881 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1882 | if (clcf->log_subrequest) { |
| 1883 | ngx_http_log_request(r); |
| 1884 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1885 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1886 | r->logged = 1; |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 1887 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1888 | } else { |
| 1889 | ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
| 1890 | "subrequest: \"%V?%V\" logged again", |
| 1891 | &r->uri, &r->args); |
| 1892 | } |
| 1893 | |
| 1894 | r->done = 1; |
| 1895 | |
| 1896 | if (pr->postponed && pr->postponed->request == r) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1897 | pr->postponed = pr->postponed->next; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1900 | c->data = pr; |
Igor Sysoev | ac72bd1 | 2006-05-04 15:32:46 +0000 | [diff] [blame] | 1901 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1902 | } else { |
Igor Sysoev | ac72bd1 | 2006-05-04 15:32:46 +0000 | [diff] [blame] | 1903 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1904 | r->write_event_handler = ngx_http_request_finalizer; |
| 1905 | |
| 1906 | if (r->waited) { |
| 1907 | r->done = 1; |
Igor Sysoev | df3254a | 2006-01-11 15:26:57 +0000 | [diff] [blame] | 1908 | } |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1909 | } |
Igor Sysoev | df3254a | 2006-01-11 15:26:57 +0000 | [diff] [blame] | 1910 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1911 | if (ngx_http_post_request(pr) != NGX_OK) { |
| 1912 | ngx_http_close_request(r->main, 0); |
| 1913 | return; |
| 1914 | } |
Igor Sysoev | df3254a | 2006-01-11 15:26:57 +0000 | [diff] [blame] | 1915 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1916 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 1917 | "http wake parent request: \"%V?%V\"", |
| 1918 | &pr->uri, &pr->args); |
| 1919 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1920 | return; |
| 1921 | } |
| 1922 | |
| 1923 | if (r->buffered || c->buffered || r->postponed) { |
| 1924 | |
| 1925 | if (ngx_http_set_write_handler(r) != NGX_OK) { |
| 1926 | ngx_http_close_request(r, 0); |
Igor Sysoev | 2eec1e1 | 2008-09-27 15:08:02 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1929 | return; |
| 1930 | } |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 1931 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1932 | if (r != c->data) { |
| 1933 | ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
| 1934 | "http finalize non-active request: \"%V?%V\"", |
| 1935 | &r->uri, &r->args); |
Igor Sysoev | 1342d9c | 2003-10-09 07:00:45 +0000 | [diff] [blame] | 1936 | return; |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 1937 | } |
Igor Sysoev | 1342d9c | 2003-10-09 07:00:45 +0000 | [diff] [blame] | 1938 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1939 | r->done = 1; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1940 | |
Igor Sysoev | 6d16e1e | 2006-04-05 13:40:54 +0000 | [diff] [blame] | 1941 | if (!r->post_action) { |
| 1942 | r->request_complete = 1; |
| 1943 | } |
| 1944 | |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 1945 | if (ngx_http_post_action(r) == NGX_OK) { |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1946 | return; |
| 1947 | } |
| 1948 | |
Igor Sysoev | ae0347a | 2007-08-27 15:53:00 +0000 | [diff] [blame] | 1949 | if (c->read->timer_set) { |
| 1950 | ngx_del_timer(c->read); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Igor Sysoev | ae0347a | 2007-08-27 15:53:00 +0000 | [diff] [blame] | 1953 | if (c->write->timer_set) { |
| 1954 | c->write->delayed = 0; |
| 1955 | ngx_del_timer(c->write); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Igor Sysoev | ae0347a | 2007-08-27 15:53:00 +0000 | [diff] [blame] | 1958 | if (c->destroyed) { |
Igor Sysoev | 2402502 | 2005-12-16 15:07:08 +0000 | [diff] [blame] | 1959 | return; |
| 1960 | } |
| 1961 | |
Igor Sysoev | ae0347a | 2007-08-27 15:53:00 +0000 | [diff] [blame] | 1962 | if (c->read->eof) { |
Igor Sysoev | db7b126 | 2007-08-27 15:40:19 +0000 | [diff] [blame] | 1963 | ngx_http_close_request(r, 0); |
| 1964 | return; |
| 1965 | } |
| 1966 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 1967 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 1968 | |
Igor Sysoev | c599198 | 2004-01-16 06:15:48 +0000 | [diff] [blame] | 1969 | if (!ngx_terminate |
Igor Sysoev | 3f4685f | 2004-04-25 20:13:21 +0000 | [diff] [blame] | 1970 | && !ngx_exiting |
Igor Sysoev | cdd4354 | 2008-05-21 17:39:51 +0000 | [diff] [blame] | 1971 | && r->keepalive |
Igor Sysoev | c599198 | 2004-01-16 06:15:48 +0000 | [diff] [blame] | 1972 | && clcf->keepalive_timeout > 0) |
| 1973 | { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1974 | ngx_http_set_keepalive(r); |
Igor Sysoev | c599198 | 2004-01-16 06:15:48 +0000 | [diff] [blame] | 1975 | return; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1976 | |
Igor Sysoev | dc867cd | 2003-12-14 20:10:27 +0000 | [diff] [blame] | 1977 | } else if (r->lingering_close && clcf->lingering_timeout > 0) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1978 | ngx_http_set_lingering_close(r); |
Igor Sysoev | c599198 | 2004-01-16 06:15:48 +0000 | [diff] [blame] | 1979 | return; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1980 | } |
Igor Sysoev | 1342d9c | 2003-10-09 07:00:45 +0000 | [diff] [blame] | 1981 | |
Igor Sysoev | c599198 | 2004-01-16 06:15:48 +0000 | [diff] [blame] | 1982 | ngx_http_close_request(r, 0); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 1986 | static ngx_int_t |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 1987 | ngx_http_set_write_handler(ngx_http_request_t *r) |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1988 | { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1989 | ngx_event_t *wev; |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 1990 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 1991 | |
Igor Sysoev | b1af9bb | 2004-06-25 14:42:03 +0000 | [diff] [blame] | 1992 | r->http_state = NGX_HTTP_WRITING_REQUEST_STATE; |
| 1993 | |
Igor Sysoev | 7ab4d9c | 2007-08-06 15:22:40 +0000 | [diff] [blame] | 1994 | r->read_event_handler = ngx_http_test_reading; |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 1995 | r->write_event_handler = ngx_http_writer; |
| 1996 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1997 | wev = r->connection->write; |
| 1998 | |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 1999 | if (wev->ready && wev->delayed) { |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2000 | return NGX_OK; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2003 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 2004 | if (!wev->delayed) { |
Igor Sysoev | 73a73b5 | 2004-06-20 19:54:15 +0000 | [diff] [blame] | 2005 | ngx_add_timer(wev, clcf->send_timeout); |
| 2006 | } |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2007 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2008 | if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) { |
Igor Sysoev | b3e73d8 | 2003-10-10 15:10:50 +0000 | [diff] [blame] | 2009 | ngx_http_close_request(r, 0); |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2010 | return NGX_ERROR; |
Igor Sysoev | b3e73d8 | 2003-10-10 15:10:50 +0000 | [diff] [blame] | 2011 | } |
Igor Sysoev | 85ef94b | 2005-06-23 13:41:06 +0000 | [diff] [blame] | 2012 | |
| 2013 | return NGX_OK; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2017 | static void |
| 2018 | ngx_http_writer(ngx_http_request_t *r) |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2019 | { |
| 2020 | int rc; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2021 | ngx_event_t *wev; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2022 | ngx_connection_t *c; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2023 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2024 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2025 | c = r->connection; |
| 2026 | wev = c->write; |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 2027 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2028 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0, |
| 2029 | "http writer handler: \"%V?%V\"", &r->uri, &r->args); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2030 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 2031 | clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module); |
| 2032 | |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 2033 | if (wev->timedout) { |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 2034 | if (!wev->delayed) { |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 2035 | ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, |
| 2036 | "client timed out"); |
| 2037 | c->timedout = 1; |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 2038 | |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 2039 | ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT); |
Igor Sysoev | 73a73b5 | 2004-06-20 19:54:15 +0000 | [diff] [blame] | 2040 | return; |
| 2041 | } |
| 2042 | |
| 2043 | wev->timedout = 0; |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 2044 | wev->delayed = 0; |
Igor Sysoev | 73a73b5 | 2004-06-20 19:54:15 +0000 | [diff] [blame] | 2045 | |
| 2046 | if (!wev->ready) { |
Igor Sysoev | 73a73b5 | 2004-06-20 19:54:15 +0000 | [diff] [blame] | 2047 | ngx_add_timer(wev, clcf->send_timeout); |
| 2048 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2049 | if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) { |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2050 | ngx_http_close_request(r, 0); |
Igor Sysoev | 73a73b5 | 2004-06-20 19:54:15 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | return; |
| 2054 | } |
| 2055 | |
| 2056 | } else { |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 2057 | if (wev->delayed) { |
Igor Sysoev | 73a73b5 | 2004-06-20 19:54:15 +0000 | [diff] [blame] | 2058 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, |
| 2059 | "http writer delayed"); |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 2060 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2061 | if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) { |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2062 | ngx_http_close_request(r, 0); |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Igor Sysoev | 73a73b5 | 2004-06-20 19:54:15 +0000 | [diff] [blame] | 2065 | return; |
| 2066 | } |
Igor Sysoev | 10fc9ef | 2003-10-27 08:53:49 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2069 | rc = ngx_http_output_filter(r, NULL); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2070 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2071 | if (c->destroyed) { |
| 2072 | return; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2075 | ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 2076 | "http writer output filter: %d, \"%V?%V\"", |
| 2077 | rc, &r->uri, &r->args); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2078 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 2079 | if (r->buffered || r->postponed || (r == r->main && c->buffered)) { |
| 2080 | |
Igor Sysoev | ef06648 | 2004-06-21 15:59:32 +0000 | [diff] [blame] | 2081 | if (!wev->ready && !wev->delayed) { |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2082 | ngx_add_timer(wev, clcf->send_timeout); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2085 | if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) { |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2086 | ngx_http_close_request(r, 0); |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2087 | } |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2088 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 2089 | return; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2092 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0, |
| 2093 | "http writer done: \"%V?%V\"", &r->uri, &r->args); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2094 | |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2095 | ngx_http_finalize_request(r, rc); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | |
Igor Sysoev | 851cd73 | 2008-12-08 14:23:20 +0000 | [diff] [blame] | 2099 | static void |
| 2100 | ngx_http_request_finalizer(ngx_http_request_t *r) |
| 2101 | { |
| 2102 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2103 | "http finalizer done: \"%V?%V\"", &r->uri, &r->args); |
| 2104 | |
| 2105 | ngx_http_finalize_request(r, 0); |
| 2106 | } |
| 2107 | |
| 2108 | |
Igor Sysoev | 7ab4d9c | 2007-08-06 15:22:40 +0000 | [diff] [blame] | 2109 | void |
| 2110 | ngx_http_block_reading(ngx_http_request_t *r) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2111 | { |
Igor Sysoev | f53d0f3 | 2007-07-10 20:53:45 +0000 | [diff] [blame] | 2112 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 7ab4d9c | 2007-08-06 15:22:40 +0000 | [diff] [blame] | 2113 | "http reading blocked"); |
Igor Sysoev | f53d0f3 | 2007-07-10 20:53:45 +0000 | [diff] [blame] | 2114 | |
| 2115 | /* aio does not call this handler */ |
| 2116 | |
| 2117 | if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) |
| 2118 | && r->connection->read->active) |
| 2119 | { |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2120 | if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) { |
Igor Sysoev | f53d0f3 | 2007-07-10 20:53:45 +0000 | [diff] [blame] | 2121 | ngx_http_close_request(r, 0); |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | |
Igor Sysoev | abe378e | 2008-11-10 15:22:33 +0000 | [diff] [blame] | 2127 | void |
Igor Sysoev | 7ab4d9c | 2007-08-06 15:22:40 +0000 | [diff] [blame] | 2128 | ngx_http_test_reading(ngx_http_request_t *r) |
Igor Sysoev | f53d0f3 | 2007-07-10 20:53:45 +0000 | [diff] [blame] | 2129 | { |
Igor Sysoev | 8d6de94 | 2007-01-19 08:10:06 +0000 | [diff] [blame] | 2130 | int n; |
| 2131 | char buf[1]; |
| 2132 | ngx_err_t err; |
| 2133 | ngx_event_t *rev; |
| 2134 | ngx_connection_t *c; |
| 2135 | |
| 2136 | c = r->connection; |
| 2137 | rev = c->read; |
| 2138 | |
Igor Sysoev | 7ab4d9c | 2007-08-06 15:22:40 +0000 | [diff] [blame] | 2139 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading"); |
Igor Sysoev | 8d6de94 | 2007-01-19 08:10:06 +0000 | [diff] [blame] | 2140 | |
| 2141 | #if (NGX_HAVE_KQUEUE) |
| 2142 | |
| 2143 | if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
| 2144 | |
| 2145 | if (!rev->pending_eof) { |
| 2146 | return; |
| 2147 | } |
| 2148 | |
| 2149 | rev->eof = 1; |
| 2150 | c->error = 1; |
| 2151 | err = rev->kq_errno; |
| 2152 | |
| 2153 | goto closed; |
| 2154 | } |
| 2155 | |
| 2156 | #endif |
| 2157 | |
| 2158 | n = recv(c->fd, buf, 1, MSG_PEEK); |
| 2159 | |
| 2160 | if (n == 0) { |
| 2161 | rev->eof = 1; |
| 2162 | c->error = 1; |
| 2163 | err = 0; |
| 2164 | |
| 2165 | goto closed; |
| 2166 | |
| 2167 | } else if (n == -1) { |
| 2168 | err = ngx_socket_errno; |
| 2169 | |
| 2170 | if (err != NGX_EAGAIN) { |
| 2171 | rev->eof = 1; |
| 2172 | c->error = 1; |
| 2173 | |
| 2174 | goto closed; |
| 2175 | } |
| 2176 | } |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 2177 | |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 2178 | /* aio does not call this handler */ |
| 2179 | |
Igor Sysoev | 8d6de94 | 2007-01-19 08:10:06 +0000 | [diff] [blame] | 2180 | if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) { |
| 2181 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2182 | if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) { |
Igor Sysoev | 6ddfbf0 | 2003-05-15 15:42:53 +0000 | [diff] [blame] | 2183 | ngx_http_close_request(r, 0); |
Igor Sysoev | 6ddfbf0 | 2003-05-15 15:42:53 +0000 | [diff] [blame] | 2184 | } |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 2185 | } |
Igor Sysoev | 8d6de94 | 2007-01-19 08:10:06 +0000 | [diff] [blame] | 2186 | |
| 2187 | return; |
| 2188 | |
| 2189 | closed: |
| 2190 | |
| 2191 | if (err) { |
| 2192 | rev->error = 1; |
| 2193 | } |
| 2194 | |
| 2195 | ngx_log_error(NGX_LOG_INFO, c->log, err, |
| 2196 | "client closed prematurely connection"); |
| 2197 | |
Igor Sysoev | d74b66c | 2007-02-15 18:53:48 +0000 | [diff] [blame] | 2198 | ngx_http_finalize_request(r, 0); |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2202 | static void |
| 2203 | ngx_http_set_keepalive(ngx_http_request_t *r) |
Igor Sysoev | 1e7ec9d | 2003-02-11 07:14:40 +0000 | [diff] [blame] | 2204 | { |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 2205 | int tcp_nodelay; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2206 | ngx_int_t i; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2207 | ngx_buf_t *b, *f; |
Igor Sysoev | 187fcd8 | 2003-05-23 11:53:01 +0000 | [diff] [blame] | 2208 | ngx_event_t *rev, *wev; |
| 2209 | ngx_connection_t *c; |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 2210 | ngx_http_connection_t *hc; |
Igor Sysoev | 187fcd8 | 2003-05-23 11:53:01 +0000 | [diff] [blame] | 2211 | ngx_http_core_srv_conf_t *cscf; |
| 2212 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | 1e7ec9d | 2003-02-11 07:14:40 +0000 | [diff] [blame] | 2213 | |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2214 | c = r->connection; |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 2215 | rev = c->read; |
Igor Sysoev | 1e7ec9d | 2003-02-11 07:14:40 +0000 | [diff] [blame] | 2216 | |
Igor Sysoev | 4fbd868 | 2007-08-07 10:53:27 +0000 | [diff] [blame] | 2217 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 2218 | |
Igor Sysoev | e04084c | 2004-01-26 08:52:49 +0000 | [diff] [blame] | 2219 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler"); |
Igor Sysoev | 6a1cc90 | 2003-05-22 15:23:47 +0000 | [diff] [blame] | 2220 | |
Igor Sysoev | 4fbd868 | 2007-08-07 10:53:27 +0000 | [diff] [blame] | 2221 | if (r->discard_body) { |
Igor Sysoev | 32856e1 | 2009-01-31 20:44:30 +0000 | [diff] [blame] | 2222 | r->write_event_handler = ngx_http_request_empty_handler; |
Igor Sysoev | 4fbd868 | 2007-08-07 10:53:27 +0000 | [diff] [blame] | 2223 | r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000); |
| 2224 | ngx_add_timer(rev, clcf->lingering_timeout); |
| 2225 | return; |
| 2226 | } |
| 2227 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2228 | c->log->action = "closing request"; |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 2229 | |
| 2230 | hc = r->http_connection; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2231 | b = r->header_in; |
| 2232 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2233 | if (b->pos < b->last) { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2234 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2235 | /* the pipelined request */ |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2236 | |
| 2237 | if (b != c->buffer) { |
| 2238 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 2239 | /* |
| 2240 | * If the large header buffers were allocated while the previous |
| 2241 | * request processing then we do not use c->buffer for |
| 2242 | * the pipelined request (see ngx_http_init_request()). |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2243 | * |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 2244 | * Now we would move the large header buffers to the free list. |
| 2245 | */ |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2246 | |
| 2247 | cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
| 2248 | |
| 2249 | if (hc->free == NULL) { |
| 2250 | hc->free = ngx_palloc(c->pool, |
| 2251 | cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *)); |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2252 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2253 | if (hc->free == NULL) { |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 2254 | ngx_http_close_request(r, 0); |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2255 | return; |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | for (i = 0; i < hc->nbusy - 1; i++) { |
| 2260 | f = hc->busy[i]; |
| 2261 | hc->free[hc->nfree++] = f; |
| 2262 | f->pos = f->start; |
| 2263 | f->last = f->start; |
| 2264 | } |
| 2265 | |
| 2266 | hc->busy[0] = b; |
| 2267 | hc->nbusy = 1; |
| 2268 | } |
| 2269 | } |
| 2270 | |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2271 | ngx_http_request_done(r, 0); |
| 2272 | |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 2273 | c->data = hc; |
Igor Sysoev | 1e7ec9d | 2003-02-11 07:14:40 +0000 | [diff] [blame] | 2274 | |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 2275 | ngx_add_timer(rev, clcf->keepalive_timeout); |
| 2276 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2277 | if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2278 | ngx_http_close_connection(c); |
| 2279 | return; |
Igor Sysoev | b738757 | 2003-03-11 20:38:13 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Igor Sysoev | 7b6062a | 2004-02-12 20:57:10 +0000 | [diff] [blame] | 2282 | wev = c->write; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2283 | wev->handler = ngx_http_empty_handler; |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 2284 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 2285 | if (b->pos < b->last) { |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 2286 | |
Igor Sysoev | e04084c | 2004-01-26 08:52:49 +0000 | [diff] [blame] | 2287 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request"); |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2288 | |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 2289 | #if (NGX_STAT_STUB) |
| 2290 | ngx_atomic_fetch_add(ngx_stat_reading, 1); |
| 2291 | #endif |
| 2292 | |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 2293 | hc->pipeline = 1; |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2294 | c->log->action = "reading client pipelined request line"; |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 2295 | |
Igor Sysoev | 48242f4 | 2008-05-19 10:24:22 +0000 | [diff] [blame] | 2296 | rev->handler = ngx_http_init_request; |
| 2297 | ngx_post_event(rev, &ngx_posted_events); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2298 | return; |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Igor Sysoev | dd888c4 | 2004-09-21 05:38:28 +0000 | [diff] [blame] | 2301 | hc->pipeline = 0; |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 2302 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 2303 | /* |
| 2304 | * To keep a memory footprint as small as possible for an idle |
| 2305 | * keepalive connection we try to free the ngx_http_request_t and |
| 2306 | * c->buffer's memory if they were allocated outside the c->pool. |
| 2307 | * The large header buffers are always allocated outside the c->pool and |
| 2308 | * are freed too. |
| 2309 | */ |
| 2310 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2311 | if (ngx_pfree(c->pool, r) == NGX_OK) { |
| 2312 | hc->request = NULL; |
| 2313 | } |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2314 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2315 | b = c->buffer; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2316 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2317 | if (ngx_pfree(c->pool, b->start) == NGX_OK) { |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 2318 | |
| 2319 | /* |
| 2320 | * the special note for ngx_http_keepalive_handler() that |
| 2321 | * c->buffer's memory was freed |
| 2322 | */ |
| 2323 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2324 | b->pos = NULL; |
| 2325 | |
| 2326 | } else { |
| 2327 | b->pos = b->start; |
| 2328 | b->last = b->start; |
| 2329 | } |
| 2330 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2331 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d", |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2332 | hc->free, hc->nfree); |
| 2333 | |
| 2334 | if (hc->free) { |
| 2335 | for (i = 0; i < hc->nfree; i++) { |
Igor Sysoev | acb2f5d | 2007-12-12 15:42:39 +0000 | [diff] [blame] | 2336 | ngx_pfree(c->pool, hc->free[i]->start); |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2337 | hc->free[i] = NULL; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2340 | hc->nfree = 0; |
| 2341 | } |
| 2342 | |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2343 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d", |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2344 | hc->busy, hc->nbusy); |
| 2345 | |
| 2346 | if (hc->busy) { |
| 2347 | for (i = 0; i < hc->nbusy; i++) { |
Igor Sysoev | acb2f5d | 2007-12-12 15:42:39 +0000 | [diff] [blame] | 2348 | ngx_pfree(c->pool, hc->busy[i]->start); |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2349 | hc->busy[i] = NULL; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2352 | hc->nbusy = 0; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
Igor Sysoev | cd2aa8e | 2007-12-26 21:07:30 +0000 | [diff] [blame] | 2355 | #if (NGX_HTTP_SSL) |
| 2356 | if (c->ssl) { |
| 2357 | ngx_ssl_free_buffer(c); |
| 2358 | } |
| 2359 | #endif |
| 2360 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2361 | rev->handler = ngx_http_keepalive_handler; |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 2362 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2363 | if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) { |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2364 | if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) { |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2365 | ngx_http_close_connection(c); |
| 2366 | return; |
Igor Sysoev | 3d062ad | 2003-03-05 06:37:42 +0000 | [diff] [blame] | 2367 | } |
| 2368 | } |
| 2369 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2370 | c->log->action = "keepalive"; |
Igor Sysoev | 6a644c6 | 2003-03-04 06:33:48 +0000 | [diff] [blame] | 2371 | |
Igor Sysoev | 67f450d | 2004-06-01 06:04:46 +0000 | [diff] [blame] | 2372 | if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2373 | if (ngx_tcp_push(c->fd) == -1) { |
Igor Sysoev | 4cec79f | 2004-04-28 06:14:50 +0000 | [diff] [blame] | 2374 | ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed"); |
| 2375 | ngx_http_close_connection(c); |
| 2376 | return; |
Igor Sysoev | 9cf7830 | 2003-06-04 17:28:33 +0000 | [diff] [blame] | 2377 | } |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 2378 | |
Igor Sysoev | 67f450d | 2004-06-01 06:04:46 +0000 | [diff] [blame] | 2379 | c->tcp_nopush = NGX_TCP_NOPUSH_UNSET; |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 2380 | tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 2381 | |
| 2382 | } else { |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 2383 | tcp_nodelay = 1; |
| 2384 | } |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 2385 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2386 | if (tcp_nodelay |
| 2387 | && clcf->tcp_nodelay |
| 2388 | && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) |
| 2389 | { |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 2390 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay"); |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 2391 | |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 2392 | if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, |
Igor Sysoev | cdf609a | 2006-04-17 19:55:41 +0000 | [diff] [blame] | 2393 | (const void *) &tcp_nodelay, sizeof(int)) |
| 2394 | == -1) |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 2395 | { |
| 2396 | ngx_connection_error(c, ngx_socket_errno, |
| 2397 | "setsockopt(TCP_NODELAY) failed"); |
| 2398 | ngx_http_close_connection(c); |
| 2399 | return; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 2400 | } |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 2401 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2402 | c->tcp_nodelay = NGX_TCP_NODELAY_SET; |
Igor Sysoev | 9cf7830 | 2003-06-04 17:28:33 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2405 | #if 0 |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 2406 | /* if ngx_http_request_t was freed then we need some other place */ |
Igor Sysoev | c83f683 | 2004-06-24 07:53:37 +0000 | [diff] [blame] | 2407 | r->http_state = NGX_HTTP_KEEPALIVE_STATE; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2408 | #endif |
Igor Sysoev | c83f683 | 2004-06-24 07:53:37 +0000 | [diff] [blame] | 2409 | |
Igor Sysoev | 4ddeff4 | 2007-03-19 13:20:15 +0000 | [diff] [blame] | 2410 | c->idle = 1; |
| 2411 | |
Igor Sysoev | b5faed2 | 2003-10-29 08:30:44 +0000 | [diff] [blame] | 2412 | if (rev->ready) { |
Igor Sysoev | 48242f4 | 2008-05-19 10:24:22 +0000 | [diff] [blame] | 2413 | ngx_post_event(rev, &ngx_posted_events); |
Igor Sysoev | b738757 | 2003-03-11 20:38:13 +0000 | [diff] [blame] | 2414 | } |
Igor Sysoev | 1e7ec9d | 2003-02-11 07:14:40 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2418 | static void |
| 2419 | ngx_http_keepalive_handler(ngx_event_t *rev) |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 2420 | { |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2421 | size_t size; |
| 2422 | ssize_t n; |
| 2423 | ngx_buf_t *b; |
| 2424 | ngx_connection_t *c; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 2425 | |
Igor Sysoev | 0ad2537 | 2004-07-16 06:33:35 +0000 | [diff] [blame] | 2426 | c = rev->data; |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 2427 | |
Igor Sysoev | e04084c | 2004-01-26 08:52:49 +0000 | [diff] [blame] | 2428 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler"); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2429 | |
Igor Sysoev | 4ddeff4 | 2007-03-19 13:20:15 +0000 | [diff] [blame] | 2430 | if (rev->timedout || c->close) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2431 | ngx_http_close_connection(c); |
| 2432 | return; |
Igor Sysoev | 90ace68 | 2003-03-12 17:32:22 +0000 | [diff] [blame] | 2433 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2434 | |
Igor Sysoev | f690604 | 2004-11-25 16:17:31 +0000 | [diff] [blame] | 2435 | #if (NGX_HAVE_KQUEUE) |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2436 | |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 2437 | if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
Igor Sysoev | cdc4630 | 2005-12-07 14:51:31 +0000 | [diff] [blame] | 2438 | if (rev->pending_eof) { |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2439 | c->log->handler = NULL; |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2440 | ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno, |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2441 | "kevent() reported that client %V closed " |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2442 | "keepalive connection", &c->addr_text); |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 2443 | #if (NGX_HTTP_SSL) |
| 2444 | if (c->ssl) { |
Igor Sysoev | 9fa5a82 | 2005-09-30 14:41:25 +0000 | [diff] [blame] | 2445 | c->ssl->no_send_shutdown = 1; |
Igor Sysoev | 42b12b3 | 2004-12-02 18:40:46 +0000 | [diff] [blame] | 2446 | } |
| 2447 | #endif |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2448 | ngx_http_close_connection(c); |
| 2449 | return; |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | #endif |
| 2454 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2455 | b = c->buffer; |
| 2456 | size = b->end - b->start; |
| 2457 | |
| 2458 | if (b->pos == NULL) { |
Igor Sysoev | c0edbcc | 2004-10-21 15:34:38 +0000 | [diff] [blame] | 2459 | |
| 2460 | /* |
| 2461 | * The c->buffer's memory was freed by ngx_http_set_keepalive(). |
| 2462 | * However, the c->buffer->start and c->buffer->end were not changed |
| 2463 | * to keep the buffer size. |
| 2464 | */ |
| 2465 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2466 | b->pos = ngx_palloc(c->pool, size); |
| 2467 | if (b->pos == NULL) { |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2468 | ngx_http_close_connection(c); |
| 2469 | return; |
| 2470 | } |
| 2471 | |
| 2472 | b->start = b->pos; |
| 2473 | b->last = b->pos; |
| 2474 | b->end = b->pos + size; |
| 2475 | } |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2476 | |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2477 | /* |
| 2478 | * MSIE closes a keepalive connection with RST flag |
| 2479 | * so we ignore ECONNRESET here. |
| 2480 | */ |
Igor Sysoev | b738757 | 2003-03-11 20:38:13 +0000 | [diff] [blame] | 2481 | |
Igor Sysoev | c7a2f68 | 2004-02-10 16:23:38 +0000 | [diff] [blame] | 2482 | c->log_error = NGX_ERROR_IGNORE_ECONNRESET; |
Igor Sysoev | b738757 | 2003-03-11 20:38:13 +0000 | [diff] [blame] | 2483 | ngx_set_socket_errno(0); |
Igor Sysoev | 74a5ddb | 2004-07-18 19:11:20 +0000 | [diff] [blame] | 2484 | |
Igor Sysoev | 236e045 | 2004-09-23 16:39:34 +0000 | [diff] [blame] | 2485 | n = c->recv(c, b->last, size); |
Igor Sysoev | c7a2f68 | 2004-02-10 16:23:38 +0000 | [diff] [blame] | 2486 | c->log_error = NGX_ERROR_INFO; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 2487 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2488 | if (n == NGX_AGAIN) { |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2489 | if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
Igor Sysoev | 6a12fc9 | 2004-12-06 14:45:08 +0000 | [diff] [blame] | 2490 | ngx_http_close_connection(c); |
| 2491 | } |
| 2492 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2493 | return; |
| 2494 | } |
| 2495 | |
| 2496 | if (n == NGX_ERROR) { |
| 2497 | ngx_http_close_connection(c); |
| 2498 | return; |
Igor Sysoev | 90ace68 | 2003-03-12 17:32:22 +0000 | [diff] [blame] | 2499 | } |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 2500 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2501 | c->log->handler = NULL; |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 2502 | |
| 2503 | if (n == 0) { |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 2504 | ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno, |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2505 | "client %V closed keepalive connection", &c->addr_text); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2506 | ngx_http_close_connection(c); |
| 2507 | return; |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2510 | b->last += n; |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2511 | |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 2512 | #if (NGX_STAT_STUB) |
| 2513 | ngx_atomic_fetch_add(ngx_stat_reading, 1); |
| 2514 | #endif |
| 2515 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2516 | c->log->handler = ngx_http_log_error; |
| 2517 | c->log->action = "reading client request line"; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 2518 | |
Igor Sysoev | 4ddeff4 | 2007-03-19 13:20:15 +0000 | [diff] [blame] | 2519 | c->idle = 0; |
| 2520 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2521 | ngx_http_init_request(rev); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 2524 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2525 | static void |
| 2526 | ngx_http_set_lingering_close(ngx_http_request_t *r) |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2527 | { |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2528 | ngx_event_t *rev, *wev; |
Igor Sysoev | b738757 | 2003-03-11 20:38:13 +0000 | [diff] [blame] | 2529 | ngx_connection_t *c; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2530 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 2531 | |
Igor Sysoev | 1e7ec9d | 2003-02-11 07:14:40 +0000 | [diff] [blame] | 2532 | c = r->connection; |
Igor Sysoev | 9b25d69 | 2003-01-26 21:08:14 +0000 | [diff] [blame] | 2533 | |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2534 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 2535 | |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2536 | rev = c->read; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2537 | rev->handler = ngx_http_lingering_close_handler; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 2538 | |
Igor Sysoev | 208eed2 | 2005-10-07 13:30:52 +0000 | [diff] [blame] | 2539 | r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000); |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2540 | ngx_add_timer(rev, clcf->lingering_timeout); |
Igor Sysoev | 9b25d69 | 2003-01-26 21:08:14 +0000 | [diff] [blame] | 2541 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2542 | if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2543 | ngx_http_close_request(r, 0); |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2544 | return; |
| 2545 | } |
| 2546 | |
| 2547 | wev = c->write; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2548 | wev->handler = ngx_http_empty_handler; |
Igor Sysoev | 0a280a3 | 2003-10-12 16:49:16 +0000 | [diff] [blame] | 2549 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2550 | if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) { |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2551 | if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) { |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2552 | ngx_http_close_request(r, 0); |
| 2553 | return; |
Igor Sysoev | b738757 | 2003-03-11 20:38:13 +0000 | [diff] [blame] | 2554 | } |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Igor Sysoev | b738757 | 2003-03-11 20:38:13 +0000 | [diff] [blame] | 2557 | if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) { |
Igor Sysoev | 4cec79f | 2004-04-28 06:14:50 +0000 | [diff] [blame] | 2558 | ngx_connection_error(c, ngx_socket_errno, |
| 2559 | ngx_shutdown_socket_n " failed"); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2560 | ngx_http_close_request(r, 0); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2561 | return; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Igor Sysoev | b5faed2 | 2003-10-29 08:30:44 +0000 | [diff] [blame] | 2564 | if (rev->ready) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2565 | ngx_http_lingering_close_handler(rev); |
Igor Sysoev | b738757 | 2003-03-11 20:38:13 +0000 | [diff] [blame] | 2566 | } |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2570 | static void |
| 2571 | ngx_http_lingering_close_handler(ngx_event_t *rev) |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 2572 | { |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 2573 | ssize_t n; |
| 2574 | ngx_msec_t timer; |
| 2575 | ngx_connection_t *c; |
| 2576 | ngx_http_request_t *r; |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2577 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2578 | u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE]; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2579 | |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2580 | c = rev->data; |
| 2581 | r = c->data; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 2582 | |
Igor Sysoev | e04084c | 2004-01-26 08:52:49 +0000 | [diff] [blame] | 2583 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, |
| 2584 | "http lingering close handler"); |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (rev->timedout) { |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2587 | c->timedout = 1; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2588 | ngx_http_close_request(r, 0); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2589 | return; |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 2590 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2591 | |
Igor Sysoev | 1d04b14 | 2007-11-15 14:26:36 +0000 | [diff] [blame] | 2592 | timer = (ngx_msec_t) (r->lingering_time - ngx_time()); |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 2593 | if (timer <= 0) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2594 | ngx_http_close_request(r, 0); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2595 | return; |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 2596 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2597 | |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 2598 | do { |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2599 | n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2600 | |
Igor Sysoev | e04084c | 2004-01-26 08:52:49 +0000 | [diff] [blame] | 2601 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2602 | |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 2603 | if (n == NGX_ERROR || n == 0) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2604 | ngx_http_close_request(r, 0); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2605 | return; |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | } while (rev->ready); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2609 | |
Igor Sysoev | c9aae14 | 2008-12-09 17:27:48 +0000 | [diff] [blame] | 2610 | if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
Igor Sysoev | b1dfe47 | 2004-12-21 12:30:30 +0000 | [diff] [blame] | 2611 | ngx_http_close_request(r, 0); |
Igor Sysoev | 6a12fc9 | 2004-12-06 14:45:08 +0000 | [diff] [blame] | 2612 | return; |
| 2613 | } |
| 2614 | |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2615 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 2616 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 2617 | timer *= 1000; |
Igor Sysoev | f7abd72 | 2004-09-23 06:32:00 +0000 | [diff] [blame] | 2618 | |
Igor Sysoev | 6253ca1 | 2003-05-27 12:18:54 +0000 | [diff] [blame] | 2619 | if (timer > clcf->lingering_timeout) { |
| 2620 | timer = clcf->lingering_timeout; |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 2621 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2622 | |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 2623 | ngx_add_timer(rev, timer); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 2626 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2627 | void |
| 2628 | ngx_http_empty_handler(ngx_event_t *wev) |
Igor Sysoev | 6a1cc90 | 2003-05-22 15:23:47 +0000 | [diff] [blame] | 2629 | { |
Igor Sysoev | e04084c | 2004-01-26 08:52:49 +0000 | [diff] [blame] | 2630 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler"); |
Igor Sysoev | 6a1cc90 | 2003-05-22 15:23:47 +0000 | [diff] [blame] | 2631 | |
| 2632 | return; |
| 2633 | } |
| 2634 | |
| 2635 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2636 | void |
| 2637 | ngx_http_request_empty_handler(ngx_http_request_t *r) |
| 2638 | { |
| 2639 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2640 | "http request empty handler"); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2641 | |
| 2642 | return; |
| 2643 | } |
| 2644 | |
| 2645 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2646 | ngx_int_t |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2647 | ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags) |
Igor Sysoev | 1342d9c | 2003-10-09 07:00:45 +0000 | [diff] [blame] | 2648 | { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 2649 | ngx_buf_t *b; |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 2650 | ngx_chain_t out; |
Igor Sysoev | 1342d9c | 2003-10-09 07:00:45 +0000 | [diff] [blame] | 2651 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2652 | b = ngx_calloc_buf(r->pool); |
| 2653 | if (b == NULL) { |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 2654 | return NGX_ERROR; |
| 2655 | } |
| 2656 | |
Igor Sysoev | c31a9bb | 2005-11-26 10:11:11 +0000 | [diff] [blame] | 2657 | if (flags & NGX_HTTP_LAST) { |
| 2658 | b->last_buf = 1; |
| 2659 | } |
| 2660 | |
| 2661 | if (flags & NGX_HTTP_FLUSH) { |
| 2662 | b->flush = 1; |
| 2663 | } |
| 2664 | |
Igor Sysoev | 369145c | 2004-05-28 15:49:23 +0000 | [diff] [blame] | 2665 | out.buf = b; |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 2666 | out.next = NULL; |
Igor Sysoev | 1342d9c | 2003-10-09 07:00:45 +0000 | [diff] [blame] | 2667 | |
Igor Sysoev | 419f9ac | 2003-10-21 16:49:56 +0000 | [diff] [blame] | 2668 | return ngx_http_output_filter(r, &out); |
Igor Sysoev | 1342d9c | 2003-10-09 07:00:45 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 2672 | static ngx_int_t |
| 2673 | ngx_http_post_action(ngx_http_request_t *r) |
| 2674 | { |
| 2675 | ngx_http_core_loc_conf_t *clcf; |
| 2676 | |
| 2677 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 2678 | |
| 2679 | if (clcf->post_action.data == NULL) { |
| 2680 | return NGX_DECLINED; |
| 2681 | } |
| 2682 | |
Igor Sysoev | 8fea885 | 2006-03-15 09:53:04 +0000 | [diff] [blame] | 2683 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 2684 | "post action: \"%V\"", &clcf->post_action); |
| 2685 | |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 2686 | r->http_version = NGX_HTTP_VERSION_9; |
| 2687 | r->header_only = 1; |
Igor Sysoev | 6d16e1e | 2006-04-05 13:40:54 +0000 | [diff] [blame] | 2688 | r->post_action = 1; |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 2689 | |
Igor Sysoev | 7ab4d9c | 2007-08-06 15:22:40 +0000 | [diff] [blame] | 2690 | r->read_event_handler = ngx_http_block_reading; |
Igor Sysoev | cf100a7 | 2006-12-11 08:31:54 +0000 | [diff] [blame] | 2691 | |
Igor Sysoev | ce30765 | 2008-02-15 12:46:40 +0000 | [diff] [blame] | 2692 | if (clcf->post_action.data[0] == '/') { |
| 2693 | ngx_http_internal_redirect(r, &clcf->post_action, NULL); |
| 2694 | |
| 2695 | } else { |
| 2696 | ngx_http_named_location(r, &clcf->post_action); |
| 2697 | } |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 2698 | |
| 2699 | return NGX_OK; |
| 2700 | } |
| 2701 | |
| 2702 | |
| 2703 | static void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2704 | ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error) |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2705 | { |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 2706 | ngx_connection_t *c; |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2707 | |
| 2708 | c = r->connection; |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2709 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 2710 | ngx_http_request_done(r->main, error); |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2711 | ngx_http_close_connection(c); |
| 2712 | } |
| 2713 | |
| 2714 | |
Igor Sysoev | c2807ec | 2006-02-16 15:26:46 +0000 | [diff] [blame] | 2715 | static void |
Igor Sysoev | 9ac946b | 2005-10-24 15:09:41 +0000 | [diff] [blame] | 2716 | ngx_http_request_done(ngx_http_request_t *r, ngx_int_t error) |
| 2717 | { |
Igor Sysoev | 2eec1e1 | 2008-09-27 15:08:02 +0000 | [diff] [blame] | 2718 | ngx_log_t *log; |
| 2719 | struct linger linger; |
| 2720 | ngx_http_cleanup_t *cln; |
| 2721 | ngx_http_log_ctx_t *ctx; |
| 2722 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2723 | |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 2724 | log = r->connection->log; |
| 2725 | |
| 2726 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request"); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2727 | |
| 2728 | if (r->pool == NULL) { |
Igor Sysoev | 90c0814 | 2005-07-25 09:41:38 +0000 | [diff] [blame] | 2729 | ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed"); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2730 | return; |
| 2731 | } |
| 2732 | |
Igor Sysoev | 4ecb4d7 | 2006-04-21 12:06:44 +0000 | [diff] [blame] | 2733 | for (cln = r->cleanup; cln; cln = cln->next) { |
| 2734 | if (cln->handler) { |
| 2735 | cln->handler(cln->data); |
| 2736 | } |
| 2737 | } |
| 2738 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame] | 2739 | #if (NGX_STAT_STUB) |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 2740 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame] | 2741 | if (r->stat_reading) { |
Igor Sysoev | 7845223 | 2005-10-12 13:50:36 +0000 | [diff] [blame] | 2742 | ngx_atomic_fetch_add(ngx_stat_reading, -1); |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | if (r->stat_writing) { |
Igor Sysoev | 7845223 | 2005-10-12 13:50:36 +0000 | [diff] [blame] | 2746 | ngx_atomic_fetch_add(ngx_stat_writing, -1); |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame] | 2747 | } |
Igor Sysoev | 055951d | 2005-10-21 19:12:18 +0000 | [diff] [blame] | 2748 | |
Igor Sysoev | 85cccfb | 2004-09-15 16:00:43 +0000 | [diff] [blame] | 2749 | #endif |
| 2750 | |
Igor Sysoev | 669e331 | 2003-12-22 09:40:48 +0000 | [diff] [blame] | 2751 | if (error && r->headers_out.status == 0) { |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2752 | r->headers_out.status = error; |
| 2753 | } |
| 2754 | |
Igor Sysoev | 504efff | 2008-06-30 15:32:57 +0000 | [diff] [blame] | 2755 | log->action = "logging request"; |
| 2756 | |
Igor Sysoev | 2eec1e1 | 2008-09-27 15:08:02 +0000 | [diff] [blame] | 2757 | ngx_http_log_request(r); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2758 | |
Igor Sysoev | 504efff | 2008-06-30 15:32:57 +0000 | [diff] [blame] | 2759 | log->action = "closing request"; |
| 2760 | |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 2761 | if (r->connection->timedout) { |
| 2762 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 2763 | |
| 2764 | if (clcf->reset_timedout_connection) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2765 | linger.l_onoff = 1; |
| 2766 | linger.l_linger = 0; |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 2767 | |
| 2768 | if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER, |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2769 | (const void *) &linger, sizeof(struct linger)) == -1) |
Igor Sysoev | 0ab91b9 | 2004-06-06 19:49:18 +0000 | [diff] [blame] | 2770 | { |
| 2771 | ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, |
| 2772 | "setsockopt(SO_LINGER) failed"); |
| 2773 | } |
| 2774 | } |
| 2775 | } |
| 2776 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2777 | /* the various request strings were allocated from r->pool */ |
Igor Sysoev | 865c150 | 2003-11-30 20:03:18 +0000 | [diff] [blame] | 2778 | ctx = log->data; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2779 | ctx->request = NULL; |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2780 | |
Igor Sysoev | c83f683 | 2004-06-24 07:53:37 +0000 | [diff] [blame] | 2781 | r->request_line.len = 0; |
| 2782 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2783 | r->connection->destroyed = 1; |
| 2784 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 2785 | ngx_destroy_pool(r->pool); |
| 2786 | } |
| 2787 | |
| 2788 | |
Igor Sysoev | 90c0814 | 2005-07-25 09:41:38 +0000 | [diff] [blame] | 2789 | static void |
Igor Sysoev | 2eec1e1 | 2008-09-27 15:08:02 +0000 | [diff] [blame] | 2790 | ngx_http_log_request(ngx_http_request_t *r) |
| 2791 | { |
| 2792 | ngx_uint_t i, n; |
| 2793 | ngx_http_handler_pt *log_handler; |
| 2794 | ngx_http_core_main_conf_t *cmcf; |
| 2795 | |
| 2796 | cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); |
| 2797 | |
| 2798 | log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts; |
| 2799 | n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts; |
| 2800 | |
| 2801 | for (i = 0; i < n; i++) { |
| 2802 | log_handler[i](r); |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | |
| 2807 | static void |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2808 | ngx_http_close_connection(ngx_connection_t *c) |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 2809 | { |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2810 | ngx_pool_t *pool; |
| 2811 | |
Igor Sysoev | e04084c | 2004-01-26 08:52:49 +0000 | [diff] [blame] | 2812 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 2813 | "close http connection: %d", c->fd); |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 2814 | |
Igor Sysoev | ceb9929 | 2005-09-06 16:09:32 +0000 | [diff] [blame] | 2815 | #if (NGX_HTTP_SSL) |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 2816 | |
| 2817 | if (c->ssl) { |
| 2818 | if (ngx_ssl_shutdown(c) == NGX_AGAIN) { |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 2819 | c->ssl->handler = ngx_http_close_connection; |
Igor Sysoev | 924bd79 | 2004-10-11 15:07:03 +0000 | [diff] [blame] | 2820 | return; |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | #endif |
| 2825 | |
Igor Sysoev | dc3b2a7 | 2004-09-14 19:39:54 +0000 | [diff] [blame] | 2826 | #if (NGX_STAT_STUB) |
Igor Sysoev | 7845223 | 2005-10-12 13:50:36 +0000 | [diff] [blame] | 2827 | ngx_atomic_fetch_add(ngx_stat_active, -1); |
Igor Sysoev | dc3b2a7 | 2004-09-14 19:39:54 +0000 | [diff] [blame] | 2828 | #endif |
| 2829 | |
Igor Sysoev | d3283ff | 2005-12-05 13:18:09 +0000 | [diff] [blame] | 2830 | c->destroyed = 1; |
| 2831 | |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2832 | pool = c->pool; |
| 2833 | |
Igor Sysoev | 59cf56c | 2004-09-07 15:29:22 +0000 | [diff] [blame] | 2834 | ngx_close_connection(c); |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 2835 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2836 | ngx_destroy_pool(pool); |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | |
Igor Sysoev | c157172 | 2005-03-19 12:38:37 +0000 | [diff] [blame] | 2840 | static u_char * |
| 2841 | ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len) |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 2842 | { |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2843 | u_char *p; |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 2844 | ngx_http_request_t *r; |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2845 | ngx_http_log_ctx_t *ctx; |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 2846 | |
Igor Sysoev | e5a222c | 2005-01-25 12:27:35 +0000 | [diff] [blame] | 2847 | if (log->action) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2848 | p = ngx_snprintf(buf, len, " while %s", log->action); |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2849 | len -= p - buf; |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 2850 | buf = p; |
Igor Sysoev | dc479b4 | 2003-03-20 16:09:44 +0000 | [diff] [blame] | 2851 | } |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2852 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2853 | ctx = log->data; |
| 2854 | |
Igor Sysoev | cc59567 | 2007-12-30 08:01:50 +0000 | [diff] [blame] | 2855 | p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2856 | len -= p - buf; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2857 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 2858 | r = ctx->request; |
| 2859 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2860 | if (r) { |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 2861 | return r->log_handler(r, ctx->current_request, p, len); |
Igor Sysoev | cc59567 | 2007-12-30 08:01:50 +0000 | [diff] [blame] | 2862 | |
| 2863 | } else { |
| 2864 | p = ngx_snprintf(p, len, ", server: %V", |
| 2865 | &ctx->connection->listening->addr_text); |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2868 | return p; |
| 2869 | } |
| 2870 | |
| 2871 | |
| 2872 | static u_char * |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 2873 | ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr, |
| 2874 | u_char *buf, size_t len) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 2875 | { |
Igor Sysoev | feee726 | 2007-12-30 08:15:27 +0000 | [diff] [blame] | 2876 | char *uri_separator; |
| 2877 | u_char *p; |
| 2878 | ngx_http_upstream_t *u; |
| 2879 | ngx_http_core_srv_conf_t *cscf; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2880 | |
Igor Sysoev | feee726 | 2007-12-30 08:15:27 +0000 | [diff] [blame] | 2881 | cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module); |
| 2882 | |
| 2883 | p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name); |
| 2884 | len -= p - buf; |
| 2885 | buf = p; |
Igor Sysoev | 4d656dc | 2005-03-22 16:02:46 +0000 | [diff] [blame] | 2886 | |
Igor Sysoev | a6b5957 | 2007-11-27 12:22:01 +0000 | [diff] [blame] | 2887 | if (r->request_line.data == NULL && r->request_start) { |
| 2888 | for (p = r->request_start; p < r->header_in->last; p++) { |
| 2889 | if (*p == CR || *p == LF) { |
| 2890 | break; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | r->request_line.len = p - r->request_start; |
| 2895 | r->request_line.data = r->request_start; |
| 2896 | } |
| 2897 | |
| 2898 | if (r->request_line.len) { |
| 2899 | p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line); |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 2900 | len -= p - buf; |
| 2901 | buf = p; |
Igor Sysoev | 1b73583 | 2004-11-11 14:07:14 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 2904 | if (r != sr) { |
| 2905 | p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri); |
| 2906 | len -= p - buf; |
| 2907 | buf = p; |
| 2908 | } |
| 2909 | |
| 2910 | u = sr->upstream; |
| 2911 | |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2912 | if (u && u->peer.name) { |
| 2913 | |
| 2914 | uri_separator = ""; |
| 2915 | |
| 2916 | #if (NGX_HAVE_UNIX_DOMAIN) |
| 2917 | if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) { |
| 2918 | uri_separator = ":"; |
| 2919 | } |
| 2920 | #endif |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 2921 | |
| 2922 | p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"", |
Igor Sysoev | 96dd8af | 2007-11-27 13:34:13 +0000 | [diff] [blame] | 2923 | &u->schema, u->peer.name, |
Igor Sysoev | 3d2fd18 | 2006-12-04 16:46:13 +0000 | [diff] [blame] | 2924 | uri_separator, &u->uri); |
Igor Sysoev | bb28b6d | 2006-07-11 13:20:19 +0000 | [diff] [blame] | 2925 | len -= p - buf; |
| 2926 | buf = p; |
| 2927 | } |
| 2928 | |
Igor Sysoev | c04deca | 2005-03-28 14:43:02 +0000 | [diff] [blame] | 2929 | if (r->headers_in.host) { |
| 2930 | p = ngx_snprintf(buf, len, ", host: \"%V\"", |
| 2931 | &r->headers_in.host->value); |
| 2932 | len -= p - buf; |
| 2933 | buf = p; |
| 2934 | } |
| 2935 | |
| 2936 | if (r->headers_in.referer) { |
| 2937 | p = ngx_snprintf(buf, len, ", referrer: \"%V\"", |
| 2938 | &r->headers_in.referer->value); |
| 2939 | buf = p; |
| 2940 | } |
| 2941 | |
| 2942 | return buf; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 2943 | } |