src/server/safs/addlog.c

Tue, 10 Nov 2015 21:18:51 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 10 Nov 2015 21:18:51 +0100
changeset 112
b962d83124bc
parent 77
f1cff81e425a
child 196
0459c94a60ed
permissions
-rw-r--r--

merge

/*
 * 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"

int common_log(pblock *pb, Session *sn, Request *rq) {
    NSAPIRequest *request = (NSAPIRequest*)rq;
    VirtualServer *vs = request->vs;
    AccessLog *log = vs->log;
    
    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);
    
    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