UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 32 #include "addlog.h" 33 34 #include "../daemon/request.h" 35 #include "../daemon/vserver.h" 36 #include "../daemon/log.h" 37 #include "../util/util.h" 38 39 int common_log(pblock *pb, Session *sn, Request *rq) { 40 NSAPIRequest *request = (NSAPIRequest*)rq; 41 VirtualServer *vs = request->vs; 42 AccessLog *log = vs->log; 43 44 char *combined_str = pblock_findval("combined", pb); 45 WSBool combined = FALSE; 46 if(combined_str) { 47 combined = util_getboolean(combined_str, FALSE); 48 } 49 50 if(log == NULL) { 51 return REQ_NOACTION; 52 } 53 54 char *ip = pblock_findval("ip", sn->client); 55 char *user = pblock_findval("auth-user", rq->vars); 56 time_t t = time(NULL); 57 char *time = ctime(&t); 58 char *req = pblock_findval("clf-request", rq->reqpb); 59 60 // hack to get the content length 61 // http_start_response should not modify the header names 62 char *len = pblock_findval("Content-length", rq->srvhdrs); 63 64 if(!ip) { 65 ip = "-"; 66 } 67 if(!user) { 68 user = "-"; 69 } 70 if(!len) { 71 len = "0"; 72 } 73 74 // remove trailing line feed 75 cxstring tmstr = cx_str(time); 76 if(tmstr.ptr[tmstr.length-1] == '\n') { 77 tmstr.length--; 78 } 79 80 if(combined) { 81 char *referer = pblock_findval("referer", rq->headers); 82 char *user_agent = pblock_findval("user-agent", rq->headers); 83 int refq = 1; 84 int uaq = 1; 85 if(!referer) { 86 referer = "-"; 87 refq = 0; 88 } 89 if(!user_agent) { 90 user_agent = "-"; 91 uaq = 0; 92 } 93 fprintf( 94 log->log->file, 95 "%s - %s [%.*s] \"%s\" %d %s %.*s%s%.*s %.*s%s%.*s\n", 96 ip, 97 user, 98 (int)tmstr.length, 99 tmstr.ptr, 100 req, 101 rq->status_num, 102 len, 103 refq, 104 "\"", 105 referer, 106 refq, 107 "\"", 108 uaq, 109 "\"", 110 user_agent, 111 uaq, 112 "\"" 113 ); 114 } else { 115 fprintf( 116 log->log->file, 117 "%s - %s [%.*s] \"%s\" %d %s\n", 118 ip, 119 user, 120 (int)tmstr.length, 121 tmstr.ptr, 122 req, 123 rq->status_num, 124 len); 125 } 126 fflush(log->log->file); 127 128 129 return REQ_PROCEED; 130 } 131 132