nginx-0.0.1-2003-11-19-00:34:08 import
diff --git a/src/http/modules/ngx_http_static_handler.c b/src/http/modules/ngx_http_static_handler.c
index c922870..69f1a14 100644
--- a/src/http/modules/ngx_http_static_handler.c
+++ b/src/http/modules/ngx_http_static_handler.c
@@ -200,8 +200,10 @@
*last++ = '/';
*last = '\0';
+#if 0
r->headers_out.location->key.len = 8;
r->headers_out.location->key.data = "Location" ;
+#endif
r->headers_out.location->value.len = last - location;
r->headers_out.location->value.data = location;
diff --git a/src/http/modules/proxy/ngx_http_proxy_cache.c b/src/http/modules/proxy/ngx_http_proxy_cache.c
index 28ffde1..958471b 100644
--- a/src/http/modules/proxy/ngx_http_proxy_cache.c
+++ b/src/http/modules/proxy/ngx_http_proxy_cache.c
@@ -55,6 +55,7 @@
p->header_in->tag = (ngx_hunk_tag_t) &ngx_http_proxy_module;
c->ctx.buf = p->header_in;
+ c->ctx.log = r->connection->log;
return ngx_http_proxy_process_cached_response(p,
ngx_http_cache_get_file(r, &c->ctx));
@@ -341,8 +342,7 @@
*ctx = p->cache->ctx;
- rc = ngx_http_cache_open_file(p->request, ctx,
- ngx_file_uniq(&p->cache->ctx.file.info));
+ rc = ngx_http_cache_open_file(ctx, ngx_file_uniq(&p->cache->ctx.file.info));
if (rc == NGX_HTTP_CACHE_THE_SAME) {
p->try_busy_lock = 1;
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c
index 83b9066..7a012c8 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -114,14 +114,14 @@
ngx_conf_set_path_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_proxy_loc_conf_t, cache_path),
- NULL },
+ ngx_garbage_collector_http_cache_handler },
{ ngx_string("proxy_temp_path"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
ngx_conf_set_path_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_proxy_loc_conf_t, temp_path),
- NULL },
+ ngx_garbage_collector_temp_handler },
{ ngx_string("proxy_temp_file_write_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
@@ -255,6 +255,8 @@
offsetof(ngx_http_proxy_headers_in_t, content_length) },
{ ngx_string("Last-Modified"),
offsetof(ngx_http_proxy_headers_in_t, last_modified) },
+ { ngx_string("Location"),
+ offsetof(ngx_http_proxy_headers_in_t, location) },
{ ngx_string("Accept-Ranges"),
offsetof(ngx_http_proxy_headers_in_t, accept_ranges) },
@@ -932,6 +934,7 @@
clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
lcf->upstream->location = &clcf->name;
clcf->handler = ngx_http_proxy_handler;
+ clcf->auto_redirect = 1;
return NULL;
}
diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.h b/src/http/modules/proxy/ngx_http_proxy_handler.h
index e822a3f..0258de5 100644
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -116,6 +116,7 @@
ngx_table_elt_t *content_type;
ngx_table_elt_t *content_length;
ngx_table_elt_t *last_modified;
+ ngx_table_elt_t *location;
ngx_table_elt_t *accept_ranges;
off_t content_length_n;
diff --git a/src/http/modules/proxy/ngx_http_proxy_header.c b/src/http/modules/proxy/ngx_http_proxy_header.c
index 4dda797..76d3321 100644
--- a/src/http/modules/proxy/ngx_http_proxy_header.c
+++ b/src/http/modules/proxy/ngx_http_proxy_header.c
@@ -5,6 +5,9 @@
#include <ngx_http_proxy_handler.h>
+static int ngx_http_proxy_rewrite_location_header(ngx_http_proxy_ctx_t *p,
+ ngx_table_elt_t *loc);
+
int ngx_http_proxy_copy_header(ngx_http_proxy_ctx_t *p,
ngx_http_proxy_headers_in_t *headers_in)
{
@@ -36,6 +39,16 @@
if (&h[i] == headers_in->server && !p->lcf->pass_server) {
continue;
}
+
+ if (&h[i] == headers_in->location) {
+ if (ngx_http_proxy_rewrite_location_header(p, &h[i])
+ == NGX_ERROR)
+ {
+ return NGX_ERROR;
+ }
+
+ continue;
+ }
}
if (&h[i] == headers_in->content_type) {
@@ -79,3 +92,47 @@
return NGX_OK;
}
+
+
+static int ngx_http_proxy_rewrite_location_header(ngx_http_proxy_ctx_t *p,
+ ngx_table_elt_t *loc)
+{
+ char *last;
+ ngx_http_request_t *r;
+ ngx_http_proxy_upstream_conf_t *uc;
+
+ r = p->request;
+ uc = p->lcf->upstream;
+
+ r->headers_out.location = ngx_http_add_header(&r->headers_out,
+ ngx_http_headers_out);
+ if (r->headers_out.location == NULL) {
+ return NGX_ERROR;
+ }
+
+ if (uc->url.len > loc->value.len
+ || ngx_rstrncmp(loc->value.data, uc->url.data, uc->url.len) != 0)
+ {
+ *r->headers_out.location = *loc;
+ return NGX_OK;
+ }
+
+ /* TODO: proxy_reverse */
+
+ r->headers_out.location->value.len = uc->location->len
+ + (loc->value.len - uc->url.len) + 1;
+ r->headers_out.location->value.data =
+ ngx_palloc(r->pool, r->headers_out.location->value.len);
+
+ if (r->headers_out.location->value.data == NULL) {
+ return NGX_ERROR;
+ }
+
+ last = ngx_cpymem(r->headers_out.location->value.data,
+ uc->location->data, uc->location->len);
+
+ ngx_cpystrn(last, loc->value.data + uc->url.len,
+ loc->value.len - uc->url.len + 1);
+
+ return NGX_OK;
+}
diff --git a/src/http/modules/proxy/ngx_http_proxy_upstream.c b/src/http/modules/proxy/ngx_http_proxy_upstream.c
index 135050e..12a4c62 100644
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -1182,8 +1182,8 @@
}
if (p->request->connection->write->eof) {
- ngx_http_proxy_finalize_request(p, status ? status:
- NGX_HTTP_CLIENT_CLOSED_REQUEST);
+ ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
+ return;
}
if (status) {
diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h
index aef53e8..f4f68ff 100644
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -4,6 +4,8 @@
#include <ngx_config.h>
#include <ngx_core.h>
+#include <ngx_garbage_collector.h>
+
#include <ngx_http_request.h>
#include <ngx_http_config.h>
#include <ngx_http_cache.h>
diff --git a/src/http/ngx_http_cache.c b/src/http/ngx_http_cache.c
index b3245a0..8eb62eb 100644
--- a/src/http/ngx_http_cache.c
+++ b/src/http/ngx_http_cache.c
@@ -3,6 +3,7 @@
#include <ngx_core.h>
#include <ngx_http.h>
+
#include <md5.h>
#if (HAVE_OPENSSL_MD5)
@@ -41,14 +42,13 @@
/* TODO: look open files cache */
- return ngx_http_cache_open_file(r, ctx, 0);
+ return ngx_http_cache_open_file(ctx, 0);
}
/* TODO: Win32 inode analogy */
-int ngx_http_cache_open_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx,
- ngx_file_uniq_t uniq)
+int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq)
{
ssize_t n;
ngx_err_t err;
@@ -64,14 +64,14 @@
return NGX_DECLINED;
}
- ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
+ ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
ngx_open_file_n " \"%s\" failed", ctx->file.name.data);
return NGX_ERROR;
}
if (uniq) {
if (ngx_fd_info(ctx->file.fd, &ctx->file.info) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
+ ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
ngx_fd_info_n " \"%s\" failed", ctx->file.name.data);
return NGX_ERROR;
@@ -79,7 +79,7 @@
if (ngx_file_uniq(&ctx->file.info) == uniq) {
if (ngx_close_file(ctx->file.fd) == NGX_FILE_ERROR) {
- ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
+ ngx_log_error(NGX_LOG_ALERT, ctx->log, ngx_errno,
ngx_close_file_n " \"%s\" failed",
ctx->file.name.data);
}
@@ -96,7 +96,7 @@
}
if (n <= ctx->header_size) {
- ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
+ ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
"cache file \"%s\" is too small", ctx->file.name.data);
return NGX_ERROR;
}
@@ -108,17 +108,18 @@
ctx->length = h->length;
if (h->key_len > (size_t) (ctx->buf->last - ctx->buf->pos)) {
- ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
+ ngx_log_error(NGX_LOG_ALERT, ctx->log, 0,
"cache file \"%s\" is probably invalid",
ctx->file.name.data);
return NGX_DECLINED;
}
- if (h->key_len != ctx->key.len
- || ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0)
+ if (ctx->key.len
+ && (h->key_len != ctx->key.len
+ || ngx_strncmp(h->key, ctx->key.data, h->key_len) != 0))
{
h->key[h->key_len] = '\0';
- ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
+ ngx_log_error(NGX_LOG_ALERT, ctx->log, 0,
"md5 collision: \"%s\" and \"%s\"",
h->key, ctx->key.data);
return NGX_DECLINED;
@@ -127,7 +128,7 @@
ctx->buf->last += n;
if (ctx->expires < ngx_time()) {
-ngx_log_debug(r->connection->log, "EXPIRED");
+ngx_log_debug(ctx->log, "EXPIRED");
return NGX_HTTP_CACHE_STALE;
}
@@ -137,6 +138,50 @@
}
+int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name,
+ ngx_dir_t *dir)
+{
+ int rc;
+ char data[sizeof(ngx_http_cache_header_t)];
+ ngx_hunk_t buf;
+ ngx_http_cache_ctx_t ctx;
+
+ ctx.file.fd = NGX_INVALID_FILE;
+ ctx.file.name = *name;
+ ctx.file.log = gc->log;
+
+ ctx.header_size = sizeof(ngx_http_cache_header_t);
+ ctx.buf = &buf;
+ ctx.log = gc->log;
+ ctx.key.len = 0;
+
+ buf.type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
+ buf.pos = data;
+ buf.last = data;
+ buf.start = data;
+ buf.end = data + sizeof(ngx_http_cache_header_t);
+
+ rc = ngx_http_cache_open_file(&ctx, 0);
+
+ /* TODO: NGX_AGAIN */
+
+ if (rc != NGX_ERROR && rc != NGX_DECLINED && rc != NGX_HTTP_CACHE_STALE) {
+ return NGX_OK;
+ }
+
+ if (ngx_delete_file(name->data) == NGX_FILE_ERROR) {
+ ngx_log_error(NGX_LOG_CRIT, gc->log, ngx_errno,
+ ngx_delete_file_n " \"%s\" failed", name->data);
+ return NGX_ERROR;
+ }
+
+ gc->deleted++;
+ gc->freed += ngx_de_size(dir);
+
+ return NGX_OK;
+}
+
+
int ngx_http_cache_update_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx,
ngx_str_t *temp_file)
{
diff --git a/src/http/ngx_http_cache.h b/src/http/ngx_http_cache.h
index 67ca223..70a7928 100644
--- a/src/http/ngx_http_cache.h
+++ b/src/http/ngx_http_cache.h
@@ -43,6 +43,7 @@
off_t length;
ssize_t header_size;
size_t file_start;
+ ngx_log_t *log;
} ngx_http_cache_ctx_t;
@@ -52,8 +53,9 @@
int ngx_http_cache_get_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx);
-int ngx_http_cache_open_file(ngx_http_request_t *r, ngx_http_cache_ctx_t *ctx,
- ngx_file_uniq_t uniq);
+int ngx_http_cache_open_file(ngx_http_cache_ctx_t *ctx, ngx_file_uniq_t uniq);
+int ngx_garbage_collector_http_cache_handler(ngx_gc_t *gc, ngx_str_t *name,
+ ngx_dir_t *dir);
int ngx_http_cache_update_file(ngx_http_request_t *r,ngx_http_cache_ctx_t *ctx,
ngx_str_t *temp_file);
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 529f2ee..f4f5d4f 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -369,16 +369,27 @@
int ngx_http_find_location_config(ngx_http_request_t *r)
{
int i, rc;
+ ngx_str_t *auto_redirect;
ngx_http_core_loc_conf_t *clcf, **clcfp;
ngx_http_core_srv_conf_t *cscf;
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
+ auto_redirect = NULL;
clcfp = cscf->locations.elts;
for (i = 0; i < cscf->locations.nelts; i++) {
#if 0
ngx_log_debug(r->connection->log, "trans: %s" _ clcfp[i]->name.data);
#endif
+ if (clcfp[i]->auto_redirect
+ && r->uri.len == clcfp[i]->name.len - 1
+ && ngx_strncmp(r->uri.data, clcfp[i]->name.data,
+ clcfp[i]->name.len - 1) == 0)
+ {
+ auto_redirect = &clcfp[i]->name;
+ continue;
+ }
+
if (r->uri.len < clcfp[i]->name.len) {
continue;
}
@@ -386,6 +397,7 @@
rc = ngx_strncmp(r->uri.data, clcfp[i]->name.data, clcfp[i]->name.len);
if (rc < 0) {
+ /* locations are lexicographically sorted */
break;
}
@@ -406,6 +418,22 @@
r->sendfile = 1;
}
+ if (auto_redirect) {
+ if (!(r->headers_out.location =
+ ngx_http_add_header(&r->headers_out, ngx_http_headers_out)))
+ {
+ return NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+#if 0
+ r->headers_out.location->key.len = 8;
+ r->headers_out.location->key.data = "Location";
+#endif
+ r->headers_out.location->value = *auto_redirect;
+
+ return NGX_HTTP_MOVED_PERMANENTLY;
+ }
+
if (clcf->handler) {
/*
* if the location already has content handler then skip
diff --git a/src/http/ngx_http_core_module.h b/src/http/ngx_http_core_module.h
index 7bd7eea..695d2e5 100644
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -128,6 +128,8 @@
int msie_padding; /* msie_padding */
ngx_array_t *error_pages; /* error_page */
+ unsigned auto_redirect:1;
+
ngx_log_t *err_log;
} ngx_http_core_loc_conf_t;
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
index ef26a60..2a28a67 100644
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -51,9 +51,11 @@
/*
* HTTP does not define a code for the case when a client closed a connection
- * while we are processing request so we introduce own code to log this case
+ * while we are processing request so we introduce own code to log such
+ * situation when client has closed a connection before we even try to
+ * send HTTP header to it
*/
-#define NGX_HTTP_CLIENT_CLOSED_REQUEST 420
+#define NGX_HTTP_CLIENT_CLOSED_REQUEST 499
#define NGX_HTTP_INTERNAL_SERVER_ERROR 500
#define NGX_HTTP_NOT_IMPLEMENTED 501