log METHOD PATH, properly integrate Next

This commit is contained in:
Peter Harpending 2025-09-17 17:26:12 -07:00
parent eeb149e6e5
commit 60b149d520
2 changed files with 22 additions and 7 deletions

View File

@ -1,6 +1,13 @@
VIDEO 1 - 2025-09-16
TODONE
- add qhl as dep
TODO (GOAL QUEUE)
- listen by default
- talk to a web browser
- alpine works
VIDEO 2 - 2025-09-17
TODONE
- properly integrate NEXT into state
- log path/method
TODO (GOAL QUEUE)
- listen by default

View File

@ -30,7 +30,8 @@
-include("http.hrl").
-record(s, {socket = none :: none | gen_tcp:socket()}).
-record(s, {socket = none :: none | gen_tcp:socket(),
next = none :: none | binary()}).
%% An alias for the state record above. Aliasing state can smooth out annoyances
@ -126,15 +127,21 @@ listen(Parent, Debug, ListenSocket) ->
%% The service loop itself. This is the service state. The process blocks on receive
%% of Erlang messages, TCP segments being received themselves as Erlang messages.
loop(Parent, Debug, State = #s{socket = Socket}) ->
loop(Parent, Debug, State = #s{socket = Socket, next = Next}) ->
ok = inet:setopts(Socket, [{active, once}]),
receive
{tcp, Socket, Message} ->
%ok = io:format("~p received: ~tp~n", [self(), Message]),
case qhl:parse(Socket, Message) of
{ok, Req, none} ->
Received =
case Next of
none -> Message;
_ -> <<Next/binary, Message/binary>>
end,
case qhl:parse(Socket, Received) of
{ok, Req, NewNext} ->
handle_request(Socket, Req),
loop(Parent, Debug, State);
NewState = State#s{next = NewNext},
loop(Parent, Debug, NewState);
Error ->
io:format("~tp:~tp error: ~tp~n", [self(), ?LINE, Error]),
gen_tcp:shutdown(Socket, read_write),
@ -217,6 +224,7 @@ system_replace_state(StateFun, State) ->
%%% http request handling
handle_request(Sock, R = #request{method = M, path = P}) when M =/= undefined, P =/= undefined ->
zx:tell("~p ~p ~ts", [self(), M, P]),
route(Sock, M, P, R).