mirror of
https://github.com/cesanta/mongoose.git
synced 2025-06-11 12:14:41 +08:00
log_access() moved to log.c
This commit is contained in:
parent
71dd7a96c7
commit
ebdf973af9
@ -29,7 +29,7 @@ VERSION = $(shell perl -lne \
|
||||
SOURCES = src/internal.h src/util.c src/string.c src/parse_date.c \
|
||||
src/options.c src/crypto.c src/auth.c src/win32.c src/unix.c \
|
||||
src/mg_printf.c src/ssl.c src/http_client.c src/mime.c \
|
||||
src/directory.c src/mongoose.c src/lua.c
|
||||
src/directory.c src/log.c src/mongoose.c src/lua.c
|
||||
|
||||
TINY_SOURCES = ../mongoose.c main.c
|
||||
LUA_SOURCES = $(TINY_SOURCES) sqlite3.c lsqlite3.c lua_5.2.1.c
|
||||
|
44
build/src/log.c
Normal file
44
build/src/log.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include "internal.h"
|
||||
|
||||
static void log_header(const struct mg_connection *conn, const char *header,
|
||||
FILE *fp) {
|
||||
const char *header_value;
|
||||
|
||||
if ((header_value = mg_get_header(conn, header)) == NULL) {
|
||||
(void) fprintf(fp, "%s", " -");
|
||||
} else {
|
||||
(void) fprintf(fp, " \"%s\"", header_value);
|
||||
}
|
||||
}
|
||||
|
||||
static void log_access(const struct mg_connection *conn) {
|
||||
const struct mg_request_info *ri;
|
||||
FILE *fp;
|
||||
char date[64], src_addr[IP_ADDR_STR_LEN];
|
||||
|
||||
fp = conn->ctx->config[ACCESS_LOG_FILE] == NULL ? NULL :
|
||||
fopen(conn->ctx->config[ACCESS_LOG_FILE], "a+");
|
||||
|
||||
if (fp == NULL)
|
||||
return;
|
||||
|
||||
strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S %z",
|
||||
localtime(&conn->birth_time));
|
||||
|
||||
ri = &conn->request_info;
|
||||
flockfile(fp);
|
||||
|
||||
sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
|
||||
fprintf(fp, "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT,
|
||||
src_addr, ri->remote_user == NULL ? "-" : ri->remote_user, date,
|
||||
ri->request_method ? ri->request_method : "-",
|
||||
ri->uri ? ri->uri : "-", ri->http_version,
|
||||
conn->status_code, conn->num_bytes_sent);
|
||||
log_header(conn, "Referer", fp);
|
||||
log_header(conn, "User-Agent", fp);
|
||||
fputc('\n', fp);
|
||||
fflush(fp);
|
||||
|
||||
funlockfile(fp);
|
||||
fclose(fp);
|
||||
}
|
@ -28,21 +28,6 @@ static FILE *mg_fopen(const char *path, const char *mode) {
|
||||
#endif
|
||||
}
|
||||
|
||||
static void sockaddr_to_string(char *buf, size_t len,
|
||||
const union usa *usa) {
|
||||
buf[0] = '\0';
|
||||
#if defined(USE_IPV6)
|
||||
inet_ntop(usa->sa.sa_family, usa->sa.sa_family == AF_INET ?
|
||||
(void *) &usa->sin.sin_addr :
|
||||
(void *) &usa->sin6.sin6_addr, buf, len);
|
||||
#elif defined(_WIN32)
|
||||
// Only Windoze Vista (and newer) have inet_ntop()
|
||||
strncpy(buf, inet_ntoa(usa->sin.sin_addr), len);
|
||||
#else
|
||||
inet_ntop(usa->sa.sa_family, (void *) &usa->sin.sin_addr, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Print error message to the opened error log stream.
|
||||
static void cry(struct mg_connection *conn, const char *fmt, ...) {
|
||||
char buf[MG_BUF_LEN], src_addr[IP_ADDR_STR_LEN];
|
||||
@ -2122,49 +2107,6 @@ static int set_ports_option(struct mg_context *ctx) {
|
||||
return success;
|
||||
}
|
||||
|
||||
static void log_header(const struct mg_connection *conn, const char *header,
|
||||
FILE *fp) {
|
||||
const char *header_value;
|
||||
|
||||
if ((header_value = mg_get_header(conn, header)) == NULL) {
|
||||
(void) fprintf(fp, "%s", " -");
|
||||
} else {
|
||||
(void) fprintf(fp, " \"%s\"", header_value);
|
||||
}
|
||||
}
|
||||
|
||||
static void log_access(const struct mg_connection *conn) {
|
||||
const struct mg_request_info *ri;
|
||||
FILE *fp;
|
||||
char date[64], src_addr[IP_ADDR_STR_LEN];
|
||||
|
||||
fp = conn->ctx->config[ACCESS_LOG_FILE] == NULL ? NULL :
|
||||
fopen(conn->ctx->config[ACCESS_LOG_FILE], "a+");
|
||||
|
||||
if (fp == NULL)
|
||||
return;
|
||||
|
||||
strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S %z",
|
||||
localtime(&conn->birth_time));
|
||||
|
||||
ri = &conn->request_info;
|
||||
flockfile(fp);
|
||||
|
||||
sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
|
||||
fprintf(fp, "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT,
|
||||
src_addr, ri->remote_user == NULL ? "-" : ri->remote_user, date,
|
||||
ri->request_method ? ri->request_method : "-",
|
||||
ri->uri ? ri->uri : "-", ri->http_version,
|
||||
conn->status_code, conn->num_bytes_sent);
|
||||
log_header(conn, "Referer", fp);
|
||||
log_header(conn, "User-Agent", fp);
|
||||
fputc('\n', fp);
|
||||
fflush(fp);
|
||||
|
||||
funlockfile(fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
// Verify given socket address against the ACL.
|
||||
// Return -1 if ACL is malformed, 0 if address is disallowed, 1 if allowed.
|
||||
static int check_acl(struct mg_context *ctx, uint32_t remote_ip) {
|
||||
|
@ -9,3 +9,18 @@ static struct mg_connection *fc(struct mg_context *ctx) {
|
||||
fake_connection.event.user_data = ctx->user_data;
|
||||
return &fake_connection;
|
||||
}
|
||||
|
||||
static void sockaddr_to_string(char *buf, size_t len,
|
||||
const union usa *usa) {
|
||||
buf[0] = '\0';
|
||||
#if defined(USE_IPV6)
|
||||
inet_ntop(usa->sa.sa_family, usa->sa.sa_family == AF_INET ?
|
||||
(void *) &usa->sin.sin_addr :
|
||||
(void *) &usa->sin6.sin6_addr, buf, len);
|
||||
#elif defined(_WIN32)
|
||||
// Only Windoze Vista (and newer) have inet_ntop()
|
||||
strncpy(buf, inet_ntoa(usa->sin.sin_addr), len);
|
||||
#else
|
||||
inet_ntop(usa->sa.sa_family, (void *) &usa->sin.sin_addr, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
117
mongoose.c
117
mongoose.c
@ -480,6 +480,22 @@ static struct mg_connection *fc(struct mg_context *ctx) {
|
||||
return &fake_connection;
|
||||
}
|
||||
|
||||
static void sockaddr_to_string(char *buf, size_t len,
|
||||
const union usa *usa) {
|
||||
buf[0] = '\0';
|
||||
#if defined(USE_IPV6)
|
||||
inet_ntop(usa->sa.sa_family, usa->sa.sa_family == AF_INET ?
|
||||
(void *) &usa->sin.sin_addr :
|
||||
(void *) &usa->sin6.sin6_addr, buf, len);
|
||||
#elif defined(_WIN32)
|
||||
// Only Windoze Vista (and newer) have inet_ntop()
|
||||
strncpy(buf, inet_ntoa(usa->sin.sin_addr), len);
|
||||
#else
|
||||
inet_ntop(usa->sa.sa_family, (void *) &usa->sin.sin_addr, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static void mg_strlcpy(register char *dst, register const char *src, size_t n) {
|
||||
for (; *src != '\0' && n > 1; n--) {
|
||||
*dst++ = *src++;
|
||||
@ -2594,6 +2610,49 @@ static void handle_directory_request(struct mg_connection *conn,
|
||||
}
|
||||
|
||||
|
||||
static void log_header(const struct mg_connection *conn, const char *header,
|
||||
FILE *fp) {
|
||||
const char *header_value;
|
||||
|
||||
if ((header_value = mg_get_header(conn, header)) == NULL) {
|
||||
(void) fprintf(fp, "%s", " -");
|
||||
} else {
|
||||
(void) fprintf(fp, " \"%s\"", header_value);
|
||||
}
|
||||
}
|
||||
|
||||
static void log_access(const struct mg_connection *conn) {
|
||||
const struct mg_request_info *ri;
|
||||
FILE *fp;
|
||||
char date[64], src_addr[IP_ADDR_STR_LEN];
|
||||
|
||||
fp = conn->ctx->config[ACCESS_LOG_FILE] == NULL ? NULL :
|
||||
fopen(conn->ctx->config[ACCESS_LOG_FILE], "a+");
|
||||
|
||||
if (fp == NULL)
|
||||
return;
|
||||
|
||||
strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S %z",
|
||||
localtime(&conn->birth_time));
|
||||
|
||||
ri = &conn->request_info;
|
||||
flockfile(fp);
|
||||
|
||||
sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
|
||||
fprintf(fp, "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT,
|
||||
src_addr, ri->remote_user == NULL ? "-" : ri->remote_user, date,
|
||||
ri->request_method ? ri->request_method : "-",
|
||||
ri->uri ? ri->uri : "-", ri->http_version,
|
||||
conn->status_code, conn->num_bytes_sent);
|
||||
log_header(conn, "Referer", fp);
|
||||
log_header(conn, "User-Agent", fp);
|
||||
fputc('\n', fp);
|
||||
fflush(fp);
|
||||
|
||||
funlockfile(fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
// Return number of bytes left to read for this connection
|
||||
static int64_t left_to_read(const struct mg_connection *conn) {
|
||||
return conn->content_len + conn->request_len - conn->num_bytes_read;
|
||||
@ -2622,21 +2681,6 @@ static FILE *mg_fopen(const char *path, const char *mode) {
|
||||
#endif
|
||||
}
|
||||
|
||||
static void sockaddr_to_string(char *buf, size_t len,
|
||||
const union usa *usa) {
|
||||
buf[0] = '\0';
|
||||
#if defined(USE_IPV6)
|
||||
inet_ntop(usa->sa.sa_family, usa->sa.sa_family == AF_INET ?
|
||||
(void *) &usa->sin.sin_addr :
|
||||
(void *) &usa->sin6.sin6_addr, buf, len);
|
||||
#elif defined(_WIN32)
|
||||
// Only Windoze Vista (and newer) have inet_ntop()
|
||||
strncpy(buf, inet_ntoa(usa->sin.sin_addr), len);
|
||||
#else
|
||||
inet_ntop(usa->sa.sa_family, (void *) &usa->sin.sin_addr, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Print error message to the opened error log stream.
|
||||
static void cry(struct mg_connection *conn, const char *fmt, ...) {
|
||||
char buf[MG_BUF_LEN], src_addr[IP_ADDR_STR_LEN];
|
||||
@ -4716,49 +4760,6 @@ static int set_ports_option(struct mg_context *ctx) {
|
||||
return success;
|
||||
}
|
||||
|
||||
static void log_header(const struct mg_connection *conn, const char *header,
|
||||
FILE *fp) {
|
||||
const char *header_value;
|
||||
|
||||
if ((header_value = mg_get_header(conn, header)) == NULL) {
|
||||
(void) fprintf(fp, "%s", " -");
|
||||
} else {
|
||||
(void) fprintf(fp, " \"%s\"", header_value);
|
||||
}
|
||||
}
|
||||
|
||||
static void log_access(const struct mg_connection *conn) {
|
||||
const struct mg_request_info *ri;
|
||||
FILE *fp;
|
||||
char date[64], src_addr[IP_ADDR_STR_LEN];
|
||||
|
||||
fp = conn->ctx->config[ACCESS_LOG_FILE] == NULL ? NULL :
|
||||
fopen(conn->ctx->config[ACCESS_LOG_FILE], "a+");
|
||||
|
||||
if (fp == NULL)
|
||||
return;
|
||||
|
||||
strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S %z",
|
||||
localtime(&conn->birth_time));
|
||||
|
||||
ri = &conn->request_info;
|
||||
flockfile(fp);
|
||||
|
||||
sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
|
||||
fprintf(fp, "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT,
|
||||
src_addr, ri->remote_user == NULL ? "-" : ri->remote_user, date,
|
||||
ri->request_method ? ri->request_method : "-",
|
||||
ri->uri ? ri->uri : "-", ri->http_version,
|
||||
conn->status_code, conn->num_bytes_sent);
|
||||
log_header(conn, "Referer", fp);
|
||||
log_header(conn, "User-Agent", fp);
|
||||
fputc('\n', fp);
|
||||
fflush(fp);
|
||||
|
||||
funlockfile(fp);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
// Verify given socket address against the ACL.
|
||||
// Return -1 if ACL is malformed, 0 if address is disallowed, 1 if allowed.
|
||||
static int check_acl(struct mg_context *ctx, uint32_t remote_ip) {
|
||||
|
Loading…
Reference in New Issue
Block a user