1
Fork 0
hare-http/net/http/server.ha

45 lines
962 B
Hare
Raw Permalink Normal View History

use net;
use net::ip;
use net::tcp;
2024-06-01 13:44:54 +00:00
use net::tcp::{listen_option};
export type server = struct {
socket: net::socket,
};
export type server_request = struct {
socket: net::socket,
request: request,
};
2024-06-01 13:44:54 +00:00
export fn listen(ip: ip::addr, port: u16, options: listen_option...) (*server | net::error) = {
return alloc(server {
2024-06-01 13:44:54 +00:00
socket = tcp::listen(ip, port, options...)?,
});
};
export fn server_finish(server: *server) void = {
net::close(server.socket)!;
free(server);
};
export fn accept(server: *server) (net::socket | net::error) = {
return net::accept(server.socket)?;
};
export fn serve(server: *server) (*server_request | net::error) = {
const socket = accept(server)?;
const request = request_parse(socket)!;
return alloc(server_request {
request = request,
socket = socket,
});
};
export fn serve_finish(serv_req: *server_request) void = {
parsed_request_finish(&serv_req.request);
net::close(serv_req.socket)!;
free(serv_req);
};