From 1a4998723886155d39f780c8668ce4a806f57590 Mon Sep 17 00:00:00 2001 From: phk Date: Thu, 23 Mar 2006 12:20:30 +0000 Subject: [PATCH] Now we're starting to get somewhere: Accept connections and assemble a HTTP request. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@61 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/Makefile.am | 4 +- varnish-cache/bin/varnishd/cache.h | 14 +++ varnish-cache/bin/varnishd/cache_acceptor.c | 111 ++++++++++++++++++++ varnish-cache/bin/varnishd/cache_main.c | 9 +- varnish-cache/bin/varnishd/mgt_child.c | 10 +- varnish-cache/bin/varnishd/tcp.c | 9 +- varnish-cache/bin/varnishd/varnishd.c | 15 ++- 7 files changed, 161 insertions(+), 11 deletions(-) create mode 100644 varnish-cache/bin/varnishd/cache.h create mode 100644 varnish-cache/bin/varnishd/cache_acceptor.c diff --git a/varnish-cache/bin/varnishd/Makefile.am b/varnish-cache/bin/varnishd/Makefile.am index 4f89f09f..0f49ac31 100644 --- a/varnish-cache/bin/varnishd/Makefile.am +++ b/varnish-cache/bin/varnishd/Makefile.am @@ -5,6 +5,7 @@ INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/contrib/libevent bin_PROGRAMS = varnishd varnishd_SOURCES = \ + cache_acceptor.c \ cache_main.c \ cli_event.c \ mgt_child.c \ @@ -14,4 +15,5 @@ varnishd_SOURCES = \ varnishd_LDADD = \ $(top_builddir)/lib/libvarnish/libvarnish.la \ $(top_builddir)/lib/libsbuf/libsbuf.la \ - $(top_builddir)/contrib/libevent/libevent.la + $(top_builddir)/contrib/libevent/libevent.la \ + -lpthread diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h new file mode 100644 index 00000000..0f371315 --- /dev/null +++ b/varnish-cache/bin/varnishd/cache.h @@ -0,0 +1,14 @@ +/* + * $Id$ + */ + +#define VCA_RXBUFSIZE 1024 +struct sess { + int fd; + char rcv[VCA_RXBUFSIZE + 1]; + unsigned rcv_len; + struct event rd_e; +}; + +/* cache_acceptor.c */ +void *vca_main(void *arg); diff --git a/varnish-cache/bin/varnishd/cache_acceptor.c b/varnish-cache/bin/varnishd/cache_acceptor.c new file mode 100644 index 00000000..43e1b714 --- /dev/null +++ b/varnish-cache/bin/varnishd/cache_acceptor.c @@ -0,0 +1,111 @@ +/* + * $Id$ + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "heritage.h" +#include "cache.h" + +static struct event_base *evb; + +static struct event accept_e[2 * HERITAGE_NSOCKS]; + +static void +http_read_f(int fd, short event, void *arg) +{ + struct sess *sp = arg; + const char *p; + int i; + + printf("%s(%d, %d, ...)\n", __func__, fd, event); + assert(VCA_RXBUFSIZE - sp->rcv_len > 0); + i = read(fd, sp->rcv + sp->rcv_len, VCA_RXBUFSIZE - sp->rcv_len); + assert(i > 0); + sp->rcv_len += i; + sp->rcv[sp->rcv_len] = '\0'; + + p = sp->rcv; + while (1) { + /* XXX: we could save location of all linebreaks for later */ + p = strchr(p, '\n'); + if (p == NULL) + return; + p++; + if (*p == '\r') + p++; + if (*p != '\n') + continue; + break; + } + printf("full <%s>\n", sp->rcv); + event_del(&sp->rd_e); +} + +static void +accept_f(int fd, short event, void *arg __unused) +{ + socklen_t l; + struct sockaddr addr; + struct sess *sp; + + sp = calloc(sizeof *sp, 1); + assert(sp != NULL); + + printf("%s(%d, %d, ...)\n", __func__, fd, event); + + l = sizeof addr; + sp->fd = accept(fd, &addr, &l); + if (sp->fd < 0) { + free(sp); + return; + } + + event_set(&sp->rd_e, sp->fd, EV_READ | EV_PERSIST, + http_read_f, sp); + event_base_set(evb, &sp->rd_e); + event_add(&sp->rd_e, NULL); /* XXX: timeout */ +} + +void * +vca_main(void *arg) +{ + unsigned u; + struct event *ep; + + evb = event_init(); + + ep = accept_e; + for (u = 0; u < HERITAGE_NSOCKS; u++) { + if (heritage.sock_local[u] >= 0) { + event_set(ep, heritage.sock_local[u], + EV_READ | EV_PERSIST, + accept_f, NULL); + event_base_set(evb, ep); + event_add(ep, NULL); + ep++; + } + if (heritage.sock_remote[u] >= 0) { + event_set(ep, heritage.sock_remote[u], + EV_READ | EV_PERSIST, + accept_f, NULL); + event_base_set(evb, ep); + event_add(ep, NULL); + ep++; + } + } + + event_base_loop(evb, 0); + + return ("FOOBAR"); +} diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index 1f36273c..266fd172 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -8,12 +8,16 @@ #include #include +#include + #include #include #include +#include "libvarnish.h" #include "heritage.h" +#include "cache.h" #include "cli_event.h" static struct event ev_keepalive; @@ -48,7 +52,6 @@ cli_func_url_query(struct cli *cli, char **av, void *priv __unused) { cli_out(cli, "url <%s>", av[2]); - sleep(1); cli_result(cli, CLIS_UNIMPL); } @@ -76,6 +79,8 @@ static struct cli_proto cli_proto[] = { { NULL } }; +static pthread_t vca_thread; + void child_main(void) { @@ -87,6 +92,8 @@ child_main(void) setbuf(stderr, NULL); printf("Child starts\n"); + AZ(pthread_create(&vca_thread, NULL, vca_main, NULL)); + eb = event_init(); assert(eb != NULL); diff --git a/varnish-cache/bin/varnishd/mgt_child.c b/varnish-cache/bin/varnishd/mgt_child.c index 3db88f91..af4d226f 100644 --- a/varnish-cache/bin/varnishd/mgt_child.c +++ b/varnish-cache/bin/varnishd/mgt_child.c @@ -62,10 +62,12 @@ std_rdcb(struct bufferevent *bev, void *arg) { const char *p; - p = evbuffer_readline(bev->input); - if (p == NULL) - return; - printf("Child said <%s>\n", p); + while (1) { + p = evbuffer_readline(bev->input); + if (p == NULL) + return; + printf("Child said <%s>\n", p); + } } static void diff --git a/varnish-cache/bin/varnishd/tcp.c b/varnish-cache/bin/varnishd/tcp.c index 3db866dd..368c0fd0 100644 --- a/varnish-cache/bin/varnishd/tcp.c +++ b/varnish-cache/bin/varnishd/tcp.c @@ -10,6 +10,7 @@ #include #include "heritage.h" +#include "libvarnish.h" static void create_listen_socket(const char *addr, const char *port, int *sp, int nsp) @@ -24,7 +25,7 @@ create_listen_socket(const char *addr, const char *port, int *sp, int nsp) i = getaddrinfo(addr, port, &ai, &r0); if (i) { - fprintf("getaddrinfo failed: %s\n", gai_strerror(i)); + fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(i)); return; } @@ -62,5 +63,11 @@ open_tcp(const char *port) create_listen_socket(NULL, port, &heritage.sock_remote[0], HERITAGE_NSOCKS); + for (u = 0; u < HERITAGE_NSOCKS; u++) { + if (heritage.sock_local[u] >= 0) + AZ(listen(heritage.sock_local[u], 16)); + if (heritage.sock_remote[u] >= 0) + AZ(listen(heritage.sock_remote[u], 16)); + } return (0); } diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index 6f77ca53..bc9c6d8c 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -176,20 +176,18 @@ int main(int argc, char *argv[]) { int o; - unsigned portnumber = 8080; + const char *portnumber = "8080"; unsigned dflag = 1; /* XXX: debug=on for now */ register_printf_render_std((const unsigned char *)"HVQ"); - open_tcp("8080"); - while ((o = getopt(argc, argv, "dp:")) != -1) switch (o) { case 'd': dflag++; break; case 'p': - portnumber = strtoul(optarg, NULL, 0); + portnumber = optarg; break; default: usage(); @@ -201,6 +199,15 @@ main(int argc, char *argv[]) if (argc != 0) usage(); + /* + * XXX: Lacking the suspend/resume facility (due to the socket API + * missing an unlisten(2) facility) we may want to push this into + * the child to limit the amount of time where the socket(s) exists + * but do not answer. That, on the other hand, would eliminate the + * possibility of doing a "no-glitch" restart of the child process. + */ + open_tcp(portnumber); + testme(); -- 2.39.5