feat(lnm): add some internal logging

new-lnm-integration
Jef Roosens 2023-12-11 15:34:49 +01:00
parent 3aa0ace863
commit dde83584a7
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 30 additions and 4 deletions

View File

@ -6,9 +6,12 @@
#include "lnm/http/loop.h" #include "lnm/http/loop.h"
#include "lnm/http/loop_internal.h" #include "lnm/http/loop_internal.h"
#include "lnm/http/req.h" #include "lnm/http/req.h"
#include "lnm/log.h"
#include "lnm/loop.h" #include "lnm/loop.h"
#include "lnm/loop_internal.h" #include "lnm/loop_internal.h"
static const char *section = "http";
/* static const lnm_http_loop_state lnm_http_loop_state_first_req = /* static const lnm_http_loop_state lnm_http_loop_state_first_req =
* lnm_http_loop_state_parse_req; */ * lnm_http_loop_state_parse_req; */
static const lnm_http_loop_state lnm_http_loop_state_first_res = static const lnm_http_loop_state lnm_http_loop_state_first_res =
@ -29,10 +32,15 @@ void lnm_http_loop_process_parse_req(lnm_http_conn *conn) {
// If the request is already the size of the read buffer, we close the // If the request is already the size of the read buffer, we close the
// request. Otherwise, we wait for anything read // request. Otherwise, we wait for anything read
if (conn->r.size - conn->r.read == LNM_LOOP_BUF_SIZE) { if (conn->r.size - conn->r.read == LNM_LOOP_BUF_SIZE) {
lnm_linfo(section, "Received request larger than buffer (%i bytes)",
LNM_LOOP_BUF_SIZE);
conn->state = lnm_loop_state_end; conn->state = lnm_loop_state_end;
} }
break; break;
case lnm_http_parse_err_invalid: case lnm_http_parse_err_invalid:
lnm_linfo(section, "%s", "Received invalid request");
conn->state = lnm_loop_state_end; conn->state = lnm_loop_state_end;
break; break;
case lnm_http_parse_err_unknown_method: case lnm_http_parse_err_unknown_method:
@ -40,6 +48,10 @@ void lnm_http_loop_process_parse_req(lnm_http_conn *conn) {
ctx->state = lnm_http_loop_state_first_res; ctx->state = lnm_http_loop_state_first_res;
break; break;
} }
lnm_linfo(section, "%s %.*s HTTP/1.%i",
lnm_http_method_names[ctx->req.method], (int)ctx->req.path.len,
ctx->req.path.s, ctx->req.minor_version);
} }
void lnm_http_loop_process_route(lnm_http_conn *conn) { void lnm_http_loop_process_route(lnm_http_conn *conn) {
@ -163,6 +175,13 @@ void lnm_http_loop_state_process_add_headers(lnm_http_conn *conn) {
false); false);
} }
if (res->status == 0) {
res->status = lnm_http_status_ok;
}
lnm_linfo(section, "%i %s", res->status,
lnm_http_status_names[res->status / 100 - 1][res->status % 100]);
ctx->state = lnm_http_loop_state_write_status_line; ctx->state = lnm_http_loop_state_write_status_line;
} }
@ -172,10 +191,6 @@ void lnm_http_loop_process_write_status_line(lnm_http_conn *conn) {
lnm_http_loop_ctx *ctx = conn->ctx; lnm_http_loop_ctx *ctx = conn->ctx;
lnm_http_res *res = &ctx->res; lnm_http_res *res = &ctx->res;
if (res->status == 0) {
res->status = lnm_http_status_ok;
}
const char *response_type_name = const char *response_type_name =
lnm_http_status_names[res->status / 100 - 1][res->status % 100]; lnm_http_status_names[res->status / 100 - 1][res->status % 100];

View File

@ -6,8 +6,11 @@
#include <unistd.h> #include <unistd.h>
#include "lnm/common.h" #include "lnm/common.h"
#include "lnm/log.h"
#include "lnm/loop_internal.h" #include "lnm/loop_internal.h"
static const char *section = "loop";
lnm_err lnm_loop_init(lnm_loop **out, void *gctx, lnm_err lnm_loop_init(lnm_loop **out, void *gctx,
lnm_err (*ctx_init)(void **out, void *gctx), lnm_err (*ctx_init)(void **out, void *gctx),
void (*ctx_free)(void *ctx), void (*ctx_free)(void *ctx),
@ -34,6 +37,8 @@ lnm_err lnm_loop_accept(lnm_loop *l) {
int conn_fd = accept(l->listen_fd, NULL, NULL); int conn_fd = accept(l->listen_fd, NULL, NULL);
if (conn_fd < 0) { if (conn_fd < 0) {
lnm_lcritical(section, "accept failed: %i", conn_fd);
return lnm_err_failed_network; return lnm_err_failed_network;
} }
@ -72,6 +77,8 @@ lnm_err lnm_loop_accept(lnm_loop *l) {
l->conns.open++; l->conns.open++;
lnm_ldebug(section, "connection opened with fd %i", conn_fd);
return lnm_err_ok; return lnm_err_ok;
} }
@ -130,6 +137,8 @@ lnm_err lnm_loop_run(lnm_loop *l) {
poll_args[0].fd = l->listen_fd; poll_args[0].fd = l->listen_fd;
poll_args[0].events = POLLIN; poll_args[0].events = POLLIN;
lnm_linfo(section, "started on fd %i", l->listen_fd);
while (1) { while (1) {
nfds_t poll_args_len = 1; nfds_t poll_args_len = 1;
@ -171,6 +180,8 @@ lnm_err lnm_loop_run(lnm_loop *l) {
close(conn->fd); close(conn->fd);
l->conns.open--; l->conns.open--;
lnm_ldebug(section, "connection closed with fd %i", conn->fd);
lnm_loop_conn_free(l, conn); lnm_loop_conn_free(l, conn);
} }