80 lines · 1.4 KB · Text
RawDownload
use stduse bio use "types" pkg http =	const mksession		: (schema : schema, host : byte[:], port : uint16 -> std.result(session#, err))	const mksrvsession	: (fd	: std.fd -> session#)	const freesession	: (s : session# -> void) 	pkglocal const ioput	: (s : session#, fmt : byte[:], args : ... -> bool)	pkglocal const iowrite	: (s : session#, buf : byte[:] -> 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 mksrvsession = {fd	-> std.mk([		.err = false,		.srvname = std.sldup("Myrfoo HTTP Server"),		.f = bio.mkfile(fd, bio.Rw),	])} 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)	| `std.Ok _:	/* nothing */	| `std.Err _:	s.err = true	;;	-> s.err} const iowrite = {s, buf	if s.err		-> false	;;	match bio.write(s.f, buf)	| `std.Ok _:	/* nothing */	| `std.Err _:	s.err = true	;;	-> s.err} const ioflush = {s	bio.flush(s.f)}