src/server/safs/addlog.c

Tue, 13 Aug 2019 22:03:19 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 13 Aug 2019 22:03:19 +0200
changeset 209
a6d8181a8127
parent 196
0459c94a60ed
child 415
d938228c382e
permissions
-rw-r--r--

replace source with . in configure script and fix toolchain detection

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>

#include "addlog.h"

#include "../daemon/request.h"
#include "../daemon/vserver.h"
#include "../daemon/log.h"
#include "../util/util.h"

int common_log(pblock *pb, Session *sn, Request *rq) {
    NSAPIRequest *request = (NSAPIRequest*)rq;
    VirtualServer *vs = request->vs;
    AccessLog *log = vs->log;
    
    char *combined_str = pblock_findval("combined", pb);
    WSBool combined = FALSE;
    if(combined_str) {
        combined = util_getboolean(combined_str, FALSE);
    }
    
    if(log == NULL) {
        return REQ_NOACTION;
    }
    
    char *ip = pblock_findval("ip", sn->client);
    char *user = pblock_findval("auth-user", rq->vars);
    time_t t = time(NULL);
    char *time = ctime(&t);
    char *req = pblock_findval("clf-request", rq->reqpb);
    
    // hack to get the content length
    // http_start_response should not modify the header names
    char *len = pblock_findval("Content-length", rq->srvhdrs);
    
    if(!ip) {
        ip = "-";
    }
    if(!user) {
        user = "-";
    }
    if(!len) {
        len = "0";
    }
    
    // remove trailing line feed
    sstr_t tmstr = sstr(time);
    if(tmstr.ptr[tmstr.length-1] == '\n') {
        tmstr.length--;
    }
    tmstr = sstrdup_pool(sn->pool, tmstr);
    
    if(combined) {
        char *referer = pblock_findval("referer", rq->headers);
        char *user_agent = pblock_findval("user-agent", rq->headers);
        int refq = 1;
        int uaq = 1;
        if(!referer) {
            referer = "-";
            refq = 0;
        }
        if(!user_agent) {
            user_agent = "-";
            uaq = 0;
        }
        fprintf(
                log->log->file,
                "%s - %s [%s] \"%s\" %d %s %.*s%s%.*s %.*s%s%.*s\n",
                ip,
                user,
                tmstr.ptr,
                req,
                rq->status_num,
                len,
                refq,
                "\"",
                referer,
                refq,
                "\"",
                uaq,
                "\"",
                user_agent,
                uaq,
                "\""
                );
    } else {
        fprintf(
                log->log->file,
                "%s - %s [%s] \"%s\" %d %s\n",
                ip,
                user,
                tmstr.ptr,
                req,
                rq->status_num,
                len);
    }
    fflush(log->log->file);
    
    
    return REQ_PROCEED;
}

mercurial