shithub: mc

ref: f6e16f0c9f8d68a66c279059e4185d79a5b665a0
dir: /lib/http/session.myr/

View raw version
use std
use bio

use "types"

pkg http =
	const mksession	: (schema : schema, host : byte[:], port : int -> std.result(session#, err))
	const freesession	: (s : session# -> void)

	//const setcookie	: (s : session#, name : byte[:], val : byte[:] -> void)
	//const getcookie	: (s : session#, name : byte[:] -> void)
	//const clearjar	: (s : session# -> void)

	pkglocal const ioput	: (s : session#, fmt : byte[:], args : ... -> bool)
	pkglocal const ioflush	: (s : session# -> void)
;;

const mksession = {schema, host, port
	var s, sess

	match schema
	| `Http:	/* nothing */
	| `Https:	std.fatal("unsupported protocol\n")
	;;

	s = std.fmt("tcp!{}!{}", host, port)
	match std.dial(s)
	| `std.Err e:	sess = `std.Err `Econn
	| `std.Ok fd:	sess = `std.Ok std.mk([
		.err = false,
		.ua = std.sldup("Myrfoo HTTP"),
		.host = std.sldup(host),
		.f = bio.mkfile(fd, bio.Rw)
	])
	;;
	std.slfree(s)
	-> sess
}

const freesession = {s
	bio.close(s.f)
	std.slfree(s.host)
	std.slfree(s.ua)
	std.free(s)
}

const ioput = {s, fmt, args
	var ap

	if s.err
		-> false
	;;
	ap = std.vastart(&args)
	match bio.putv(s.f, fmt, &ap)
	| `bio.Ok _:	/* nothing */
	| `bio.Err _:	s.err = true
	| `bio.Eof:	s.err = true
	;;
	-> s.err
}

const ioflush = {s
	bio.flush(s.f)
}