changeset 83:a7e45c45a95c

nginx-0.0.1-2003-04-28-19:06:39 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 28 Apr 2003 15:06:39 +0000
parents fccdb921e8b8
children fab4cb00fe5b
files src/core/nginx.c src/core/ngx_conf_file.c src/core/ngx_conf_file.h src/core/ngx_file.c src/core/ngx_file.h src/core/ngx_log.c src/core/ngx_string.c src/event/modules/ngx_select_module.c src/event/ngx_event_proxy.c src/event/ngx_event_proxy.h src/http/modules/proxy/ngx_http_event_proxy_handler.c src/http/ngx_http.h src/http/ngx_http_output_filter.c src/http/ngx_http_request_body.c src/http/ngx_http_write_filter.c src/os/unix/freebsd/ngx_rfork_thread.h src/os/unix/ngx_recv_chain.c src/os/unix/ngx_x86_mutex.h
diffstat 18 files changed, 241 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -89,6 +89,8 @@ int main(int argc, char *const *argv)
         return 1;
     }
 
+    ngx_init_temp_number();
+
     for (i = 0; ngx_modules[i]; i++) {
         if (ngx_modules[i]->init_module) {
             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);
 
     /* TODO: daemon */
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -432,16 +432,34 @@ char *ngx_conf_set_str_slot(ngx_conf_t *
 
 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;
 
     value = (ngx_str_t *) cf->args->elts;
 
-    size = atoi(value[1].data);
-    if (size < 0) {
+    len = value[1].len;
+    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";
     }
 
+    size *= scale;
+
     *(int *) (conf + cmd->offset) = size;
 
     return NGX_CONF_OK;
@@ -450,17 +468,65 @@ char *ngx_conf_set_size_slot(ngx_conf_t 
 
 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;
 
     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) {
         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;
 }
--- a/src/core/ngx_conf_file.h
+++ b/src/core/ngx_conf_file.h
@@ -45,6 +45,7 @@ struct ngx_command_s {
     char    *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
     int        conf;
     int        offset;
+    void      *bounds;
 };
 
 
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -7,10 +7,14 @@
 #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,
-                         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;
 
     file->name.len = path->name.len + 1 + path->len + 10;
@@ -26,6 +30,8 @@ int ngx_create_temp_file(ngx_file_t *fil
 
     ngx_memcpy(file->name.data, path->name.data, path->name.len);
 
+    num = ngx_next_temp_number(0);
+
     for ( ;; ) {
         snprintf(file->name.data + path->name.len + 1 + path->len, 11,
                  "%010u", num);
@@ -58,7 +64,7 @@ ngx_log_debug(file->log, "temp fd: %d" _
         err = ngx_errno;
 
         if (err == NGX_EEXIST) {
-            num = (num + 1) * step;
+            num = ngx_next_temp_number(1);
             continue;
         }
 
@@ -140,3 +146,25 @@ int ngx_create_path(ngx_file_t *file, ng
 
     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++;
+}
--- a/src/core/ngx_file.h
+++ b/src/core/ngx_file.h
@@ -30,10 +30,12 @@ typedef struct {
 
 
 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);
 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_ */
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -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] anything"
@@ -58,14 +58,19 @@ void ngx_log_error_core(int level, ngx_l
 #endif
 
     if (err) {
+
 #if (WIN32)
-        if ((unsigned) err >= 0x80000000)
+        if ((unsigned) err >= 0x80000000) {
             len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
                                 " (%X: ", err);
-        else
-#endif
+        } else {
             len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
                                 " (%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);
         if (len < sizeof(errstr) - 2) {
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -27,14 +27,18 @@ int ngx_atoi(char *line, size_t n)
     int  value;
 
     for (value = 0; n--; line++) {
-       if (*line < '0' || *line > '9') {
-           return NGX_ERROR;
-       }
+        if (*line < '0' || *line > '9') {
+            return NGX_ERROR;
+        }
 
-       value = value * 10 + (*line - '0');
+        value = value * 10 + (*line - '0');
     }
 
-    return value;
+    if (value < 0) {
+        return NGX_ERROR;
+    } else {
+        return value;
+    }
 }
 
 
--- a/src/event/modules/ngx_select_module.c
+++ b/src/event/modules/ngx_select_module.c
@@ -242,6 +242,7 @@ int ngx_select_process_events(ngx_log_t 
 #endif
 
     if (timer) {
+        /* TODO: Linux returns time in tv */
         delta = ngx_msec() - delta;
         ngx_event_expire_timers(delta);
 
--- a/src/event/ngx_event_proxy.c
+++ b/src/event/ngx_event_proxy.c
@@ -600,7 +600,7 @@ ngx_log_debug(p->log, "write to file");
 
     if (p->temp_file->fd == NGX_INVALID_FILE) {
         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) {
             p->fatal_error = 1;
--- a/src/event/ngx_event_proxy.h
+++ b/src/event/ngx_event_proxy.h
@@ -75,8 +75,6 @@ struct ngx_event_proxy_s {
 
     ngx_file_t        *temp_file;
     ngx_path_t        *temp_path;
-    int                number;
-    int                random;
     char              *temp_file_warn;
 };
 
--- a/src/http/modules/proxy/ngx_http_event_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_event_proxy_handler.c
@@ -902,9 +902,6 @@ static int ngx_http_proxy_read_upstream_
         ep->temp_file->fd = NGX_INVALID_FILE;
         ep->temp_file->log = p->log;
 
-        ep->number = 10;
-        ep->random = 5;
-
         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_warn = "an upstream response is buffered "
@@ -1396,7 +1393,7 @@ static int ngx_http_proxy_init(ngx_pool_
         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);
 }
 
 
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -86,6 +86,18 @@ typedef struct {
 
 
 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 {
     int               status;
     ngx_str_t         status_line;
 
@@ -116,11 +128,12 @@ struct ngx_http_request_s {
 
     ngx_file_t           file;
 
-    ngx_pool_t          *pool;
-    ngx_hunk_t          *header_in;
+    ngx_pool_t               *pool;
+    ngx_hunk_t               *header_in;
+    ngx_http_request_body_t  *request_body;
 
-    ngx_http_headers_in_t   headers_in;
-    ngx_http_headers_out_t  headers_out;
+    ngx_http_headers_in_t     headers_in;
+    ngx_http_headers_out_t    headers_out;
 
     int  (*handler)(ngx_http_request_t *r);
 
@@ -224,6 +237,12 @@ int ngx_read_http_header_line(ngx_http_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_special_response_handler(ngx_http_request_t *r, int error);
 
--- a/src/http/ngx_http_output_filter.c
+++ b/src/http/ngx_http_output_filter.c
@@ -26,9 +26,10 @@ static ngx_command_t  ngx_http_output_fi
      NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_size_slot,
      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}
 };
 
 
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -1,56 +1,55 @@
 
-
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_http.h>
 
 
-int ngx_http_start_read_client_body(ngx_http_proxy_ctx_t *p)
+int ngx_http_init_client_request_body(ngx_http_request_t *r, int size)
 {
-    int                  first_part, size;
-    ngx_hunk_t          *h;
-    ngx_http_request_t  *r;
+    int                       header_in_part, len;
+    ngx_hunk_t               *h;
+    ngx_http_request_body_t  *rb;
+
+    ngx_test_null(rb, ngx_pcalloc(r->pool, sizeof(ngx_http_request_body_t)),
+                  NGX_HTTP_INTERNAL_SERVER_ERROR);
 
-    r = p->request;
-
-    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) {
-        first_part = r->headers_in.content_length_n;
-        size = 0;
+    if (header_in_part) {
+        rb->header_in_pos = r->header_in->pos;
+    }
+
+    if (header_in_part > r->headers_in.content_length_n) {
+        header_in_part = r->headers_in.content_length_n;
 
     } else {
-        size = r->headers_in.content_length_n - first_part;
-        if (size > p->lcf->client_request_buffer_size) {
-            size = p->lcf->client_request_buffer_size;
+        len = r->headers_in.content_length_n - header_in_part;
+        if (len > size) {
+            len = size;
 
-        } else if (size > NGX_PAGE_SIZE) {
-            size = ((size + NGX_PAGE_SIZE) / NGX_PAGE_SIZE) * NGX_PAGE_SIZE;
+        } else if (len > NGX_PAGE_SIZE) {
+            len = ((len + NGX_PAGE_SIZE - 1) / NGX_PAGE_SIZE) * NGX_PAGE_SIZE;
         }
 
-        if (size) {
-            ngx_test_null(p->client_request_hunk, ngx_palloc(r->pool, size),
-                          NGX_ERROR);
+        if (len) {
+            ngx_test_null(rb->hunk, ngx_create_temp_hunk(r->pool, len, 0, 0),
+                          NGX_HTTP_INTERNAL_SERVER_ERROR);
         }
     }
 
-    if (first_part) {
-        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;
-    }
+    r->request_body = rb;
 
     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 {
         if (r->header_in->last < r->header_in->end) {
@@ -70,7 +69,7 @@ int ngx_http_read_client_body(ngx_event_
             rb->chain[0].next = NULL;
         }
 
-        n = ngx_recv_chain(c, &rb->chain);
+        n = ngx_recv_chain(r->connection, rb->chain);
 
         if (n == NGX_ERROR) {
             return NGX_ERROR;
@@ -80,7 +79,7 @@ int ngx_http_read_client_body(ngx_event_
             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;
 
             if (n >= size) {
@@ -96,9 +95,9 @@ int ngx_http_read_client_body(ngx_event_
         }
 
         if (rb->hunk && rb->hunk->last == rb->hunk->end) {
-            if (rb->temp_file->fd == NGX_INVALID_FILE) {
-                rc = ngx_create_temp_file(rb->temp_file, rb->temp_path, r->pool,
-                                          rb->number, rb->random, 0);
+            if (rb->temp_file.fd == NGX_INVALID_FILE) {
+                rc = ngx_create_temp_file(&rb->temp_file, rb->temp_path,
+                                          r->pool, 0);
 
                 if (rc == NGX_ERROR) {
                     return NGX_ERROR;
@@ -109,7 +108,7 @@ int ngx_http_read_client_body(ngx_event_
                 }
             }
 
-            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);
 
             if (rc == NGX_ERROR) {
@@ -120,13 +119,13 @@ int ngx_http_read_client_body(ngx_event_
             rb->hunk->last = rb->hunk->pos;
         }
 
-    } while (rev->ready);
+    } while (r->connection->read->ready);
 
     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;
     ngx_hunk_t               *h;
@@ -143,7 +142,8 @@ int ngx_init_client_request_body_chain(n
         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) {
             ngx_test_null(h, ngx_alloc_hunk(r->pool), NGX_ERROR);
 
@@ -152,7 +152,7 @@ int ngx_init_client_request_body_chain(n
             h->last = h->end = h->post_end = 0;
             h->file_pos = 0;
             h->file_last = rb->offset;
-            h->file = rb->temp_file;
+            h->file = &rb->temp_file;
             h->shadow = NULL;
             h->tag = 0;
 
@@ -176,7 +176,7 @@ int ngx_init_client_request_body_chain(n
 }
 
 
-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;
 
@@ -187,7 +187,7 @@ int ngx_reinit_client_request_body_hunks
     }
 
     if (rb->file_hunk) {
-        rb->file_hunk->file_pos = rb->file_hunk->file_start;
+        rb->file_hunk->file_pos = 0;
     }
 
     if (rb->hunk) {
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -25,9 +25,10 @@ static ngx_command_t ngx_http_write_filt
      NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_size_slot,
      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}
 };
 
 
--- a/src/os/unix/freebsd/ngx_rfork_thread.h
+++ b/src/os/unix/freebsd/ngx_rfork_thread.h
@@ -16,6 +16,7 @@ static inline ngx_tid_t ngx_gettid()
     char *sp;
 
     __asm__ ("mov %%esp,%0" : "=r" (sp));
+
     return (sp > ngx_stacks_end) ? 0:
            (sp - ngx_stacks_start) / ngx_stack_size + 1;
 }
--- a/src/os/unix/ngx_recv_chain.c
+++ b/src/os/unix/ngx_recv_chain.c
@@ -6,20 +6,20 @@
 #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;
     ngx_err_t      err;
     ngx_array_t    io;
 
     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);
-        iov->iov_base = ce->hunk->pos;
-        iov->iov_len = ce->hunk->end - ce->hunk->last;
-        ce = ce->next;
+        iov->iov_base = entry->hunk->pos;
+        iov->iov_len = entry->hunk->end - entry->hunk->last;
+        entry = entry->next;
     }
 
 ngx_log_debug(c->log, "recv: %d:%d" _ io.nelts _ iov->iov_len);
new file mode 100644
--- /dev/null
+++ b/src/os/unix/ngx_x86_mutex.h
@@ -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));
+}