X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Fresp.c;fp=lib%2Fresp.c;h=1271d2cb0824156fd78a978e33f17fff32cf0fee;hb=d422fdfd62d851b345562ad4d093465d2bec604b;hp=0000000000000000000000000000000000000000;hpb=54cefabaa47acc0536805ee5ff2e6dda3df4b043;p=ashd.git diff --git a/lib/resp.c b/lib/resp.c new file mode 100644 index 0000000..1271d2c --- /dev/null +++ b/lib/resp.c @@ -0,0 +1,82 @@ +/* + ashd - A Sane HTTP Daemon + Copyright (C) 2008 Fredrik Tolf + + 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 . +*/ + +#include +#include +#include +#include + +#ifdef HAVE_CONFIG_H +#include +#endif +#include +#include + +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, "\r\n"); + bufcatstr(buf, "\r\n"); + bufcatstr(buf, "\r\n"); + bufcatstr(buf, "\r\n"); + bprintf(&buf, "%s\r\n", msg); + bufcatstr(buf, "\r\n"); + bufcatstr(buf, "\r\n"); + bprintf(&buf, "

%s

\r\n", msg); + bprintf(&buf, "

%s

\r\n", tmp2); + bufcatstr(buf, "\r\n"); + bufcatstr(buf, "\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); +}