nginx-0.0.1-2003-04-28-19:06:39 import

This commit is contained in:
Igor Sysoev 2003-04-28 15:06:39 +00:00
parent a09f08dbab
commit 1d8d9eee94
18 changed files with 241 additions and 93 deletions

View File

@ -89,6 +89,8 @@ int main(int argc, char *const *argv)
return 1; return 1;
} }
ngx_init_temp_number();
for (i = 0; ngx_modules[i]; i++) { for (i = 0; ngx_modules[i]; i++) {
if (ngx_modules[i]->init_module) { if (ngx_modules[i]->init_module) {
if (ngx_modules[i]->init_module(ngx_pool) == NGX_ERROR) { if (ngx_modules[i]->init_module(ngx_pool) == NGX_ERROR) {
@ -97,14 +99,6 @@ int main(int argc, char *const *argv)
} }
} }
#if 0
/* STUB */
/* TODO: init chain of global modules (like ngx_http.c),
they would init its modules and ngx_listening_sockets */
ngx_http_init(ngx_pool, &ngx_log);
#endif
ngx_open_listening_sockets(&ngx_log); ngx_open_listening_sockets(&ngx_log);
/* TODO: daemon */ /* TODO: daemon */

View File

@ -432,16 +432,34 @@ char *ngx_conf_set_str_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
{ {
int size; int size, len, scale;
char last;
ngx_str_t *value; ngx_str_t *value;
value = (ngx_str_t *) cf->args->elts; value = (ngx_str_t *) cf->args->elts;
size = atoi(value[1].data); len = value[1].len;
if (size < 0) { last = value[1].data[len - 1];
if (last == 'K' || last == 'k') {
len--;
scale = 1024;
} else if (last == 'M' || last == 'm') {
len--;
scale = 1024 * 1024;
} else {
scale = 1;
}
size = ngx_atoi(value[1].data, len);
if (size == NGX_ERROR) {
return "value must be greater or equal to zero"; return "value must be greater or equal to zero";
} }
size *= scale;
*(int *) (conf + cmd->offset) = size; *(int *) (conf + cmd->offset) = size;
return NGX_CONF_OK; return NGX_CONF_OK;
@ -450,17 +468,65 @@ char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
char *ngx_conf_set_time_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf) char *ngx_conf_set_time_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
{ {
int size; int size, len, scale;
char last;
ngx_str_t *value; ngx_str_t *value;
value = (ngx_str_t *) cf->args->elts; value = (ngx_str_t *) cf->args->elts;
size = atoi(value[1].data); len = value[1].len;
last = value[1].data[len - 1];
if (last == 'm') {
len--;
scale = 1000 * 60;
} else if (last == 'h') {
len--;
scale = 1000 * 60 * 60;
} else if (last == 'd') {
len--;
scale = 1000 * 60 * 60 * 24;
} else if (last == 'w') {
len--;
scale = 1000 * 60 * 60 * 24 * 7;
#if 0 /* overflow */
} else if (last == 'M') {
len--;
scale = 1000 * 60 * 60 * 24 * 30;
} else if (last == 'y') {
len--;
scale = 1000 * 60 * 60 * 24 * 365;
#endif
} else if (last == 's') {
len--;
if (value[1].data[len - 1] == 'm') {
len--;
scale = 1;
} else {
scale = 1000;
}
} else {
scale = 1000;
}
size = ngx_atoi(value[1].data, len);
if (size < 0) { if (size < 0) {
return "value must be greater or equal to zero"; return "value must be greater or equal to zero";
} }
*(int *) (conf + cmd->offset) = size * 1000; size *= scale;
*(int *) (conf + cmd->offset) = size;
return NGX_CONF_OK; return NGX_CONF_OK;
} }

View File

@ -45,6 +45,7 @@ struct ngx_command_s {
char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, char *conf); char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
int conf; int conf;
int offset; int offset;
void *bounds;
}; };

View File

@ -7,10 +7,14 @@
#include <ngx_files.h> #include <ngx_files.h>
static int ngx_temp_number;
static int ngx_random;
int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
ngx_pool_t *pool, int num, int step, int persistent) ngx_pool_t *pool, int persistent)
{ {
int i; int i, num;
ngx_err_t err; ngx_err_t err;
file->name.len = path->name.len + 1 + path->len + 10; file->name.len = path->name.len + 1 + path->len + 10;
@ -26,6 +30,8 @@ int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
ngx_memcpy(file->name.data, path->name.data, path->name.len); ngx_memcpy(file->name.data, path->name.data, path->name.len);
num = ngx_next_temp_number(0);
for ( ;; ) { for ( ;; ) {
snprintf(file->name.data + path->name.len + 1 + path->len, 11, snprintf(file->name.data + path->name.len + 1 + path->len, 11,
"%010u", num); "%010u", num);
@ -58,7 +64,7 @@ ngx_log_debug(file->log, "temp fd: %d" _ file->fd);
err = ngx_errno; err = ngx_errno;
if (err == NGX_EEXIST) { if (err == NGX_EEXIST) {
num = (num + 1) * step; num = ngx_next_temp_number(1);
continue; continue;
} }
@ -140,3 +146,25 @@ int ngx_create_path(ngx_file_t *file, ngx_path_t *path)
return NGX_OK; return NGX_OK;
} }
void ngx_init_temp_number()
{
ngx_random = 0;
ngx_temp_number = ngx_random;
while (ngx_random < 10000) {
ngx_random = 123456;
}
}
int ngx_next_temp_number(int collision)
{
if (collision) {
ngx_temp_number += ngx_random;
}
return ngx_temp_number++;
}

View File

@ -30,10 +30,12 @@ typedef struct {
int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path, int ngx_create_temp_file(ngx_file_t *file, ngx_path_t *path,
ngx_pool_t *pool, int num, int step, int persistent); ngx_pool_t *pool, int persistent);
void ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path); void ngx_create_hashed_filename(ngx_file_t *file, ngx_path_t *path);
int ngx_create_path(ngx_file_t *file, ngx_path_t *path); int ngx_create_path(ngx_file_t *file, ngx_path_t *path);
void ngx_init_temp_number();
int ngx_next_temp_number(int collision);
#endif /* _NGX_FILE_H_INCLUDED_ */ #endif /* _NGX_FILE_H_INCLUDED_ */

View File

@ -4,7 +4,7 @@
*/ */
/* /*
"[time as ctime()] [alert] 412:3 (32)Broken pipe: anything" "[time as ctime()] [alert] 412#3 (32)Broken pipe: anything"
"[time as ctime()] [alert] (32)Broken pipe: anything" "[time as ctime()] [alert] (32)Broken pipe: anything"
"[time as ctime()] [alert] anything" "[time as ctime()] [alert] anything"
@ -58,14 +58,19 @@ void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err,
#endif #endif
if (err) { if (err) {
#if (WIN32) #if (WIN32)
if ((unsigned) err >= 0x80000000) if ((unsigned) err >= 0x80000000) {
len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
" (%X: ", err); " (%X: ", err);
else } else {
#endif
len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
" (%d: ", err); " (%d: ", err);
}
#else
len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
" (%d: ", err);
#endif
len += ngx_strerror_r(err, errstr + len, sizeof(errstr) - len - 1); len += ngx_strerror_r(err, errstr + len, sizeof(errstr) - len - 1);
if (len < sizeof(errstr) - 2) { if (len < sizeof(errstr) - 2) {

View File

@ -27,14 +27,18 @@ int ngx_atoi(char *line, size_t n)
int value; int value;
for (value = 0; n--; line++) { for (value = 0; n--; line++) {
if (*line < '0' || *line > '9') { if (*line < '0' || *line > '9') {
return NGX_ERROR; return NGX_ERROR;
} }
value = value * 10 + (*line - '0'); value = value * 10 + (*line - '0');
} }
return value; if (value < 0) {
return NGX_ERROR;
} else {
return value;
}
} }

View File

@ -242,6 +242,7 @@ int ngx_select_process_events(ngx_log_t *log)
#endif #endif
if (timer) { if (timer) {
/* TODO: Linux returns time in tv */
delta = ngx_msec() - delta; delta = ngx_msec() - delta;
ngx_event_expire_timers(delta); ngx_event_expire_timers(delta);

View File

@ -600,7 +600,7 @@ ngx_log_debug(p->log, "write to file");
if (p->temp_file->fd == NGX_INVALID_FILE) { if (p->temp_file->fd == NGX_INVALID_FILE) {
rc = ngx_create_temp_file(p->temp_file, p->temp_path, p->pool, rc = ngx_create_temp_file(p->temp_file, p->temp_path, p->pool,
p->number, p->random, p->cachable); p->cachable);
if (rc == NGX_ERROR) { if (rc == NGX_ERROR) {
p->fatal_error = 1; p->fatal_error = 1;

View File

@ -75,8 +75,6 @@ struct ngx_event_proxy_s {
ngx_file_t *temp_file; ngx_file_t *temp_file;
ngx_path_t *temp_path; ngx_path_t *temp_path;
int number;
int random;
char *temp_file_warn; char *temp_file_warn;
}; };

View File

@ -902,9 +902,6 @@ static int ngx_http_proxy_read_upstream_header(ngx_http_proxy_ctx_t *p)
ep->temp_file->fd = NGX_INVALID_FILE; ep->temp_file->fd = NGX_INVALID_FILE;
ep->temp_file->log = p->log; ep->temp_file->log = p->log;
ep->number = 10;
ep->random = 5;
ep->max_temp_file_size = p->lcf->max_temp_file_size; ep->max_temp_file_size = p->lcf->max_temp_file_size;
ep->temp_file_write_size = p->lcf->temp_file_write_size; ep->temp_file_write_size = p->lcf->temp_file_write_size;
ep->temp_file_warn = "an upstream response is buffered " ep->temp_file_warn = "an upstream response is buffered "
@ -1396,7 +1393,7 @@ static int ngx_http_proxy_init(ngx_pool_t *pool)
path.len += path.level[i] + 1; path.len += path.level[i] + 1;
} }
return ngx_create_temp_file(&file, &path, pool, 123456789, 2, 0); return ngx_create_temp_file(&file, &path, pool, 0);
} }

