changeset 136:da00cde00e8a

nginx-0.0.1-2003-10-02-09:39:37 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 02 Oct 2003 05:39:37 +0000
parents e29909bd9b8a
children 2a615b036870
files src/core/nginx.h src/core/ngx_hunk.c src/event/ngx_event_connect.c src/event/ngx_event_connect.h src/http/modules/ngx_http_gzip_filter.c src/http/modules/ngx_http_not_modified_filter.c src/http/modules/ngx_http_range_filter.c src/http/modules/proxy/ngx_http_proxy_handler.c src/http/modules/proxy/ngx_http_proxy_handler.h src/http/ngx_http_request.h
diffstat 10 files changed, 156 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/nginx.h
+++ b/src/core/nginx.h
@@ -2,7 +2,7 @@
 #define _NGINX_H_INCLUDED_
 
 
-#define  NGINX_VER   "nginx/0.0.1"
+#define  NGINX_VER   "ng:nx/0.0.1"
 #define  NGINX_CONF  "nginx.conf"
 
 
--- a/src/core/ngx_hunk.c
+++ b/src/core/ngx_hunk.c
@@ -113,12 +113,13 @@ int ngx_chain_add_copy(ngx_pool_t *pool,
         ngx_test_null(ce, ngx_alloc_chain_entry(pool), NGX_ERROR);
 
         ce->hunk = in->hunk;
-        ce->next = NULL;
         *le = ce;
         le = &ce->next;
         in = in->next;
     }
 
+    *le = NULL;
+
     return NGX_OK;
 }
 
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -1,4 +1,7 @@
 
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_event.h>
 #include <ngx_event_connect.h>
 
 
