nginx-0.0.1-2003-11-03-20:33:31 import

This commit is contained in:
Igor Sysoev 2003-11-03 17:33:31 +00:00
parent 659774979f
commit a1512b1904
7 changed files with 1245 additions and 1226 deletions

View File

@ -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 @@ int ngx_http_proxy_get_cached_response(ngx_http_proxy_ctx_t *p)
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 @@ int ngx_http_proxy_get_cached_response(ngx_http_proxy_ctx_t *p)
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_get_cached_response(ngx_http_proxy_ctx_t *p)
}
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 @@ int ngx_http_proxy_process_cached_response(ngx_http_proxy_ctx_t *p)
"\"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 @@ int ngx_http_proxy_process_cached_response(ngx_http_proxy_ctx_t *p)
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 @@ int ngx_http_proxy_process_cached_response(ngx_http_proxy_ctx_t *p)
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 @@ int ngx_http_proxy_process_cached_response(ngx_http_proxy_ctx_t *p)
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 @@ int ngx_http_proxy_process_cached_response(ngx_http_proxy_ctx_t *p)
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 @@ int ngx_http_proxy_process_cached_response(ngx_http_proxy_ctx_t *p)
"\"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 @@ int ngx_http_proxy_send_cached_response(ngx_http_proxy_ctx_t *p)
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 @@ int ngx_http_proxy_send_cached_response(ngx_http_proxy_ctx_t *p)
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);
}

File diff suppressed because it is too large Load Diff

View File

@ -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 @@ struct ngx_http_proxy_ctx_s {
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 @@ struct ngx_http_proxy_ctx_s {
};
#define NGX_STALE 1
#define NGX_HTTP_PROXY_PARSE_NO_HEADER 20
#define NGX_HTTP_PROXY_FT_ERROR 2
@ -134,11 +143,16 @@ struct ngx_http_proxy_ctx_s {
#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,

File diff suppressed because it is too large Load Diff

View File

@ -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 @@ ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data);
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 @@ ngx_log_debug(r->connection->log, "FILE: %s" _ ctx->file.name.data);
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;
}

View File

@ -10,31 +10,14 @@ typedef struct {
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 @@ typedef struct {
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_ */

View File

@ -18,15 +18,13 @@ int ngx_http_read_client_request_body(ngx_http_request_t *r,
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 @@ int ngx_http_read_client_request_body(ngx_http_request_t *r,
}
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 @@ int ngx_http_read_client_request_body(ngx_http_request_t *r,
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;