diff options
| author | Igor Sysoev <igor@sysoev.ru> | 2002-12-10 18:05:12 +0000 |
|---|---|---|
| committer | Igor Sysoev <igor@sysoev.ru> | 2002-12-10 18:05:12 +0000 |
| commit | b0869056bb4385a6b30fc58c653716c45ed33916 (patch) | |
| tree | 8522c66320ee59e4671f23058a971d9556d1a6a0 /src/http/ngx_http_event.c | |
| parent | ef259d140f378be8d7936d04038354a93cccb461 (diff) | |
| download | nginx-b0869056bb4385a6b30fc58c653716c45ed33916.tar.gz nginx-b0869056bb4385a6b30fc58c653716c45ed33916.tar.bz2 | |
nginx-0.0.1-2002-12-10-21:05:12 import
Diffstat (limited to '')
| -rw-r--r-- | src/http/ngx_http_event.c | 392 |
1 files changed, 148 insertions, 244 deletions
diff --git a/src/http/ngx_http_event.c b/src/http/ngx_http_event.c index 30d1ac5b4..905af4736 100644 --- 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(ngx_http_request_t *r); 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_connection_t *c) 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_connection_t *c) 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_event_t *ev) 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_event_t *ev) 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_event_t *ev) 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_event_t *ev) 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_event_t *ev) /* 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_event_t *ev) 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_event_t *ev) 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(ngx_http_request_t *r) 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(ngx_http_request_t *r) /* */ 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(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) { 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_header(ngx_http_request_t *r) } 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,7 +422,89 @@ static int ngx_http_process_request_header_line(ngx_http_request_t *r) } -/* ******************** */ +static int ngx_http_event_handler(ngx_http_request_t *r) +{ + int rc; + ngx_msec_t timeout; + + 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); + + /* handler is still busy */ + if (rc == NGX_WAITING) + return rc; + + /* handler has done its work but transfer is not completed */ + if (rc == NGX_AGAIN) { +#if (HAVE_CLEAR_EVENT) + if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, + NGX_CLEAR_EVENT) == NGX_ERROR) { +#else + if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, + NGX_ONESHOT_EVENT) == NGX_ERROR) { +#endif + return ngx_http_close_request(r); + } + + if (r->connection->sent > 0) { + ngx_log_debug(r->connection->log, "sent: " QD_FMT _ + r->connection->sent); + timeout = (ngx_msec_t) (r->connection->sent * 10); + ngx_log_debug(r->connection->log, "timeout: %d" _ timeout); + ngx_add_timer(r->connection->write, timeout); + + } else { + ngx_add_timer(r->connection->write, 10000); + } + + r->connection->write->event_handler = ngx_http_writer; + return rc; + } + + if (rc == NGX_ERROR) { + /* log http request */ + return ngx_http_close_request(r); + } + + if (rc >= NGX_HTTP_SPECIAL_RESPONSE) + return ngx_http_special_response(r, rc); + + /* rc == NGX_OK */ + + if (!r->keepalive) { + if (r->lingering_close) { + return ngx_http_set_lingering_close(r); + + } else { + return ngx_http_close_request(r); + } + } + + /* keepalive */ + + ngx_http_close_request(r); + r->connection->buffer->pos.mem = r->connection->buffer->last.mem + = r->connection->buffer->start; + r->connection->read->event_handler = ngx_http_keepalive_handler; +} + + +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) { @@ -420,6 +516,7 @@ void ngx_http_discard_body(ngx_http_request_t *r) r->connection->read->event_handler = ngx_http_read_discarded_body; } + static int ngx_http_read_discarded_body(ngx_event_t *ev) { size_t size; @@ -456,6 +553,7 @@ static int ngx_http_read_discarded_body(ngx_event_t *ev) return NGX_OK; } + static int ngx_http_discarded_read(ngx_event_t *ev) { ssize_t n; @@ -484,91 +582,16 @@ static int ngx_http_discarded_read(ngx_event_t *ev) /* ******************** */ -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); - - rc = ngx_http_handler(r); - - /* handler is still busy */ - if (rc == NGX_WAITING) - return rc; - - /* handler has done its work but transfer is not completed */ - if (rc == NGX_AGAIN) { -#if (HAVE_CLEAR_EVENT) - if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, - NGX_CLEAR_EVENT) == NGX_ERROR) { -#else - if (ngx_add_event(r->connection->write, NGX_WRITE_EVENT, - NGX_ONESHOT_EVENT) == NGX_ERROR) { -#endif - return ngx_http_close_request(r); - } - - if (r->connection->sent > 0) { - ngx_log_debug(r->connection->log, "sent: " QD_FMT _ - r->connection->sent); - timeout = (ngx_msec_t) (r->connection->sent * 10); - ngx_log_debug(r->connection->log, "timeout: %d" _ timeout); - ngx_add_timer(r->connection->write, timeout); - - } else { - ngx_add_timer(r->connection->write, 10000); - } - - r->connection->write->event_handler = ngx_http_writer; - return rc; - } - - if (rc == NGX_ERROR) { - /* log http request */ - return ngx_http_close_request(r); - } - - if (rc >= NGX_HTTP_SPECIAL_RESPONSE) - return ngx_http_special_response(r, rc); - - /* rc == NGX_OK */ - - if (!r->keepalive) { - if (r->lingering_close) { - return ngx_http_set_lingering_close(r); - - } else { - return ngx_http_close_request(r); - } - } - - /* keepalive */ - - ngx_http_close_request(r); - r->connection->buffer->pos.mem = r->connection->buffer->last.mem - = r->connection->buffer->start; - r->connection->read->event_handler = ngx_http_keepalive_handler; -} - -static int ngx_http_handler(ngx_http_request_t *r) +#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_request_t *r) return rc; } +#endif -int ngx_http_internal_redirect(ngx_http_request_t *r, char *uri) -{ - ngx_log_debug(r->connection->log, "internal redirect: '%s'" _ uri); - - 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(ngx_http_request_t *r) 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 *ev) 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_event_t *ev) } -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; |
