have hello world running

This commit is contained in:
Peter Harpending
2025-09-16 15:50:31 -07:00
parent 71909e79b0
commit c63436e380
7 changed files with 1205 additions and 16 deletions
+83 -15
View File
@@ -27,6 +27,8 @@
%%% Type and Record Definitions
-include("http.hrl").
-record(s, {socket = none :: none | gen_tcp:socket()}).
@@ -127,22 +129,17 @@ listen(Parent, Debug, ListenSocket) ->
loop(Parent, Debug, State = #s{socket = Socket}) ->
ok = inet:setopts(Socket, [{active, once}]),
receive
{tcp, Socket, <<"bye\r\n">>} ->
ok = io:format("~p Client saying goodbye. Bye!~n", [self()]),
ok = gen_tcp:send(Socket, "Bye!\r\n"),
ok = gen_tcp:shutdown(Socket, read_write),
exit(normal);
{tcp, Socket, Message} ->
ok = io:format("~p received: ~tp~n", [self(), Message]),
ok = fd_client_man:echo(Message),
loop(Parent, Debug, State);
{relay, Sender, Message} when Sender == self() ->
ok = gen_tcp:send(Socket, ["Message from YOU: ", Message]),
loop(Parent, Debug, State);
{relay, Sender, Message} ->
From = io_lib:format("Message from ~tp: ", [Sender]),
ok = gen_tcp:send(Socket, [From, Message]),
loop(Parent, Debug, State);
%ok = io:format("~p received: ~tp~n", [self(), Message]),
case qhl:parse(Socket, Message) of
{ok, Req, none} ->
handle_request(Socket, Req),
loop(Parent, Debug, State);
Error ->
io:format("~tp:~tp error: ~tp~n", [self(), ?LINE, Error]),
gen_tcp:shutdown(Socket, read_write),
exit(normal)
end;
{tcp_closed, Socket} ->
ok = io:format("~p Socket closed, retiring.~n", [self()]),
exit(normal);
@@ -151,6 +148,18 @@ loop(Parent, Debug, State = #s{socket = Socket}) ->
Unexpected ->
ok = io:format("~p Unexpected message: ~tp", [self(), Unexpected]),
loop(Parent, Debug, State)
%{tcp, Socket, <<"bye\r\n">>} ->
% ok = io:format("~p Client saying goodbye. Bye!~n", [self()]),
% ok = gen_tcp:send(Socket, "Bye!\r\n"),
% ok = gen_tcp:shutdown(Socket, read_write),
% exit(normal);
%{relay, Sender, Message} when Sender == self() ->
% ok = gen_tcp:send(Socket, ["Message from YOU: ", Message]),
% loop(Parent, Debug, State);
%{relay, Sender, Message} ->
% From = io_lib:format("Message from ~tp: ", [Sender]),
% ok = gen_tcp:send(Socket, [From, Message]),
% loop(Parent, Debug, State);
end.
@@ -202,3 +211,62 @@ system_get_state(State) -> {ok, State}.
system_replace_state(StateFun, State) ->
{ok, StateFun(State), State}.
%%% http request handling
handle_request(Sock, R = #request{method = M, path = P}) when M =/= undefined, P =/= undefined ->
route(Sock, M, P, R).
route(Sock, get, <<"/">>, _) -> home(Sock);
route(Sock, _, _, _) -> http_err(Sock, 404).
home(Sock) ->
%% fixme: cache
Path_IH = filename:join([zx:get_home(), "priv", "index.html"]),
case file:read_file(Path_IH) of
{ok, Body} ->
Resp = #response{headers = [{"content-type", "text/html"}],
body = Body},
respond(Sock, Resp);
Error ->
io:format("~p error: ~p~n", [self(), Error]),
http_err(Sock, 500)
end.
http_err(_, _) ->
error(todo).
respond(Sock, Response) ->
gen_tcp:send(Sock, fmtresp(Response)).
fmtresp(#response{type = page, %% no idea what {data, String} is
version = http11,
code = Code,
headers = Hs,
body = Body}) ->
%% need byte size for binary
Headers = add_headers(Hs, Body),
[io_lib:format("HTTP/1.1 ~tp ~ts", [Code, qhl:slogan(Code)]), "\r\n",
[io_lib:format("~ts: ~ts\r\n", [K, V]) || {K, V} <- Headers],
"\r\n",
Body].
%% body needed just for size
add_headers(Hs, Body) ->
Defaults = default_headers(Body),
Hs2 = proplists:to_map(Hs),
proplists:from_map(maps:merge(Defaults, Hs2)).
default_headers(Body) ->
BodySize = byte_size(iolist_to_binary(Body)),
#{"Server" => "fewd 0.1.0",
"Date" => qhl:ridiculous_web_date(),
"Content-Length" => io_lib:format("~p", [BodySize])}.
+1076
View File
File diff suppressed because it is too large Load Diff