Skip to content

Commit

Permalink
Initial access log support.
Browse files Browse the repository at this point in the history
  • Loading branch information
VBart committed Apr 11, 2018
1 parent c7e575d commit 204c394
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/nxt_conf_validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_root_members[] = {
&nxt_conf_vldt_object_iterator,
(void *) &nxt_conf_vldt_app },

{ nxt_string("access_log"),
NXT_CONF_VLDT_STRING,
NULL,
NULL },

NXT_CONF_VLDT_END
};

Expand Down
31 changes: 31 additions & 0 deletions src/nxt_h1proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static void nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r,
static nxt_buf_t *nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r,
nxt_buf_t *out);
static void nxt_h1p_conn_request_sent(nxt_task_t *task, void *obj, void *data);
static nxt_off_t nxt_h1p_request_body_bytes_sent(nxt_task_t *task,
nxt_http_proto_t proto);
static void nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r,
nxt_buf_t *last);
static void nxt_h1p_request_close(nxt_task_t *task, nxt_http_proto_t proto);
Expand Down Expand Up @@ -87,6 +89,13 @@ const nxt_http_proto_send_t nxt_http_proto_send[3] = {
};


const nxt_http_proto_body_bytes_sent_t nxt_http_proto_body_bytes_sent[3] = {
nxt_h1p_request_body_bytes_sent,
NULL,
NULL,
};


const nxt_http_proto_discard_t nxt_http_proto_discard[3] = {
nxt_h1p_request_discard,
NULL,
Expand All @@ -110,6 +119,10 @@ static nxt_http_field_proc_t nxt_h1p_fields[] = {
{ nxt_string("Host"), &nxt_http_request_host, 0 },
{ nxt_string("Cookie"), &nxt_http_request_field,
offsetof(nxt_http_request_t, cookie) },
{ nxt_string("Referer"), &nxt_http_request_field,
offsetof(nxt_http_request_t, referer) },
{ nxt_string("User-Agent"), &nxt_http_request_field,
offsetof(nxt_http_request_t, user_agent) },
{ nxt_string("Content-Type"), &nxt_http_request_field,
offsetof(nxt_http_request_t, content_type) },
{ nxt_string("Content-Length"), &nxt_http_request_content_length, 0 },
Expand Down Expand Up @@ -802,6 +815,8 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r)

header->mem.free = p;

h1p->header_size = nxt_buf_mem_used_size(&header->mem);

c = h1p->conn;

c->write = header;
Expand Down Expand Up @@ -930,6 +945,20 @@ nxt_h1p_conn_request_sent(nxt_task_t *task, void *obj, void *data)
}


static nxt_off_t
nxt_h1p_request_body_bytes_sent(nxt_task_t *task, nxt_http_proto_t proto)
{
nxt_off_t sent;
nxt_h1proto_t *h1p;

h1p = proto.h1;

sent = h1p->conn->sent - h1p->header_size;

return (sent > 0) ? sent : 0;
}


static void
nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r,
nxt_buf_t *last)
Expand Down Expand Up @@ -993,6 +1022,8 @@ nxt_h1p_keepalive(nxt_task_t *task, nxt_h1proto_t *h1p, nxt_conn_t *c)

nxt_memzero(h1p, offsetof(nxt_h1proto_t, conn));

c->sent = 0;

in = c->read;

