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
--- /dev/null
+/*
+ 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, "<");
+ else if(*text == '>')
+ bufcatstr(buf, ">");
+ else if(*text == '&')
+ bufcatstr(buf, "&");
+ 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);
+}
--- /dev/null
+#ifndef _LIB_HTRESP_H
+#define _LIB_HTRESP_H
+
+void simpleerror(int fd, int code, char *msg, char *fmt, ...);
+
+#endif
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);
+}
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
#include <log.h>
#include <req.h>
#include <proc.h>
+#include <resp.h>
#define CH_SOCKET 0
#define CH_FORK 1
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;
}
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)
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);
free(tmp);
break;
}
+ simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
goto fail;
}
break;
}
+ simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
goto fail;
next:
} 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);
}