@@ -6,10 +9,14 @@
 
 int ngx_event_connect_peer(ngx_peer_connection_t *pc)
 {
+    int                  rc, instance;
     time_t               now;
-    ngx_peer_r          *peer;
+    ngx_err_t            err;
+    ngx_peer_t          *peer;
     ngx_socket_t         s;
-    struct sockaddr_in  *addr;
+    ngx_event_t         *rev, *wev;
+    ngx_connection_t    *c;
+    struct sockaddr_in   addr;
 
     now = ngx_time();
 
@@ -31,6 +38,8 @@ int ngx_event_connect_peer(ngx_peer_conn
     pc->cached = 0;
     pc->connection = NULL;
 
+    peer = &pc->peers->peers[0];
+
     if (pc->peers->number > 1) {
 
         /* there are several peers */
@@ -41,7 +50,7 @@ int ngx_event_connect_peer(ngx_peer_conn
 
             pc->cur_peer = pc->peers->current++;
 
-            if (cp->peers->current >= cp->peers->number) {
+            if (pc->peers->current >= pc->peers->number) {
                 pc->peers->current = 0;
             }
         }
@@ -69,6 +78,7 @@ int ngx_event_connect_peer(ngx_peer_conn
 
                 if (pc->tries == 0) {
                     /* ngx_unlock_mutex(pc->peers->mutex); */
+
                     return NGX_ERROR;
                 }
             }
@@ -77,7 +87,9 @@ int ngx_event_connect_peer(ngx_peer_conn
 
     /* ngx_unlock_mutex(pc->peers->mutex); */
 
+#if 0
     pc->addr_port_text = peer->addr_port_text;
+#endif
 
     s = ngx_socket(AF_INET, SOCK_STREAM, IPPROTO_IP, 0);
 
@@ -163,13 +175,11 @@ int ngx_event_connect_peer(ngx_peer_conn
         }
     } 
 
-    addr = p->sockaddr;
+    addr.sin_family = AF_INET;
+    addr.sin_addr.s_addr = peer->addr;
+    addr.sin_port = htons(peer->port);
 
-    addr->sin_family = AF_INET;
-    addr->sin_addr.s_addr = peer->addr;
-    addr->sin_port = htons(peer->port);
-
-    rc = connect(s, p->sockaddr, sizeof(struct sockaddr_in));
+    rc = connect(s, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));
 
     if (rc == -1) {
         err = ngx_socket_errno;
@@ -185,9 +195,7 @@ int ngx_event_connect_peer(ngx_peer_conn
         }
     }
 
-    c->data = ???;
-
-
+    return NGX_OK;
 }
 
 
--- a/src/event/ngx_event_connect.h
+++ b/src/event/ngx_event_connect.h
@@ -7,6 +7,9 @@
 #include <ngx_event.h>
 
 
+#define NGX_CONNECT_ERROR   -10
+
+
 typedef struct {
     u_int32_t          addr;
     ngx_str_t          host;
@@ -19,15 +22,16 @@ typedef struct {
 
 
 typedef struct {
-    int                current;
-    int                number;
-    int                max_fails;
-    int                fail_timeout;
+    int                 current;
+    int                 number;
+    int                 max_fails;
+    int                 fail_timeout;
+    int                 last_cached;
 
- /* ngx_mutex_t       *mutex; */
-    ngx_connection_t  *cached;
+ /* ngx_mutex_t        *mutex; */
+    ngx_connection_t  **cached;
 
-    ngx_peer_t         peers[1];
+    ngx_peer_t          peers[1];
 } ngx_peers_t;
 
 
@@ -46,4 +50,8 @@ typedef struct {
 } ngx_peer_connection_t;
 
 
+int ngx_event_connect_peer(ngx_peer_connection_t *pc);
+void ngx_event_connect_peer_failed(ngx_peer_connection_t *pc);
+
+
 #endif /* _NGX_EVENT_CONNECT_H_INCLUDED_ */
--- a/src/http/modules/ngx_http_gzip_filter.c
+++ b/src/http/modules/ngx_http_gzip_filter.c
@@ -173,7 +173,7 @@ static int ngx_http_gzip_header_filter(n
 
 static int ngx_http_gzip_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
-    int                    rc, zin, zout;
+    int                    rc, wbits, mem_level, zin, zout;
     struct gztrailer      *trailer;
     ngx_hunk_t            *h;
     ngx_chain_t           *ce;
@@ -189,14 +189,27 @@ static int ngx_http_gzip_body_filter(ngx
     conf = ngx_http_get_module_loc_conf(r, ngx_http_gzip_filter_module);
 
     if (ctx->alloc == NULL) {
+        wbits = MAX_WBITS;
+        mem_level = MAX_MEM_LEVEL - 1;
+
+        if (ctx->length > 0) {
+
+            /* the actual zlib window size is smaller by 262 bytes */
+
+            while (ctx->length < ((1 << (wbits - 1)) - 262)) {
+                wbits--;
+                mem_level--;
+            }
+        }
+
 #if 0
         ngx_test_null(ctx->alloc, ngx_alloc(200K, r->log), NGX_ERROR);
 #else
         ctx->alloc = (void *) ~NULL;
 #endif
+
         rc = deflateInit2(&ctx->zstream, /**/ 1, Z_DEFLATED,
-                          /**/ -MAX_WBITS, /**/ MAX_MEM_LEVEL - 1,
-                          Z_DEFAULT_STRATEGY);
+                          -wbits, mem_level, Z_DEFAULT_STRATEGY);
 
         if (rc != Z_OK) {
             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
@@ -244,7 +257,7 @@ static int ngx_http_gzip_body_filter(ngx
                 ctx->in_hunk = ctx->in->hunk;
                 ctx->in = ctx->in->next;
 
-                ctx->zstream.next_in = ctx->in_hunk->pos;
+                ctx->zstream.next_in = (unsigned char *) ctx->in_hunk->pos;
                 ctx->zstream.avail_in = ctx->in_hunk->last - ctx->in_hunk->pos;
 
                 if (ctx->in_hunk->type & NGX_HUNK_LAST) {
@@ -283,7 +296,7 @@ static int ngx_http_gzip_body_filter(ngx
                     break;
                 }
 
-                ctx->zstream.next_out = ctx->out_hunk->pos;
+                ctx->zstream.next_out = (unsigned char *) ctx->out_hunk->pos;
                 ctx->zstream.avail_out = conf->hunk_size;
             }
 
@@ -302,7 +315,7 @@ ngx_log_debug(r->connection->log, "DEFLA
               ctx->zstream.next_in _ ctx->zstream.next_out _
               ctx->zstream.avail_in _ ctx->zstream.avail_out _ rc);
 
-            ctx->in_hunk->pos = ctx->zstream.next_in;
+            ctx->in_hunk->pos = (char *) ctx->zstream.next_in;
 
             if (ctx->zstream.avail_out == 0) {
                 ctx->out_hunk->last += conf->hunk_size;
@@ -313,7 +326,7 @@ ngx_log_debug(r->connection->log, "DEFLA
                 ctx->redo = 1;
 
             } else {
-                ctx->out_hunk->last = ctx->zstream.next_out;
+                ctx->out_hunk->last = (char *) ctx->zstream.next_out;
                 ctx->redo = 0;
 
                 if (ctx->flush == Z_SYNC_FLUSH) {
@@ -378,10 +391,11 @@ ngx_log_debug(r->connection->log, "DEFLA
 
                     ctx->zstream.avail_in = 0;
                     ctx->zstream.avail_out = 0;
+#if 0
+                    ngx_free(ctx->alloc);
+#endif
                     ngx_http_delete_ctx(r, ngx_http_gzip_filter_module);
-#if 0
-                    ngx_free();
-#endif
+
                     break;
 
                 } else if (conf->no_buffer && ctx->in == NULL) {
--- a/src/http/modules/ngx_http_not_modified_filter.c
+++ b/src/http/modules/ngx_http_not_modified_filter.c
@@ -57,8 +57,7 @@ static int ngx_http_not_modified_header_
         r->headers_out.content_length = -1;
         r->headers_out.content_type->key.len = 0;
         r->headers_out.content_type = NULL;
-
-        /* TODO: delete "Accept-Ranges" header
+        r->headers_out.accept_ranges->key.len = 0;
     }
 
     return next_header_filter(r);
--- a/src/http/modules/ngx_http_range_filter.c
+++ b/src/http/modules/ngx_http_range_filter.c
@@ -43,7 +43,6 @@ static int ngx_http_range_header_filter(
     int                           rc, boundary, len, i;
     char                         *p;
     off_t                         start, end;
-    ngx_table_elt_t              *accept_ranges;
     ngx_http_range_t             *range;
     ngx_http_range_filter_ctx_t  *ctx;
 
@@ -61,14 +60,14 @@ static int ngx_http_range_header_filter(
         || r->headers_in.range->value.len < 7
         || ngx_strncasecmp(r->headers_in.range->value.data, "bytes=", 6) != 0)
     {
-        ngx_test_null(accept_ranges,
+        ngx_test_null(r->headers_out.accept_ranges,
                       ngx_push_table(r->headers_out.headers),
                       NGX_ERROR);
 
-        accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
-        accept_ranges->key.data = "Accept-Ranges";
-        accept_ranges->value.len = sizeof("bytes") - 1;
-        accept_ranges->value.data = "bytes";
+        r->headers_out.accept_ranges->key.len = sizeof("Accept-Ranges") - 1;
+        r->headers_out.accept_ranges->key.data = "Accept-Ranges";
+        r->headers_out.accept_ranges->value.len = sizeof("bytes") - 1;
+        r->headers_out.accept_ranges->value.data = "bytes";
 
         return next_header_filter(r);
     }
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -9,6 +9,8 @@
 
 
 static void ngx_http_proxy_send_request(ngx_event_t *wev);
+static void ngx_http_proxy_close_connection(ngx_connection_t *c);
+static ngx_chain_t *ngx_http_proxy_copy_request_hunks(ngx_http_proxy_ctx_t *p);
 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
 
 
@@ -66,33 +68,50 @@ int ngx_http_proxy_handler(ngx_http_requ
     p->action = "connecting to upstream";
     p->request = r;
     p->upstream.peers = p->lcf->peers;
+    p->upstream.tries = p->lcf->peers->number;
 
-    /* TODO: change log->data, how to restore log->data ? */
+    /* TODO: log->data would be changed, how to restore log->data ? */
     p->upstream.log = r->connection->log;
 
-    do {
+    for ( ;; ) {
         rc = ngx_event_connect_peer(&p->upstream);
 
         if (rc == NGX_ERROR) {
             return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
+        if (rc == NGX_CONNECT_ERROR) {
+            ngx_event_connect_peer_failed(&p->upstream);
+
+            if (p->upstream.tries == 0) {
+                return NGX_HTTP_BAD_GATEWAY;
+            }
+        }
+
+        p->upstream.connection->data = p;
+        p->upstream.connection->write->event_handler =
+                                                   ngx_http_proxy_send_request;
+        p->upstream.connection->read->event_handler = /* STUB */ NULL;
+
+        if (p->upstream.tries > 1) {
+            ngx_test_null(p->work_request_hunks,
+                          ngx_http_proxy_copy_request_hunks(p),
+                          NGX_HTTP_INTERNAL_SERVER_ERROR);
+        } else {
+            p->work_request_hunks = p->request_hunks;
+        }
+
         if (rc == NGX_OK) {
             ngx_http_proxy_send_request(p->upstream.connection->write);
             return NGX_OK;
         }
 
-        if (rc == NGX_AGAIN) {
-            /* TODO */ return NGX_OK;
-        }
-
-        /* rc == NGX_CONNECT_FAILED */
+        /* rc == NGX_AGAIN */
 
-        ngx_event_connect_peer_failed(&p->upstream);
+        /* timer */
 
-    } while (p->upstream.tries);
-
-    return NGX_HTTP_BAD_GATEWAY;
+        /* TODO */ return NGX_OK;
+    }
 }
 
 
@@ -112,52 +131,45 @@ static void ngx_http_proxy_send_request(
         if (chain == (ngx_chain_t *) -1) {
             ngx_http_proxy_close_connection(c);
 
-            do {
+            for ( ;; ) {
                 rc = ngx_event_connect_peer(&p->upstream);
 
-                if (rc == NGX_OK) {
-
-                    /* copy chain and hunks p->request_hunks
-                       from p->initial_request_hunks */
-
-                    p->request_hunks = NULL;
-                    if (ngx_chain_add_copy(r->pool, p->request_hunks,
-                                        p->initial_request_hunks) == NGX_ERROR)
-                    {
-                        return NGX_HTTP_INTERNAL_SERVER_ERROR;
-                    }
-
-                    for (ce = p->request_hunks; ce; ce = ce->next) {
-                        ce->hunk->pos = ce->hunk->start;
-                    }
-
-
-                    c = p->connection;
-                    wev = c->write;
-
-                    break;
-                }
-
                 if (rc == NGX_ERROR) {
                     ngx_http_finalize_request(p->request,
                                               NGX_HTTP_INTERNAL_SERVER_ERROR);
                     return;
                 }
 
-                if (rc == NGX_AGAIN) {
-                    return;
+                if (rc == NGX_CONNECT_ERROR) {
+                    ngx_event_connect_peer_failed(&p->upstream);
+
+                    if (p->upstream.tries == 0) {
+                        return;
+                    }
                 }
 
-                /* rc == NGX_CONNECT_FAILED */
-
-                ngx_event_connect_peer_failed(&p->upstream);
+                if (p->upstream.tries > 1) {
+                    ngx_test_null(p->work_request_hunks,
+                                  ngx_http_proxy_copy_request_hunks(p),
+                                  /* void */);
+                } else {
+                    p->work_request_hunks = p->request_hunks;
+                }
 
-            } while (p->upstream.tries);
+                if (rc == NGX_OK) {
+                    c = p->connection;
+                    wev = c->write;
 
-            return;
+                    break;
+                }
+
+                /* rc == NGX_AGAIN */
+                return;
+
+            }
 
         } else {
-            p->request_hunks = chain;
+            p->work_request_hunks = chain;
 
             ngx_del_timer(wev);
 
@@ -175,6 +187,37 @@ static void ngx_http_proxy_send_request(
     }
 }
 
+static void ngx_http_proxy_close_connection(ngx_connection_t *c)
+{
+    return;
+}
+
+static ngx_chain_t *ngx_http_proxy_copy_request_hunks(ngx_http_proxy_ctx_t *p)
+{
+    ngx_chain_t  *ce, *te, *fe, **le;
+
+#if (NGX_SUPPRESS_WARN)
+    le = NULL;
+#endif
+
+    ngx_test_null(fe, ngx_alloc_chain_entry(p->request->pool), NULL);
+
+    te = fe;
+
+    for (ce = p->request_hunks; ce; ce = ce->next) {
+        te->hunk = ce->hunk;
+        *le = te;
+        le = &te->next;
+        ce->hunk->pos = ce->hunk->start;
+
+        ngx_test_null(te, ngx_alloc_chain_entry(p->request->pool), NULL);
+    }
+
+    *le = NULL;
+
+    return fe;
+}
+
 
 static size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len)
 {
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -26,6 +26,7 @@ struct ngx_http_proxy_ctx_s {
 
     ngx_http_proxy_loc_conf_t  *lcf;
 
+    ngx_chain_t                *work_request_hunks;
     ngx_chain_t                *request_hunks;
 
     char                       *action;
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -121,6 +121,7 @@ typedef struct {
     ngx_table_elt_t  *location;
     ngx_table_elt_t  *last_modified;
     ngx_table_elt_t  *content_range;
+    ngx_table_elt_t  *accept_ranges;
 
     ngx_str_t         charset;
     ngx_array_t       ranges;