Send error reports from dirplex.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 21 Dec 2008 19:53:50 +0000 (20:53 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 21 Dec 2008 20:02:05 +0000 (21:02 +0100)
lib/Makefile.am
lib/resp.c [new file with mode: 0644]
lib/resp.h [new file with mode: 0644]
lib/utils.c
lib/utils.h
src/dirplex.c

index b5cc5f6..c5471a8 100644 (file)
@@ -1,4 +1,4 @@
 noinst_LIBRARIES = libht.a
 
-libht_a_SOURCES = utils.c mt.c log.c req.c proc.c mtio.c
+libht_a_SOURCES = utils.c mt.c log.c req.c proc.c mtio.c resp.c
 libht_a_CFLAGS = -fPIC
diff --git a/lib/resp.c b/lib/resp.c
new file mode 100644 (file)
index 0000000..1271d2c
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+    ashd - A Sane HTTP Daemon
+    Copyright (C) 2008  Fredrik Tolf <fredrik@dolda2000.com>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <utils.h>
+#include <resp.h>
+
+char *htmlquote(char *text)
+{
+    struct charbuf buf;
+    
+    bufinit(buf);
+    for(; *text; text++) {
+       if(*text == '<')
+           bufcatstr(buf, "&lt;");
+       else if(*text == '>')
+           bufcatstr(buf, "&gt;");
+       else if(*text == '&')
+           bufcatstr(buf, "&amp;");
+       else
+           bufadd(buf, *text);
+    }
+    bufadd(buf, 0);
+    return(buf.b);
+}
+
+void simpleerror(int fd, int code, char *msg, char *fmt, ...)
+{
+    struct charbuf buf;
+    char *tmp1, *tmp2;
+    va_list args;
+    FILE *out;
+    
+    va_start(args, fmt);
+    tmp1 = vsprintf2(fmt, args);
+    va_end(args);
+    tmp2 = htmlquote(tmp1);
+    free(tmp1);
+    bufinit(buf);
+    bufcatstr(buf, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n");
+    bufcatstr(buf, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n");
+    bufcatstr(buf, "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-US\" xml:lang=\"en-US\">\r\n");
+    bufcatstr(buf, "<head>\r\n");
+    bprintf(&buf, "<title>%s</title>\r\n", msg);
+    bufcatstr(buf, "</head>\r\n");
+    bufcatstr(buf, "<body>\r\n");
+    bprintf(&buf, "<h1>%s</h1>\r\n", msg);
+    bprintf(&buf, "<p>%s</p>\r\n", tmp2);
+    bufcatstr(buf, "</body>\r\n");
+    bufcatstr(buf, "</html>\r\n");
+    free(tmp2);
+    out = fdopen(fd, "w");
+    fprintf(out, "HTTP/1.1 %i %s\r\n", code, msg);
+    fprintf(out, "Content-Type: text/html\r\n");
+    fprintf(out, "Content-Length: %i\r\n", buf.d);
+    fprintf(out, "\r\n");
+    fwrite(buf.b, 1, buf.d, out);
+    fclose(out);
+    buffree(buf);
+}
diff --git a/lib/resp.h b/lib/resp.h
new file mode 100644 (file)
index 0000000..a1f01f3
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef _LIB_HTRESP_H
+#define _LIB_HTRESP_H
+
+void simpleerror(int fd, int code, char *msg, char *fmt, ...);
+
+#endif
index e0dc22c..2734410 100644 (file)
@@ -175,3 +175,29 @@ int calen(char **a)
     for(i = 0; *a; a++, i++);
     return(i);
 }
+
+void bvprintf(struct charbuf *buf, char *format, va_list al)
+{
+    va_list al2;
+    int ret;
+    
+    while(1) {
+       va_copy(al2, al);
+       ret = vsnprintf(buf->b + buf->d, buf->s - buf->d, format, al2);
+       va_end(al2);
+       if(ret < buf->s - buf->d) {
+           buf->d += ret;
+           return;
+       }
+       sizebuf(*buf, buf->d + ret + 1);
+    }
+}
+
+void bprintf(struct charbuf *buf, char *format, ...)
+{
+    va_list args;
+    
+    va_start(args, format);
+    bvprintf(buf, format, args);
+    va_end(args);
+}
index e359d6c..78241b3 100644 (file)
@@ -68,5 +68,7 @@ off_t atoo(char *n);
 char **tokenize(char *src);
 void freeca(char **ca);
 int calen(char **a);
+void bvprintf(struct charbuf *buf, char *format, va_list al);
+void bprintf(struct charbuf *buf, char *format, ...);
 
 #endif
index 45d8a11..ef001cf 100644 (file)
@@ -34,6 +34,7 @@
 #include <log.h>
 #include <req.h>
 #include <proc.h>
+#include <resp.h>
 
 #define CH_SOCKET 0
 #define CH_FORK 1
@@ -453,8 +454,8 @@ static void handlefile(struct hthead *req, int fd, char *path)
        return;
     }
     if((ch = findchild(path, pat->childnm)) == NULL) {
-       /* XXX: Send a 500 error. */
        flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
+       simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
        return;
     }
     
@@ -467,6 +468,8 @@ static void handlefile(struct hthead *req, int fd, char *path)
 
 static void handledir(struct hthead *req, int fd, char *path)
 {
+    /* XXX: Todo */
+    simpleerror(fd, 403, "Not Authorized", "Will not send directory listings or indices yet");
 }
 
 static int checkdir(struct hthead *req, int fd, char *path)
@@ -494,15 +497,19 @@ static void serve(struct hthead *req, int fd)
            if(p2 == NULL) {
                if(stat(path, &sb)) {
                    flog(LOG_WARNING, "failed to stat previously stated directory %s: %s", path, strerror(errno));
+                   simpleerror(fd, 500, "Internal Server Error", "The server encountered an unexpected condition.");
                    goto fail;
                }
                break;
            } else {
+               simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
                goto fail;
            }
        }
-       if(*p == '.')
+       if(*p == '.') {
+           simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
            goto fail;
+       }
        
        getconfig(path);
        
@@ -528,6 +535,7 @@ static void serve(struct hthead *req, int fd)
                free(tmp);
                break;
            }
+           simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
            goto fail;
        }
 
@@ -555,6 +563,7 @@ static void serve(struct hthead *req, int fd)
                break;
        }
        
+       simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
        goto fail;
        
     next:
@@ -573,12 +582,13 @@ static void serve(struct hthead *req, int fd)
     } else if(S_ISREG(sb.st_mode)) {
        handlefile(req, fd, path);
     } else {
+       simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
        goto fail;
     }
     goto out;
     
 fail:
-    /* XXX: Send error report. */
+    /* No special handling, for now at least. */
 out:
     free(path);
 }