shithub: mc

ref: fd3d8637eec28cba6a0fe52811e39b0de5ec977e
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.Fail e:	sess = `std.Fail `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)
	std.putv(fmt, &ap)
	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)
}