View File

@ -85,6 +85,18 @@ typedef struct {
} ngx_http_headers_in_t; } ngx_http_headers_in_t;
typedef struct {
ngx_chain_t chain[4];
ngx_hunk_t *header_out;
ngx_hunk_t *hunk;
ngx_hunk_t *file_hunk;
ngx_file_t temp_file;
ngx_path_t *temp_path;
off_t offset;
char *header_in_pos;
} ngx_http_request_body_t;
typedef struct { typedef struct {
int status; int status;
ngx_str_t status_line; ngx_str_t status_line;
@ -116,11 +128,12 @@ struct ngx_http_request_s {
ngx_file_t file; ngx_file_t file;
ngx_pool_t *pool; ngx_pool_t *pool;
ngx_hunk_t *header_in; ngx_hunk_t *header_in;
ngx_http_request_body_t *request_body;
ngx_http_headers_in_t headers_in; ngx_http_headers_in_t headers_in;
ngx_http_headers_out_t headers_out; ngx_http_headers_out_t headers_out;
int (*handler)(ngx_http_request_t *r); int (*handler)(ngx_http_request_t *r);
@ -224,6 +237,12 @@ int ngx_read_http_header_line(ngx_http_request_t *r, ngx_hunk_t *h);
int ngx_http_handler(ngx_http_request_t *r); int ngx_http_handler(ngx_http_request_t *r);
int ngx_http_init_client_request_body(ngx_http_request_t *r, int size);
int ngx_http_read_client_request_body(ngx_http_request_t *r);
int ngx_http_init_client_request_body_chain(ngx_http_request_t *r);
void ngx_http_reinit_client_request_body_hunks(ngx_http_request_t *r);
int ngx_http_send_header(ngx_http_request_t *r); int ngx_http_send_header(ngx_http_request_t *r);
int ngx_http_special_response_handler(ngx_http_request_t *r, int error); int ngx_http_special_response_handler(ngx_http_request_t *r, int error);

View File

@ -26,9 +26,10 @@ static ngx_command_t ngx_http_output_filter_commands[] = {
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot, ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_output_filter_conf_t, hunk_size)}, offsetof(ngx_http_output_filter_conf_t, hunk_size),
NULL},
{ngx_null_string, 0, NULL, 0, 0} {ngx_null_string, 0, NULL, 0, 0, NULL}
}; };

