Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 368 lines (302 sloc) 9.309 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

#include "common-internal.h"
#include "logger_ncsa.h"

#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#else
# include <time.h>
#endif

#include "util.h"
#include "request.h"
#include "request-protected.h"
#include "header.h"
#include "header-protected.h"
#include "server.h"
#include "server-protected.h"
#include "request.h"
#include "module.h"


/* Plug-in initialization
*/
PLUGIN_INFO_LOGGER_EASIEST_INIT (ncsa);

/* Global stuff
*/
static cherokee_buffer_t now;


ret_t
cherokee_logger_ncsa_new (cherokee_logger_t **logger,
cherokee_virtual_server_t *vsrv,
cherokee_config_node_t *config)
{
ret_t ret;
CHEROKEE_NEW_STRUCT (n, logger_ncsa);

/* Init the base class object
*/
cherokee_logger_init_base (LOGGER(n), PLUGIN_INFO_PTR(ncsa), config);

MODULE(n)->init = (logger_func_init_t) cherokee_logger_ncsa_init;
MODULE(n)->free = (logger_func_free_t) cherokee_logger_ncsa_free;

LOGGER(n)->flush = (logger_func_flush_t) cherokee_logger_ncsa_flush;
LOGGER(n)->reopen = (logger_func_reopen_t) cherokee_logger_ncsa_reopen;
LOGGER(n)->write_access = (logger_func_write_access_t) cherokee_logger_ncsa_write_access;

n->writer_access = NULL;

ret = cherokee_logger_ncsa_init_base (n, VSERVER(vsrv), config);
if (unlikely(ret < ret_ok)) {
cherokee_logger_free (LOGGER(n));
return ret;
}

/* Return the object
*/
*logger = LOGGER(n);
return ret_ok;
}


static void
bogotime_callback (void *param)
{
struct tm *pnow_tm;
cherokee_logger_ncsa_t *logger = LOG_NCSA(param);

/* Choose between local and universal time
*/
if (LOGGER(logger)->utc_time) {
pnow_tm = &cherokee_bogonow_tmgmt;
} else {
pnow_tm = &cherokee_bogonow_tmloc;
}

/* Render the string
*/
cherokee_buffer_clean (&now);
cherokee_buffer_add_va (&now,
" [%02d/%s/%d:%02d:%02d:%02d %c%02d%02d] \"",
pnow_tm->tm_mday,
month[pnow_tm->tm_mon],
1900 + pnow_tm->tm_year,
pnow_tm->tm_hour,
pnow_tm->tm_min,
pnow_tm->tm_sec,
(cherokee_bogonow_tzloc < 0) ? '-' : '+',
(int) (abs(cherokee_bogonow_tzloc) / 60),
(int) (abs(cherokee_bogonow_tzloc) % 60));
}


ret_t
cherokee_logger_ncsa_init_base (cherokee_logger_ncsa_t *logger,
cherokee_virtual_server_t *vsrv,
cherokee_config_node_t *config)
{
ret_t ret;
cherokee_config_node_t *subconf;
static int callback_init = 0;

/* Init the local buffers
*/
cherokee_buffer_init (&logger->now_dtm);
cherokee_buffer_init (&logger->referer);
cherokee_buffer_init (&logger->useragent);
cherokee_buffer_ensure_size (&logger->now_dtm, 64);
cherokee_buffer_ensure_size (&logger->referer, 1024);
cherokee_buffer_ensure_size (&logger->useragent, 512);

/* Init the logger writer
*/
ret = cherokee_config_node_get (config, "access", &subconf);
if (ret != ret_ok) {
LOG_CRITICAL (CHEROKEE_ERROR_LOGGER_NO_KEY, "access");
return ret_error;
}

ret = cherokee_server_get_log_writer (VSERVER_SRV(vsrv), subconf, &logger->writer_access);
if (ret != ret_ok) {
return ret_error;
}

/* Callback init
*/
if (callback_init == 0) {
cherokee_buffer_init (&now);
cherokee_bogotime_add_callback (bogotime_callback, logger, 1);
}

return ret_ok;
}


ret_t
cherokee_logger_ncsa_init (cherokee_logger_ncsa_t *logger)
{
ret_t ret;

ret = cherokee_logger_writer_open (logger->writer_access);
if (ret != ret_ok) {
return ret;
}

return ret_ok;
}


ret_t
cherokee_logger_ncsa_free (cherokee_logger_ncsa_t *logger)
{
cherokee_buffer_mrproper (&logger->now_dtm);
cherokee_buffer_mrproper (&logger->referer);
cherokee_buffer_mrproper (&logger->useragent);

return ret_ok;
}


ret_t
cherokee_logger_ncsa_flush (cherokee_logger_ncsa_t *logger)
{
return cherokee_logger_writer_flush (logger->writer_access, false);
}


