changeset 20:a649c0a0adb3

nginx-0.0.1-2002-12-03-18:45:38 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 03 Dec 2002 15:45:38 +0000
parents d7908993fdeb
children df7fb216a149
files src/http/modules/ngx_http_event_proxy_handler.c src/http/modules/ngx_http_event_proxy_handler.h src/http/ngx_http.h src/http/ngx_http_event.c
diffstat 4 files changed, 116 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/http/modules/ngx_http_event_proxy_handler.c
+++ b/src/http/modules/ngx_http_event_proxy_handler.c
@@ -4,12 +4,14 @@
 #include <ngx_string.h>
 #include <ngx_file.h>
 #include <ngx_hunk.h>
+#include <ngx_event_write.h>
 #include <ngx_http.h>
 #include <ngx_http_event_proxy_handler.h>
 
 ngx_http_module_t  ngx_http_proxy_module;
 
 
+static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_request_t *r);
 static int ngx_http_proxy_connect(ngx_http_request_t *r,
                                   struct sockaddr_in *addr,
                                   char *addr_text);
@@ -18,7 +20,8 @@ static int ngx_http_proxy_send_request(n
 
 int ngx_http_proxy_handler(ngx_http_request_t *r)
 {
-    struct sockaddr_in addr;
+    struct sockaddr_in     addr;
+    ngx_chain_t           *chain;
     ngx_http_proxy_ctx_t  *p;
 
     p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module);
@@ -27,6 +30,12 @@ int ngx_http_proxy_handler(ngx_http_requ
         ngx_http_create_ctx(r, p, ngx_http_proxy_module,
                             sizeof(ngx_http_proxy_ctx_t));
 
+    chain = ngx_http_proxy_create_request(r);
+    if (chain == NULL)
+        return NGX_ERROR;
+
+    p->out = chain;
+
     ngx_memzero(&addr, sizeof(struct sockaddr_in));
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = inet_addr("127.0.0.1");
@@ -35,6 +44,69 @@ int ngx_http_proxy_handler(ngx_http_requ
     ngx_http_proxy_connect(r, &addr, "connecting to 127.0.0.1:9000");
 }
 
+
+static ngx_chain_t *ngx_http_proxy_create_request(ngx_http_request_t *r)
+{
+    int     i;
+    size_t  len;
+    ngx_hunk_t       *hunk;
+    ngx_chain_t      *chain;
+    ngx_table_elt_t  *header;
+
+    /* STUB */
+    int size = 1024;
+
+    /* "+ 2" is for "\r\n" */
+    len = r->request_line.len + 2;
+
+    header = (ngx_table_elt_t *) r->headers_in.headers->elts;
+    for (i = 0; i < r->headers_in.headers->nelts; i++) {
+        if (&header[i] == r->headers_in.host)
+            continue;
+
+        /* "+ 4" is for ": " and "\r\n" */
+        len += header[i].key.len + header[i].value.len + 4;
+    }
+
+    /* add "\r\n" at the header end */
+    len += 2;
+
+    /* STUB */ len++;
+
+    ngx_test_null(hunk, ngx_create_temp_hunk(r->pool, len, 0, 0), NULL);
+    ngx_add_hunk_to_chain(chain, hunk, r->pool, NULL);
+
+    ngx_memcpy(hunk->last.mem, r->request_line.data, r->request_line.len);
+    hunk->last.mem += r->request_line.len;
+    *(hunk->last.mem++) = CR; *(hunk->last.mem++) = LF;
+
+    for (i = 0; i < r->headers_in.headers->nelts; i++) {
+        if (&header[i] == r->headers_in.host)
+            continue;
+
+        ngx_memcpy(hunk->last.mem, header[i].key.data, header[i].key.len);
+        hunk->last.mem += header[i].key.len;
+
+        *(hunk->last.mem++) = ':'; *(hunk->last.mem++) = ' ';
+
+        ngx_memcpy(hunk->last.mem, header[i].value.data, header[i].value.len);
+        hunk->last.mem += header[i].value.len;
+
+        *(hunk->last.mem++) = CR; *(hunk->last.mem++) = LF;
+
+        ngx_log_debug(r->connection->log, "proxy: '%s: %s'" _
+                  header[i].key.data _ header[i].value.data);
+    }
+
+    /* add "\r\n" at the header end */
+    *(hunk->last.mem++) = CR; *(hunk->last.mem++) = LF;
+
+    /* STUB */ *(hunk->last.mem++) = '\0';
+    ngx_log_debug(r->connection->log, "PROXY:\n'%s'" _ hunk->pos.mem);
+
+    return chain;
+}
+
 static int ngx_http_proxy_connect(ngx_http_request_t *r,
                                   struct sockaddr_in *addr,
                                   char *addr_text)
@@ -42,8 +114,7 @@ static int ngx_http_proxy_connect(ngx_ht
     int                  rc;
     ngx_err_t            err;
     ngx_socket_t         s;
-    ngx_event_t         *ev;
-    ngx_connection_t    *c;
+    ngx_connection_t    *c, *pc;
     ngx_http_log_ctx_t  *ctx;
 
     c = r->connection;
@@ -99,6 +170,8 @@ static int ngx_http_proxy_connect(ngx_ht
         }
     }
 
+    pc = &ngx_connections[s];
+
     ngx_memzero(&ngx_read_events[s], sizeof(ngx_event_t));
     ngx_memzero(&ngx_write_events[s], sizeof(ngx_event_t));
     ngx_memzero(&ngx_connections[s], sizeof(ngx_connection_t));
@@ -107,6 +180,8 @@ static int ngx_http_proxy_connect(ngx_ht
     ngx_connections[s].read = &ngx_read_events[s];
     ngx_connections[s].write = &ngx_write_events[s];
 
+    ngx_connections[s].data = r;
+
     ngx_connections[s].fd = s;
     ngx_connections[s].server = c->server;
     ngx_connections[s].servers = c->servers;
@@ -114,6 +189,10 @@ static int ngx_http_proxy_connect(ngx_ht
     ngx_connections[s].log =
         ngx_read_events[s].log = ngx_write_events[s].log = c->log;
 
+    ngx_test_null(pc->pool,
+                  ngx_create_pool(/* STUB */ 1024 /* */, pc->log),
+                  NGX_ERROR);
+
     if (rc == -1) {
         ngx_write_events[s].event_handler = ngx_http_proxy_send_request;
 
@@ -124,12 +203,28 @@ static int ngx_http_proxy_connect(ngx_ht
     ngx_write_events[s].write = 1;
     ngx_write_events[s].ready = 1;
 
-    return ngx_http_proxy_send_request(ev);
+    return ngx_http_proxy_send_request(&ngx_write_events[s]);
 }
 
 static int ngx_http_proxy_send_request(ngx_event_t *ev)
 {
-    return NGX_ERROR;
+    ngx_chain_t           *chain;
+    ngx_connection_t      *c;
+    ngx_http_request_t    *r;
+    ngx_http_proxy_ctx_t  *p;
+
+    c = (ngx_connection_t *) ev->data;
+    r = (ngx_http_request_t *) c->data;
+    p = (ngx_http_proxy_ctx_t *) ngx_get_module_ctx(r, ngx_http_proxy_module);
+
+    chain = ngx_event_write(c, p->out, 0);
+    if (chain == (ngx_chain_t *) -1)
+        return NGX_ERROR;
+
+    p->out = chain;
+
+    /* STUB */ return NGX_ERROR;
+    return NGX_OK;
 }
 
 #if 0
--- a/src/http/modules/ngx_http_event_proxy_handler.h
+++ b/src/http/modules/ngx_http_event_proxy_handler.h
@@ -8,7 +8,7 @@
 
 
 typedef struct {
-    int dummy;
+    ngx_chain_t  *out;
 } ngx_http_proxy_ctx_t;
 
 
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -115,6 +115,7 @@ struct ngx_http_request_s {
     int    http_major;
     int    http_minor;
 
+    ngx_str_t  request_line;
     char  *uri;
     char  *exten;
     ngx_http_request_t *main;
--- a/src/http/ngx_http_event.c
+++ b/src/http/ngx_http_event.c
@@ -270,11 +270,19 @@ static int ngx_http_process_request_line
     c = r->connection;
 
     if (rc == NGX_OK) {
-        ngx_test_null(r->uri,
-                      ngx_palloc(r->pool, r->uri_end - r->uri_start + 1), 
+        len = r->uri_end - r->uri_start + 1;
+        ngx_test_null(r->uri, ngx_palloc(r->pool, len),
                       ngx_http_close_request(r));
-        ngx_cpystrn(r->uri, r->uri_start, r->uri_end - r->uri_start + 1);
+        ngx_cpystrn(r->uri, r->uri_start, len);
 
+        r->request_line.len = r->request_end - r->header_in->start;
+        ngx_test_null(r->request_line.data,
+                      ngx_palloc(r->pool, r->request_line.len + 1),
+                      ngx_http_close_request(r));
+        ngx_cpystrn(r->request_line.data, r->header_in->start,
+                    r->request_line.len + 1);
+
+        /* TEMP */
         ngx_test_null(request, ngx_push_array(c->requests),
                       ngx_http_close_request(r));
 
@@ -288,6 +296,7 @@ static int ngx_http_process_request_line
         ngx_cpystrn(*request, r->header_in->start, len);
 
         ngx_log_debug(c->log, "REQ: '%s'" _ *request);
+        /* */
 
         if (r->uri_ext) {
             ngx_test_null(r->exten,
@@ -372,7 +381,7 @@ static int ngx_http_process_request_head
     int  i;
     ngx_table_elt_t *h;
 
-    ngx_test_null(h, ngx_push_array(r->headers_in.headers), NGX_ERROR);
+    ngx_test_null(h, ngx_push_table(r->headers_in.headers), NGX_ERROR);
 
     h->key.len = r->header_name_end - r->header_name_start;
     ngx_test_null(h->key.data, ngx_palloc(r->pool, h->key.len + 1), NGX_ERROR);
@@ -583,7 +592,7 @@ static int ngx_http_set_default_handler(
     int   err, rc;
     char *name, *loc, *file;
 
-#if 0
+#if 1
     /* STUB */
     r->handler = ngx_http_proxy_handler;
     return NGX_OK;