Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 1 | |
| 2 | #include <ngx_config.h> |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 3 | #include <ngx_core.h> |
| 4 | #include <ngx_string.h> |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 5 | #include <ngx_files.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 6 | #include <ngx_log.h> |
| 7 | #include <ngx_alloc.h> |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 8 | #include <ngx_array.h> |
| 9 | #include <ngx_table.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 10 | #include <ngx_hunk.h> |
| 11 | #include <ngx_connection.h> |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 12 | #include <ngx_inet.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 13 | #include <ngx_http.h> |
Igor Sysoev | e0268b9 | 2002-09-11 15:18:33 +0000 | [diff] [blame] | 14 | #include <ngx_http_config.h> |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 15 | #include <ngx_http_core_module.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 16 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 17 | /* STUB */ |
| 18 | #include <ngx_http_output_filter.h> |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 19 | int ngx_http_static_handler(ngx_http_request_t *r); |
Igor Sysoev | e0268b9 | 2002-09-11 15:18:33 +0000 | [diff] [blame] | 20 | int ngx_http_index_handler(ngx_http_request_t *r); |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 21 | int ngx_http_proxy_handler(ngx_http_request_t *r); |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 22 | /**/ |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 23 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 24 | int ngx_http_init_connection(ngx_connection_t *c); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 25 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 26 | static int ngx_http_init_request(ngx_event_t *ev); |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 27 | static int ngx_http_process_request_header(ngx_event_t *ev); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 28 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 29 | static int ngx_http_process_request_line(ngx_http_request_t *r); |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 30 | static int ngx_http_process_request_headers(ngx_http_request_t *r); |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 31 | static int ngx_http_process_request_header_line(ngx_http_request_t *r); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 32 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 33 | static int ngx_http_event_request_handler(ngx_http_request_t *r); |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 34 | |
| 35 | static int ngx_http_writer(ngx_event_t *ev); |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 36 | static int ngx_http_block_read(ngx_event_t *ev); |
| 37 | static int ngx_http_read_discarded_body(ngx_event_t *ev); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 38 | static int ngx_http_keepalive_handler(ngx_event_t *ev); |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 39 | static int ngx_http_set_lingering_close(ngx_http_request_t *r); |
| 40 | static int ngx_http_lingering_close_handler(ngx_event_t *ev); |
Igor Sysoev | 3a40d48 | 2002-09-12 14:42:29 +0000 | [diff] [blame] | 41 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 42 | #if 0 |
| 43 | int ngx_http_special_response(ngx_http_request_t *r, int error); |
| 44 | int ngx_http_redirect(ngx_http_request_t *r, int redirect); |
| 45 | int ngx_http_error(ngx_http_request_t *r, int error); |
| 46 | int ngx_http_close_request(ngx_http_request_t *r); |
| 47 | #endif |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 48 | |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 49 | static int ngx_http_close_connection(ngx_event_t *ev); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 50 | static size_t ngx_http_log_error(void *data, char *buf, size_t len); |
| 51 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 52 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 53 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 54 | static char *header_errors[] = { |
| 55 | "client %s sent invalid method", |
| 56 | "client %s sent invalid request", |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 57 | "client %s sent too long URI", |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 58 | "client %s sent HEAD method in HTTP/0.9 request" |
| 59 | }; |
| 60 | |
| 61 | |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 62 | static ngx_http_header_t headers_in[] = { |
| 63 | { 4, "Host", offsetof(ngx_http_headers_in_t, host) }, |
| 64 | { 10, "Connection", offsetof(ngx_http_headers_in_t, connection) }, |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 65 | { 17, "If-Modified-Since", |
| 66 | offsetof(ngx_http_headers_in_t,if_modified_since) }, |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 67 | |
| 68 | { 10, "User-Agent", offsetof(ngx_http_headers_in_t, user_agent) }, |
| 69 | |
| 70 | { 0, NULL, 0 } |
| 71 | }; |
| 72 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 73 | |
| 74 | int ngx_http_init_connection(ngx_connection_t *c) |
| 75 | { |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 76 | ngx_event_t *ev; |
| 77 | struct sockaddr *addr; |
| 78 | ngx_http_log_ctx_t *ctx; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 79 | |
| 80 | ev = c->read; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 81 | ev->event_handler = ngx_http_init_request; |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 82 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 83 | ngx_test_null(c->pool, |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 84 | ngx_create_pool(ngx_http_connection_pool_size, ev->log), |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 85 | NGX_ERROR); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 86 | |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 87 | ngx_test_null(c->requests, ngx_create_array(c->pool, 10, sizeof(char *)), |
| 88 | NGX_ERROR); |
| 89 | |
| 90 | ev->close_handler = ngx_http_close_connection; |
| 91 | c->write->close_handler = ngx_http_close_connection; |
| 92 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 93 | ngx_test_null(addr, ngx_palloc(c->pool, c->socklen), NGX_ERROR); |
| 94 | ngx_memcpy(addr, c->sockaddr, c->socklen); |
| 95 | c->sockaddr = addr; |
| 96 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 97 | ngx_test_null(c->addr_text.data, ngx_palloc(c->pool, c->addr_text_max_len), |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 98 | NGX_ERROR); |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 99 | |
Igor Sysoev | 5518aba | 2002-12-17 15:48:27 +0000 | [diff] [blame] | 100 | c->addr_text.len = ngx_inet_ntop(c->family, |
| 101 | (char *)c->sockaddr + c->addr, |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 102 | c->addr_text.data, c->addr_text_max_len); |
Igor Sysoev | 8809257 | 2002-12-19 07:08:55 +0000 | [diff] [blame] | 103 | if (c->addr_text.len == 0) |
Igor Sysoev | 5518aba | 2002-12-17 15:48:27 +0000 | [diff] [blame] | 104 | return NGX_ERROR; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 105 | |
| 106 | ngx_test_null(ctx, ngx_pcalloc(c->pool, sizeof(ngx_http_log_ctx_t)), |
| 107 | NGX_ERROR); |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 108 | ctx->client = c->addr_text.data; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 109 | ctx->action = "reading client request line"; |
| 110 | c->log->data = ctx; |
| 111 | c->log->handler = ngx_http_log_error; |
| 112 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 113 | #if (HAVE_DEFERRED_ACCEPT) |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 114 | if (ev->ready) { |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 115 | return ngx_http_init_request(ev); |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 116 | } else { |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 117 | #endif |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 118 | ngx_add_timer(ev, c->post_accept_timeout); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 119 | #if (USE_KQUEUE) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 120 | return ngx_add_event(ev, NGX_READ_EVENT, NGX_CLEAR_EVENT); |
| 121 | #else |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 122 | #if (HAVE_AIO_EVENT) |
| 123 | if (ngx_event_type == NGX_AIO_EVENT) |
| 124 | return ngx_http_init_request(ev); |
| 125 | else |
| 126 | #endif |
| 127 | #if (HAVE_CLEAR_EVENT) |
| 128 | if (ngx_event_type == NGX_KQUEUE_EVENT) |
| 129 | return ngx_add_event(ev, NGX_READ_EVENT, NGX_CLEAR_EVENT); |
| 130 | else |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 131 | #endif |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 132 | return ngx_add_event(ev, NGX_READ_EVENT, NGX_LEVEL_EVENT); |
| 133 | #endif /* USE_KQUEUE */ |
| 134 | #if (HAVE_DEFERRED_ACCEPT) |
| 135 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 136 | #endif |
| 137 | } |
| 138 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 139 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 140 | static int ngx_http_init_request(ngx_event_t *ev) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 141 | { |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 142 | ngx_connection_t *c; |
| 143 | ngx_http_request_t *r; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 144 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 145 | c = (ngx_connection_t *) ev->data; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 146 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 147 | ngx_test_null(r, ngx_pcalloc(c->pool, sizeof(ngx_http_request_t)), |
| 148 | NGX_ERROR); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 149 | |
| 150 | c->data = r; |
| 151 | r->connection = c; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 152 | r->file.fd = NGX_INVALID_FILE; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 153 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 154 | if (c->buffer == NULL) { |
| 155 | ngx_test_null(c->buffer, |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 156 | ngx_create_temp_hunk(c->pool, |
| 157 | ngx_http_client_header_buffer_size, |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 158 | 0, 0), |
| 159 | NGX_ERROR); |
| 160 | } else { |
| 161 | r->header_read = 1; |
| 162 | } |
| 163 | |
| 164 | r->header_in = c->buffer; |
| 165 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 166 | ngx_test_null(r->pool, ngx_create_pool(ngx_http_request_pool_size, ev->log), |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 167 | ngx_http_close_request(r)); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 168 | |
Igor Sysoev | 207ed5a | 2002-12-26 16:26:23 +0000 | [diff] [blame] | 169 | ngx_test_null(r->ctx, |
| 170 | ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module), |
Igor Sysoev | e0268b9 | 2002-09-11 15:18:33 +0000 | [diff] [blame] | 171 | ngx_http_close_request(r)); |
| 172 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 173 | r->headers_out.headers = ngx_create_table(r->pool, 10); |
| 174 | r->headers_out.content_length = -1; |
| 175 | r->headers_out.last_modified_time = -1; |
| 176 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 177 | ev->event_handler = ngx_http_process_request_header; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 178 | r->state_handler = ngx_http_process_request_line; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 179 | r->header_timeout = 1; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 180 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 181 | return ngx_http_process_request_header(ev); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 184 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 185 | static int ngx_http_process_request_header(ngx_event_t *ev) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 186 | { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 187 | int n, rc; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 188 | ngx_connection_t *c ; |
| 189 | ngx_http_request_t *r; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 190 | ngx_http_log_ctx_t *ctx; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 191 | |
| 192 | c = (ngx_connection_t *) ev->data; |
| 193 | r = (ngx_http_request_t *) c->data; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 194 | |
| 195 | ngx_log_debug(ev->log, "http process request"); |
| 196 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 197 | if (r->header_read) { |
| 198 | r->header_read = 0; |
| 199 | ngx_log_debug(ev->log, "http preread %d" _ |
| 200 | r->header_in->last.mem - r->header_in->pos.mem); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 201 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 202 | } else { |
| 203 | n = ngx_event_recv(c, r->header_in->last.mem, |
| 204 | r->header_in->end - r->header_in->last.mem); |
| 205 | |
| 206 | if (n == NGX_AGAIN) { |
| 207 | if (r->header_timeout) { |
| 208 | r->header_timeout = 0; |
| 209 | ngx_del_timer(ev); |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 210 | ngx_add_timer(ev, ngx_http_client_header_timeout); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 211 | } |
| 212 | return NGX_AGAIN; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 213 | } |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 214 | |
| 215 | if (n == NGX_ERROR) |
| 216 | return ngx_http_close_request(r); |
| 217 | |
| 218 | ngx_log_debug(ev->log, "http read %d" _ n); |
| 219 | |
| 220 | if (n == 0) { |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 221 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 222 | "client has prematurely closed connection"); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 223 | return ngx_http_close_request(r); |
| 224 | } |
| 225 | |
| 226 | r->header_in->last.mem += n; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 227 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 228 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 229 | /* state_handlers are called in following order: |
| 230 | ngx_http_process_request_line(r) |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 231 | ngx_http_process_request_headers(r) */ |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 232 | |
| 233 | do { |
| 234 | rc = (r->state_handler)(r); |
| 235 | |
| 236 | if (rc == NGX_ERROR) |
| 237 | return rc; |
| 238 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 239 | } while (rc == NGX_AGAIN && r->header_in->pos.mem < r->header_in->last.mem); |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 240 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 241 | if (r->header_timeout) { |
| 242 | r->header_timeout = 0; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 243 | ngx_del_timer(ev); |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 244 | ngx_add_timer(ev, ngx_http_client_header_timeout); |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 247 | if (rc == NGX_OK) |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 248 | return ngx_http_event_request_handler(r); |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 249 | else |
| 250 | return rc; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 253 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 254 | static int ngx_http_process_request_line(ngx_http_request_t *r) |
| 255 | { |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 256 | int rc, len; |
| 257 | char **request; |
| 258 | ngx_connection_t *c; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 259 | ngx_http_log_ctx_t *ctx; |
| 260 | |
| 261 | rc = ngx_read_http_request_line(r); |
| 262 | |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 263 | c = r->connection; |
| 264 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 265 | if (rc == NGX_OK) { |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 266 | r->uri.len = (r->args_start ? r->args_start - 1 : r->uri_end) |
| 267 | - r->uri_start; |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 268 | ngx_test_null(r->uri.data, ngx_palloc(r->pool, r->uri.len + 1), |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 269 | ngx_http_close_request(r)); |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 270 | ngx_cpystrn(r->uri.data, r->uri_start, r->uri.len + 1); |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 271 | |
Igor Sysoev | 2d0d909 | 2002-12-03 15:45:38 +0000 | [diff] [blame] | 272 | r->request_line.len = r->request_end - r->header_in->start; |
| 273 | ngx_test_null(r->request_line.data, |
| 274 | ngx_palloc(r->pool, r->request_line.len + 1), |
| 275 | ngx_http_close_request(r)); |
| 276 | ngx_cpystrn(r->request_line.data, r->header_in->start, |
| 277 | r->request_line.len + 1); |
| 278 | |
| 279 | /* TEMP */ |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 280 | ngx_test_null(request, ngx_push_array(c->requests), |
| 281 | ngx_http_close_request(r)); |
| 282 | |
| 283 | if (r->request_end) |
| 284 | len = r->request_end - r->header_in->start + 1; |
| 285 | else |
| 286 | len = 1; |
| 287 | c->requests_len += len; |
| 288 | ngx_test_null(*request, ngx_palloc(c->pool, len), |
| 289 | ngx_http_close_request(r)); |
| 290 | ngx_cpystrn(*request, r->header_in->start, len); |
| 291 | |
| 292 | ngx_log_debug(c->log, "REQ: '%s'" _ *request); |
Igor Sysoev | 2d0d909 | 2002-12-03 15:45:38 +0000 | [diff] [blame] | 293 | /* */ |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 294 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 295 | if (r->uri_ext) { |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 296 | r->exten.len = (r->args_start ? r->args_start - 1 : r->uri_end) |
| 297 | - r->uri_ext; |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 298 | ngx_test_null(r->exten.data, |
| 299 | ngx_palloc(r->pool, r->exten.len + 1), |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 300 | ngx_http_close_request(r)); |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 301 | ngx_cpystrn(r->exten.data, r->uri_ext, r->exten.len + 1); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Igor Sysoev | 295bb63 | 2002-12-23 18:22:18 +0000 | [diff] [blame] | 304 | #if 0 |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 305 | ngx_log_debug(r->connection->log, "HTTP: %d, %d, %s %s" _ |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 306 | r->method _ r->http_version _ |
| 307 | r->uri.data _ r->exten.data); |
Igor Sysoev | 295bb63 | 2002-12-23 18:22:18 +0000 | [diff] [blame] | 308 | #endif |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 309 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 310 | if (r->http_version == 9) |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 311 | return NGX_OK; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 312 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 313 | /* TODO: check too long URI - no space for header, compact buffer */ |
| 314 | |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 315 | r->headers_in.headers = ngx_create_table(r->pool, 10); |
| 316 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 317 | r->state_handler = ngx_http_process_request_headers; |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 318 | ctx = r->connection->log->data; |
| 319 | ctx->action = "reading client request headers"; |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 320 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 321 | return NGX_AGAIN; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 324 | if (r->header_in->last.mem >= r->header_in->end) { |
Igor Sysoev | ef259d1 | 2002-12-06 16:32:33 +0000 | [diff] [blame] | 325 | rc = NGX_HTTP_PARSE_TOO_LONG_URI; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 326 | |
| 327 | } else if (rc == NGX_AGAIN) { |
| 328 | return NGX_AGAIN; |
| 329 | } |
| 330 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 331 | ctx = r->connection->log->data; |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 332 | r->connection->log->handler = NULL; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 333 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 334 | header_errors[rc - NGX_HTTP_PARSE_INVALID_METHOD], |
| 335 | ctx->client); |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 336 | r->connection->log->handler = ngx_http_log_error; |
| 337 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 338 | return ngx_http_error(r, (rc == NGX_HTTP_PARSE_TOO_LONG_URI) ? |
| 339 | NGX_HTTP_REQUEST_URI_TOO_LARGE: |
| 340 | NGX_HTTP_BAD_REQUEST); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 343 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 344 | static int ngx_http_process_request_headers(ngx_http_request_t *r) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 345 | { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 346 | int rc; |
| 347 | ngx_http_log_ctx_t *ctx; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 348 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 349 | for ( ;; ) { |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 350 | rc = ngx_read_http_header_line(r, r->header_in); |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 351 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 352 | /* TODO: check too long header, compact buffer */ |
| 353 | |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 354 | if (rc == NGX_OK) { |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 355 | if (ngx_http_process_request_header_line(r) == NGX_ERROR) |
| 356 | return ngx_http_error(r, NGX_HTTP_BAD_REQUEST); |
| 357 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 358 | } else if (rc == NGX_HTTP_PARSE_HEADER_DONE) { |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 359 | ngx_log_debug(r->connection->log, "HTTP header done"); |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 360 | |
| 361 | if (r->http_version > NGX_HTTP_VERSION_10 |
| 362 | && r->headers_in.host == NULL) |
| 363 | { |
| 364 | return ngx_http_error(r, NGX_HTTP_BAD_REQUEST); |
| 365 | } else { |
| 366 | return NGX_OK; |
| 367 | } |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 368 | |
| 369 | } else if (rc == NGX_AGAIN) { |
| 370 | return NGX_AGAIN; |
| 371 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 372 | } else if (rc == NGX_HTTP_PARSE_INVALID_HEADER) { |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 373 | ctx = r->connection->log->data; |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 374 | r->connection->log->handler = NULL; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 375 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 376 | "client %s sent invalid header", ctx->client); |
| 377 | r->connection->log->handler = ngx_http_log_error; |
| 378 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 379 | return ngx_http_error(r, NGX_HTTP_BAD_REQUEST); |
Igor Sysoev | 016b852 | 2002-08-29 16:59:54 +0000 | [diff] [blame] | 380 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 381 | } |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 382 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 383 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 384 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 385 | static int ngx_http_process_request_header_line(ngx_http_request_t *r) |
| 386 | { |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 387 | int i; |
| 388 | ngx_table_elt_t *h; |
| 389 | |
Igor Sysoev | 2d0d909 | 2002-12-03 15:45:38 +0000 | [diff] [blame] | 390 | ngx_test_null(h, ngx_push_table(r->headers_in.headers), NGX_ERROR); |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 391 | |
| 392 | h->key.len = r->header_name_end - r->header_name_start; |
| 393 | ngx_test_null(h->key.data, ngx_palloc(r->pool, h->key.len + 1), NGX_ERROR); |
| 394 | ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1); |
| 395 | |
| 396 | h->value.len = r->header_end - r->header_start; |
| 397 | ngx_test_null(h->value.data, ngx_palloc(r->pool, h->value.len + 1), |
| 398 | NGX_ERROR); |
| 399 | ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1); |
| 400 | |
| 401 | for (i = 0; headers_in[i].len != 0; i++) { |
| 402 | if (headers_in[i].len == h->key.len) { |
| 403 | if (strcasecmp(headers_in[i].data, h->key.data) == 0) { |
| 404 | *((ngx_table_elt_t **) |
| 405 | ((char *) &r->headers_in + headers_in[i].offset)) = h; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 410 | ngx_log_debug(r->connection->log, "HTTP header: '%s: %s'" _ |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 411 | h->key.data _ h->value.data); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 412 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 413 | return NGX_OK; |
| 414 | } |
| 415 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 416 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 417 | static int ngx_http_event_request_handler(ngx_http_request_t *r) |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 418 | { |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 419 | int rc; |
Igor Sysoev | 96f8377 | 2002-09-07 10:14:25 +0000 | [diff] [blame] | 420 | ngx_msec_t timeout; |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 421 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 422 | ngx_del_timer(r->connection->read); |
| 423 | r->header_timeout = 0; |
| 424 | |
| 425 | r->state_handler = NULL; |
| 426 | r->connection->read->event_handler = ngx_http_block_read; |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 427 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 428 | rc = ngx_http_handler(r); |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 429 | |
Igor Sysoev | ef259d1 | 2002-12-06 16:32:33 +0000 | [diff] [blame] | 430 | /* handler is still busy */ |
Igor Sysoev | 6ed07e4 | 2002-12-05 16:21:24 +0000 | [diff] [blame] | 431 | if (rc == NGX_WAITING) |
| 432 | return rc; |
| 433 | |
Igor Sysoev | ef259d1 | 2002-12-06 16:32:33 +0000 | [diff] [blame] | 434 | /* handler has done its work but transfer is not completed */ |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 435 | if (rc == NGX_AGAIN) { |
| 436 | #if (HAVE_CLEAR_EVENT) |
| 437 | if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, |
| 438 | NGX_CLEAR_EVENT) == NGX_ERROR) { |
| 439 | #else |
| 440 | if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, |
| 441 | NGX_ONESHOT_EVENT) == NGX_ERROR) { |
| 442 | #endif |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 443 | return ngx_http_close_request(r); |
| 444 | } |
| 445 | |
Igor Sysoev | 96f8377 | 2002-09-07 10:14:25 +0000 | [diff] [blame] | 446 | if (r->connection->sent > 0) { |
| 447 | ngx_log_debug(r->connection->log, "sent: " QD_FMT _ |
| 448 | r->connection->sent); |
| 449 | timeout = (ngx_msec_t) (r->connection->sent * 10); |
| 450 | ngx_log_debug(r->connection->log, "timeout: %d" _ timeout); |
| 451 | ngx_add_timer(r->connection->write, timeout); |
| 452 | |
| 453 | } else { |
| 454 | ngx_add_timer(r->connection->write, 10000); |
| 455 | } |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 456 | |
| 457 | r->connection->write->event_handler = ngx_http_writer; |
| 458 | return rc; |
| 459 | } |
| 460 | |
| 461 | if (rc == NGX_ERROR) { |
| 462 | /* log http request */ |
| 463 | return ngx_http_close_request(r); |
| 464 | } |
| 465 | |
| 466 | if (rc >= NGX_HTTP_SPECIAL_RESPONSE) |
| 467 | return ngx_http_special_response(r, rc); |
| 468 | |
| 469 | /* rc == NGX_OK */ |
| 470 | |
Igor Sysoev | 3a40d48 | 2002-09-12 14:42:29 +0000 | [diff] [blame] | 471 | if (!r->keepalive) { |
| 472 | if (r->lingering_close) { |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 473 | return ngx_http_set_lingering_close(r); |
| 474 | |
Igor Sysoev | 3a40d48 | 2002-09-12 14:42:29 +0000 | [diff] [blame] | 475 | } else { |
| 476 | return ngx_http_close_request(r); |
| 477 | } |
| 478 | } |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 479 | |
| 480 | /* keepalive */ |
| 481 | |
| 482 | ngx_http_close_request(r); |
| 483 | r->connection->buffer->pos.mem = r->connection->buffer->last.mem |
| 484 | = r->connection->buffer->start; |
| 485 | r->connection->read->event_handler = ngx_http_keepalive_handler; |
| 486 | } |
| 487 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 488 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 489 | static int ngx_http_writer(ngx_event_t *ev) |
| 490 | { |
| 491 | int rc; |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 492 | ngx_msec_t timeout; |
| 493 | ngx_connection_t *c; |
| 494 | ngx_http_request_t *r; |
| 495 | ngx_http_core_loc_conf_t *conf; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 496 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 497 | c = (ngx_connection_t *) ev->data; |
| 498 | r = (ngx_http_request_t *) c->data; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 499 | |
Igor Sysoev | 96f8377 | 2002-09-07 10:14:25 +0000 | [diff] [blame] | 500 | c->sent = 0; |
| 501 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 502 | rc = ngx_http_output_filter(r, NULL); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 503 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 504 | ngx_log_debug(ev->log, "output filter in writer: %d" _ rc); |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 505 | |
| 506 | if (rc == NGX_AGAIN) { |
| 507 | |
| 508 | if (c->sent > 0) { |
Igor Sysoev | a0bb31f | 2002-12-02 16:09:40 +0000 | [diff] [blame] | 509 | conf = (ngx_http_core_loc_conf_t *) |
Igor Sysoev | 207ed5a | 2002-12-26 16:26:23 +0000 | [diff] [blame] | 510 | ngx_http_get_module_loc_conf(r->main ? r->main : r, |
| 511 | ngx_http_core_module_ctx); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 512 | |
| 513 | timeout = (ngx_msec_t) (c->sent * conf->send_timeout); |
| 514 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 515 | ngx_log_debug(ev->log, "sent: " QD_FMT _ c->sent); |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 516 | ngx_log_debug(ev->log, "timeout: %d" _ timeout); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 517 | |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 518 | ngx_del_timer(ev); |
| 519 | ngx_add_timer(ev, timeout); |
| 520 | } |
| 521 | |
| 522 | if (ev->oneshot) |
| 523 | if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, |
| 524 | NGX_ONESHOT_EVENT) == NGX_ERROR) { |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 525 | return ngx_http_close_request(r); |
| 526 | } |
| 527 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 528 | return rc; |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 529 | } |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 530 | |
| 531 | if (rc == NGX_ERROR) |
| 532 | return rc; |
| 533 | |
| 534 | /* rc == NGX_OK */ |
| 535 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 536 | ngx_log_debug(ev->log, "http writer done"); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 537 | |
Igor Sysoev | 3a40d48 | 2002-09-12 14:42:29 +0000 | [diff] [blame] | 538 | if (!r->keepalive) { |
| 539 | if (r->lingering_close) { |
Igor Sysoev | 3a17f24 | 2002-12-24 17:30:59 +0000 | [diff] [blame] | 540 | return ngx_http_set_lingering_close(r); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 541 | |
Igor Sysoev | 3a40d48 | 2002-09-12 14:42:29 +0000 | [diff] [blame] | 542 | } else { |
| 543 | return ngx_http_close_request(r); |
| 544 | } |
| 545 | } |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 546 | |
Igor Sysoev | 3a40d48 | 2002-09-12 14:42:29 +0000 | [diff] [blame] | 547 | /* keepalive */ |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 548 | |
Igor Sysoev | 3a40d48 | 2002-09-12 14:42:29 +0000 | [diff] [blame] | 549 | ngx_http_close_request(r); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 550 | c->buffer->pos.mem = c->buffer->last.mem = c->buffer->start; |
| 551 | c->read->event_handler = ngx_http_keepalive_handler; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 554 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 555 | static int ngx_http_block_read(ngx_event_t *ev) |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 556 | { |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 557 | ngx_log_debug(ev->log, "http read blocked"); |
| 558 | |
| 559 | ev->blocked = 1; |
| 560 | return ngx_del_event(ev, NGX_READ_EVENT, 0); |
| 561 | } |
| 562 | |
| 563 | |
| 564 | int ngx_http_discard_body(ngx_http_request_t *r) |
| 565 | { |
| 566 | ngx_log_debug(r->connection->log, "set discard body"); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 567 | |
| 568 | ngx_del_timer(r->connection->read); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 569 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 570 | if (r->client_content_length) |
| 571 | r->connection->read->event_handler = ngx_http_read_discarded_body; |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 572 | |
| 573 | return NGX_OK; |
| 574 | } |
| 575 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 576 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 577 | static int ngx_http_read_discarded_body(ngx_event_t *ev) |
| 578 | { |
| 579 | size_t size; |
| 580 | ssize_t n; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 581 | ngx_connection_t *c; |
| 582 | ngx_http_request_t *r; |
| 583 | ngx_http_core_loc_conf_t *lcf; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 584 | |
| 585 | ngx_log_debug(ev->log, "http read discarded body"); |
| 586 | |
| 587 | if (ev->timedout) |
| 588 | return NGX_ERROR; |
| 589 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 590 | c = (ngx_connection_t *) ev->data; |
| 591 | r = (ngx_http_request_t *) c->data; |
| 592 | |
| 593 | lcf = (ngx_http_core_loc_conf_t *) |
| 594 | ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); |
| 595 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 596 | if (r->discarded_buffer == NULL) |
| 597 | ngx_test_null(r->discarded_buffer, |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 598 | ngx_palloc(r->pool, lcf->discarded_buffer_size), |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 599 | NGX_ERROR); |
| 600 | |
| 601 | size = r->client_content_length; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 602 | if (size > lcf->discarded_buffer_size) |
| 603 | size = lcf->discarded_buffer_size; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 604 | |
| 605 | n = ngx_event_recv(c, r->discarded_buffer, size); |
| 606 | if (n == NGX_ERROR) |
| 607 | return NGX_ERROR; |
| 608 | |
| 609 | if (n == NGX_AGAIN) |
| 610 | return NGX_OK; |
| 611 | |
| 612 | r->client_content_length -= n; |
| 613 | /* XXX: what if r->client_content_length == 0 ? */ |
| 614 | return NGX_OK; |
| 615 | } |
| 616 | |
| 617 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 618 | static int ngx_http_keepalive_handler(ngx_event_t *ev) |
| 619 | { |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 620 | ssize_t n; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 621 | ngx_connection_t *c; |
| 622 | ngx_http_log_ctx_t *ctx; |
| 623 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 624 | c = (ngx_connection_t *) ev->data; |
| 625 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 626 | ngx_log_debug(ev->log, "http keepalive handler"); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 627 | |
| 628 | if (ev->timedout) |
| 629 | return NGX_DONE; |
| 630 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 631 | n = ngx_event_recv(c, c->buffer->last.mem, |
| 632 | c->buffer->end - c->buffer->last.mem); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 633 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 634 | if (n == NGX_AGAIN || n == NGX_ERROR) |
| 635 | return n; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 636 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 637 | ctx = (ngx_http_log_ctx_t *) ev->log->data; |
| 638 | ev->log->handler = NULL; |
| 639 | |
| 640 | if (n == 0) { |
| 641 | ngx_log_error(NGX_LOG_INFO, ev->log, 0, |
| 642 | "client %s closed keepalive connection", ctx->client); |
| 643 | return NGX_DONE; |
| 644 | } |
| 645 | |
| 646 | c->buffer->last.mem += n; |
| 647 | ev->log->handler = ngx_http_log_error; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 648 | ctx->action = "reading client request line"; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 649 | |
| 650 | return ngx_http_init_request(ev); |
| 651 | } |
| 652 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 653 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 654 | static int ngx_http_set_lingering_close(ngx_http_request_t *r) |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 655 | { |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 656 | ngx_http_core_loc_conf_t *lcf; |
| 657 | |
| 658 | lcf = (ngx_http_core_loc_conf_t *) |
| 659 | ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); |
| 660 | |
| 661 | r->lingering_time = ngx_time() + lcf->lingering_time; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 662 | r->connection->read->event_handler = ngx_http_lingering_close_handler; |
| 663 | |
| 664 | ngx_del_timer(r->connection->read); |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 665 | ngx_add_timer(r->connection->read, lcf->lingering_timeout); |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 666 | |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 667 | if (r->connection->read->blocked) { |
| 668 | if (ngx_add_event(r->connection->read, NGX_READ_EVENT, |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 669 | #if (HAVE_CLEAR_EVENT) |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 670 | NGX_CLEAR_EVENT) == NGX_ERROR) |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 671 | #else |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 672 | NGX_ONESHOT_EVENT) == NGX_ERROR) |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 673 | #endif |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 674 | { |
| 675 | return ngx_http_close_request(r); |
| 676 | } |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | if (ngx_shutdown_socket(r->connection->fd, NGX_WRITE_SHUTDOWN) == -1) |
| 680 | { |
| 681 | ngx_log_error(NGX_LOG_ERR, r->connection->log, ngx_socket_errno, |
| 682 | ngx_shutdown_socket_n " failed"); |
| 683 | return ngx_http_close_request(r); |
| 684 | } |
| 685 | |
| 686 | return NGX_OK; |
| 687 | } |
| 688 | |
| 689 | |
| 690 | static int ngx_http_lingering_close_handler(ngx_event_t *ev) |
| 691 | { |
| 692 | ssize_t n; |
| 693 | ngx_msec_t timer; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 694 | ngx_connection_t *c; |
| 695 | ngx_http_request_t *r; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 696 | ngx_http_core_loc_conf_t *lcf; |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 697 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 698 | ngx_log_debug(ev->log, "http lingering close handler"); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 699 | |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 700 | if (ev->timedout) { |
| 701 | return ngx_http_close_request(r); |
| 702 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 703 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 704 | c = (ngx_connection_t *) ev->data; |
| 705 | r = (ngx_http_request_t *) c->data; |
| 706 | |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 707 | timer = r->lingering_time - ngx_time(); |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 708 | if (timer <= 0) { |
| 709 | return ngx_http_close_request(r); |
| 710 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 711 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 712 | lcf = (ngx_http_core_loc_conf_t *) |
| 713 | ngx_http_get_module_loc_conf(r, ngx_http_core_module_ctx); |
| 714 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 715 | if (r->discarded_buffer == NULL) { |
| 716 | if (r->header_in->end - r->header_in->last.mem |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 717 | >= lcf->discarded_buffer_size) { |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 718 | r->discarded_buffer = r->header_in->last.mem; |
| 719 | |
| 720 | } else { |
| 721 | ngx_test_null(r->discarded_buffer, |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 722 | ngx_palloc(c->pool, lcf->discarded_buffer_size), |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 723 | ngx_http_close_request(r)); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 726 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 727 | n = ngx_event_recv(c, r->discarded_buffer, lcf->discarded_buffer_size); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 728 | |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 729 | ngx_log_debug(ev->log, "lingering read: %d" _ n); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 730 | |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 731 | if (n == NGX_ERROR || n == 0) { |
| 732 | return ngx_http_close_request(r); |
| 733 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 734 | |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 735 | timer *= 1000; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame^] | 736 | if (timer > lcf->lingering_timeout) { |
| 737 | timer = lcf->lingering_timeout; |
Igor Sysoev | 0d2bda5 | 2002-12-24 07:09:57 +0000 | [diff] [blame] | 738 | } |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 739 | |
| 740 | ngx_del_timer(ev); |
Igor Sysoev | 682bf8e | 2002-09-16 15:01:44 +0000 | [diff] [blame] | 741 | ngx_add_timer(ev, timer); |
Igor Sysoev | 1af7c82 | 2002-09-13 14:47:42 +0000 | [diff] [blame] | 742 | |
| 743 | return NGX_OK; |
| 744 | } |
| 745 | |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 746 | |
Igor Sysoev | 2ba1ee0 | 2002-10-04 17:58:04 +0000 | [diff] [blame] | 747 | static int ngx_http_close_connection(ngx_event_t *ev) |
| 748 | { |
| 749 | int i, len; |
| 750 | char **requests, *requests_line, *prev, *new; |
| 751 | ngx_connection_t *c = (ngx_connection_t *) ev->data; |
| 752 | |
| 753 | if (c->requests->nelts > 1) { |
| 754 | len = c->requests_len + c->requests->nelts * 2 - 1; |
| 755 | |
| 756 | ngx_test_null(requests_line, ngx_palloc(c->pool, len), |
| 757 | ngx_event_close_connection(ev)); |
| 758 | |
| 759 | requests = (char **) c->requests->elts; |
| 760 | prev = requests_line; |
| 761 | new = ngx_cpystrn(prev, requests[0], len); |
| 762 | len -= new - prev; |
| 763 | prev = new; |
| 764 | |
| 765 | for (i = 1; i < c->requests->nelts; i++) { |
| 766 | new = ngx_cpystrn(prev, ", ", len); |
| 767 | new = ngx_cpystrn(new, requests[i], len); |
| 768 | len -= new - prev; |
| 769 | prev = new; |
| 770 | } |
| 771 | |
| 772 | } else { |
| 773 | requests_line = * (char **) c->requests->elts; |
| 774 | } |
| 775 | |
| 776 | ngx_log_error(NGX_LOG_INFO, c->log, 0, |
| 777 | "REQUESTS: %d, '%s'", c->requests->nelts, requests_line); |
| 778 | |
| 779 | return ngx_event_close_connection(ev); |
| 780 | } |
| 781 | |
| 782 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 783 | static size_t ngx_http_log_error(void *data, char *buf, size_t len) |
| 784 | { |
| 785 | ngx_http_log_ctx_t *ctx = (ngx_http_log_ctx_t *) data; |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 786 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 787 | if (ctx->url) |
| 788 | return ngx_snprintf(buf, len, " while %s, client: %s, URL: %s", |
| 789 | ctx->action, ctx->client, ctx->url); |
| 790 | else |
| 791 | return ngx_snprintf(buf, len, " while %s, client: %s", |
| 792 | ctx->action, ctx->client); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 793 | } |