static ret_t
build_log_string (cherokee_logger_ncsa_t *logger,
cherokee_request_t *cnt,
cherokee_buffer_t *buf)
{
ret_t ret;
const char *method;
const char *username;
const char *version;
cuint_t method_len = 0;
size_t username_len = 0;
cuint_t version_len = 0;
cherokee_buffer_t *referer = &logger->referer;
cherokee_buffer_t *useragent = &logger->useragent;
char ipaddr[CHE_INET_ADDRSTRLEN];

/* Look for the user
*/
if (cnt->validator && !cherokee_buffer_is_empty (&cnt->validator->user)) {
username_len = cnt->validator->user.len;
username = cnt->validator->user.buf;
} else {
username_len = 1;
username = "-";
}

/* Get the method and version strings
*/
ret = cherokee_http_method_to_string (cnt->header.method, &method, &method_len);
if (unlikely (ret < ret_ok)) {
method = "";
method_len = 0;
}

ret = cherokee_http_version_to_string (cnt->header.version, &version, &version_len);
if (unlikely (ret < ret_ok)) {
version = "";
version_len = 0;
}

/* Build the log string
*
* "%s - %s [%02d/%s/%d:%02d:%02d:%02d %c%02d%02d] \"%s %s %s\" %d "
* FMT_OFFSET
*/
if (cherokee_buffer_is_empty (&cnt->logger_real_ip)) {
memset (ipaddr, 0, sizeof(ipaddr));
cherokee_socket_ntop (&cnt->socket, ipaddr, sizeof(ipaddr)-1);
cherokee_buffer_add (buf, ipaddr, strlen(ipaddr));
} else {
cherokee_buffer_add_buffer (buf, &cnt->logger_real_ip);
}

cherokee_buffer_add_str (buf, " - ");
cherokee_buffer_add (buf, username, username_len);

/* " [date time] "
*/
cherokee_buffer_add_buffer (buf, &now);
cherokee_buffer_add (buf, method, method_len);
cherokee_buffer_add_char (buf, ' ');

if (! cherokee_buffer_is_empty (&cnt->request_original)) {
cherokee_buffer_add_buffer (buf, &cnt->request_original);
if (! cherokee_buffer_is_empty (&cnt->query_string_original)) {
cherokee_buffer_add_char (buf, '?');
cherokee_buffer_add_buffer (buf, &cnt->query_string_original);
}
} else {
cherokee_buffer_add_buffer (buf, &cnt->request);
if (! cherokee_buffer_is_empty (&cnt->query_string)) {
cherokee_buffer_add_char (buf, '?');
cherokee_buffer_add_buffer (buf, &cnt->query_string);
}
}

cherokee_buffer_add_char (buf, ' ');
cherokee_buffer_add (buf, version, version_len);
cherokee_buffer_add_str (buf, "\" ");

if (unlikely (cnt->error_internal_code != http_unset)) {
cherokee_buffer_add_long10 (buf, cnt->error_internal_code);
} else {
cherokee_buffer_add_long10 (buf, cnt->error_code);
}

cherokee_buffer_add_char (buf, ' ');
cherokee_buffer_add_ullong10 (buf, cnt->tx);

/* Look for the "combined" information
*/
if (!logger->combined) {
cherokee_buffer_add_char (buf, '\n');
return ret_ok;
}

/* "combined" information
*/
cherokee_buffer_clean (referer);
cherokee_buffer_clean (useragent);

cherokee_header_copy_known (&cnt->header, header_referer, referer);
cherokee_header_copy_known (&cnt->header, header_user_agent, useragent);
cherokee_buffer_ensure_addlen (buf, 8 + referer->len + referer->len);

if (referer->len > 0) {
cherokee_buffer_add_str (buf, " \"");
cherokee_buffer_add_buffer (buf, referer);
cherokee_buffer_add_str (buf, "\" \"");
} else {
cherokee_buffer_add_str (buf, " \"-\" \"");
}

if (useragent->len > 0) {
cherokee_buffer_add_buffer (buf, useragent);
}

cherokee_buffer_add_str (buf, "\"\n");
return ret_ok;
}


ret_t
cherokee_logger_ncsa_write_access (cherokee_logger_ncsa_t *logger, cherokee_request_t *cnt)
{
ret_t ret;
cherokee_buffer_t *log;

/* Get the buffer
*/
cherokee_logger_writer_get_buf (logger->writer_access, &log);

/* Add the new string
*/
ret = build_log_string (logger, cnt, log);
if (unlikely (ret != ret_ok)) {
goto error;
}

/* Flush buffer if full
*/
if (log->len < logger->writer_access->max_bufsize)
goto ok;

ret = cherokee_logger_writer_flush (logger->writer_access, true);
if (unlikely (ret != ret_ok)) {
goto error;
}

ok:
cherokee_logger_writer_release_buf (logger->writer_access);
return ret_ok;

error:
cherokee_logger_writer_release_buf (logger->writer_access);
return ret_error;
}


ret_t
cherokee_logger_ncsa_reopen (cherokee_logger_ncsa_t *logger)
{
ret_t ret;

ret = cherokee_logger_writer_reopen (logger->writer_access);
if (ret != ret_ok) {
return ret;
}

return ret_ok;
}
Something went wrong with that request. Please try again.