src/server/safs/addlog.c

Sun, 11 Aug 2024 18:51:39 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 11 Aug 2024 18:51:39 +0200
changeset 542
1327febf99c4
parent 415
d938228c382e
permissions
-rw-r--r--

refactore keep alive handler

/*
 * 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
    cxstring tmstr = cx_str(time);
    if(tmstr.ptr[tmstr.length-1] == '\n') {
        tmstr.length--;
    }
    
    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,
                (int)tmstr.length,
                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,
                (int)tmstr.length,
                tmstr.ptr,
                req,
                rq->status_num,
                len);
    }
    fflush(log->log->file);
    
    
    return REQ_PROCEED;
}

mercurial