if (in == NULL) {
Expand Down
19 changes: 13 additions & 6 deletions src/nxt_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ typedef struct {
uint8_t chunked; /* 1 bit */
nxt_http_te_t transfer_encoding:8; /* 2 bits */

uint32_t header_size;

nxt_http_request_t *request;
nxt_buf_t *buffers;
/*
Expand Down Expand Up @@ -118,6 +120,8 @@ struct nxt_http_request_s {
nxt_http_field_t *content_type;
nxt_http_field_t *content_length;
nxt_http_field_t *cookie;
nxt_http_field_t *referer;
nxt_http_field_t *user_agent;
nxt_off_t content_length_n;

nxt_sockaddr_t *remote;
Expand All @@ -144,6 +148,8 @@ typedef void (*nxt_http_proto_header_send_t)(nxt_task_t *task,
nxt_http_request_t *r);
typedef void (*nxt_http_proto_send_t)(nxt_task_t *task, nxt_http_request_t *r,
nxt_buf_t *out);
typedef nxt_off_t (*nxt_http_proto_body_bytes_sent_t)(nxt_task_t *task,
nxt_http_proto_t proto);
typedef void (*nxt_http_proto_discard_t)(nxt_task_t *task,
nxt_http_request_t *r, nxt_buf_t *last);
typedef void (*nxt_http_proto_close_t)(nxt_task_t *task,
Expand Down Expand Up @@ -180,12 +186,13 @@ nxt_int_t nxt_http_request_content_length(void *ctx, nxt_http_field_t *field,
extern nxt_lvlhsh_t nxt_response_fields_hash;
extern const nxt_conn_state_t nxt_router_conn_close_state;

extern const nxt_http_proto_body_read_t nxt_http_proto_body_read[];
extern const nxt_http_proto_local_addr_t nxt_http_proto_local_addr[];
extern const nxt_http_proto_header_send_t nxt_http_proto_header_send[];
extern const nxt_http_proto_send_t nxt_http_proto_send[];
extern const nxt_http_proto_discard_t nxt_http_proto_discard[];
extern const nxt_http_proto_close_t nxt_http_proto_close[];
extern const nxt_http_proto_body_read_t nxt_http_proto_body_read[];
extern const nxt_http_proto_local_addr_t nxt_http_proto_local_addr[];
extern const nxt_http_proto_header_send_t nxt_http_proto_header_send[];
extern const nxt_http_proto_send_t nxt_http_proto_send[];
extern const nxt_http_proto_body_bytes_sent_t nxt_http_proto_body_bytes_sent[];
extern const nxt_http_proto_discard_t nxt_http_proto_discard[];
extern const nxt_http_proto_close_t nxt_http_proto_close[];


#endif /* _NXT_HTTP_H_INCLUDED_ */
18 changes: 10 additions & 8 deletions src/nxt_http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,10 @@ nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
void
nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
{
nxt_http_proto_t proto;
nxt_http_request_t *r;
nxt_http_proto_close_t handler;
nxt_http_proto_t proto;
nxt_http_request_t *r;
nxt_http_proto_close_t handler;
nxt_router_access_log_t *access_log;

r = obj;
proto.any = data;
Expand All @@ -453,11 +454,12 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)

if (!r->logged) {
r->logged = 1;
// STUB
nxt_debug(task, "http request log: \"%*s \"%V %V %V\" %d\"",
(size_t) r->remote->address_length,
nxt_sockaddr_address(r->remote),
r->method, &r->target, &r->version, r->status);

access_log = r->socket_conf->router_conf->access_log;

if (access_log != NULL) {
access_log->handler(task, r, access_log);
}
}

handler = nxt_http_proto_close[r->protocol];
Expand Down
37 changes: 37 additions & 0 deletions src/nxt_main_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ static void nxt_main_port_modules_handler(nxt_task_t *task,
static int nxt_cdecl nxt_app_lang_compare(const void *v1, const void *v2);
static void nxt_main_port_conf_store_handler(nxt_task_t *task,
nxt_port_recv_msg_t *msg);
static void nxt_main_port_access_log_handler(nxt_task_t *task,
nxt_port_recv_msg_t *msg);


const nxt_sig_event_t nxt_main_process_signals[] = {
Expand Down Expand Up @@ -314,6 +316,7 @@ static nxt_port_handlers_t nxt_main_process_port_handlers = {
.socket = nxt_main_port_socket_handler,
.modules = nxt_main_port_modules_handler,
.conf_store = nxt_main_port_conf_store_handler,
.access_log = nxt_main_port_access_log_handler,
.rpc_ready = nxt_port_rpc_handler,
.rpc_error = nxt_port_rpc_handler,
};
Expand Down Expand Up @@ -1282,3 +1285,37 @@ nxt_main_port_conf_store_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)

nxt_alert(task, "failed to store current configuration");
}


static void
nxt_main_port_access_log_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
{
u_char *path;
nxt_int_t ret;
nxt_file_t file;
nxt_port_t *port;
nxt_port_msg_type_t type;

nxt_debug(task, "opening access log file");

path = msg->buf->mem.pos;

nxt_memzero(&file, sizeof(nxt_file_t));

file.name = (nxt_file_name_t *) path;
file.log_level = NXT_LOG_ERR;

ret = nxt_file_open(task, &file, O_WRONLY | O_APPEND, O_CREAT,
NXT_FILE_OWNER_ACCESS);

type = (ret == NXT_OK) ? NXT_PORT_MSG_RPC_READY_LAST | NXT_PORT_MSG_CLOSE_FD
: NXT_PORT_MSG_RPC_ERROR;

port = nxt_runtime_port_find(task->thread->runtime, msg->port_msg.pid,
msg->port_msg.reply_port);

if (nxt_fast_path(port != NULL)) {
(void) nxt_port_socket_write(task, port, type, file.fd,
msg->port_msg.stream, 0, NULL);
}
}
3 changes: 3 additions & 0 deletions src/nxt_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct nxt_port_handlers_s {
nxt_port_handler_t socket;
nxt_port_handler_t modules;
nxt_port_handler_t conf_store;
nxt_port_handler_t access_log;

/* File descriptor exchange. */
nxt_port_handler_t change_file;
Expand Down Expand Up @@ -56,6 +57,7 @@ typedef enum {
_NXT_PORT_MSG_SOCKET = nxt_port_handler_idx(socket),
_NXT_PORT_MSG_MODULES = nxt_port_handler_idx(modules),
_NXT_PORT_MSG_CONF_STORE = nxt_port_handler_idx(conf_store),
_NXT_PORT_MSG_ACCESS_LOG = nxt_port_handler_idx(access_log),

_NXT_PORT_MSG_CHANGE_FILE = nxt_port_handler_idx(change_file),
_NXT_PORT_MSG_NEW_PORT = nxt_port_handler_idx(new_port),
Expand All @@ -79,6 +81,7 @@ typedef enum {
NXT_PORT_MSG_SOCKET = _NXT_PORT_MSG_SOCKET | NXT_PORT_MSG_LAST,
NXT_PORT_MSG_MODULES = _NXT_PORT_MSG_MODULES | NXT_PORT_MSG_LAST,
NXT_PORT_MSG_CONF_STORE = _NXT_PORT_MSG_CONF_STORE | NXT_PORT_MSG_LAST,
NXT_PORT_MSG_ACCESS_LOG = _NXT_PORT_MSG_ACCESS_LOG | NXT_PORT_MSG_LAST,

NXT_PORT_MSG_CHANGE_FILE = _NXT_PORT_MSG_CHANGE_FILE | NXT_PORT_MSG_LAST,
NXT_PORT_MSG_NEW_PORT = _NXT_PORT_MSG_NEW_PORT | NXT_PORT_MSG_LAST,
Expand Down
Loading

0 comments on commit 204c394

Please sign in to comment.