summaryrefslogtreecommitdiff
path: root/lib/http/session.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/http/session.myr')
-rw-r--r--lib/http/session.myr80
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/http/session.myr b/lib/http/session.myr
new file mode 100644
index 0000000..4091165
--- /dev/null
+++ b/lib/http/session.myr
@@ -0,0 +1,80 @@
+use std
+use 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)
+}