Mercurial > hg > nginx-quic
diff src/http/ngx_http_event.c @ 24:77c7629a2627
nginx-0.0.1-2002-12-10-21:05:12 import
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 10 Dec 2002 18:05:12 +0000 |
parents | f540a63026c9 |
children | 53cb81681040 |
line wrap: on
line diff
--- a/src/http/ngx_http_event.c +++ b/src/http/ngx_http_event.c @@ -22,22 +22,24 @@ int ngx_http_static_handler(ngx_http_request_t *r); int ngx_http_index_handler(ngx_http_request_t *r); int ngx_http_proxy_handler(ngx_http_request_t *r); -/* */ +/**/ int ngx_http_init_connection(ngx_connection_t *c); static int ngx_http_init_request(ngx_event_t *ev); -static int ngx_http_process_request(ngx_event_t *ev); +static int ngx_http_process_request_header(ngx_event_t *ev); static int ngx_http_process_request_line(ngx_http_request_t *r); -static int ngx_http_process_request_header(ngx_http_request_t *r); +static int ngx_http_process_request_headers(ngx_http_request_t *r); static int ngx_http_process_request_header_line(ngx_http_request_t *r); +static int ngx_http_event_handler(ngx_http_request_t *r); static int ngx_http_block_read(ngx_event_t *ev); + + static int ngx_http_read_discarded_body(ngx_event_t *ev); -static int ngx_http_event_handler(ngx_http_request_t *r); -static int ngx_http_handler(ngx_http_request_t *r); +int ngx_http_handler(ngx_http_request_t *r); static int ngx_http_set_default_handler(ngx_http_request_t *r); static int ngx_http_writer(ngx_event_t *ev); @@ -45,11 +47,13 @@ static int ngx_http_set_lingering_close( static int ngx_http_keepalive_handler(ngx_event_t *ev); static int ngx_http_lingering_close(ngx_event_t *ev); -static int ngx_http_special_response(ngx_http_request_t *r, int error); -static int ngx_http_redirect(ngx_http_request_t *r, int redirect); -static int ngx_http_error(ngx_http_request_t *r, int error); +#if 0 +int ngx_http_special_response(ngx_http_request_t *r, int error); +int ngx_http_redirect(ngx_http_request_t *r, int redirect); +int ngx_http_error(ngx_http_request_t *r, int error); +int ngx_http_close_request(ngx_http_request_t *r); +#endif -static int ngx_http_close_request(ngx_http_request_t *r); static int ngx_http_close_connection(ngx_event_t *ev); static size_t ngx_http_log_error(void *data, char *buf, size_t len); @@ -99,19 +103,22 @@ int ngx_http_init_connection(ngx_connect ngx_memcpy(addr, c->sockaddr, c->socklen); c->sockaddr = addr; - ngx_test_null(c->addr_text, ngx_palloc(c->pool, c->addr_textlen), + ngx_test_null(c->addr_text.data, ngx_palloc(c->pool, c->addr_text.len), NGX_ERROR); + + /* STUB: should be ngx_inet_ntop() */ #if (WIN32) - c->addr_text = inet_ntoa((struct in_addr *) + c->addr_text.data = inet_ntoa((struct in_addr *) ((char *)c->sockaddr + c->addr)); #else inet_ntop(c->family, (char *)c->sockaddr + c->addr, - c->addr_text, c->addr_textlen); + c->addr_text.data, c->addr_text.len); #endif + /**/ ngx_test_null(ctx, ngx_pcalloc(c->pool, sizeof(ngx_http_log_ctx_t)), NGX_ERROR); - ctx->client = c->addr_text; + ctx->client = c->addr_text.data; ctx->action = "reading client request line"; c->log->data = ctx; c->log->handler = ngx_http_log_error; @@ -121,6 +128,7 @@ int ngx_http_init_connection(ngx_connect return ngx_http_init_request(ev); } else { #endif + /* STUB: post_accept_timeout should be in http_conf */ ngx_add_timer(ev, c->post_accept_timeout); #if (USE_KQUEUE) return ngx_add_event(ev, NGX_READ_EVENT, NGX_CLEAR_EVENT); @@ -159,8 +167,10 @@ static int ngx_http_init_request(ngx_eve r->connection = c; r->server = srv; + /* STUB */ r->srv_conf = ngx_srv_conf; r->loc_conf = ngx_loc_conf; + /**/ if (c->buffer == NULL) { ngx_test_null(c->buffer, @@ -179,16 +189,15 @@ static int ngx_http_init_request(ngx_eve ngx_test_null(r->ctx, ngx_pcalloc(r->pool, sizeof(void *) * ngx_max_module), ngx_http_close_request(r)); - ev->event_handler = ngx_http_process_request; + ev->event_handler = ngx_http_process_request_header; r->state_handler = ngx_http_process_request_line; - r->process_header = 1; r->header_timeout = 1; - return ngx_http_process_request(ev); + return ngx_http_process_request_header(ev); } -static int ngx_http_process_request(ngx_event_t *ev) +static int ngx_http_process_request_header(ngx_event_t *ev) { int n, rc; ngx_connection_t *c ; @@ -213,6 +222,8 @@ static int ngx_http_process_request(ngx_ if (r->header_timeout) { r->header_timeout = 0; ngx_del_timer(ev); + /* STUB: r->server->header_timeout + OR r->srv_conf->header_timeout ? */ ngx_add_timer(ev, r->server->header_timeout); } return NGX_AGAIN; @@ -224,9 +235,8 @@ static int ngx_http_process_request(ngx_ ngx_log_debug(ev->log, "http read %d" _ n); if (n == 0) { - if (c->unexpected_eof) - ngx_log_error(NGX_LOG_INFO, c->log, 0, - "client prematurely closed connection"); + ngx_log_error(NGX_LOG_INFO, c->log, 0, + "client has prematurely closed connection"); return ngx_http_close_request(r); } @@ -235,7 +245,7 @@ static int ngx_http_process_request(ngx_ /* state_handlers are called in following order: ngx_http_process_request_line(r) - ngx_http_process_request_header(r) */ + ngx_http_process_request_headers(r) */ do { rc = (r->state_handler)(r); @@ -243,10 +253,7 @@ static int ngx_http_process_request(ngx_ if (rc == NGX_ERROR) return rc; - /* rc == NGX_OK || rc == NGX_AGAIN */ - - } while (r->process_header - && r->header_in->pos.mem < r->header_in->last.mem); + } while (rc == NGX_AGAIN && r->header_in->pos.mem < r->header_in->last.mem); if (r->header_timeout) { r->header_timeout = 0; @@ -254,7 +261,10 @@ static int ngx_http_process_request(ngx_ ngx_add_timer(ev, r->server->header_timeout); } - return rc; + if (rc == NGX_OK) + return ngx_http_event_handler(r); + else + return rc; } @@ -270,10 +280,10 @@ static int ngx_http_process_request_line c = r->connection; if (rc == NGX_OK) { - len = r->uri_end - r->uri_start + 1; - ngx_test_null(r->uri, ngx_palloc(r->pool, len), + r->uri.len = r->uri_end - r->uri_start; + ngx_test_null(r->uri.data, ngx_palloc(r->pool, r->uri.len + 1), ngx_http_close_request(r)); - ngx_cpystrn(r->uri, r->uri_start, len); + ngx_cpystrn(r->uri.data, r->uri_start, r->uri.len + 1); r->request_line.len = r->request_end - r->header_in->start; ngx_test_null(r->request_line.data, @@ -299,27 +309,31 @@ static int ngx_http_process_request_line /* */ if (r->uri_ext) { - ngx_test_null(r->exten, - ngx_palloc(r->pool, r->uri_end - r->uri_ext + 1), + r->exten.len = r->uri_end - r->uri_ext; + ngx_test_null(r->exten.data, + ngx_palloc(r->pool, r->exten.len + 1), ngx_http_close_request(r)); - ngx_cpystrn(r->exten, r->uri_ext, r->uri_end - r->uri_ext + 1); + ngx_cpystrn(r->exten.data, r->uri_ext, r->exten.len + 1); } ngx_log_debug(r->connection->log, "HTTP: %d, %d, %s %s" _ - r->method _ r->http_version _ r->uri _ r->exten); + r->method _ r->http_version _ + r->uri.data _ r->exten.data); if (r->http_version == 9) - return ngx_http_event_handler(r); + return NGX_OK; /* TODO: check too long URI - no space for header, compact buffer */ r->headers_in.headers = ngx_create_table(r->pool, 10); + /* THINK: when to create out.headers ? */ + r->headers_out.headers = ngx_create_table(r->pool, 10); - r->state_handler = ngx_http_process_request_header; + r->state_handler = ngx_http_process_request_headers; ctx = r->connection->log->data; ctx->action = "reading client request headers"; - return NGX_OK; + return NGX_AGAIN; } if (r->header_in->last.mem >= r->header_in->end) { @@ -342,13 +356,13 @@ static int ngx_http_process_request_line } -static int ngx_http_process_request_header(ngx_http_request_t *r) +static int ngx_http_process_request_headers(ngx_http_request_t *r) { int rc; ngx_http_log_ctx_t *ctx; for ( ;; ) { - rc = ngx_read_http_header_line(r); + rc = ngx_read_http_header_line(r, r->header_in); /* TODO: check too long header, compact buffer */ @@ -358,7 +372,7 @@ static int ngx_http_process_request_head } else if (rc == NGX_HTTP_PARSE_HEADER_DONE) { ngx_log_debug(r->connection->log, "HTTP header done"); - return ngx_http_event_handler(r); + return NGX_OK; } else if (rc == NGX_AGAIN) { return NGX_AGAIN; @@ -408,90 +422,16 @@ static int ngx_http_process_request_head } -/* ******************** */ - -void ngx_http_discard_body(ngx_http_request_t *r) -{ - ngx_log_debug(r->connection->log, "set discard body"); - - ngx_del_timer(r->connection->read); - - if (r->client_content_length) - r->connection->read->event_handler = ngx_http_read_discarded_body; -} - -static int ngx_http_read_discarded_body(ngx_event_t *ev) -{ - size_t size; - ssize_t n; - ngx_connection_t *c; - ngx_http_request_t *r; - - c = (ngx_connection_t *) ev->data; - r = (ngx_http_request_t *) c->data; - - ngx_log_debug(ev->log, "http read discarded body"); - - if (ev->timedout) - return NGX_ERROR; - - if (r->discarded_buffer == NULL) - ngx_test_null(r->discarded_buffer, - ngx_palloc(r->pool, r->server->discarded_buffer_size), - NGX_ERROR); - - size = r->client_content_length; - if (size > r->server->discarded_buffer_size) - size = r->server->discarded_buffer_size; - - n = ngx_event_recv(c, r->discarded_buffer, size); - if (n == NGX_ERROR) - return NGX_ERROR; - - if (n == NGX_AGAIN) - return NGX_OK; - - r->client_content_length -= n; - /* XXX: what if r->client_content_length == 0 ? */ - return NGX_OK; -} - -static int ngx_http_discarded_read(ngx_event_t *ev) -{ - ssize_t n; - ngx_connection_t *c; - ngx_http_request_t *r; - - c = (ngx_connection_t *) ev->data; - r = (ngx_http_request_t *) c->data; - - ngx_log_debug(ev->log, "http discarded read"); - - if (ev->timedout) - return NGX_ERROR; - - if (r->discarded_buffer == NULL) - ngx_test_null(r->discarded_buffer, - ngx_palloc(r->pool, r->server->discarded_buffer_size), - NGX_ERROR); - - n = ngx_event_recv(c, r->discarded_buffer, - r->server->discarded_buffer_size); - - return n; -} - -/* ******************** */ - - static int ngx_http_event_handler(ngx_http_request_t *r) { int rc; ngx_msec_t timeout; - ngx_log_debug(r->connection->log, "UA: '%s: %s'" _ - r->headers_in.user_agent->key.data _ - r->headers_in.user_agent->value.data); + ngx_del_timer(r->connection->read); + r->header_timeout = 0; + + r->state_handler = NULL; + r->connection->read->event_handler = ngx_http_block_read; rc = ngx_http_handler(r); @@ -553,22 +493,105 @@ static int ngx_http_event_handler(ngx_ht r->connection->read->event_handler = ngx_http_keepalive_handler; } -static int ngx_http_handler(ngx_http_request_t *r) + +static int ngx_http_block_read(ngx_event_t *ev) +{ + ngx_log_debug(ev->log, "http read blocked"); + + ev->blocked = 1; + return ngx_del_event(ev, NGX_READ_EVENT); +} + + + +/* FIND PLACE ******************** */ + +void ngx_http_discard_body(ngx_http_request_t *r) +{ + ngx_log_debug(r->connection->log, "set discard body"); + + ngx_del_timer(r->connection->read); + + if (r->client_content_length) + r->connection->read->event_handler = ngx_http_read_discarded_body; +} + + +static int ngx_http_read_discarded_body(ngx_event_t *ev) +{ + size_t size; + ssize_t n; + ngx_connection_t *c; + ngx_http_request_t *r; + + c = (ngx_connection_t *) ev->data; + r = (ngx_http_request_t *) c->data; + + ngx_log_debug(ev->log, "http read discarded body"); + + if (ev->timedout) + return NGX_ERROR; + + if (r->discarded_buffer == NULL) + ngx_test_null(r->discarded_buffer, + ngx_palloc(r->pool, r->server->discarded_buffer_size), + NGX_ERROR); + + size = r->client_content_length; + if (size > r->server->discarded_buffer_size) + size = r->server->discarded_buffer_size; + + n = ngx_event_recv(c, r->discarded_buffer, size); + if (n == NGX_ERROR) + return NGX_ERROR; + + if (n == NGX_AGAIN) + return NGX_OK; + + r->client_content_length -= n; + /* XXX: what if r->client_content_length == 0 ? */ + return NGX_OK; +} + + +static int ngx_http_discarded_read(ngx_event_t *ev) +{ + ssize_t n; + ngx_connection_t *c; + ngx_http_request_t *r; + + c = (ngx_connection_t *) ev->data; + r = (ngx_http_request_t *) c->data; + + ngx_log_debug(ev->log, "http discarded read"); + + if (ev->timedout) + return NGX_ERROR; + + if (r->discarded_buffer == NULL) + ngx_test_null(r->discarded_buffer, + ngx_palloc(r->pool, r->server->discarded_buffer_size), + NGX_ERROR); + + n = ngx_event_recv(c, r->discarded_buffer, + r->server->discarded_buffer_size); + + return n; +} + +/* ******************** */ + + +#if 0 +int ngx_http_handler(ngx_http_request_t *r) { int rc; - ngx_del_timer(r->connection->read); - r->header_timeout = 0; - - r->process_header = 0; - r->state_handler = NULL; r->connection->unexpected_eof = 0; r->lingering_close = 1; - r->connection->read->event_handler = ngx_http_block_read; - /* STUB: should find handler */ -#if 0 +#if 1 r->filter = NGX_HTTP_FILTER_NEED_IN_MEMORY; #endif rc = ngx_http_set_default_handler(r); @@ -580,23 +603,16 @@ static int ngx_http_handler(ngx_http_req return rc; } - -int ngx_http_internal_redirect(ngx_http_request_t *r, char *uri) -{ - ngx_log_debug(r->connection->log, "internal redirect: '%s'" _ uri); +#endif - r->uri = uri; - r->uri_start = uri; - r->uri_end = uri + strlen(uri); - return ngx_http_handler(r); -} +#if 0 static int ngx_http_set_default_handler(ngx_http_request_t *r) { - int err, rc; - char *name, *loc, *file; + ngx_err_t err; + char *name, *loc, *file; -#if 1 +#if 0 /* STUB */ r->handler = ngx_http_proxy_handler; return NGX_OK; @@ -650,15 +666,7 @@ static int ngx_http_set_default_handler( return NGX_OK; } - - -static int ngx_http_block_read(ngx_event_t *ev) -{ - ngx_log_debug(ev->log, "http read blocked"); - - ev->blocked = 1; - return ngx_del_event(ev, NGX_READ_EVENT); -} +#endif static int ngx_http_writer(ngx_event_t *ev) @@ -726,6 +734,7 @@ static int ngx_http_writer(ngx_event_t * c->read->event_handler = ngx_http_keepalive_handler; } + static int ngx_http_set_lingering_close(ngx_http_request_t *r) { r->lingering_time = ngx_time() + r->server->lingering_time; @@ -842,111 +851,6 @@ static int ngx_http_lingering_close(ngx_ } -static int ngx_http_special_response(ngx_http_request_t *r, int error) -{ - return ngx_http_error(r, error); -} - - -static int ngx_http_redirect(ngx_http_request_t *r, int redirect) -{ - /* STUB */ - - /* log request */ - - return ngx_http_close_request(r); -} - - -static int ngx_http_error(ngx_http_request_t *r, int error) -{ - /* STUB */ - ngx_log_debug(r->connection->log, "http error: %d" _ error); - - /* log request */ - - return ngx_http_close_request(r); -} - -#if 0 - -static int ngx_process_http_request(ngx_http_request_t *r) -{ - int fd; - struct stat sb; - ngx_http_header_out_t *header_out; - ngx_chunk_t *header, *ch; - - int index = (*(r->uri_end - 1) == '/') ? sizeof(NGX_INDEX) : 1; - char *name = ngx_palloc(r->pool, - r->uri_end - r->uri_start + strlen(ngx_root) + index); - strcpy(name, ngx_root); - strcat(name, r->uri_start); - if (*(r->uri_end - 1) == '/') - strcat(name, NGX_INDEX); - - ngx_log_debug(r->connection->log, "HTTP URI: '%s'", name); - - if ((fd = open(name, O_RDONLY)) == -1) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, errno, - "open %s failed", name); - return -1; - } - - if (fstat(fd, &sb) == -1) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, errno, - "fstat %s failed", name); - return -1; - } - - header_out = ngx_palloc(r->pool, sizeof(ngx_http_header_out_t)); - - header_out->status = NGX_HTTP_OK; - header_out->content_length = sb.st_size; - header_out->last_modified = sb.st_mtime; - header_out->content_type = "text/html"; - header_out->charset = "koi8-r"; - header_out->date = time(NULL); - header_out->connection = NGX_HTTP_CONN_CLOSE; - -/* - header_out->connection = NGX_HTTP_CONN_KEEP_ALIVE; - r->connection->read->event_handler = ngx_http_init_request; -*/ - - header = ngx_http_header(r, header_out); - ch = ngx_palloc(r->pool, sizeof(ngx_chunk_t)); - ch->ident = fd; - ch->offset = 0; - ch->size = sb.st_size; - ch->next = NULL; - header->next = ch; - - ngx_event_write(r->connection, header); - - return 0; -} - -#endif - -static int ngx_http_close_request(ngx_http_request_t *r) -{ -/* - if (r->logging) - ngx_http_log_request(r); -*/ - - ngx_destroy_pool(r->pool); - - ngx_log_debug(r->connection->log, "http close"); - - ngx_del_timer(r->connection->read); - ngx_del_timer(r->connection->write); - - return NGX_DONE; -} - - static int ngx_http_close_connection(ngx_event_t *ev) { int i, len;