View File

@ -1,56 +1,55 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
int ngx_http_init_client_request_body(ngx_http_request_t *r, int size)
int ngx_http_start_read_client_body(ngx_http_proxy_ctx_t *p)
{ {
int first_part, size; int header_in_part, len;
ngx_hunk_t *h; ngx_hunk_t *h;
ngx_http_request_t *r; ngx_http_request_body_t *rb;
r = p->request; ngx_test_null(rb, ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t)),
NGX_HTTP_INTERNAL_SERVER_ERROR);
first_part = r->header_in->last - r->header_in->pos; header_in_part = r->header_in->end - r->header_in->pos;
if (first_part > r->headers_in.content_length_n) { if (header_in_part) {
first_part = r->headers_in.content_length_n; rb->header_in_pos = r->header_in->pos;
size = 0; }
if (header_in_part > r->headers_in.content_length_n) {
header_in_part = r->headers_in.content_length_n;
} else { } else {
size = r->headers_in.content_length_n - first_part; len = r->headers_in.content_length_n - header_in_part;
if (size > p->lcf->client_request_buffer_size) { if (len > size) {
size = p->lcf->client_request_buffer_size; len = size;
} else if (size > NGX_PAGE_SIZE) { } else if (len > NGX_PAGE_SIZE) {
size = ((size + NGX_PAGE_SIZE) / NGX_PAGE_SIZE) * NGX_PAGE_SIZE; len = ((len + NGX_PAGE_SIZE - 1) / NGX_PAGE_SIZE) * NGX_PAGE_SIZE;
} }
if (size) { if (len) {
ngx_test_null(p->client_request_hunk, ngx_palloc(r->pool, size), ngx_test_null(rb->hunk, ngx_create_temp_hunk(r->pool, len, 0, 0),
NGX_ERROR); NGX_HTTP_INTERNAL_SERVER_ERROR);
} }
} }
if (first_part) { r->request_body = rb;
ngx_test_null(h, ngx_alloc_hunk(r->pool), NGX_ERROR);
h->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP;
h->pos = h->start = h->pre_start = r->header_in->pos;
h->last = h->end = h->post_end = r->header_in->pos + first_part;
h->file_pos = h->file_last = 0;
h->file = NULL;
h->shadow = NULL;
h->tag = 0;
p->client_first_part_hunk = h;
}
return NGX_OK; return NGX_OK;
} }
int ngx_http_read_client_body(ngx_event_t *rev) int ngx_http_read_client_request_body(ngx_http_request_t *r)
{ {
int size, n, rc;
ngx_chain_t *entry;
ngx_http_request_body_t *rb;
rb = r->request_body;
do { do {
if (r->header_in->last < r->header_in->end) { if (r->header_in->last < r->header_in->end) {
@ -70,7 +69,7 @@ int ngx_http_read_client_body(ngx_event_t *rev)
rb->chain[0].next = NULL; rb->chain[0].next = NULL;
} }
n = ngx_recv_chain(c, &rb->chain); n = ngx_recv_chain(r->connection, rb->chain);
if (n == NGX_ERROR) { if (n == NGX_ERROR) {
return NGX_ERROR; return NGX_ERROR;
@ -80,7 +79,7 @@ int ngx_http_read_client_body(ngx_event_t *rev)
return NGX_AGAIN; return NGX_AGAIN;
} }
for (entry = &rb->chain; entry; entry = entry->next) { for (entry = rb->chain; entry; entry = entry->next) {
size = entry->hunk->end - entry->hunk->last; size = entry->hunk->end - entry->hunk->last;
if (n >= size) { if (n >= size) {
@ -96,9 +95,9 @@ int ngx_http_read_client_body(ngx_event_t *rev)
} }
if (rb->hunk && rb->hunk->last == rb->hunk->end) { if (rb->hunk && rb->hunk->last == rb->hunk->end) {
if (rb->temp_file->fd == NGX_INVALID_FILE) { if (rb->temp_file.fd == NGX_INVALID_FILE) {
rc = ngx_create_temp_file(rb->temp_file, rb->temp_path, r->pool, rc = ngx_create_temp_file(&rb->temp_file, rb->temp_path,
rb->number, rb->random, 0); r->pool, 0);
if (rc == NGX_ERROR) { if (rc == NGX_ERROR) {
return NGX_ERROR; return NGX_ERROR;
@ -109,7 +108,7 @@ int ngx_http_read_client_body(ngx_event_t *rev)
} }
} }
n = ngx_write_file(rb->temp_file, rb->hunk, n = ngx_write_file(&rb->temp_file, rb->hunk->pos,
rb->hunk->last - rb->hunk->pos, rb->offset); rb->hunk->last - rb->hunk->pos, rb->offset);
if (rc == NGX_ERROR) { if (rc == NGX_ERROR) {
@ -120,13 +119,13 @@ int ngx_http_read_client_body(ngx_event_t *rev)
rb->hunk->last = rb->hunk->pos; rb->hunk->last = rb->hunk->pos;
} }
} while (rev->ready); } while (r->connection->read->ready);
return NGX_OK; return NGX_OK;
} }
int ngx_init_client_request_body_chain(ngx_http_reuqest_t *r) int ngx_http_init_client_request_body_chain(ngx_http_request_t *r)
{ {
int i; int i;
ngx_hunk_t *h; ngx_hunk_t *h;
@ -143,7 +142,8 @@ int ngx_init_client_request_body_chain(ngx_http_reuqest_t *r)
rb->chain[i].hunk = r->header_in; rb->chain[i].hunk = r->header_in;
} }
if (rb->temp_file->fd != NGX_INVALID_FILE) { if (rb->temp_file.fd != NGX_INVALID_FILE) {
if (rb->file_hunk == NULL) { if (rb->file_hunk == NULL) {
ngx_test_null(h, ngx_alloc_hunk(r->pool), NGX_ERROR); ngx_test_null(h, ngx_alloc_hunk(r->pool), NGX_ERROR);
@ -152,7 +152,7 @@ int ngx_init_client_request_body_chain(ngx_http_reuqest_t *r)
h->last = h->end = h->post_end = 0; h->last = h->end = h->post_end = 0;
h->file_pos = 0; h->file_pos = 0;
h->file_last = rb->offset; h->file_last = rb->offset;
h->file = rb->temp_file; h->file = &rb->temp_file;
h->shadow = NULL; h->shadow = NULL;
h->tag = 0; h->tag = 0;
@ -176,7 +176,7 @@ int ngx_init_client_request_body_chain(ngx_http_reuqest_t *r)
} }
int ngx_reinit_client_request_body_hunks(ngx_http_reuqest_t *r) void ngx_http_reinit_client_request_body_hunks(ngx_http_request_t *r)
{ {
ngx_http_request_body_t *rb; ngx_http_request_body_t *rb;
@ -187,7 +187,7 @@ int ngx_reinit_client_request_body_hunks(ngx_http_reuqest_t *r)
} }
if (rb->file_hunk) { if (rb->file_hunk) {
rb->file_hunk->file_pos = rb->file_hunk->file_start; rb->file_hunk->file_pos = 0;
} }
if (rb->hunk) { if (rb->hunk) {

View File

@ -25,9 +25,10 @@ static ngx_command_t ngx_http_write_filter_commands[] = {
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot, ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET, NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_write_filter_conf_t, buffer_output)}, offsetof(ngx_http_write_filter_conf_t, buffer_output),
NULL},
{ngx_null_string, 0, NULL, 0, 0} {ngx_null_string, 0, NULL, 0, 0, NULL}
}; };

View File

@ -16,6 +16,7 @@ static inline ngx_tid_t ngx_gettid()
char *sp; char *sp;
__asm__ ("mov %%esp,%0" : "=r" (sp)); __asm__ ("mov %%esp,%0" : "=r" (sp));
return (sp > ngx_stacks_end) ? 0: return (sp > ngx_stacks_end) ? 0:
(sp - ngx_stacks_start) / ngx_stack_size + 1; (sp - ngx_stacks_start) / ngx_stack_size + 1;
} }

View File

@ -6,20 +6,20 @@
#include <ngx_connection.h> #include <ngx_connection.h>
ssize_t ngx_recv_chain(ngx_connection_t *c, ngx_chain_t *ce) ssize_t ngx_recv_chain(ngx_connection_t *c, ngx_chain_t *entry)
{ {
ssize_t n; ssize_t n;
struct iovec *iov; struct iovec *iov;
ngx_err_t err; ngx_err_t err;
ngx_array_t io; ngx_array_t io;
ngx_init_array(io, c->pool, 10, sizeof(struct iovec), NGX_ERROR); ngx_init_array(io, c->pool, 10, sizeof(struct iovec), NGX_ERROR);
while (ce) { while (entry) {
ngx_test_null(iov, ngx_push_array(&io), NGX_ERROR); ngx_test_null(iov, ngx_push_array(&io), NGX_ERROR);
iov->iov_base = ce->hunk->pos; iov->iov_base = entry->hunk->pos;
iov->iov_len = ce->hunk->end - ce->hunk->last; iov->iov_len = entry->hunk->end - entry->hunk->last;
ce = ce->next; entry = entry->next;
} }
ngx_log_debug(c->log, "recv: %d:%d" _ io.nelts _ iov->iov_len); ngx_log_debug(c->log, "recv: %d:%d" _ io.nelts _ iov->iov_len);

View File

@ -0,0 +1,30 @@
typedef struct {
int lock;
} ngx_mutex_t;
static inline int ngx_spin_lock(ngx_mutex_t *m, int count)
{
int lock;
__asm__ __volatile("
get_lock:
mov $1, %1
xchg %1, %2
cmp $0, %1
jne spin_lock
spin_lock:
cmp $0, %3
je failed
dec %3
rep nop
cmp $0, %2
jne spin_lock
": "=q" (lock), "m" (m->lock), "q" (count));
}