Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/civetweb.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ struct mg_connection {
SSL_CTX *client_ssl_ctx; /* SSL context for client connections */
struct socket client; /* Connected client */
time_t birth_time; /* Time when request was received */
struct timeval start_tv; /* Time when request was received */
struct timeval end_tv; /* Time when request was finished */
int64_t num_bytes_sent; /* Total bytes sent to client */
int64_t content_len; /* Content-Length header value */
int64_t consumed_content; /* How many bytes of content have been read */
Expand Down Expand Up @@ -6048,6 +6050,7 @@ static void log_access(const struct mg_connection *conn)
FILE *fp;
char date[64], src_addr[IP_ADDR_STR_LEN];
struct tm *tm;
int64_t spent_time;

const char *referer;
const char *user_agent;
Expand All @@ -6068,18 +6071,21 @@ static void log_access(const struct mg_connection *conn)
date[sizeof(date) - 1] = '\0';
}

spent_time = 1000000 * (conn->end_tv.tv_sec - conn->start_tv.tv_sec);
spent_time = spent_time + conn->end_tv.tv_usec - conn->start_tv.tv_usec;

ri = &conn->request_info;

sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
referer = header_val(conn, "Referer");
user_agent = header_val(conn, "User-Agent");

snprintf(buf, sizeof(buf), "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT " %s %s",
snprintf(buf, sizeof(buf), "%s - %s [%s] \"%s %s HTTP/%s\" %d %" INT64_FMT " %s %s %" 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,
referer, user_agent);
referer, user_agent, spent_time);

if (conn->ctx->callbacks.log_access) {
conn->ctx->callbacks.log_access(conn, buf);
Expand Down Expand Up @@ -6511,6 +6517,7 @@ static int getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len, int *
conn->content_len = 0;
}
conn->birth_time = time(NULL);
gettimeofday(&conn->start_tv, NULL);
}
return 1;
}
Expand Down Expand Up @@ -6572,6 +6579,7 @@ static void process_new_connection(struct mg_connection *conn)
if (conn->ctx->callbacks.end_request != NULL) {
conn->ctx->callbacks.end_request(conn, conn->status_code);
}
gettimeofday(&conn->end_tv, NULL);
log_access(conn);
}
if (ri->remote_user != NULL) {
Expand Down Expand Up @@ -6660,6 +6668,7 @@ static void *worker_thread_run(void *thread_func_param)
produce_socket() */
while (consume_socket(ctx, &conn->client)) {
conn->birth_time = time(NULL);
gettimeofday(&conn->start_tv, NULL);

/* Fill in IP, port info early so even if SSL setup below fails,
error handler would have the corresponding info.
Expand Down