From 1d1e9c0e16894bc523a75a194614b65aacdb91c5 Mon Sep 17 00:00:00 2001 From: Philip Monk Date: Sat, 9 Nov 2019 12:22:59 -0800 Subject: [PATCH] eyre: better userspace libraries --- pkg/arvo/age/clock.hoon | 44 +++++++++++++++++++--------------- pkg/arvo/lib/http-handler.hoon | 28 +++++++++------------- pkg/arvo/lib/server.hoon | 13 ++++++++-- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/pkg/arvo/age/clock.hoon b/pkg/arvo/age/clock.hoon index 6f6e61c8d3..1bbff5a93f 100644 --- a/pkg/arvo/age/clock.hoon +++ b/pkg/arvo/age/clock.hoon @@ -9,25 +9,8 @@ =, format :: %+ verb & +%- http-handler ^- agent:mall -%+ http-handler - %- require-authorization:app - |= =inbound-request:eyre - ^- response:http-handler - =/ request-line (parse-request-line url.request.inbound-request) - =/ back-path (flop site.request-line) - =/ name=@t - =/ back-path (flop site.request-line) - ?~ back-path - '' - i.back-path - :: - ?~ back-path - not-found:gen - ?: =(name 'tile') - (js-response:gen tile-js) - not-found:gen -:: |_ =bowl:mall +* this . def ~(. (default-agent this %|) bowl) @@ -42,7 +25,30 @@ == ++ on-save on-save:def ++ on-load on-load:def -++ on-poke on-poke:def +++ on-poke + |= [=mark =vase] + ^- (quip card:agent:mall _this) + ?. ?=(%http-request mark) + (on-poke:def mark vase) + =+ !<([=path =inbound-request:eyre] vase) + :_ this + %+ give-simple-payload:app path + %+ require-authorization:app inbound-request + |= =inbound-request:eyre + =/ request-line (parse-request-line url.request.inbound-request) + =/ back-path (flop site.request-line) + =/ name=@t + =/ back-path (flop site.request-line) + ?~ back-path + '' + i.back-path + :: + ?~ back-path + not-found:gen + ?: =(name 'tile') + (js-response:gen tile-js) + not-found:gen +:: ++ on-watch |= =path ^- (quip card:agent:mall _this) diff --git a/pkg/arvo/lib/http-handler.hoon b/pkg/arvo/lib/http-handler.hoon index fb3e4cb949..4ca9ec939c 100644 --- a/pkg/arvo/lib/http-handler.hoon +++ b/pkg/arvo/lib/http-handler.hoon @@ -1,11 +1,7 @@ -:: wrap an http handler without having to worry about subscriptions +:: delay incoming http requests until eyre is subscribed to responses. :: -|% -+$ response simple-payload:http -+$ handler $-(inbound-request:eyre response) --- -|= [=handler =agent:mall] -=| state=[count=@ud map=(map app-id=@ud response)] +|= =agent:mall +=| state=[count=@ud map=(map app-id=@ud inbound-request:eyre)] ^- agent:mall |_ =bowl:mall +* this . @@ -34,10 +30,9 @@ =^ cards agent (on-poke:ag mark vase) [cards this] =+ !<([eyre-id=@ud =inbound-request:eyre] vase) - =/ response (handler inbound-request) =/ app-id count.state =: count.state +(count.state) - map.state (~(put by map.state) app-id response) + map.state (~(put by map.state) app-id inbound-request) == :_ this :_ ~ [%pass / %arvo %e %start-watching eyre-id app-id] @@ -49,18 +44,17 @@ =^ cards agent (on-watch:ag path) [cards this] =/ app-id (slav %ud i.t.path) - =/ response (~(get by map.state) app-id) - :_ this(map.state (~(del by map.state) app-id)) - ?~ response + =/ request (~(get by map.state) app-id) + =. map.state (~(del by map.state) app-id) + ?~ request + :_ this ^- (list card:agent:mall) :~ [%give %fact `path %http-response-cancel !>(~)] [%give %kick `path ~] == - ^- (list card:agent:mall) - :~ [%give %fact `path %http-response-header !>(response-header.u.response)] - [%give %fact `path %http-response-data !>(data.u.response)] - [%give %kick `path ~] - == + =^ cards agent + (on-poke:ag %http-request !>([path u.request])) + [cards this] :: ++ on-leave |= =path diff --git a/pkg/arvo/lib/server.hoon b/pkg/arvo/lib/server.hoon index ff5180fad1..de58f9e13c 100644 --- a/pkg/arvo/lib/server.hoon +++ b/pkg/arvo/lib/server.hoon @@ -25,8 +25,9 @@ :: +require-authorization: redirect to the login page when unauthenticated :: ++ require-authorization - |= handler=$-(inbound-request:eyre simple-payload:http) - |= =inbound-request:eyre + |= $: =inbound-request:eyre + handler=$-(inbound-request:eyre simple-payload:http) + == ^- simple-payload:http :: ?: authenticated.inbound-request @@ -39,6 +40,14 @@ "/~/login?redirect={(trip url.request.inbound-request)}" [[307 ['location' redirect]~] ~] :: + ++ give-simple-payload + |= [=path =simple-payload:http] + ^- (list card:agent:mall) + :~ [%give %fact `path %http-response-header !>(response-header.simple-payload)] + [%give %fact `path %http-response-data !>(data.simple-payload)] + [%give %kick `path ~] + == + :: ++ html-response |= oct-html=octs ^- http-event:http