2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
36 #define DEFFORMAT "%{%Y-%m-%d %H:%M:%S}t %m %u %A \"%G\""
44 static void qputs(char *s, FILE *o)
49 } else if(*s == '\\') {
51 } else if(*s == '\n') {
53 } else if(*s == '\t') {
55 } else if((*s < 32) || (*s >= 128)) {
56 fprintf(o, "\\x%02x", *s);
63 static void logitem(struct hthead *req, char o, char *d)
74 if((h = getheader(req, d)) == NULL) {
84 strcpy(buf, req->url);
85 if((p = strchr(buf, '?')) != NULL)
90 qputs(req->method, out);
93 qputs(req->rest, out);
100 d = "%a, %d %b %Y %H:%M:%S %z";
101 strftime(buf, sizeof(buf), d, localtime(&now));
106 d = "%a, %d %b %Y %H:%M:%S %z";
107 strftime(buf, sizeof(buf), d, gmtime(&now));
111 gettimeofday(&tv, NULL);
112 fprintf(out, "%06i", (int)tv.tv_usec);
115 logitem(req, 'h', "X-Ash-Address");
118 logitem(req, 'h', "Host");
121 logitem(req, 'h', "Referer");
124 logitem(req, 'h', "User-Agent");
129 static void logreq(struct hthead *req)
132 char d[strlen(format)];
141 if((p2 = strchr(p, '}')) == NULL)
143 memcpy(d, p, p2 - p);
162 static void serve(struct hthead *req, int fd)
165 if(sendreq(ch, req, fd)) {
166 flog(LOG_ERR, "accesslog: could not pass request to child: %s", strerror(errno));
172 static void usage(FILE *out)
174 fprintf(out, "usage: accesslog [-hFa] [-f FORMAT] OUTFILE CHILD [ARGS...]\n");
177 int main(int argc, char **argv)
182 struct pollfd pfd[2];
185 while((c = getopt(argc, argv, "+hFaf:")) >= 0) {
197 format = "%A - - [%{%d/%b/%Y:%H:%M:%S %z}t] \"%m %u %v\" - - \"%R\" \"%G\"";
204 if(argc - optind < 2) {
210 if(!strcmp(argv[optind], "-")) {
213 if((out = fopen(argv[optind], "a")) == NULL) {
214 flog(LOG_ERR, "accesslog: could not open %s for logging: %s", argv[optind], strerror(errno));
218 if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) {
219 flog(LOG_ERR, "accesslog: could fork child: %s", strerror(errno));
223 memset(pfd, 0, sizeof(pfd));
225 pfd[0].events = POLLIN;
227 pfd[1].events = POLLHUP;
228 if((ret = poll(pfd, 2, -1)) < 0) {
230 flog(LOG_ERR, "accesslog: error in poll: %s", strerror(errno));
235 if((fd = recvreq(0, &req)) < 0) {
238 flog(LOG_ERR, "accesslog: error in recvreq: %s", strerror(errno));
245 if(pfd[1].revents & POLLHUP)