Fix source IP in logging

This commit is contained in:
Sergey Lyubka 2014-01-06 14:53:41 +00:00
parent 9624200663
commit 347a8c2f70

View File

@ -3324,22 +3324,17 @@ static void log_header(const struct mg_connection *conn, const char *header,
static void log_access(const struct connection *conn, const char *path) { static void log_access(const struct connection *conn, const char *path) {
const struct mg_connection *c = &conn->mg_conn; const struct mg_connection *c = &conn->mg_conn;
FILE *fp = (path == NULL) ? NULL : fopen(path, "a+"); FILE *fp = (path == NULL) ? NULL : fopen(path, "a+");
char date[64], src_addr[100], user[100]; char date[64], user[100];
union socket_address sa;
socklen_t len = sizeof(sa);
if (fp == NULL) return; if (fp == NULL) return;
strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S %z", strftime(date, sizeof(date), "%d/%b/%Y:%H:%M:%S %z",
localtime(&conn->birth_time)); localtime(&conn->birth_time));
flockfile(fp); flockfile(fp);
getsockname(conn->client_sock, &sa.sa, &len);
mg_parse_header(mg_get_header(&conn->mg_conn, "Authorization"), "username", mg_parse_header(mg_get_header(&conn->mg_conn, "Authorization"), "username",
user, sizeof(user)); user, sizeof(user));
sockaddr_to_string(src_addr, sizeof(src_addr), &sa);
fprintf(fp, "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT, fprintf(fp, "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT,
src_addr, user[0] == '\0' ? "-" : user, date, c->remote_ip, user[0] == '\0' ? "-" : user, date,
c->request_method ? c->request_method : "-", c->request_method ? c->request_method : "-",
c->uri ? c->uri : "-", c->http_version, c->uri ? c->uri : "-", c->http_version,
c->status_code, conn->num_bytes_sent); c->status_code, conn->num_bytes_sent);