nginx-0.0.1-2003-11-03-20:33:31 import
diff --git a/src/http/modules/proxy/ngx_http_proxy_cache.c b/src/http/modules/proxy/ngx_http_proxy_cache.c
index bfa135c..6309b39 100644
--- a/src/http/modules/proxy/ngx_http_proxy_cache.c
+++ b/src/http/modules/proxy/ngx_http_proxy_cache.c
@@ -5,13 +5,16 @@
#include <ngx_http_proxy_handler.h>
+static int ngx_http_proxy_process_cached_header(ngx_http_proxy_ctx_t *p);
+
+
int ngx_http_proxy_get_cached_response(ngx_http_proxy_ctx_t *p)
{
- int rc;
- char *last;
- ngx_http_request_t *r;
- ngx_http_proxy_cache_t *c;
- ngx_http_proxy_upstream_t *u;
+ int rc;
+ char *last;
+ ngx_http_request_t *r;
+ ngx_http_proxy_cache_t *c;
+ ngx_http_proxy_upstream_conf_t *u;
r = p->request;
@@ -19,6 +22,8 @@
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
+ p->cache = c;
+
c->ctx.file.fd = NGX_INVALID_FILE;
c->ctx.file.log = r->connection->log;
c->ctx.path = p->lcf->cache_path;
@@ -48,15 +53,21 @@
p->header_in->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
c->ctx.buf = p->header_in;
- p->cache = c;
rc = ngx_http_cache_get_file(r, &c->ctx);
+ if (rc == NGX_STALE) {
+ p->stale = 1;
+ }
+
if (rc == NGX_OK || rc == NGX_STALE) {
- p->header_in->pos += c->ctx.header.size;
+ p->header_in->pos += c->ctx.header_size;
+ if (ngx_http_proxy_process_cached_header(p) == NGX_ERROR) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
} else if (rc == NGX_DECLINED) {
- p->header_in->pos += c->ctx.header.size;
+ p->header_in->pos += c->ctx.header_size;
p->header_in->last = p->header_in->pos;
}
@@ -64,7 +75,7 @@
}
-int ngx_http_proxy_process_cached_response(ngx_http_proxy_ctx_t *p)
+static int ngx_http_proxy_process_cached_header(ngx_http_proxy_ctx_t *p)
{
int rc, i;
ngx_table_elt_t *h;
@@ -81,14 +92,14 @@
"\"proxy_header_buffer_size\" "
"is too small to read header from \"%s\"",
c->ctx.file.name.data);
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ return NGX_ERROR;
}
if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"no valid HTTP/1.0 header in \"%s\"",
c->ctx.file.name.data);
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ return NGX_ERROR;
}
/* rc == NGX_OK */
@@ -97,7 +108,7 @@
c->status_line.len = p->status_end - p->status_start;
c->status_line.data = ngx_palloc(r->pool, c->status_line.len + 1);
if (c->status_line.data == NULL) {
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ return NGX_ERROR;
}
ngx_cpystrn(c->status_line.data, p->status_start, c->status_line.len + 1);
@@ -116,7 +127,7 @@
h = ngx_http_add_header(&c->headers_in, ngx_http_proxy_headers_in);
if (h == NULL) {
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ return NGX_ERROR;
}
h->key.len = r->header_name_end - r->header_name_start;
@@ -125,7 +136,7 @@
h->key.data = ngx_palloc(r->pool,
h->key.len + 1 + h->value.len + 1);
if (h->key.data == NULL) {
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ return NGX_ERROR;
}
h->value.data = h->key.data + h->key.len + 1;
@@ -157,14 +168,14 @@
ngx_log_debug(r->connection->log, "HTTP header done");
- return ngx_http_proxy_send_cached_response(p);
+ return NGX_OK;
} else if (rc == NGX_HTTP_PARSE_INVALID_HEADER) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"invalid header in \"%s\"",
c->ctx.file.name.data);
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ return NGX_ERROR;
}
/* rc == NGX_AGAIN || rc == NGX_HTTP_PARSE_TOO_LONG_HEADER */
@@ -173,7 +184,7 @@
"\"proxy_header_buffer_size\" "
"is too small to read header from \"%s\"",
c->ctx.file.name.data);
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ return NGX_ERROR;
}
}
@@ -187,7 +198,7 @@
r = p->request;
- r->headers_out.status = p->status;
+ r->headers_out.status = p->cache->status;
#if 0
r->headers_out.content_length_n = -1;
@@ -233,3 +244,14 @@
return ngx_http_output_filter(r, &out);
}
+
+
+int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p)
+{
+ if (p->cache == NULL) {
+ return NGX_OK;
+ }
+
+ return ngx_http_cache_update_file(p->request, &p->cache->ctx,
+ &p->upstream->event_pipe->temp_file->file.name);
+}
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index afa99d9..7c9fa9e 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -6,25 +6,6 @@
static int ngx_http_proxy_handler(ngx_http_request_t *r);
-static int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p);
-static void ngx_http_proxy_init_request(void *data);
-static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p);
-static void ngx_http_proxy_send_request_handler(ngx_event_t *wev);
-static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p);
-static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p);
-static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev);
-static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev);
-static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *);
-static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p);
-static void ngx_http_proxy_process_body(ngx_event_t *ev);
-static int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p);
-
-static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type);
-static int ngx_http_proxy_log_state(ngx_http_proxy_ctx_t *p, int status);
-static void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc);
-static void ngx_http_proxy_close_connection(ngx_connection_t *c);
-
-static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len);
static int ngx_http_proxy_init(ngx_cycle_t *cycle);
static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
@@ -34,7 +15,7 @@
static char *ngx_http_proxy_set_pass(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_proxy_parse_upstream(ngx_str_t *url,
- ngx_http_proxy_upstream_t *u);
+ ngx_http_proxy_upstream_conf_t *u);
static ngx_conf_bitmask_t next_upstream_masks[] = {
@@ -190,22 +171,16 @@
};
-static ngx_str_t http_methods[] = {
- ngx_string("GET "),
- ngx_string("HEAD "),
- ngx_string("POST ")
-};
-
-
-static char *upstream_header_errors[] = {
- "upstream sent invalid header",
- "upstream sent too long header line"
-};
-
-
ngx_http_header_t ngx_http_proxy_headers_in[] = {
{ ngx_string("Date"), offsetof(ngx_http_proxy_headers_in_t, date) },
{ ngx_string("Server"), offsetof(ngx_http_proxy_headers_in_t, server) },
+
+ { ngx_string("Expires"), offsetof(ngx_http_proxy_headers_in_t, expires) },
+ { ngx_string("Cache-Control"),
+ offsetof(ngx_http_proxy_headers_in_t, cache_control) },
+ { ngx_string("X-Accel-Expires"),
+ offsetof(ngx_http_proxy_headers_in_t, x_accel_expires) },
+
{ ngx_string("Connection"),
offsetof(ngx_http_proxy_headers_in_t, connection) },
{ ngx_string("Content-Type"),
@@ -221,19 +196,12 @@
};
-static char http_version[] = " HTTP/1.0" CRLF;
-static char host_header[] = "Host: ";
-static char connection_close_header[] = "Connection: close" CRLF;
-
-
-
static int ngx_http_proxy_handler(ngx_http_request_t *r)
{
- int rc;
- char *last;
- ngx_http_cache_ctx_t *cctx;
- ngx_http_proxy_ctx_t *p;
- ngx_http_proxy_upstream_t *u;
+ int rc;
+ char *last;
+ ngx_http_cache_ctx_t *cctx;
+ ngx_http_proxy_ctx_t *p;
ngx_http_create_ctx(r, p, ngx_http_proxy_module,
sizeof(ngx_http_proxy_ctx_t),
@@ -252,7 +220,7 @@
rc = ngx_http_proxy_get_cached_response(p);
if (rc == NGX_OK) {
- return ngx_http_proxy_process_cached_response(p);
+ return ngx_http_proxy_send_cached_response(p);
}
if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
@@ -267,1012 +235,7 @@
}
-static int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p)
-{
- ngx_http_request_t *r;
-
- r = p->request;
-
- p->upstream.peers = p->lcf->peers;
- p->upstream.tries = p->lcf->peers->number;
-
- ngx_init_array(p->states, r->pool, p->upstream.tries,
- sizeof(ngx_http_proxy_state_t),
- NGX_HTTP_INTERNAL_SERVER_ERROR);
-
- p->method = r->method;
-
- /* STUB */ p->cachable = p->lcf->cache;
-
- if (r->headers_in.content_length_n > 0) {
- if (!(r->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) {
- return NGX_HTTP_INTERNAL_SERVER_ERROR;
- }
-
- r->temp_file->file.fd = NGX_INVALID_FILE;
- r->temp_file->file.log = r->connection->log;
- r->temp_file->path = p->lcf->temp_path;
- r->temp_file->pool = r->pool;
- r->temp_file->warn = "a client request body is buffered "
- "to a temporary file";
- /* r->temp_file->persistent = 0; */
-
- r->request_body_handler = ngx_http_proxy_init_request;
- r->data = p;
-
- /* TODO: we ignore return value of ngx_http_read_client_request_body,
- probably we should not return anything */
- ngx_http_read_client_request_body(r, p->lcf->request_buffer_size);
-
- return NGX_DONE;
- }
-
- ngx_http_proxy_init_request(p);
-
- return NGX_DONE;
-}
-
-
-static void ngx_http_proxy_init_request(void *data)
-{
- ngx_http_proxy_ctx_t *p = data;
-
- ngx_chain_t *cl;
- ngx_http_request_t *r;
- ngx_output_chain_ctx_t *octx;
- ngx_chain_writer_ctx_t *wctx;
-
- r = p->request;
-
-ngx_log_debug(r->connection->log, "timer_set: %d" _
- r->connection->read->timer_set);
-
- if (r->connection->read->timer_set) {
- ngx_del_timer(r->connection->read);
- }
-
- if (!(cl = ngx_http_proxy_create_request(p))) {
- ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- if (r->request_hunks) {
- cl->next = r->request_hunks;
- }
-
- r->request_hunks = cl;
-
- p->upstream.log = r->connection->log;
- p->saved_ctx = r->connection->log->data;
- p->saved_handler = r->connection->log->handler;
- r->connection->log->data = p;
- r->connection->log->handler = ngx_http_proxy_log_error;
- p->action = "connecting to upstream";
-
- if (!(octx = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t)))) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- p->output_chain_ctx = octx;
- octx->sendfile = r->sendfile;
- octx->pool = r->pool;
- octx->bufs.num = 1;
- octx->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
- octx->output_filter = (ngx_output_chain_filter_pt) ngx_chain_writer;
-
- if (!(wctx = ngx_palloc(r->pool, sizeof(ngx_chain_writer_ctx_t)))) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- octx->output_ctx = wctx;
- wctx->pool = r->pool;
-
- ngx_http_proxy_connect(p);
-}
-
-
-static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p)
-{
- int i;
- size_t len;
- ngx_hunk_t *h;
- ngx_chain_t *chain;
- ngx_table_elt_t *header;
- ngx_http_request_t *r;
- ngx_http_proxy_upstream_t *u;
-
- r = p->request;
- u = p->lcf->upstream;
-
- len = http_methods[p->method - 1].len
- + u->uri.len
- + r->uri.len - u->location->len
- + 1 + r->args.len /* 1 is for "?" */
- + sizeof(http_version) - 1
- + sizeof(host_header) - 1 + u->host_header.len + 2
- /* 2 is for "\r\n" */
- + sizeof(connection_close_header) - 1
- + 2; /* 2 is for "\r\n" at the header end */
-
- header = (ngx_table_elt_t *) r->headers_in.headers->elts;
- for (i = 0; i < r->headers_in.headers->nelts; i++) {
-
- if (&header[i] == r->headers_in.host) {
- continue;
- }
-
- if (&header[i] == r->headers_in.connection) {
- continue;
- }
-
- /* 2 is for ": " and 2 is for "\r\n" */
- len += header[i].key.len + 2 + header[i].value.len + 2;
- }
-
- /* STUB */ len++;
-
- ngx_test_null(h, ngx_create_temp_hunk(r->pool, len), NULL);
- ngx_alloc_link_and_set_hunk(chain, h, r->pool, NULL);
-
-
- /* the request line */
-
- h->last = ngx_cpymem(h->last, http_methods[p->method - 1].data,
- http_methods[p->method - 1].len);
-
- h->last = ngx_cpymem(h->last, u->uri.data, u->uri.len);
-
- h->last = ngx_cpymem(h->last,
- r->uri.data + u->location->len,
- r->uri.len - u->location->len);
-
- if (r->args.len > 0) {
- *(h->last++) = '?';
- h->last = ngx_cpymem(h->last, r->args.data, r->args.len);
- }
-
- h->last = ngx_cpymem(h->last, http_version, sizeof(http_version) - 1);
-
-
- /* "Host" header */
-
- h->last = ngx_cpymem(h->last, host_header, sizeof(host_header) - 1);
- h->last = ngx_cpymem(h->last, u->host_header.data, u->host_header.len);
- *(h->last++) = CR; *(h->last++) = LF;
-
-
- /* "Connection: close" header */
-
- h->last = ngx_cpymem(h->last, connection_close_header,
- sizeof(connection_close_header) - 1);
-
-
- for (i = 0; i < r->headers_in.headers->nelts; i++) {
-
- if (&header[i] == r->headers_in.host) {
- continue;
- }
-
- if (&header[i] == r->headers_in.connection) {
- continue;
- }
-
- if (&header[i] == r->headers_in.keep_alive) {
- continue;
- }
-
- h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len);
-
- *(h->last++) = ':'; *(h->last++) = ' ';
-
- h->last = ngx_cpymem(h->last, header[i].value.data,
- header[i].value.len);
-
- *(h->last++) = CR; *(h->last++) = LF;
-
- ngx_log_debug(r->connection->log, "proxy: '%s: %s'" _
- header[i].key.data _ header[i].value.data);
- }
-
- /* add "\r\n" at the header end */
- *(h->last++) = CR; *(h->last++) = LF;
-
- /* STUB */ *(h->last) = '\0';
- ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ h->pos);
-
- return chain;
-}
-
-
-static void ngx_http_proxy_send_request_handler(ngx_event_t *wev)
-{
- ngx_connection_t *c;
- ngx_http_proxy_ctx_t *p;
-
- c = wev->data;
- p = c->data;
-
- if (wev->timedout) {
- p->action = "sending request to upstream";
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
- return;
- }
-
- ngx_http_proxy_send_request(p);
-}
-
-
-static void ngx_http_proxy_dummy_handler(ngx_event_t *wev)
-{
- ngx_log_debug(wev->log, "dummy handler");
-}
-
-
-static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p)
-{
- int rc;
- ngx_chain_t *cl;
- ngx_connection_t *c;
- ngx_http_request_t *r;
- ngx_output_chain_ctx_t *octx;
-
- p->action = "connecting to upstream";
-
- rc = ngx_event_connect_peer(&p->upstream);
-
- if (rc == NGX_ERROR) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- if (rc == NGX_CONNECT_ERROR) {
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
- return;
- }
-
- p->upstream.connection->data = p;
- p->upstream.connection->write->event_handler =
- ngx_http_proxy_send_request_handler;
- p->upstream.connection->read->event_handler =
- ngx_http_proxy_process_upstream_status_line;
-
- r = p->request;
- c = p->upstream.connection;
- c->pool = r->pool;
- c->read->log = c->write->log = c->log = r->connection->log;
-
- octx = p->output_chain_ctx;
-
- if (p->upstream.tries > 1 && p->request_sent) {
- ngx_http_proxy_reinit_upstream(p);
- }
-
- /* init or reinit ngx_output_chain() context */
-
- if (r->request_body_hunk) {
- if (!(octx->free = ngx_alloc_chain_link(r->pool))) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- octx->free->hunk = r->request_body_hunk;
- octx->free->next = NULL;
- octx->hunks = 1;
-
- r->request_body_hunk->pos = r->request_body_hunk->start;
- r->request_body_hunk->last = r->request_body_hunk->start;
- }
-
- p->request_sent = 0;
-
- if (rc == NGX_AGAIN) {
- ngx_add_timer(c->write, p->lcf->connect_timeout);
- return;
- }
-
- /* rc == NGX_OK */
-
-#if 1 /* test only */
-
- if (c->read->ready) {
- /* post aio operation */
- ngx_http_proxy_process_upstream_status_line(c->read);
- return;
- }
-#endif
-
- ngx_http_proxy_send_request(p);
-}
-
-
-static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p)
-{
- int rc;
- ngx_connection_t *c;
- ngx_chain_writer_ctx_t *wctx;
-
- c = p->upstream.connection;
-
-#if (HAVE_KQUEUE)
-
- if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
- && !p->request_sent
- && c->write->kq_eof)
- {
- ngx_log_error(NGX_LOG_ERR, c->log, c->write->kq_errno,
- "connect() failed");
-
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
- return;
- }
-
-#endif
-
- p->action = "sending request to upstream";
-
- wctx = p->output_chain_ctx->output_ctx;
- wctx->out = NULL;
- wctx->last = &wctx->out;
- wctx->connection = c;
-
- rc = ngx_output_chain(p->output_chain_ctx,
- p->request_sent ? NULL : p->request->request_hunks);
-
- if (rc == NGX_ERROR) {
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
- return;
- }
-
- p->request_sent = 1;
-
- if (c->write->timer_set) {
- ngx_del_timer(c->write);
- }
-
- if (rc == NGX_AGAIN) {
- ngx_add_timer(c->write, p->lcf->send_timeout);
-
- if (ngx_handle_write_event(c->write, /* STUB: lowat */ 0) == NGX_ERROR)
- {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- return;
- }
-
- /* rc == NGX_OK */
-
- if (c->tcp_nopush) {
- if (ngx_tcp_push(c->fd) == NGX_ERROR) {
- ngx_log_error(NGX_LOG_CRIT, c->log,
- ngx_socket_errno,
- ngx_tcp_push_n " failed");
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- c->tcp_nopush = 0;
- return;
- }
-
-#if 0
- if (c->read->ready) {
-
- /* post aio operation */
-
- /*
- * although we can post aio operation just in the end
- * of ngx_http_proxy_connect() CHECK IT !!!
- * it's better to do here because we postpone header buffer allocation
- */
-
- ngx_http_proxy_process_upstream_status_line(c->read);
- return;
- }
-#endif
-
- p->upstream.connection->write->event_handler = ngx_http_proxy_dummy_handler;
-
- if (ngx_handle_level_write_event(c->write) == NGX_ERROR) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-}
-
-
-static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev)
-{
- int rc;
- ssize_t n;
- ngx_connection_t *c;
- ngx_http_proxy_ctx_t *p;
-
- c = rev->data;
- p = c->data;
- p->action = "reading upstream status line";
-
- ngx_log_debug(rev->log, "http proxy process status line");
-
- if (rev->timedout) {
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
- return;
- }
-
- if (p->header_in == NULL) {
- p->header_in = ngx_create_temp_hunk(p->request->pool,
- p->lcf->header_buffer_size);
- if (p->header_in == NULL) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- p->header_in->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
-
- if (p->cache) {
- p->header_in->pos += p->cache->ctx.header.size;
- p->header_in->last = p->header_in->pos;
- }
- }
-
- n = ngx_http_proxy_read_upstream_header(p);
-
- if (n == NGX_AGAIN) {
- return;
- }
-
- if (n == 0) {
- ngx_log_error(NGX_LOG_ERR, rev->log, 0,
- "upstream prematurely closed connection");
- }
-
- if (n == NGX_ERROR || n == 0) {
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
- return;
- }
-
- p->upstream.cached = 0;
-
- rc = ngx_http_proxy_parse_status_line(p);
-
- if (rc == NGX_AGAIN) {
- if (p->header_in->pos == p->header_in->last) {
- ngx_log_error(NGX_LOG_ERR, rev->log, 0,
- "upstream sent too long status line");
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
- }
- return;
- }
-
- if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
- ngx_log_error(NGX_LOG_ERR, rev->log, 0,
- "upstream sent no valid HTTP/1.0 header");
-
- if (p->accel) {
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
-
- } else {
- p->request->http_version = NGX_HTTP_VERSION_9;
- p->status = NGX_HTTP_OK;
- ngx_http_proxy_send_response(p);
- }
-
- return;
- }
-
- /* rc == NGX_OK */
-
- if (p->status == NGX_HTTP_INTERNAL_SERVER_ERROR) {
-
- if (p->upstream.tries > 1
- && (p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_500))
- {
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_500);
- return;
- }
-
- if (p->upstream.tries == 0
- && p->stale
- && (p->lcf->use_stale & NGX_HTTP_PROXY_FT_HTTP_500))
- {
- /*
- * TODO: use stale cached response if it exists and enabled
- */
-
- return;
- }
- }
-
- p->status_line.len = p->status_end - p->status_start;
- p->status_line.data = ngx_palloc(p->request->pool, p->status_line.len + 1);
- if (p->status_line.data == NULL) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
- ngx_cpystrn(p->status_line.data, p->status_start, p->status_line.len + 1);
-
- ngx_log_debug(rev->log, "http proxy status %d '%s'" _
- p->status _ p->status_line.data);
-
- if (p->headers_in.headers) {
- p->headers_in.headers->nelts = 0;
- } else {
- p->headers_in.headers = ngx_create_table(p->request->pool, 20);
- }
-
- c->read->event_handler = ngx_http_proxy_process_upstream_headers;
- ngx_http_proxy_process_upstream_headers(rev);
-}
-
-
-static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev)
-{
- int i, rc;
- ssize_t n;
- ngx_table_elt_t *h;
- ngx_connection_t *c;
- ngx_http_request_t *r;
- ngx_http_proxy_ctx_t *p;
-
- c = rev->data;
- p = c->data;
- r = p->request;
- p->action = "reading upstream headers";
-
- ngx_log_debug(rev->log, "http proxy process header line");
-
- if (rev->timedout) {
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
- return;
- }
-
- rc = NGX_AGAIN;
-
- for ( ;; ) {
- if (rc == NGX_AGAIN) {
- n = ngx_http_proxy_read_upstream_header(p);
-
- if (n == 0) {
- ngx_log_error(NGX_LOG_ERR, rev->log, 0,
- "upstream prematurely closed connection");
- }
-
- if (n == NGX_ERROR || n == 0) {
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
- return;
- }
-
- if (n == NGX_AGAIN) {
- return;
- }
- }
-
- rc = ngx_http_parse_header_line(p->request, p->header_in);
-
- if (rc == NGX_OK) {
-
- /* a header line has been parsed successfully */
-
- h = ngx_http_add_header(&p->headers_in, ngx_http_proxy_headers_in);
- if (h == NULL) {
- ngx_http_proxy_finalize_request(p,
- NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- h->key.len = r->header_name_end - r->header_name_start;
- h->value.len = r->header_end - r->header_start;
-
- h->key.data = ngx_palloc(p->request->pool,
- h->key.len + 1 + h->value.len + 1);
- if (h->key.data == NULL) {
- ngx_http_proxy_finalize_request(p,
- NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- h->value.data = h->key.data + h->key.len + 1;
- ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
- ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
-
- for (i = 0; ngx_http_proxy_headers_in[i].name.len != 0; i++) {
- if (ngx_http_proxy_headers_in[i].name.len != h->key.len) {
- continue;
- }
-
- if (ngx_strcasecmp(ngx_http_proxy_headers_in[i].name.data,
- h->key.data) == 0)
- {
- *((ngx_table_elt_t **) ((char *) &p->headers_in
- + ngx_http_proxy_headers_in[i].offset)) = h;
- break;
- }
- }
-
- ngx_log_debug(c->log, "HTTP proxy header: '%s: %s'" _
- h->key.data _ h->value.data);
-
- continue;
-
- } else if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
-
- /* a whole header has been parsed successfully */
-
- ngx_log_debug(c->log, "HTTP header done");
-
- ngx_http_proxy_send_response(p);
- return;
-
- } else if (rc != NGX_AGAIN) {
-
- /* there was error while a header line parsing */
-
- ngx_log_error(NGX_LOG_ERR, rev->log, 0,
- upstream_header_errors[rc - NGX_HTTP_PARSE_HEADER_ERROR]);
-
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
- return;
- }
-
- /* rc == NGX_AGAIN: a header line parsing is still not complete */
-
- if (p->header_in->last == p->header_in->end) {
- ngx_log_error(NGX_LOG_ERR, rev->log, 0,
- "upstream sent too big header");
-
- ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
- return;
- }
- }
-}
-
-
-static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p)
-{
- ssize_t n;
- ngx_event_t *rev;
-
- rev = p->upstream.connection->read;
-
- n = p->header_in->last - p->header_in->pos;
-
- if (n > 0) {
- return n;
- }
-
- n = ngx_recv(p->upstream.connection, p->header_in->last,
- p->header_in->end - p->header_in->last);
-
- if (n == NGX_AGAIN) {
- ngx_add_timer(rev, p->lcf->read_timeout);
-
- if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return NGX_ERROR;
- }
-
- return NGX_AGAIN;
- }
-
- if (n == 0) {
- ngx_log_error(NGX_LOG_ERR, rev->log, 0,
- "upstream closed prematurely connection");
- }
-
- if (n == 0 || n == NGX_ERROR) {
- return NGX_ERROR;
- }
-
- p->header_in->last += n;
-
- return n;
-}
-
-
-static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p)
-{
- int rc, i;
- ngx_table_elt_t *ho, *h;
- ngx_event_pipe_t *ep;
- ngx_http_request_t *r;
- ngx_http_bin_cache_t *header;
- ngx_http_core_loc_conf_t *clcf;
-
- r = p->request;
-
- r->headers_out.status = p->status;
-
-#if 0
- r->headers_out.content_length_n = -1;
- r->headers_out.content_length = NULL;
-#endif
-
- /* copy an upstream header to r->headers_out */
-
- if (ngx_http_proxy_copy_header(p, &p->headers_in) == NGX_ERROR) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- /* TODO: preallocate event_pipe hunks, look "Content-Length" */
-
- rc = ngx_http_send_header(r);
-
- p->header_sent = 1;
-
- if (p->cache) {
- header = (ngx_http_bin_cache_t *) p->header_in->start;
- header->type = 0x42424242; /* "BBBB" */
- header->header.length = r->headers_out.content_length_n;
- header->key_len = p->cache->ctx.key.len;
- ngx_memcpy(&header->key, p->cache->ctx.key.data, header->key_len);
- header->key[header->key_len] = LF;
- }
-
- ep = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
- if (ep == NULL) {
- ngx_http_proxy_finalize_request(p, 0);
- return;
- }
-
- ep->input_filter = ngx_event_pipe_copy_input_filter;
- ep->output_filter = (ngx_event_pipe_output_filter_pt)
- ngx_http_output_filter;
- ep->output_ctx = r;
- ep->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
- ep->bufs = p->lcf->bufs;
- ep->busy_size = p->lcf->busy_buffers_size;
- ep->upstream = p->upstream.connection;
- ep->downstream = r->connection;
- ep->pool = r->pool;
- ep->log = r->connection->log;
-
- if (!(ep->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) {
- ngx_http_proxy_finalize_request(p, 0);
- return;
- }
-
- ep->temp_file->file.fd = NGX_INVALID_FILE;
- ep->temp_file->file.log = r->connection->log;
- ep->temp_file->path = p->lcf->temp_path;
- ep->temp_file->pool = r->pool;
- ep->temp_file->warn = "an upstream response is buffered "
- "to a temporary file";
-
- ep->max_temp_file_size = p->lcf->max_temp_file_size;
- ep->temp_file_write_size = p->lcf->temp_file_write_size;
-
- ep->preread_hunks = ngx_alloc_chain_link(r->pool);
- if (ep->preread_hunks == NULL) {
- ngx_http_proxy_finalize_request(p, 0);
- return;
- }
- ep->preread_hunks->hunk = p->header_in;
- ep->preread_hunks->next = NULL;
- p->header_in->type |= NGX_HUNK_PREREAD;
-
- ep->preread_size = p->header_in->last - p->header_in->pos;
-
- if (ngx_event_flags & NGX_USE_AIO_EVENT) {
-
- /* the posted aio operation can currupt shadow buf */
- ep->single_buf = 1;
- }
-
- /* TODO: ep->free_bufs = 0 if use ngx_create_chain_of_hunks() */
- ep->free_bufs = 1;
-
- /*
- * event_pipe would do p->header_in->last += ep->preread_size
- * as though these bytes were read.
- */
- p->header_in->last = p->header_in->pos;
-
- ep->cachable = p->cachable;
-
- if (p->lcf->cyclic_temp_file) {
-
- /*
- * we need to disable the use of sendfile() if we use cyclic temp file
- * because the writing a new data can interfere with sendfile()
- * that uses the same kernel file pages (at least on FreeBSD)
- */
-
- ep->cyclic_temp_file = 1;
- r->sendfile = 0;
-
- } else {
- ep->cyclic_temp_file = 0;
- r->sendfile = 1;
- }
-
- clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
- ep->read_timeout = p->lcf->read_timeout;
- ep->send_timeout = clcf->send_timeout;
- ep->send_lowat = clcf->send_lowat;
-
- p->event_pipe = ep;
-
- p->upstream.connection->read->event_handler = ngx_http_proxy_process_body;
- r->connection->write->event_handler = ngx_http_proxy_process_body;
-
- ngx_http_proxy_process_body(p->upstream.connection->read);
-
- return;
-}
-
-
-static void ngx_http_proxy_process_body(ngx_event_t *ev)
-{
- ngx_connection_t *c;
- ngx_http_request_t *r;
- ngx_http_proxy_ctx_t *p;
- ngx_event_pipe_t *ep;
-
- c = ev->data;
-
- if (ev->write) {
- ngx_log_debug(ev->log, "http proxy process downstream");
- r = c->data;
- p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
- p->action = "sending to client";
-
- } else {
- ngx_log_debug(ev->log, "http proxy process upstream");
- p = c->data;
- r = p->request;
- p->action = "reading upstream body";
- }
-
- ep = p->event_pipe;
-
- if (ev->timedout) {
- if (ev->write) {
- ep->downstream_error = 1;
- ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT,
- "client timed out");
-
- } else {
- ep->upstream_error = 1;
- ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT,
- "upstream timed out");
- }
-
- } else {
- if (ngx_event_pipe(ep, ev->write) == NGX_ABORT) {
- ngx_http_proxy_finalize_request(p, 0);
- return;
- }
- }
-
- if (p->upstream.connection) {
- if (ep->upstream_done) {
- if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
- ngx_http_proxy_finalize_request(p, 0);
- return;
- }
-
- } else if (ep->upstream_eof) {
-
- /* TODO: check length & update cache */
-
- if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
- ngx_http_proxy_finalize_request(p, 0);
- return;
- }
- }
-
- if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) {
- ngx_http_proxy_close_connection(p->upstream.connection);
- p->upstream.connection = NULL;
- }
- }
-
- if (ep->downstream_done) {
- ngx_log_debug(ev->log, "http proxy downstream done");
- ngx_http_proxy_finalize_request(p, r->main ? 0 : ngx_http_send_last(r));
- return;
- }
-
- if (ep->downstream_error) {
- if (!p->cachable && p->upstream.connection) {
- ngx_http_proxy_close_connection(p->upstream.connection);
- p->upstream.connection = NULL;
- }
-
- if (p->upstream.connection == NULL) {
- ngx_http_close_connection(c);
- }
- }
-
- return;
-}
-
-
-static int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p)
-{
- if (p->cache == NULL) {
- return NGX_OK;
- }
-
- return ngx_http_cache_update_file(p->request, &p->cache->ctx,
- &p->event_pipe->temp_file->file.name);
-}
-
-
-static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type)
-{
- int status;
-
-ngx_log_debug(p->request->connection->log, "next upstream: %d" _ ft_type);
-
- if (ft_type != NGX_HTTP_PROXY_FT_HTTP_404) {
- ngx_event_connect_peer_failed(&p->upstream);
- }
-
- if (ft_type == NGX_HTTP_PROXY_FT_TIMEOUT) {
- ngx_log_error(NGX_LOG_ERR, p->request->connection->log, NGX_ETIMEDOUT,
- "upstream timed out");
- }
-
- if (p->upstream.cached && ft_type == NGX_HTTP_PROXY_FT_ERROR) {
- status = 0;
-
- } else {
- switch(ft_type) {
- case NGX_HTTP_PROXY_FT_TIMEOUT:
- status = NGX_HTTP_GATEWAY_TIME_OUT;
- break;
-
- case NGX_HTTP_PROXY_FT_HTTP_500:
- status = NGX_HTTP_INTERNAL_SERVER_ERROR;
- break;
-
- case NGX_HTTP_PROXY_FT_HTTP_404:
- status = NGX_HTTP_NOT_FOUND;
- break;
-
- /*
- * NGX_HTTP_PROXY_FT_BUSY_LOCK and NGX_HTTP_PROXY_FT_MAX_WAITING
- * never reach here
- */
-
- default:
- status = NGX_HTTP_BAD_GATEWAY;
- }
- }
-
- if (p->upstream.connection) {
- ngx_http_proxy_close_connection(p->upstream.connection);
- p->upstream.connection = NULL;
- }
-
- if (status) {
- if (ngx_http_proxy_log_state(p, status) == NGX_ERROR) {
- ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
- return;
- }
-
- if (p->upstream.tries == 0 || !(p->lcf->next_upstream & ft_type)) {
-
- if (p->stale && (p->lcf->use_stale & ft_type)) {
- /*
- * TODO: use stale cached response if it exists and enabled
- */
- }
-
- ngx_http_proxy_finalize_request(p, status);
- return;
- }
- }
-
- ngx_http_proxy_connect(p);
-}
-
-
-static int ngx_http_proxy_log_state(ngx_http_proxy_ctx_t *p, int status)
+int ngx_http_proxy_log_state(ngx_http_proxy_ctx_t *p, int status)
{
ngx_http_proxy_state_t *state;
@@ -1282,20 +245,20 @@
state->status = status;
state->peer =
- &p->upstream.peers->peers[p->upstream.cur_peer].addr_port_text;
+ &p->upstream->peer.peers->peers[p->upstream->peer.cur_peer].addr_port_text;
return NGX_OK;
}
-static void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc)
+void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc)
{
ngx_log_debug(p->request->connection->log,
"finalize http proxy request");
- if (p->upstream.connection) {
- ngx_http_proxy_close_connection(p->upstream.connection);
- p->upstream.connection = NULL;
+ if (p->upstream->peer.connection) {
+ ngx_http_proxy_close_connection(p->upstream->peer.connection);
+ p->upstream->peer.connection = NULL;
}
if (p->header_sent
@@ -1311,7 +274,7 @@
}
-static void ngx_http_proxy_close_connection(ngx_connection_t *c)
+void ngx_http_proxy_close_connection(ngx_connection_t *c)
{
ngx_log_debug(c->log, "proxy close connection: %d" _ c->fd);
@@ -1354,20 +317,26 @@
}
-static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len)
+size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len)
{
ngx_http_proxy_ctx_t *p = data;
+ ngx_http_request_t *r;
+ ngx_peer_connection_t *peer;
+
+ r = p->request;
+ peer = &p->upstream->peer;
+
return ngx_snprintf(buf, len,
- " while %s, client: %s, URL: %s, upstream: %s%s%s%s%s",
- p->action,
- p->request->connection->addr_text.data,
- p->request->unparsed_uri.data,
- p->upstream.peers->peers[p->upstream.cur_peer].addr_port_text.data,
- p->lcf->upstream->uri.data,
- p->request->uri.data + p->lcf->upstream->location->len,
- p->request->args.len ? "?" : "",
- p->request->args.len ? p->request->args.data : "");
+ " while %s, client: %s, URL: %s, upstream: %s%s%s%s%s",
+ p->action,
+ r->connection->addr_text.data,
+ r->unparsed_uri.data,
+ peer->peers->peers[peer->cur_peer].addr_port_text.data,
+ p->lcf->upstream->uri.data,
+ r->uri.data + p->lcf->upstream->location->len,
+ r->args.len ? "?" : "",
+ r->args.len ? r->args.data : "");
}
@@ -1494,7 +463,7 @@
}
ngx_test_null(lcf->upstream,
- ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_upstream_t)),
+ ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_upstream_conf_t)),
NGX_CONF_ERROR);
lcf->upstream->url.len = value[1].len;
@@ -1606,7 +575,7 @@
static char *ngx_http_proxy_parse_upstream(ngx_str_t *url,
- ngx_http_proxy_upstream_t *u)
+ ngx_http_proxy_upstream_conf_t *u)
{
size_t i;
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.h b/src/http/modules/proxy/ngx_http_proxy_handler.h
index ed515b8..f29498f 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -11,93 +11,102 @@
typedef struct {
- ngx_str_t url;
- ngx_str_t host;
- ngx_str_t uri;
- ngx_str_t host_header;
- ngx_str_t port_text;
- ngx_str_t *location;
- int port;
-} ngx_http_proxy_upstream_t;
+ ngx_str_t url;
+ ngx_str_t host;
+ ngx_str_t uri;
+ ngx_str_t host_header;
+ ngx_str_t port_text;
+ ngx_str_t *location;
+ int port;
+} ngx_http_proxy_upstream_conf_t;
typedef struct {
- ssize_t request_buffer_size;
- ngx_msec_t connect_timeout;
- ngx_msec_t send_timeout;
- ssize_t header_buffer_size;
- ngx_msec_t read_timeout;
+ ssize_t request_buffer_size;
+ ngx_msec_t connect_timeout;
+ ngx_msec_t send_timeout;
+ ssize_t header_buffer_size;
+ ngx_msec_t read_timeout;
- ngx_bufs_t bufs;
- ssize_t busy_buffers_size;
+ ngx_bufs_t bufs;
+ ssize_t busy_buffers_size;
- ssize_t max_temp_file_size;
- ssize_t temp_file_write_size;
- int cyclic_temp_file;
+ ssize_t max_temp_file_size;
+ ssize_t temp_file_write_size;
+ int cyclic_temp_file;
- int cache;
- int pass_server;
+ int cache;
+ int pass_server;
- int next_upstream;
- int use_stale;
+ int next_upstream;
+ int use_stale;
- ngx_path_t *cache_path;
- ngx_path_t *temp_path;
+ ngx_path_t *cache_path;
+ ngx_path_t *temp_path;
- ngx_http_proxy_upstream_t *upstream;
- ngx_peers_t *peers;
+ ngx_http_proxy_upstream_conf_t *upstream;
+ ngx_peers_t *peers;
} ngx_http_proxy_loc_conf_t;
typedef struct {
- int status;
- ngx_str_t *peer;
+ int status;
+ ngx_str_t *peer;
} ngx_http_proxy_state_t;
typedef struct {
- ngx_table_t *headers; /* it must be first field */
+ ngx_table_t *headers; /* it must be first field */
- ngx_table_elt_t *date;
- ngx_table_elt_t *server;
- ngx_table_elt_t *connection;
- ngx_table_elt_t *content_type;
- ngx_table_elt_t *content_length;
- ngx_table_elt_t *last_modified;
- ngx_table_elt_t *accept_ranges;
+ ngx_table_elt_t *date;
+ ngx_table_elt_t *server;
- off_t content_length_n;
+ ngx_table_elt_t *expires;
+ ngx_table_elt_t *cache_control;
+ ngx_table_elt_t *x_accel_expires;
+
+ ngx_table_elt_t *connection;
+ ngx_table_elt_t *content_type;
+ ngx_table_elt_t *content_length;
+ ngx_table_elt_t *last_modified;
+ ngx_table_elt_t *accept_ranges;
+
+ off_t content_length_n;
} ngx_http_proxy_headers_in_t;
typedef struct {
- ngx_http_cache_ctx_t ctx;
- int status;
- ngx_str_t status_line;
- ngx_http_proxy_headers_in_t headers_in;
+ ngx_http_cache_ctx_t ctx;
+ int status;
+ ngx_str_t status_line;
+
+ ngx_http_proxy_headers_in_t headers_in;
} ngx_http_proxy_cache_t;
+typedef struct {
+ ngx_peer_connection_t peer;
+ int status;
+ ngx_str_t status_line;
+ int method;
+
+ ngx_output_chain_ctx_t *output_chain_ctx;
+ ngx_event_pipe_t *event_pipe;
+
+ ngx_http_proxy_headers_in_t headers_in;
+} ngx_http_proxy_upstream_t;
+
+
typedef struct ngx_http_proxy_ctx_s ngx_http_proxy_ctx_t;
struct ngx_http_proxy_ctx_s {
- ngx_peer_connection_t upstream;
- ngx_peer_t *peer;
-
ngx_http_request_t *request;
ngx_http_proxy_loc_conf_t *lcf;
+ ngx_http_proxy_upstream_t *upstream;
ngx_http_proxy_cache_t *cache;
- ngx_http_proxy_headers_in_t headers_in;
ngx_hunk_t *header_in;
- int status;
- ngx_str_t status_line;
- ngx_output_chain_ctx_t *output_chain_ctx;
-
- int method;
-
- ngx_event_pipe_t *event_pipe;
unsigned accel:1;
@@ -107,7 +116,9 @@
unsigned request_sent:1;
unsigned header_sent:1;
+
/* used to parse an upstream HTTP header */
+ int status;
char *status_start;
char *status_end;
int status_count;
@@ -121,8 +132,6 @@
};
-#define NGX_STALE 1
-
#define NGX_HTTP_PROXY_PARSE_NO_HEADER 20
#define NGX_HTTP_PROXY_FT_ERROR 2
@@ -134,11 +143,16 @@
#define NGX_HTTP_PROXY_FT_MAX_WAITING 128
-void ngx_http_proxy_reinit_upstream(ngx_http_proxy_ctx_t *p);
+int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p);
int ngx_http_proxy_get_cached_response(ngx_http_proxy_ctx_t *p);
-int ngx_http_proxy_process_cached_response(ngx_http_proxy_ctx_t *p);
int ngx_http_proxy_send_cached_response(ngx_http_proxy_ctx_t *p);
+int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p);
+
+int ngx_http_proxy_log_state(ngx_http_proxy_ctx_t *p, int status);
+size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len);
+void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc);
+void ngx_http_proxy_close_connection(ngx_connection_t *c);
int ngx_http_proxy_parse_status_line(ngx_http_proxy_ctx_t *p);
int ngx_http_proxy_copy_header(ngx_http_proxy_ctx_t *p,
diff --git a/src/http/modules/proxy/ngx_http_proxy_upstream.c b/src/http/modules/proxy/ngx_http_proxy_upstream.c
index 532e313..f969f33 100644
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -8,12 +8,279 @@
#include <ngx_http_proxy_handler.h>
-void ngx_http_proxy_reinit_upstream(ngx_http_proxy_ctx_t *p)
+static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p);
+static void ngx_http_proxy_init_upstream(void *data);
+static void ngx_http_proxy_reinit_upstream(ngx_http_proxy_ctx_t *p);
+static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p);
+static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p);
+static void ngx_http_proxy_send_request_handler(ngx_event_t *wev);
+static void ngx_http_proxy_dummy_handler(ngx_event_t *wev);
+static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev);
+static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev);
+static void ngx_http_proxy_process_upstream_header(ngx_http_proxy_ctx_t *p);
+static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *);
+static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p);
+static void ngx_http_proxy_process_body(ngx_event_t *ev);
+static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type);
+
+
+static ngx_str_t http_methods[] = {
+ ngx_string("GET "),
+ ngx_string("HEAD "),
+ ngx_string("POST ")
+};
+
+
+static char *upstream_header_errors[] = {
+ "upstream sent invalid header",
+ "upstream sent too long header line"
+};
+
+
+static char http_version[] = " HTTP/1.0" CRLF;
+static char host_header[] = "Host: ";
+static char connection_close_header[] = "Connection: close" CRLF;
+
+
+int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p)
+{
+ int rc;
+ ngx_http_request_t *r;
+ ngx_http_proxy_upstream_t *u;
+
+ r = p->request;
+
+ if (!(u = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_upstream_t)))) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ p->upstream = u;
+
+ u->peer.peers = p->lcf->peers;
+ u->peer.tries = p->lcf->peers->number;
+
+ ngx_init_array(p->states, r->pool, u->peer.tries,
+ sizeof(ngx_http_proxy_state_t),
+ NGX_HTTP_INTERNAL_SERVER_ERROR);
+
+ u->method = r->method;
+
+ /* STUB */ p->cachable = p->lcf->cache;
+
+ if (r->headers_in.content_length_n > 0) {
+ if (!(r->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ r->temp_file->file.fd = NGX_INVALID_FILE;
+ r->temp_file->file.log = r->connection->log;
+ r->temp_file->path = p->lcf->temp_path;
+ r->temp_file->pool = r->pool;
+ r->temp_file->warn = "a client request body is buffered "
+ "to a temporary file";
+ /* r->temp_file->persistent = 0; */
+
+ r->request_body_handler = ngx_http_proxy_init_upstream;
+ r->data = p;
+
+ rc = ngx_http_read_client_request_body(r, p->lcf->request_buffer_size);
+
+ if (rc == NGX_AGAIN) {
+ return NGX_DONE;
+ }
+
+ if (rc == NGX_ERROR) {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+ }
+
+ ngx_http_proxy_init_upstream(p);
+
+ return NGX_DONE;
+}
+
+
+static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_proxy_ctx_t *p)
+{
+ int i;
+ size_t len;
+ ngx_hunk_t *h;
+ ngx_chain_t *chain;
+ ngx_table_elt_t *header;
+ ngx_http_request_t *r;
+ ngx_http_proxy_upstream_conf_t *uc;
+
+ r = p->request;
+ uc = p->lcf->upstream;
+
+ len = http_methods[p->upstream->method - 1].len
+ + uc->uri.len
+ + r->uri.len - uc->location->len
+ + 1 + r->args.len /* 1 is for "?" */
+ + sizeof(http_version) - 1
+ + sizeof(host_header) - 1 + uc->host_header.len + 2
+ /* 2 is for "\r\n" */
+ + sizeof(connection_close_header) - 1
+ + 2; /* 2 is for "\r\n" at the header end */
+
+ header = (ngx_table_elt_t *) r->headers_in.headers->elts;
+ for (i = 0; i < r->headers_in.headers->nelts; i++) {
+
+ if (&header[i] == r->headers_in.host) {
+ continue;
+ }
+
+ if (&header[i] == r->headers_in.connection) {
+ continue;
+ }
+
+ /* 2 is for ": " and 2 is for "\r\n" */
+ len += header[i].key.len + 2 + header[i].value.len + 2;
+ }
+
+ /* STUB */ len++;
+
+ ngx_test_null(h, ngx_create_temp_hunk(r->pool, len), NULL);
+ ngx_alloc_link_and_set_hunk(chain, h, r->pool, NULL);
+
+
+ /* the request line */
+
+ h->last = ngx_cpymem(h->last, http_methods[p->upstream->method - 1].data,
+ http_methods[p->upstream->method - 1].len);
+
+ h->last = ngx_cpymem(h->last, uc->uri.data, uc->uri.len);
+
+ h->last = ngx_cpymem(h->last,
+ r->uri.data + uc->location->len,
+ r->uri.len - uc->location->len);
+
+ if (r->args.len > 0) {
+ *(h->last++) = '?';
+ h->last = ngx_cpymem(h->last, r->args.data, r->args.len);
+ }
+
+ h->last = ngx_cpymem(h->last, http_version, sizeof(http_version) - 1);
+
+
+ /* "Host" header */
+
+ h->last = ngx_cpymem(h->last, host_header, sizeof(host_header) - 1);
+ h->last = ngx_cpymem(h->last, uc->host_header.data, uc->host_header.len);
+ *(h->last++) = CR; *(h->last++) = LF;
+
+
+ /* "Connection: close" header */
+
+ h->last = ngx_cpymem(h->last, connection_close_header,
+ sizeof(connection_close_header) - 1);
+
+
+ for (i = 0; i < r->headers_in.headers->nelts; i++) {
+
+ if (&header[i] == r->headers_in.host) {
+ continue;
+ }
+
+ if (&header[i] == r->headers_in.connection) {
+ continue;
+ }
+
+ if (&header[i] == r->headers_in.keep_alive) {
+ continue;
+ }
+
+ h->last = ngx_cpymem(h->last, header[i].key.data, header[i].key.len);
+
+ *(h->last++) = ':'; *(h->last++) = ' ';
+
+ h->last = ngx_cpymem(h->last, header[i].value.data,
+ header[i].value.len);
+
+ *(h->last++) = CR; *(h->last++) = LF;
+
+ ngx_log_debug(r->connection->log, "proxy: '%s: %s'" _
+ header[i].key.data _ header[i].value.data);
+ }
+
+ /* add "\r\n" at the header end */
+ *(h->last++) = CR; *(h->last++) = LF;
+
+ /* STUB */ *(h->last) = '\0';
+ ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ h->pos);
+
+ return chain;
+}
+
+
+static void ngx_http_proxy_init_upstream(void *data)
+{
+ ngx_http_proxy_ctx_t *p = data;
+
+ ngx_chain_t *cl;
+ ngx_http_request_t *r;
+ ngx_output_chain_ctx_t *octx;
+ ngx_chain_writer_ctx_t *wctx;
+
+ r = p->request;
+
+ngx_log_debug(r->connection->log, "timer_set: %d" _
+ r->connection->read->timer_set);
+
+ if (r->connection->read->timer_set) {
+ ngx_del_timer(r->connection->read);
+ }
+
+ if (!(cl = ngx_http_proxy_create_request(p))) {
+ ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ if (r->request_hunks) {
+ cl->next = r->request_hunks;
+ }
+
+ r->request_hunks = cl;
+
+ p->upstream->peer.log = r->connection->log;
+ p->saved_ctx = r->connection->log->data;
+ p->saved_handler = r->connection->log->handler;
+ r->connection->log->data = p;
+ r->connection->log->handler = ngx_http_proxy_log_error;
+ p->action = "connecting to upstream";
+
+ if (!(octx = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t)))) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ p->upstream->output_chain_ctx = octx;
+
+ octx->sendfile = r->sendfile;
+ octx->pool = r->pool;
+ octx->bufs.num = 1;
+ octx->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
+ octx->output_filter = (ngx_output_chain_filter_pt) ngx_chain_writer;
+
+ if (!(wctx = ngx_palloc(r->pool, sizeof(ngx_chain_writer_ctx_t)))) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ octx->output_ctx = wctx;
+
+ wctx->pool = r->pool;
+
+ ngx_http_proxy_connect(p);
+}
+
+
+static void ngx_http_proxy_reinit_upstream(ngx_http_proxy_ctx_t *p)
{
ngx_chain_t *cl;
ngx_output_chain_ctx_t *octx;
- octx = p->output_chain_ctx;
+ octx = p->upstream->output_chain_ctx;
/* reinit the request chain */
@@ -32,7 +299,7 @@
if (p->header_in) {
if (p->cache) {
- p->header_in->pos = p->header_in->start + p->cache->ctx.header.size;
+ p->header_in->pos = p->header_in->start + p->cache->ctx.header_size;
p->header_in->last = p->header_in->pos;
} else {
@@ -41,3 +308,801 @@
}
}
}
+
+
+static void ngx_http_proxy_connect(ngx_http_proxy_ctx_t *p)
+{
+ int rc;
+ ngx_chain_t *cl;
+ ngx_connection_t *c;
+ ngx_http_request_t *r;
+ ngx_output_chain_ctx_t *octx;
+
+ p->action = "connecting to upstream";
+
+ rc = ngx_event_connect_peer(&p->upstream->peer);
+
+ if (rc == NGX_ERROR) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ if (rc == NGX_CONNECT_ERROR) {
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
+ return;
+ }
+
+ p->upstream->peer.connection->data = p;
+ p->upstream->peer.connection->write->event_handler =
+ ngx_http_proxy_send_request_handler;
+ p->upstream->peer.connection->read->event_handler =
+ ngx_http_proxy_process_upstream_status_line;
+
+ r = p->request;
+ c = p->upstream->peer.connection;
+ c->pool = r->pool;
+ c->read->log = c->write->log = c->log = r->connection->log;
+
+ octx = p->upstream->output_chain_ctx;
+
+ if (p->upstream->peer.tries > 1 && p->request_sent) {
+ ngx_http_proxy_reinit_upstream(p);
+ }
+
+ /* init or reinit ngx_output_chain() context */
+
+ if (r->request_body_hunk) {
+ if (!(octx->free = ngx_alloc_chain_link(r->pool))) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ octx->free->hunk = r->request_body_hunk;
+ octx->free->next = NULL;
+ octx->hunks = 1;
+
+ r->request_body_hunk->pos = r->request_body_hunk->start;
+ r->request_body_hunk->last = r->request_body_hunk->start;
+ }
+
+ p->request_sent = 0;
+
+ if (rc == NGX_AGAIN) {
+ ngx_add_timer(c->write, p->lcf->connect_timeout);
+ return;
+ }
+
+ /* rc == NGX_OK */
+
+#if 1 /* test only */
+
+ if (c->read->ready) {
+ /* post aio operation */
+ ngx_http_proxy_process_upstream_status_line(c->read);
+ return;
+ }
+#endif
+
+ ngx_http_proxy_send_request(p);
+}
+
+
+static void ngx_http_proxy_send_request(ngx_http_proxy_ctx_t *p)
+{
+ int rc;
+ ngx_connection_t *c;
+ ngx_chain_writer_ctx_t *wctx;
+
+ c = p->upstream->peer.connection;
+
+#if (HAVE_KQUEUE)
+
+ if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)
+ && !p->request_sent
+ && c->write->kq_eof)
+ {
+ ngx_log_error(NGX_LOG_ERR, c->log, c->write->kq_errno,
+ "connect() failed");
+
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
+ return;
+ }
+
+#endif
+
+ p->action = "sending request to upstream";
+
+ wctx = p->upstream->output_chain_ctx->output_ctx;
+ wctx->out = NULL;
+ wctx->last = &wctx->out;
+ wctx->connection = c;
+
+ rc = ngx_output_chain(p->upstream->output_chain_ctx,
+ p->request_sent ? NULL : p->request->request_hunks);
+
+ if (rc == NGX_ERROR) {
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
+ return;
+ }
+
+ p->request_sent = 1;
+
+ if (c->write->timer_set) {
+ ngx_del_timer(c->write);
+ }
+
+ if (rc == NGX_AGAIN) {
+ ngx_add_timer(c->write, p->lcf->send_timeout);
+
+ if (ngx_handle_write_event(c->write, /* STUB: lowat */ 0) == NGX_ERROR)
+ {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ return;
+ }
+
+ /* rc == NGX_OK */
+
+ if (c->tcp_nopush) {
+ if (ngx_tcp_push(c->fd) == NGX_ERROR) {
+ ngx_log_error(NGX_LOG_CRIT, c->log,
+ ngx_socket_errno,
+ ngx_tcp_push_n " failed");
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ c->tcp_nopush = 0;
+ return;
+ }
+
+#if 0
+ if (c->read->ready) {
+
+ /* post aio operation */
+
+ /*
+ * although we can post aio operation just in the end
+ * of ngx_http_proxy_connect() CHECK IT !!!
+ * it's better to do here because we postpone header buffer allocation
+ */
+
+ ngx_http_proxy_process_upstream_status_line(c->read);
+ return;
+ }
+#endif
+
+ p->upstream->peer.connection->write->event_handler =
+ ngx_http_proxy_dummy_handler;
+
+ if (ngx_handle_level_write_event(c->write) == NGX_ERROR) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+}
+
+
+static void ngx_http_proxy_send_request_handler(ngx_event_t *wev)
+{
+ ngx_connection_t *c;
+ ngx_http_proxy_ctx_t *p;
+
+ c = wev->data;
+ p = c->data;
+
+ if (wev->timedout) {
+ p->action = "sending request to upstream";
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
+ return;
+ }
+
+ ngx_http_proxy_send_request(p);
+}
+
+
+static void ngx_http_proxy_dummy_handler(ngx_event_t *wev)
+{
+ ngx_log_debug(wev->log, "dummy handler");
+}
+
+
+static void ngx_http_proxy_process_upstream_status_line(ngx_event_t *rev)
+{
+ int rc;
+ ssize_t n;
+ ngx_connection_t *c;
+ ngx_http_proxy_ctx_t *p;
+
+ c = rev->data;
+ p = c->data;
+ p->action = "reading upstream status line";
+
+ ngx_log_debug(rev->log, "http proxy process status line");
+
+ if (rev->timedout) {
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
+ return;
+ }
+
+ if (p->header_in == NULL) {
+ p->header_in = ngx_create_temp_hunk(p->request->pool,
+ p->lcf->header_buffer_size);
+ if (p->header_in == NULL) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ p->header_in->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
+
+ if (p->cache) {
+ p->header_in->pos += p->cache->ctx.header_size;
+ p->header_in->last = p->header_in->pos;
+ }
+ }
+
+ n = ngx_http_proxy_read_upstream_header(p);
+
+ if (n == NGX_AGAIN) {
+ return;
+ }
+
+ if (n == 0) {
+ ngx_log_error(NGX_LOG_ERR, rev->log, 0,
+ "upstream prematurely closed connection");
+ }
+
+ if (n == NGX_ERROR || n == 0) {
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
+ return;
+ }
+
+ p->upstream->peer.cached = 0;
+
+ rc = ngx_http_proxy_parse_status_line(p);
+
+ if (rc == NGX_AGAIN) {
+ if (p->header_in->pos == p->header_in->last) {
+ ngx_log_error(NGX_LOG_ERR, rev->log, 0,
+ "upstream sent too long status line");
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
+ }
+ return;
+ }
+
+ if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
+ ngx_log_error(NGX_LOG_ERR, rev->log, 0,
+ "upstream sent no valid HTTP/1.0 header");
+
+ if (p->accel) {
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
+
+ } else {
+ p->request->http_version = NGX_HTTP_VERSION_9;
+ p->upstream->status = NGX_HTTP_OK;
+ ngx_http_proxy_send_response(p);
+ }
+
+ return;
+ }
+
+ /* rc == NGX_OK */
+
+ p->upstream->status = p->status;
+
+ if (p->status == NGX_HTTP_INTERNAL_SERVER_ERROR) {
+
+ if (p->upstream->peer.tries > 1
+ && (p->lcf->next_upstream & NGX_HTTP_PROXY_FT_HTTP_500))
+ {
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_HTTP_500);
+ return;
+ }
+
+ if (p->upstream->peer.tries == 0
+ && p->stale
+ && (p->lcf->use_stale & NGX_HTTP_PROXY_FT_HTTP_500))
+ {
+ ngx_http_proxy_finalize_request(p,
+ ngx_http_proxy_send_cached_response(p));
+
+ return;
+ }
+ }
+
+ p->upstream->status_line.len = p->status_end - p->status_start;
+ p->upstream->status_line.data = ngx_palloc(p->request->pool,
+ p->upstream->status_line.len + 1);
+ if (p->upstream->status_line.data == NULL) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+ ngx_cpystrn(p->upstream->status_line.data, p->status_start,
+ p->upstream->status_line.len + 1);
+
+ ngx_log_debug(rev->log, "http proxy status %d '%s'" _
+ p->upstream->status _ p->upstream->status_line.data);
+
+ if (p->upstream->headers_in.headers) {
+ p->upstream->headers_in.headers->nelts = 0;
+ } else {
+ p->upstream->headers_in.headers = ngx_create_table(p->request->pool,
+ 20);
+ }
+
+ c->read->event_handler = ngx_http_proxy_process_upstream_headers;
+ ngx_http_proxy_process_upstream_headers(rev);
+}
+
+
+static void ngx_http_proxy_process_upstream_headers(ngx_event_t *rev)
+{
+ int i, rc;
+ ssize_t n;
+ ngx_table_elt_t *h;
+ ngx_connection_t *c;
+ ngx_http_request_t *r;
+ ngx_http_proxy_ctx_t *p;
+
+ c = rev->data;
+ p = c->data;
+ r = p->request;
+ p->action = "reading upstream headers";
+
+ ngx_log_debug(rev->log, "http proxy process header line");
+
+ if (rev->timedout) {
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_TIMEOUT);
+ return;
+ }
+
+ rc = NGX_AGAIN;
+
+ for ( ;; ) {
+ if (rc == NGX_AGAIN) {
+ n = ngx_http_proxy_read_upstream_header(p);
+
+ if (n == 0) {
+ ngx_log_error(NGX_LOG_ERR, rev->log, 0,
+ "upstream prematurely closed connection");
+ }
+
+ if (n == NGX_ERROR || n == 0) {
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_ERROR);
+ return;
+ }
+
+ if (n == NGX_AGAIN) {
+ return;
+ }
+ }
+
+ rc = ngx_http_parse_header_line(p->request, p->header_in);
+
+ if (rc == NGX_OK) {
+
+ /* a header line has been parsed successfully */
+
+ h = ngx_http_add_header(&p->upstream->headers_in,
+ ngx_http_proxy_headers_in);
+ if (h == NULL) {
+ ngx_http_proxy_finalize_request(p,
+ NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ h->key.len = r->header_name_end - r->header_name_start;
+ h->value.len = r->header_end - r->header_start;
+
+ h->key.data = ngx_palloc(p->request->pool,
+ h->key.len + 1 + h->value.len + 1);
+ if (h->key.data == NULL) {
+ ngx_http_proxy_finalize_request(p,
+ NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ h->value.data = h->key.data + h->key.len + 1;
+ ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
+ ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
+
+ for (i = 0; ngx_http_proxy_headers_in[i].name.len != 0; i++) {
+ if (ngx_http_proxy_headers_in[i].name.len != h->key.len) {
+ continue;
+ }
+
+ if (ngx_strcasecmp(ngx_http_proxy_headers_in[i].name.data,
+ h->key.data) == 0)
+ {
+ *((ngx_table_elt_t **) ((char *) &p->upstream->headers_in
+ + ngx_http_proxy_headers_in[i].offset)) = h;
+ break;
+ }
+ }
+
+ ngx_log_debug(c->log, "HTTP proxy header: '%s: %s'" _
+ h->key.data _ h->value.data);
+
+ continue;
+
+ } else if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
+
+ /* a whole header has been parsed successfully */
+
+ ngx_log_debug(c->log, "HTTP header done");
+
+ ngx_http_proxy_process_upstream_header(p);
+ ngx_http_proxy_send_response(p);
+ return;
+
+ } else if (rc != NGX_AGAIN) {
+
+ /* there was error while a header line parsing */
+
+ ngx_log_error(NGX_LOG_ERR, rev->log, 0,
+ upstream_header_errors[rc - NGX_HTTP_PARSE_HEADER_ERROR]);
+
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
+ return;
+ }
+
+ /* rc == NGX_AGAIN: a header line parsing is still not complete */
+
+ if (p->header_in->last == p->header_in->end) {
+ ngx_log_error(NGX_LOG_ERR, rev->log, 0,
+ "upstream sent too big header");
+
+ ngx_http_proxy_next_upstream(p, NGX_HTTP_PROXY_FT_INVALID_HEADER);
+ return;
+ }
+ }
+}
+
+
+static void ngx_http_proxy_process_upstream_header(ngx_http_proxy_ctx_t *p)
+{
+ time_t expires;
+
+ expires = ngx_http_parse_time(p->upstream->headers_in.expires->value.data,
+ p->upstream->headers_in.expires->value.len);
+
+ p->cache->ctx.header.expires = expires;
+}
+
+
+static ssize_t ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p)
+{
+ ssize_t n;
+ ngx_event_t *rev;
+
+ rev = p->upstream->peer.connection->read;
+
+ n = p->header_in->last - p->header_in->pos;
+
+ if (n > 0) {
+ return n;
+ }
+
+ n = ngx_recv(p->upstream->peer.connection, p->header_in->last,
+ p->header_in->end - p->header_in->last);
+
+ if (n == NGX_AGAIN) {
+ ngx_add_timer(rev, p->lcf->read_timeout);
+
+ if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return NGX_ERROR;
+ }
+
+ return NGX_AGAIN;
+ }
+
+ if (n == 0) {
+ ngx_log_error(NGX_LOG_ERR, rev->log, 0,
+ "upstream closed prematurely connection");
+ }
+
+ if (n == 0 || n == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+
+ p->header_in->last += n;
+
+ return n;
+}
+
+
+static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p)
+{
+ int rc, i;
+ ngx_table_elt_t *ho, *h;
+ ngx_event_pipe_t *ep;
+ ngx_http_request_t *r;
+ ngx_http_cache_file_t *header;
+ ngx_http_core_loc_conf_t *clcf;
+
+ r = p->request;
+
+ r->headers_out.status = p->upstream->status;
+
+#if 0
+ r->headers_out.content_length_n = -1;
+ r->headers_out.content_length = NULL;
+#endif
+
+ /* copy an upstream header to r->headers_out */
+
+ if (ngx_http_proxy_copy_header(p, &p->upstream->headers_in) == NGX_ERROR) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ /* TODO: preallocate event_pipe hunks, look "Content-Length" */
+
+ rc = ngx_http_send_header(r);
+
+ p->header_sent = 1;
+
+ if (p->cache) {
+ header = (ngx_http_cache_file_t *) p->header_in->start;
+
+ /* STUB */
+ header->header.expires = 0;
+ header->header.last_modified = 0;
+
+ header->header.length = r->headers_out.content_length_n;
+ header->key_len = p->cache->ctx.key.len;
+ ngx_memcpy(&header->key, p->cache->ctx.key.data, header->key_len);
+ header->key[header->key_len] = LF;
+ }
+
+ ep = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
+ if (ep == NULL) {
+ ngx_http_proxy_finalize_request(p, 0);
+ return;
+ }
+
+ p->upstream->event_pipe = ep;
+
+ ep->input_filter = ngx_event_pipe_copy_input_filter;
+ ep->output_filter = (ngx_event_pipe_output_filter_pt)
+ ngx_http_output_filter;
+ ep->output_ctx = r;
+ ep->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
+ ep->bufs = p->lcf->bufs;
+ ep->busy_size = p->lcf->busy_buffers_size;
+ ep->upstream = p->upstream->peer.connection;
+ ep->downstream = r->connection;
+ ep->pool = r->pool;
+ ep->log = r->connection->log;
+
+ if (!(ep->temp_file = ngx_pcalloc(r->pool, sizeof(ngx_temp_file_t)))) {
+ ngx_http_proxy_finalize_request(p, 0);
+ return;
+ }
+
+ ep->temp_file->file.fd = NGX_INVALID_FILE;
+ ep->temp_file->file.log = r->connection->log;
+ ep->temp_file->path = p->lcf->temp_path;
+ ep->temp_file->pool = r->pool;
+ ep->temp_file->warn = "an upstream response is buffered "
+ "to a temporary file";
+
+ ep->max_temp_file_size = p->lcf->max_temp_file_size;
+ ep->temp_file_write_size = p->lcf->temp_file_write_size;
+
+ ep->preread_hunks = ngx_alloc_chain_link(r->pool);
+ if (ep->preread_hunks == NULL) {
+ ngx_http_proxy_finalize_request(p, 0);
+ return;
+ }
+ ep->preread_hunks->hunk = p->header_in;
+ ep->preread_hunks->next = NULL;
+ p->header_in->type |= NGX_HUNK_PREREAD;
+
+ ep->preread_size = p->header_in->last - p->header_in->pos;
+
+ if (ngx_event_flags & NGX_USE_AIO_EVENT) {
+
+ /* the posted aio operation can currupt shadow buf */
+ ep->single_buf = 1;
+ }
+
+ /* TODO: ep->free_bufs = 0 if use ngx_create_chain_of_hunks() */
+ ep->free_bufs = 1;
+
+ /*
+ * event_pipe would do p->header_in->last += ep->preread_size
+ * as though these bytes were read.
+ */
+ p->header_in->last = p->header_in->pos;
+
+ ep->cachable = p->cachable;
+
+ if (p->lcf->cyclic_temp_file) {
+
+ /*
+ * we need to disable the use of sendfile() if we use cyclic temp file
+ * because the writing a new data can interfere with sendfile()
+ * that uses the same kernel file pages (at least on FreeBSD)
+ */
+
+ ep->cyclic_temp_file = 1;
+ r->sendfile = 0;
+
+ } else {
+ ep->cyclic_temp_file = 0;
+ r->sendfile = 1;
+ }
+
+ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+ ep->read_timeout = p->lcf->read_timeout;
+ ep->send_timeout = clcf->send_timeout;
+ ep->send_lowat = clcf->send_lowat;
+
+ p->upstream->peer.connection->read->event_handler =
+ ngx_http_proxy_process_body;
+ r->connection->write->event_handler = ngx_http_proxy_process_body;
+
+ ngx_http_proxy_process_body(p->upstream->peer.connection->read);
+
+ return;
+}
+
+
+static void ngx_http_proxy_process_body(ngx_event_t *ev)
+{
+ ngx_connection_t *c;
+ ngx_http_request_t *r;
+ ngx_http_proxy_ctx_t *p;
+ ngx_event_pipe_t *ep;
+
+ c = ev->data;
+
+ if (ev->write) {
+ ngx_log_debug(ev->log, "http proxy process downstream");
+ r = c->data;
+ p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
+ p->action = "sending to client";
+
+ } else {
+ ngx_log_debug(ev->log, "http proxy process upstream");
+ p = c->data;
+ r = p->request;
+ p->action = "reading upstream body";
+ }
+
+ ep = p->upstream->event_pipe;
+
+ if (ev->timedout) {
+ if (ev->write) {
+ ep->downstream_error = 1;
+ ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT,
+ "client timed out");
+
+ } else {
+ ep->upstream_error = 1;
+ ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT,
+ "upstream timed out");
+ }
+
+ } else {
+ if (ngx_event_pipe(ep, ev->write) == NGX_ABORT) {
+ ngx_http_proxy_finalize_request(p, 0);
+ return;
+ }
+ }
+
+ if (p->upstream->peer.connection) {
+ if (ep->upstream_done) {
+ if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
+ ngx_http_proxy_finalize_request(p, 0);
+ return;
+ }
+
+ } else if (ep->upstream_eof) {
+
+ /* TODO: check length & update cache */
+
+ if (ngx_http_proxy_update_cache(p) == NGX_ERROR) {
+ ngx_http_proxy_finalize_request(p, 0);
+ return;
+ }
+ }
+
+ if (ep->upstream_done || ep->upstream_eof || ep->upstream_error) {
+ ngx_http_proxy_close_connection(p->upstream->peer.connection);
+ p->upstream->peer.connection = NULL;
+ }
+ }
+
+ if (ep->downstream_done) {
+ ngx_log_debug(ev->log, "http proxy downstream done");
+ ngx_http_proxy_finalize_request(p, r->main ? 0 : ngx_http_send_last(r));
+ return;
+ }
+
+ if (ep->downstream_error) {
+ if (!p->cachable && p->upstream->peer.connection) {
+ ngx_http_proxy_close_connection(p->upstream->peer.connection);
+ p->upstream->peer.connection = NULL;
+ }
+
+ if (p->upstream->peer.connection == NULL) {
+ ngx_http_close_connection(c);
+ }
+ }
+
+ return;
+}
+
+
+static void ngx_http_proxy_next_upstream(ngx_http_proxy_ctx_t *p, int ft_type)
+{
+ int status;
+
+ngx_log_debug(p->request->connection->log, "next upstream: %d" _ ft_type);
+
+ if (ft_type != NGX_HTTP_PROXY_FT_HTTP_404) {
+ ngx_event_connect_peer_failed(&p->upstream->peer);
+ }
+
+ if (ft_type == NGX_HTTP_PROXY_FT_TIMEOUT) {
+ ngx_log_error(NGX_LOG_ERR, p->request->connection->log, NGX_ETIMEDOUT,
+ "upstream timed out");
+ }
+
+ if (p->upstream->peer.cached && ft_type == NGX_HTTP_PROXY_FT_ERROR) {
+ status = 0;
+
+ } else {
+ switch(ft_type) {
+ case NGX_HTTP_PROXY_FT_TIMEOUT:
+ status = NGX_HTTP_GATEWAY_TIME_OUT;
+ break;
+
+ case NGX_HTTP_PROXY_FT_HTTP_500:
+ status = NGX_HTTP_INTERNAL_SERVER_ERROR;
+ break;
+
+ case NGX_HTTP_PROXY_FT_HTTP_404:
+ status = NGX_HTTP_NOT_FOUND;
+ break;
+
+ /*
+ * NGX_HTTP_PROXY_FT_BUSY_LOCK and NGX_HTTP_PROXY_FT_MAX_WAITING
+ * never reach here
+ */
+
+ default:
+ status = NGX_HTTP_BAD_GATEWAY;
+ }
+ }
+
+ if (p->upstream->peer.connection) {
+ ngx_http_proxy_close_connection(p->upstream->peer.connection);
+ p->upstream->peer.connection = NULL;
+ }
+
+ if (status) {
+ if (ngx_http_proxy_log_state(p, status) == NGX_ERROR) {
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ if (p->upstream->peer.tries == 0 || !(p->lcf->next_upstream & ft_type))
+ {
+ if (p->stale && (p->lcf->use_stale & ft_type)) {
+ ngx_http_proxy_finalize_request(p,
+ ngx_http_proxy_send_cached_response(p));
+ return;
+ }
+
+ ngx_http_proxy_finalize_request(p, status);
+ return;
+ }
+ }
+
+ ngx_http_proxy_connect(p);
+}
diff --git a/src/http/ngx_http_cache.c b/src/http/ngx_http_cache.c
index 2612293..b3a7e46 100644
--- a/src/http/ngx_http_cache.c
+++ b/src/http/ngx_http_cache.c
@@ -8,12 +8,12 @@
int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx)
{
- int small;
- ssize_t n, len;
- MD5_CTX md5;
- ngx_err_t err;
- ngx_str_t key;
- ngx_http_bin_cache_t *h;
+ ssize_t n;
+ MD5_CTX md5;
+ ngx_err_t err;
+ ngx_http_cache_file_t *h;
+
+ ctx->header_size = sizeof(ngx_http_cache_file_t) + ctx->key.len + 1;
ctx->file.name.len = ctx->path->name.len + 1 + ctx->path->len + 32;
if (!(ctx->file.name.data = ngx_palloc(r->pool, ctx->file.name.len + 1))) {
@@ -43,13 +43,6 @@
err = ngx_errno;
if (err == NGX_ENOENT || err == NGX_ENOTDIR) {
-
- /* TODO: text size */
-
- ctx->header.size = 2 * sizeof(ssize_t)
- + sizeof(ngx_http_cache_header_t)
- + ctx->key.len + 1;
-
return NGX_DECLINED;
}
@@ -65,58 +58,31 @@
return n;
}
- len = 0;
- small = 1;
-
- if (n > 1) {
- if (ctx->buf->pos[0] == 'T') {
- /* STUB */
- return NGX_ERROR;
-
- } else if (ctx->buf->pos[0] == 'B') {
-
- len = sizeof(ngx_http_bin_cache_t);
-
- if (n > len) {
- h = (ngx_http_bin_cache_t *) ctx->buf->pos;
- key.len = h->key_len;
-
- if (n >= len + (ssize_t) key.len + 1) {
- ctx->header = h->header;
- key.data = h->key;
-
- small = 0;
- }
- }
-
- } else {
- ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
- "unknown type of cache file \"%s\"",
- ctx->file.name.data);
- return NGX_ERROR;
- }
-
- }
-
- if (small) {
+ if (n <= ctx->header_size) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
- "cache file \"%s\" is to small", ctx->file.name.data);
+ "cache file \"%s\" is too small", ctx->file.name.data);
return NGX_ERROR;
}
- if (key.len != ctx->key.len
- || ngx_strncmp(key.data, ctx->key.data, key.len) != 0)
+ h = (ngx_http_cache_file_t *) ctx->buf->pos;
+ ctx->header = h->header;
+
+ if (h->key_len != ctx->key.len
+ || ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0)
{
- key.data[key.len] = '\0';
+ h->key[h->key_len] = '\0';
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
"md5 collision: \"%s\" and \"%s\"",
- key.data, ctx->key.data);
+ h->key, ctx->key.data);
return NGX_DECLINED;
}
- ctx->header.size = len + key.len + 1;
ctx->buf->last += n;
+ if (ctx->header.expires < ngx_time()) {
+ return NGX_STALE;
+ }
+
return NGX_OK;
}
diff --git a/src/http/ngx_http_cache.h b/src/http/ngx_http_cache.h
index 47dd582..9e491ad 100644
--- a/src/http/ngx_http_cache.h
+++ b/src/http/ngx_http_cache.h
@@ -10,31 +10,14 @@
time_t expires;
time_t last_modified;
off_t length;
-
- size_t size;
} ngx_http_cache_header_t;
typedef struct {
- ssize_t type;
ngx_http_cache_header_t header;
- ssize_t key_len;
+ size_t key_len;
char key[0];
-} ngx_http_bin_cache_t;
-
-
-typedef struct {
- char type;
- char space0;
- char expires[8];
- char space1;
- char last_modified[8];
- char space2;
- char length[16];
- char space3;
- char lf;
- char key_len[0];
-} ngx_http_text_cache_t;
+} ngx_http_cache_file_t;
typedef struct {
@@ -57,15 +40,17 @@
ngx_path_t *path;
ngx_hunk_t *buf;
ngx_http_cache_header_t header;
+ ssize_t header_size;
} ngx_http_cache_ctx_t;
+#define NGX_STALE 1
+
+
int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx);
int ngx_http_cache_update_file(ngx_http_request_t *r,ngx_http_cache_ctx_t *ctx,
ngx_str_t *temp_file);
-
-
#endif /* _NGX_HTTP_CACHE_H_INCLUDED_ */
diff --git a/src/http/ngx_http_request_body.c b/src/http/ngx_http_request_body.c
index 2fcdd50..a337c3a 100644
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -18,15 +18,13 @@
size = r->header_in->last - r->header_in->pos;
if (size) {
- ngx_test_null(h, ngx_calloc_hunk(r->pool),
- NGX_HTTP_INTERNAL_SERVER_ERROR);
+ ngx_test_null(h, ngx_calloc_hunk(r->pool), NGX_ERROR);
h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
h->start = h->pos = r->header_in->pos;
h->end = h->last = r->header_in->last;
- ngx_alloc_link_and_set_hunk(r->request_hunks, h, r->pool,
- NGX_HTTP_INTERNAL_SERVER_ERROR);
+ ngx_alloc_link_and_set_hunk(r->request_hunks, h, r->pool, NGX_ERROR);
if (size >= r->headers_in.content_length_n) {
r->header_in->pos += r->headers_in.content_length_n;
@@ -48,7 +46,7 @@
}
ngx_test_null(r->request_body_hunk, ngx_create_temp_hunk(r->pool, size),
- NGX_HTTP_INTERNAL_SERVER_ERROR);
+ NGX_ERROR);
r->connection->read->event_handler =
ngx_http_read_client_request_body_handler;
@@ -56,7 +54,7 @@
ngx_http_read_client_request_body_handler(r->connection->read);
ngx_alloc_link_and_set_hunk(cl, r->request_body_hunk, r->pool,
- NGX_HTTP_INTERNAL_SERVER_ERROR);
+ NGX_ERROR);
if (r->request_hunks) {
r->request_hunks->next = cl;