geomyidae

A small C-based gopherd. (gopher://bitreich.org/1/scm/geomyidae)
git clone git://r-36.net/geomyidae
Log | Files | Refs | README | LICENSE

commit 6136de9a50186b811bb80acba917e6f6ea4cfe13
parent 3717798485ae28c453c83a109d17e66188d8379d
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun, 25 Jun 2017 20:53:07 +0200

improve dcgi support

- fix a memory leak (readln).
- use getline: this also improves performance:
  - tested with a 30 MB file via netcat: 25s to 16s.

Signed-off-by: Christoph Lohmann <20h@r-36.net>

Diffstat:
handlr.c | 20+++++++++++++++++---
ind.c | 20--------------------
ind.h | 1-
3 files changed, 17 insertions(+), 24 deletions(-)

diff --git a/handlr.c b/handlr.c @@ -181,7 +181,10 @@ void handledcgi(int sock, char *file, char *port, char *base, char *args, char *sear, char *ohost) { - char *p, *path, *ln; + FILE *fp; + char *p, *path, *ln = nil; + size_t linesiz = 0; + ssize_t n; int outpipe[2]; Elems *el; @@ -226,7 +229,15 @@ handledcgi(int sock, char *file, char *port, char *base, char *args, dup2(sock, 1); close(outpipe[1]); - while((ln = readln(outpipe[0])) != nil) { + if (!(fp = fdopen(outpipe[0], "r"))) { + perror("fdopen"); + break; + } + + while ((n = getline(&ln, &linesiz, fp)) > 0) { + if (ln[n - 1] == '\n') + ln[--n] = '\0'; + el = getadv(ln); if (el == nil) continue; @@ -234,10 +245,13 @@ handledcgi(int sock, char *file, char *port, char *base, char *args, printelem(sock, el, ohost, port); freeelem(el); } + if (ferror(fp)) + perror("getline"); dprintf(sock, ".\r\n"); - wait(NULL); + free(ln); free(path); + wait(NULL); break; } } diff --git a/ind.c b/ind.c @@ -106,26 +106,6 @@ gettype(char *filename) return &type[0]; } -char * -readln(int fd) -{ - char *ret; - int len; - - len = 1; - - ret = xmalloc(2); - while(read(fd, &ret[len - 1], 1) > 0 && ret[len - 1] != '\n') - ret = xrealloc(ret, ++len + 1); - if(ret[len - 1] != '\n') { - free(ret); - return nil; - } - ret[len - 1] = '\0'; - - return ret; -} - void freeelem(Elems *e) { diff --git a/ind.h b/ind.h @@ -42,7 +42,6 @@ void addindexs(Indexs *idx, Elems *el); void addelem(Elems *e, char *s); void freeindex(Indexs *i); void freeelem(Elems *e); -char *readln(int fd); int initlogging(char *logf); void stoplogging(int fd); char *smprintf(char *fmt, ...);