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

45 lines
962 B
Hare

use net;
use net::ip;
use net::tcp;
use net::tcp::{listen_option};
export type server = struct {
socket: net::socket,
};
export type server_request = struct {
socket: net::socket,
request: request,
};
export fn listen(ip: ip::addr, port: u16, options: listen_option...) (*server | net::error) = {
return alloc(server {
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);
};