2018-09-20 02:29:36 +03:00
|
|
|
!:
|
|
|
|
:: lighter than eyre
|
|
|
|
::
|
|
|
|
|= pit=vase
|
2019-07-04 09:09:44 +03:00
|
|
|
=, eyre
|
2018-09-20 02:29:36 +03:00
|
|
|
:: internal data structures
|
|
|
|
::
|
|
|
|
=> =~
|
|
|
|
::
|
|
|
|
:: internal data structures that won't go in zuse
|
|
|
|
::
|
|
|
|
|%
|
|
|
|
+$ move
|
|
|
|
::
|
|
|
|
$: :: duct: request identifier
|
|
|
|
::
|
|
|
|
=duct
|
|
|
|
::
|
|
|
|
::
|
|
|
|
card=(wind note gift:able)
|
|
|
|
==
|
2019-02-09 02:03:46 +03:00
|
|
|
:: +note: private request from http-server to another vane
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
+$ note
|
2018-11-16 00:43:10 +03:00
|
|
|
$% :: %b: to behn
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
$: %b
|
|
|
|
::
|
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
$% [%rest p=@da]
|
|
|
|
[%wait p=@da]
|
2018-11-15 21:27:10 +03:00
|
|
|
== ==
|
2019-03-13 23:29:22 +03:00
|
|
|
:: %d: to dill
|
|
|
|
::
|
|
|
|
$: %d
|
|
|
|
::
|
|
|
|
::
|
|
|
|
$% [%flog =flog:dill]
|
|
|
|
== ==
|
2018-11-15 21:27:10 +03:00
|
|
|
:: %f: to ford
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
$: %f
|
|
|
|
::
|
|
|
|
::
|
2018-12-15 03:04:13 +03:00
|
|
|
$% [%build live=? schematic=schematic:ford]
|
|
|
|
[%kill ~]
|
2018-09-21 02:36:04 +03:00
|
|
|
== ==
|
2019-11-05 10:42:59 +03:00
|
|
|
:: %m: to mall
|
2018-09-21 02:36:04 +03:00
|
|
|
::
|
2019-11-05 10:42:59 +03:00
|
|
|
$: %m
|
2018-09-21 02:36:04 +03:00
|
|
|
::
|
|
|
|
::
|
2019-11-05 10:42:59 +03:00
|
|
|
$>(%deal task:able:mall)
|
|
|
|
== ==
|
2018-09-24 21:48:19 +03:00
|
|
|
:: +sign: private response from another vane to ford
|
|
|
|
::
|
|
|
|
+$ sign
|
2018-11-20 01:59:58 +03:00
|
|
|
$% :: %b: from behn
|
|
|
|
::
|
|
|
|
$: %b
|
|
|
|
::
|
|
|
|
::
|
2019-06-04 23:28:20 +03:00
|
|
|
$% [%wake error=(unit tang)]
|
2018-11-20 01:59:58 +03:00
|
|
|
== ==
|
|
|
|
:: %f: from ford
|
2018-10-10 21:51:52 +03:00
|
|
|
::
|
|
|
|
$: %f
|
|
|
|
::
|
|
|
|
::
|
|
|
|
$% [%made date=@da result=made-result:ford]
|
|
|
|
== ==
|
2019-11-05 10:42:59 +03:00
|
|
|
:: %m: from mall
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2019-11-05 10:42:59 +03:00
|
|
|
$: %m
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2018-10-04 00:05:36 +03:00
|
|
|
::
|
2019-11-05 10:42:59 +03:00
|
|
|
gift:able:mall
|
|
|
|
:: $>(%unto gift:able:mall)
|
|
|
|
== ==
|
2018-09-20 02:29:36 +03:00
|
|
|
--
|
|
|
|
:: more structures
|
|
|
|
::
|
|
|
|
|%
|
|
|
|
++ axle
|
2019-02-09 02:03:46 +03:00
|
|
|
$: :: date: date at which http-server's state was updated to this data structure
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
2019-10-07 02:26:03 +03:00
|
|
|
date=%~2019.10.6
|
2018-09-20 02:29:36 +03:00
|
|
|
:: server-state: state of inbound requests
|
|
|
|
::
|
|
|
|
=server-state
|
|
|
|
==
|
|
|
|
:: +server-state: state relating to open inbound HTTP connections
|
|
|
|
::
|
|
|
|
+$ server-state
|
|
|
|
$: :: bindings: actions to dispatch to when a binding matches
|
|
|
|
::
|
|
|
|
:: Eyre is responsible for keeping its bindings sorted so that it
|
|
|
|
:: will trigger on the most specific binding first. Eyre should send
|
|
|
|
:: back an error response if an already bound binding exists.
|
|
|
|
::
|
|
|
|
:: TODO: It would be nice if we had a path trie. We could decompose
|
|
|
|
:: the :binding into a (map (unit @t) (trie knot =action)).
|
|
|
|
::
|
|
|
|
bindings=(list [=binding =duct =action])
|
2018-09-24 21:48:19 +03:00
|
|
|
:: connections: open http connections not fully complete
|
2018-09-21 02:36:04 +03:00
|
|
|
::
|
2018-09-24 21:48:19 +03:00
|
|
|
connections=(map duct outstanding-connection)
|
2018-09-27 02:18:40 +03:00
|
|
|
:: authentication-state: state managed by the +authentication core
|
|
|
|
::
|
|
|
|
=authentication-state
|
2018-11-15 21:27:10 +03:00
|
|
|
:: channel-state: state managed by the +channel core
|
|
|
|
::
|
|
|
|
=channel-state
|
2019-03-21 08:28:32 +03:00
|
|
|
:: domains: domain-names that resolve to us
|
|
|
|
::
|
|
|
|
domains=(set turf)
|
|
|
|
:: http-config: our server configuration
|
|
|
|
::
|
|
|
|
=http-config
|
|
|
|
:: ports: live servers
|
|
|
|
::
|
|
|
|
ports=[insecure=@ud secure=(unit @ud)]
|
2019-04-10 00:03:21 +03:00
|
|
|
:: outgoing-duct: to unix
|
|
|
|
::
|
|
|
|
outgoing-duct=duct
|
2018-09-24 21:48:19 +03:00
|
|
|
==
|
|
|
|
:: +outstanding-connection: open http connections not fully complete:
|
|
|
|
::
|
|
|
|
:: This refers to outstanding connections where the connection to
|
|
|
|
:: outside is opened and we are currently waiting on ford or an app to
|
|
|
|
:: produce the results.
|
|
|
|
::
|
|
|
|
+$ outstanding-connection
|
|
|
|
$: :: action: the action that had matched
|
|
|
|
::
|
|
|
|
=action
|
2018-10-25 00:31:19 +03:00
|
|
|
:: inbound-request: the original request which caused this connection
|
2018-10-10 21:51:52 +03:00
|
|
|
::
|
2018-10-25 00:31:19 +03:00
|
|
|
=inbound-request
|
2019-02-09 00:16:42 +03:00
|
|
|
:: response-header: set when we get our first %start
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
response-header=(unit response-header:http)
|
2018-09-24 21:48:19 +03:00
|
|
|
:: bytes-sent: the total bytes sent in response
|
2018-09-21 02:36:04 +03:00
|
|
|
::
|
2018-09-24 21:48:19 +03:00
|
|
|
bytes-sent=@ud
|
2018-09-20 02:29:36 +03:00
|
|
|
==
|
|
|
|
:: +action: the action to take when a binding matches an incoming request
|
|
|
|
::
|
|
|
|
+$ action
|
|
|
|
$% :: dispatch to a generator
|
|
|
|
::
|
2018-10-11 01:26:51 +03:00
|
|
|
[%gen =generator]
|
2018-09-20 02:29:36 +03:00
|
|
|
:: dispatch to an application
|
|
|
|
::
|
|
|
|
[%app app=term]
|
2018-09-27 02:18:40 +03:00
|
|
|
:: internal authentication page
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
2018-11-15 21:27:10 +03:00
|
|
|
[%authentication ~]
|
|
|
|
:: gall channel system
|
2018-11-13 22:12:59 +03:00
|
|
|
::
|
2018-11-15 21:27:10 +03:00
|
|
|
[%channel ~]
|
2019-01-11 03:35:09 +03:00
|
|
|
:: respond with the default file not found page
|
|
|
|
::
|
|
|
|
[%four-oh-four ~]
|
2018-09-27 02:18:40 +03:00
|
|
|
==
|
2018-10-26 02:32:54 +03:00
|
|
|
:: +authentication-state: state used in the login system
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
|
|
|
+$ authentication-state
|
2018-11-15 21:27:10 +03:00
|
|
|
$: :: sessions: a mapping of session cookies to session information
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
|
|
|
sessions=(map @uv session)
|
|
|
|
==
|
|
|
|
:: +session: server side data about a session
|
|
|
|
::
|
|
|
|
+$ session
|
2018-11-15 21:27:10 +03:00
|
|
|
$: :: expiry-time: when this session expires
|
2018-10-26 02:32:54 +03:00
|
|
|
::
|
|
|
|
:: We check this server side, too, so we aren't relying on the browser
|
|
|
|
:: to properly handle cookie expiration as a security mechanism.
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
2018-09-27 02:18:40 +03:00
|
|
|
expiry-time=@da
|
2018-10-26 02:32:54 +03:00
|
|
|
::
|
|
|
|
:: TODO: We should add a system for individual capabilities; we should
|
|
|
|
:: mint some sort of long lived cookie for mobile apps which only has
|
|
|
|
:: access to a single application path.
|
2018-09-20 02:29:36 +03:00
|
|
|
==
|
2018-11-15 21:27:10 +03:00
|
|
|
:: channel-state: state used in the channel system
|
|
|
|
::
|
|
|
|
+$ channel-state
|
|
|
|
$: :: session: mapping between an arbitrary key to a channel
|
|
|
|
::
|
|
|
|
session=(map @t channel)
|
2018-11-22 00:37:26 +03:00
|
|
|
:: by-duct: mapping from ducts to session key
|
|
|
|
::
|
|
|
|
duct-to-key=(map duct @t)
|
2018-11-15 21:27:10 +03:00
|
|
|
==
|
2018-11-21 21:34:22 +03:00
|
|
|
:: +timer: a reference to a timer so we can cancel or update it.
|
|
|
|
::
|
|
|
|
+$ timer
|
|
|
|
$: :: date: time when the timer will fire
|
|
|
|
::
|
|
|
|
date=@da
|
|
|
|
:: duct: duct that set the timer so we can cancel
|
|
|
|
::
|
|
|
|
=duct
|
|
|
|
==
|
2018-11-15 21:27:10 +03:00
|
|
|
:: channel: connection to the browser
|
|
|
|
::
|
|
|
|
:: Channels are the main method where a webpage communicates with Gall
|
|
|
|
:: apps. Subscriptions and pokes are issues with PUT requests on a path,
|
|
|
|
:: while GET requests on that same path open a persistent EventSource
|
|
|
|
:: channel.
|
|
|
|
::
|
|
|
|
:: The EventSource API is a sequence number based API that browser provide
|
|
|
|
:: which allow the server to push individual events to the browser over a
|
|
|
|
:: connection held open. In case of reconnection, the browser will send a
|
|
|
|
:: 'Last-Event-Id: ' header to the server; the server then resends all
|
|
|
|
:: events since then.
|
|
|
|
::
|
|
|
|
+$ channel
|
2018-11-21 04:06:04 +03:00
|
|
|
$: :: channel-state: expiration time or the duct currently listening
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
:: For each channel, there is at most one open EventSource
|
|
|
|
:: connection. A 400 is issues on duplicate attempts to connect to the
|
|
|
|
:: same channel. When an EventSource isn't connected, we set a timer
|
|
|
|
:: to reap the subscriptions. This timer shouldn't be too short
|
|
|
|
:: because the
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2018-11-21 21:34:22 +03:00
|
|
|
state=(each timer duct)
|
2018-11-15 21:27:10 +03:00
|
|
|
:: next-id: next sequence number to use
|
|
|
|
::
|
|
|
|
next-id=@ud
|
|
|
|
:: events: unacknowledged events
|
|
|
|
::
|
|
|
|
:: We keep track of all events where we haven't received a
|
|
|
|
:: 'Last-Event-Id: ' response from the client or a per-poke {'ack':
|
|
|
|
:: ...} call. When there's an active EventSource connection on this
|
|
|
|
:: channel, we send the event but we still add it to events because we
|
|
|
|
:: can't assume it got received until we get an acknowledgment.
|
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
events=(qeu [id=@ud lines=wall])
|
2018-11-15 21:27:10 +03:00
|
|
|
:: subscriptions: gall subscriptions
|
|
|
|
::
|
|
|
|
:: We maintain a list of subscriptions so if a channel times out, we
|
|
|
|
:: can cancel all the subscriptions we've made.
|
|
|
|
::
|
2019-06-14 21:37:09 +03:00
|
|
|
subscriptions=(map wire [ship=@p app=term =path duc=duct])
|
2019-10-07 02:26:03 +03:00
|
|
|
:: heartbeat: sse heartbeat timer
|
|
|
|
::
|
|
|
|
heartbeat=(unit timer)
|
2018-11-15 21:27:10 +03:00
|
|
|
==
|
|
|
|
:: channel-request: an action requested on a channel
|
|
|
|
::
|
|
|
|
+$ channel-request
|
|
|
|
$% :: %ack: acknowledges that the client has received events up to :id
|
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
[%ack event-id=@ud]
|
2018-11-15 21:27:10 +03:00
|
|
|
:: %poke: pokes an application, translating :json to :mark.
|
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
[%poke request-id=@ud ship=@p app=term mark=@tas =json]
|
2019-11-07 09:19:32 +03:00
|
|
|
:: %watch: subscribes to an application path
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
[%watch request-id=@ud ship=@p app=term =path]
|
|
|
|
:: %leave: unsubscribes from an application path
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
[%leave request-id=@ud subscription-id=@ud]
|
2019-06-14 00:53:04 +03:00
|
|
|
:: %delete: kills a channel
|
|
|
|
::
|
|
|
|
[%delete ~]
|
2018-11-15 21:27:10 +03:00
|
|
|
==
|
2018-11-20 01:59:58 +03:00
|
|
|
:: channel-timeout: the delay before a channel should be reaped
|
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
++ channel-timeout ~h12
|
2018-09-20 02:29:36 +03:00
|
|
|
--
|
|
|
|
:: utilities
|
|
|
|
::
|
|
|
|
|%
|
2019-01-30 22:06:24 +03:00
|
|
|
:: +combine-octs: combine multiple octs into one
|
|
|
|
::
|
|
|
|
++ combine-octs
|
|
|
|
|= a=(list octs)
|
|
|
|
^- octs
|
|
|
|
:- %+ roll a
|
|
|
|
|= [=octs sum=@ud]
|
|
|
|
(add sum p.octs)
|
|
|
|
(can 3 a)
|
2019-01-12 01:15:17 +03:00
|
|
|
:: +prune-events: removes all items from the front of the queue up to :id
|
|
|
|
::
|
|
|
|
++ prune-events
|
|
|
|
|= [q=(qeu [id=@ud lines=wall]) id=@ud]
|
|
|
|
^+ q
|
|
|
|
:: if the queue is now empty, that's fine
|
|
|
|
::
|
2019-01-12 02:18:53 +03:00
|
|
|
?: =(~ q)
|
2019-01-12 01:15:17 +03:00
|
|
|
~
|
|
|
|
::
|
2019-01-17 01:58:07 +03:00
|
|
|
=/ next=[item=[id=@ud lines=wall] _q] ~(get to q)
|
2019-01-12 01:15:17 +03:00
|
|
|
:: if the head of the queue is newer than the acknowledged id, we're done
|
|
|
|
::
|
2019-01-17 01:58:07 +03:00
|
|
|
?: (gth id.item.next id)
|
2019-01-12 02:18:53 +03:00
|
|
|
q
|
|
|
|
:: otherwise, check next item
|
2019-01-12 01:15:17 +03:00
|
|
|
::
|
2019-01-17 01:58:07 +03:00
|
|
|
$(q +:next)
|
2018-11-15 21:27:10 +03:00
|
|
|
:: +parse-channel-request: parses a list of channel-requests
|
|
|
|
::
|
2018-11-16 00:43:10 +03:00
|
|
|
:: Parses a json array into a list of +channel-request. If any of the items
|
|
|
|
:: in the list fail to parse, the entire thing fails so we can 400 properly
|
|
|
|
:: to the client.
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
++ parse-channel-request
|
2018-11-16 00:43:10 +03:00
|
|
|
|= request-list=json
|
|
|
|
^- (unit (list channel-request))
|
|
|
|
:: parse top
|
|
|
|
::
|
|
|
|
=, dejs-soft:format
|
|
|
|
=- ((ar -) request-list)
|
|
|
|
::
|
|
|
|
|= item=json
|
|
|
|
^- (unit channel-request)
|
|
|
|
::
|
|
|
|
?~ maybe-key=((ot action+so ~) item)
|
|
|
|
~
|
|
|
|
?: =('ack' u.maybe-key)
|
2018-11-21 04:06:04 +03:00
|
|
|
((pe %ack (ot event-id+ni ~)) item)
|
2018-11-16 00:43:10 +03:00
|
|
|
?: =('poke' u.maybe-key)
|
2018-11-21 04:06:04 +03:00
|
|
|
((pe %poke (ot id+ni ship+(su fed:ag) app+so mark+(su sym) json+some ~)) item)
|
2018-11-16 00:43:10 +03:00
|
|
|
?: =('subscribe' u.maybe-key)
|
|
|
|
%. item
|
2019-11-07 09:19:32 +03:00
|
|
|
%+ pe %watch
|
2018-11-21 04:06:04 +03:00
|
|
|
(ot id+ni ship+(su fed:ag) app+so path+(su ;~(pfix fas (more fas urs:ab))) ~)
|
2018-11-16 00:43:10 +03:00
|
|
|
?: =('unsubscribe' u.maybe-key)
|
|
|
|
%. item
|
2019-11-07 09:19:32 +03:00
|
|
|
%+ pe %leave
|
2019-03-27 01:52:32 +03:00
|
|
|
(ot id+ni subscription+ni ~)
|
2019-06-14 00:53:04 +03:00
|
|
|
?: =('delete' u.maybe-key)
|
|
|
|
`[%delete ~]
|
2018-11-16 00:43:10 +03:00
|
|
|
:: if we reached this, we have an invalid action key. fail parsing.
|
|
|
|
::
|
|
|
|
~
|
2018-09-27 02:18:40 +03:00
|
|
|
:: +login-page: internal page to login to an Urbit
|
|
|
|
::
|
|
|
|
++ login-page
|
2019-07-02 01:16:52 +03:00
|
|
|
|= [redirect-url=(unit @t) our=@p]
|
2018-09-27 02:18:40 +03:00
|
|
|
^- octs
|
|
|
|
=+ redirect-str=?~(redirect-url "" (trip u.redirect-url))
|
|
|
|
%- as-octs:mimes:html
|
|
|
|
%- crip
|
|
|
|
%- en-xml:html
|
|
|
|
;html
|
|
|
|
;head
|
2019-07-02 01:16:52 +03:00
|
|
|
;meta(charset "utf-8");
|
2018-09-27 02:18:40 +03:00
|
|
|
;title:"Sign in"
|
2019-07-02 01:16:52 +03:00
|
|
|
;style:'''
|
|
|
|
@import url("https://rsms.me/inter/inter.css");
|
|
|
|
@font-face {
|
|
|
|
font-family: "Source Code Pro";
|
|
|
|
src: url("https://storage.googleapis.com/media.urbit.org/fonts/scp-medium.woff");
|
|
|
|
font-weight: 500
|
|
|
|
}
|
|
|
|
html, body {
|
|
|
|
font-family: Inter, sans-serif;
|
|
|
|
height: 100%;
|
|
|
|
margin: 0 !important;
|
|
|
|
width: 100%;
|
|
|
|
background: #000;
|
|
|
|
color: #fff;
|
|
|
|
display: table;
|
|
|
|
}
|
|
|
|
form {
|
|
|
|
margin: 0 !important;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
flex: 2;
|
|
|
|
}
|
|
|
|
#topborder {
|
|
|
|
border-top: 3px #fff solid;
|
|
|
|
}
|
2019-08-09 00:16:45 +03:00
|
|
|
#ship-name {
|
|
|
|
font-family: 'Source Code Pro', monospace, sans-serif;
|
|
|
|
}
|
2019-07-02 01:16:52 +03:00
|
|
|
h1 {
|
|
|
|
line-height: 77px;
|
|
|
|
font-size: 64px;
|
|
|
|
-webkit-margin-before: 0;
|
|
|
|
-webkit-margin-after: 0;
|
|
|
|
-webkit-margin-start: 0;
|
|
|
|
-webkit-margin-end: 0;
|
|
|
|
font-weight: 500;
|
|
|
|
flex: 1;
|
|
|
|
padding-top: 22px;
|
|
|
|
margin-bottom: 66px;
|
|
|
|
}
|
|
|
|
#main {
|
|
|
|
vertical-align: middle;
|
|
|
|
display: table-cell;
|
|
|
|
}
|
|
|
|
input {
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
font-size: 64px;
|
|
|
|
line-height: 77px;
|
|
|
|
color: #fff;
|
|
|
|
background: #000;
|
|
|
|
border: none;
|
|
|
|
flex: 1;
|
|
|
|
margin-bottom: 66px;
|
|
|
|
font-weight: 500;
|
|
|
|
font-family: 'Source Code Pro', monospace, sans-serif;
|
|
|
|
}
|
|
|
|
button {
|
|
|
|
background: #000;
|
|
|
|
border: none;
|
|
|
|
color: #fff;
|
|
|
|
line-height: 77px;
|
|
|
|
font-size: 64px;
|
|
|
|
text-align: left;
|
|
|
|
flex: 1;
|
|
|
|
font-weight: 500;
|
|
|
|
}
|
|
|
|
#inner {
|
|
|
|
width: 75%;
|
|
|
|
margin: 0 auto;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
}
|
|
|
|
'''
|
2018-09-27 02:18:40 +03:00
|
|
|
==
|
|
|
|
;body
|
2019-07-02 01:16:52 +03:00
|
|
|
;div#main
|
|
|
|
;div#inner
|
2019-07-19 01:38:58 +03:00
|
|
|
;h1#topborder:"Welcome"
|
2019-08-09 00:16:45 +03:00
|
|
|
;h1#ship-name:"{(scow %p our)}"
|
2019-07-02 01:16:52 +03:00
|
|
|
;form(action "/~/login", method "post", enctype "application/x-www-form-urlencoded")
|
|
|
|
;input(type "password", name "password", placeholder "passcode", autofocus "true");
|
|
|
|
;input(type "hidden", name "redirect", value redirect-str);
|
|
|
|
;button(type "submit"):"→ Authenticate"
|
|
|
|
==
|
|
|
|
==
|
2018-09-27 02:18:40 +03:00
|
|
|
==
|
|
|
|
==
|
|
|
|
==
|
2018-11-21 04:06:04 +03:00
|
|
|
:: +render-tang-to-marl: renders a tang and adds <br/> tags between each line
|
2018-10-11 01:26:51 +03:00
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
++ render-tang-to-marl
|
2018-10-11 01:26:51 +03:00
|
|
|
|= {wid/@u tan/tang}
|
|
|
|
^- marl
|
|
|
|
=/ raw=(list tape) (zing (turn tan |=(a/tank (wash 0^wid a))))
|
|
|
|
::
|
|
|
|
|- ^- marl
|
|
|
|
?~ raw ~
|
|
|
|
[;/(i.raw) ;br; $(raw t.raw)]
|
2018-11-21 04:06:04 +03:00
|
|
|
:: +render-tang-to-wall: renders tang as text lines
|
|
|
|
::
|
|
|
|
++ render-tang-to-wall
|
|
|
|
|= {wid/@u tan/tang}
|
|
|
|
^- wall
|
|
|
|
(zing (turn tan |=(a=tank (wash 0^wid a))))
|
|
|
|
:: +wall-to-octs: text to binary output
|
|
|
|
::
|
|
|
|
++ wall-to-octs
|
|
|
|
|= =wall
|
|
|
|
^- (unit octs)
|
|
|
|
::
|
|
|
|
?: =(~ wall)
|
|
|
|
~
|
|
|
|
::
|
|
|
|
:- ~
|
|
|
|
%- as-octs:mimes:html
|
|
|
|
%- crip
|
|
|
|
%- zing
|
|
|
|
%+ turn wall
|
|
|
|
|= t=tape
|
|
|
|
"{t}\0a"
|
2018-10-10 21:51:52 +03:00
|
|
|
:: +internal-server-error: 500 page, with a tang
|
|
|
|
::
|
|
|
|
++ internal-server-error
|
2018-10-11 01:26:51 +03:00
|
|
|
|= [authorized=? url=@t t=tang]
|
2018-10-10 21:51:52 +03:00
|
|
|
^- octs
|
|
|
|
%- as-octs:mimes:html
|
|
|
|
%- crip
|
|
|
|
%- en-xml:html
|
|
|
|
;html
|
|
|
|
;head
|
|
|
|
;title:"500 Internal Server Error"
|
|
|
|
==
|
|
|
|
;body
|
|
|
|
;h1:"Internal Server Error"
|
|
|
|
;p:"There was an error while handling the request for {<(trip url)>}."
|
|
|
|
;* ?: authorized
|
|
|
|
;=
|
2018-11-21 04:06:04 +03:00
|
|
|
;code:"*{(render-tang-to-marl 80 t)}"
|
2018-10-10 21:51:52 +03:00
|
|
|
==
|
|
|
|
~
|
|
|
|
==
|
|
|
|
==
|
2019-03-27 01:52:32 +03:00
|
|
|
:: +error-page: error page, with an error string if logged in
|
2019-03-21 02:00:46 +03:00
|
|
|
::
|
2019-03-21 23:58:37 +03:00
|
|
|
++ error-page
|
|
|
|
|= [code=@ud authorized=? url=@t t=tape]
|
2019-03-21 02:00:46 +03:00
|
|
|
^- octs
|
2019-03-21 23:58:37 +03:00
|
|
|
::
|
|
|
|
=/ code-as-tape=tape (format-ud-as-integer code)
|
|
|
|
=/ message=tape
|
|
|
|
?: =(code 400)
|
|
|
|
"Bad Request"
|
|
|
|
?: =(code 403)
|
|
|
|
"Forbidden"
|
|
|
|
?: =(code 404)
|
|
|
|
"Not Found"
|
|
|
|
"Unknown Error"
|
|
|
|
::
|
2019-03-21 02:00:46 +03:00
|
|
|
%- as-octs:mimes:html
|
|
|
|
%- crip
|
|
|
|
%- en-xml:html
|
|
|
|
;html
|
|
|
|
;head
|
2019-03-21 23:58:37 +03:00
|
|
|
;title:"{code-as-tape} {message}"
|
2019-03-21 02:00:46 +03:00
|
|
|
==
|
|
|
|
;body
|
2019-03-21 23:58:37 +03:00
|
|
|
;h1:"{message}"
|
2019-03-21 02:00:46 +03:00
|
|
|
;p:"There was an error while handling the request for {<(trip url)>}."
|
|
|
|
;* ?: authorized
|
|
|
|
;=
|
|
|
|
;code:"{t}"
|
|
|
|
==
|
|
|
|
~
|
|
|
|
==
|
|
|
|
==
|
2019-01-09 01:44:03 +03:00
|
|
|
:: +channel-js: the urbit javascript interface
|
|
|
|
::
|
2019-01-10 02:43:43 +03:00
|
|
|
:: TODO: Must send 'acks' to the server.
|
|
|
|
::
|
2019-01-09 01:44:03 +03:00
|
|
|
++ channel-js
|
|
|
|
^- octs
|
|
|
|
%- as-octs:mimes:html
|
|
|
|
'''
|
|
|
|
class Channel {
|
|
|
|
constructor() {
|
|
|
|
// unique identifier: current time and random number
|
|
|
|
//
|
|
|
|
this.uid =
|
|
|
|
new Date().getTime().toString() +
|
|
|
|
"-" +
|
|
|
|
Math.random().toString(16).slice(-6);
|
|
|
|
|
|
|
|
this.requestId = 1;
|
2019-01-10 02:43:43 +03:00
|
|
|
|
|
|
|
// the currently connected EventSource
|
|
|
|
//
|
|
|
|
this.eventSource = null;
|
|
|
|
|
|
|
|
// the id of the last EventSource event we received
|
|
|
|
//
|
|
|
|
this.lastEventId = 0;
|
2019-01-09 01:44:03 +03:00
|
|
|
|
2019-01-12 02:18:53 +03:00
|
|
|
// this last event id acknowledgment sent to the server
|
|
|
|
//
|
|
|
|
this.lastAcknowledgedEventId = 0;
|
|
|
|
|
2019-01-09 01:44:03 +03:00
|
|
|
// a registry of requestId to successFunc/failureFunc
|
|
|
|
//
|
|
|
|
// These functions are registered during a +poke and are executed
|
|
|
|
// in the onServerEvent()/onServerError() callbacks. Only one of
|
|
|
|
// the functions will be called, and the outstanding poke will be
|
|
|
|
// removed after calling the success or failure function.
|
|
|
|
//
|
2019-08-15 23:58:23 +03:00
|
|
|
|
2019-01-09 01:44:03 +03:00
|
|
|
this.outstandingPokes = new Map();
|
|
|
|
|
2019-01-10 02:43:43 +03:00
|
|
|
// a registry of requestId to subscription functions.
|
2019-01-09 01:44:03 +03:00
|
|
|
//
|
|
|
|
// These functions are registered during a +subscribe and are
|
|
|
|
// executed in the onServerEvent()/onServerError() callbacks. The
|
|
|
|
// event function will be called whenever a new piece of data on this
|
|
|
|
// subscription is available, which may be 0, 1, or many times. The
|
|
|
|
// disconnect function may be called exactly once.
|
|
|
|
//
|
|
|
|
this.outstandingSubscriptions = new Map();
|
2019-06-14 00:53:04 +03:00
|
|
|
|
|
|
|
this.deleteOnUnload();
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteOnUnload() {
|
2019-09-25 23:41:54 +03:00
|
|
|
window.addEventListener("unload", (event) => {
|
2019-06-14 00:53:04 +03:00
|
|
|
this.delete();
|
|
|
|
});
|
2019-01-09 01:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// sends a poke to an app on an urbit ship
|
|
|
|
//
|
|
|
|
poke(ship, app, mark, json, successFunc, failureFunc) {
|
2019-09-25 23:41:54 +03:00
|
|
|
let id = this.nextId();
|
2019-01-09 01:44:03 +03:00
|
|
|
this.outstandingPokes.set(
|
2019-09-25 23:41:54 +03:00
|
|
|
id,
|
|
|
|
{
|
|
|
|
success: successFunc,
|
|
|
|
fail: failureFunc
|
|
|
|
}
|
|
|
|
);
|
2019-01-09 01:44:03 +03:00
|
|
|
|
2019-01-10 02:43:43 +03:00
|
|
|
this.sendJSONToChannel({
|
2019-09-25 23:41:54 +03:00
|
|
|
id,
|
|
|
|
action: "poke",
|
|
|
|
ship,
|
|
|
|
app,
|
|
|
|
mark,
|
|
|
|
json
|
2019-01-10 02:43:43 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-27 01:52:32 +03:00
|
|
|
// subscribes to a path on an specific app and ship.
|
2019-01-10 02:43:43 +03:00
|
|
|
//
|
2019-03-27 01:52:32 +03:00
|
|
|
// Returns a subscription id, which is the same as the same internal id
|
|
|
|
// passed to your Urbit.
|
2019-09-25 23:41:54 +03:00
|
|
|
subscribe(
|
|
|
|
ship,
|
|
|
|
app,
|
|
|
|
path,
|
|
|
|
connectionErrFunc = () => {},
|
|
|
|
eventFunc = () => {},
|
|
|
|
quitFunc = () => {}) {
|
|
|
|
let id = this.nextId();
|
2019-01-10 02:43:43 +03:00
|
|
|
this.outstandingSubscriptions.set(
|
2019-09-25 23:41:54 +03:00
|
|
|
id,
|
|
|
|
{
|
|
|
|
err: connectionErrFunc,
|
|
|
|
event: eventFunc,
|
|
|
|
quit: quitFunc
|
|
|
|
}
|
|
|
|
);
|
2019-01-10 02:43:43 +03:00
|
|
|
|
|
|
|
this.sendJSONToChannel({
|
2019-09-25 23:41:54 +03:00
|
|
|
id,
|
|
|
|
action: "subscribe",
|
|
|
|
ship,
|
|
|
|
app,
|
|
|
|
path
|
|
|
|
});
|
2019-03-27 01:52:32 +03:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:53:04 +03:00
|
|
|
// quit the channel
|
|
|
|
//
|
|
|
|
delete() {
|
2019-09-25 23:41:54 +03:00
|
|
|
let id = this.nextId();
|
2019-07-02 22:22:13 +03:00
|
|
|
navigator.sendBeacon(this.channelURL(), JSON.stringify([{
|
2019-09-25 23:41:54 +03:00
|
|
|
id,
|
|
|
|
action: "delete"
|
2019-07-02 22:22:13 +03:00
|
|
|
}]));
|
2019-06-14 00:53:04 +03:00
|
|
|
}
|
|
|
|
|
2019-03-27 01:52:32 +03:00
|
|
|
// unsubscribe to a specific subscription
|
|
|
|
//
|
2019-09-25 23:41:54 +03:00
|
|
|
unsubscribe(subscription) {
|
|
|
|
let id = this.nextId();
|
2019-03-27 01:52:32 +03:00
|
|
|
this.sendJSONToChannel({
|
2019-09-25 23:41:54 +03:00
|
|
|
id,
|
|
|
|
action: "unsubscribe",
|
|
|
|
subscription
|
2019-06-14 00:53:04 +03:00
|
|
|
});
|
2019-01-10 02:43:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// sends a JSON command command to the server.
|
|
|
|
//
|
2019-07-02 22:22:13 +03:00
|
|
|
sendJSONToChannel(j) {
|
2019-09-25 23:41:54 +03:00
|
|
|
let req = new XMLHttpRequest();
|
2019-01-10 02:43:43 +03:00
|
|
|
req.open("PUT", this.channelURL());
|
|
|
|
req.setRequestHeader("Content-Type", "application/json");
|
|
|
|
|
2019-01-12 02:18:53 +03:00
|
|
|
if (this.lastEventId == this.lastAcknowledgedEventId) {
|
2019-09-25 23:41:54 +03:00
|
|
|
let x = JSON.stringify([j]);
|
2019-01-12 02:18:53 +03:00
|
|
|
req.send(x);
|
|
|
|
} else {
|
|
|
|
// we add an acknowledgment to clear the server side queue
|
|
|
|
//
|
|
|
|
// The server side puts messages it sends us in a queue until we
|
|
|
|
// acknowledge that we received it.
|
|
|
|
//
|
2019-09-25 23:41:54 +03:00
|
|
|
let x = JSON.stringify(
|
|
|
|
[{action: "ack", "event-id": parseInt(this.lastEventId)}, j]
|
|
|
|
);
|
2019-01-12 02:18:53 +03:00
|
|
|
req.send(x);
|
|
|
|
|
|
|
|
this.lastEventId = this.lastAcknowledgedEventId;
|
|
|
|
}
|
2019-01-10 02:43:43 +03:00
|
|
|
|
2019-07-02 22:22:13 +03:00
|
|
|
this.connectIfDisconnected();
|
2019-01-09 01:44:03 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 02:43:43 +03:00
|
|
|
// connects to the EventSource if we are not currently connected
|
|
|
|
//
|
2019-01-09 01:44:03 +03:00
|
|
|
connectIfDisconnected() {
|
2019-01-11 03:35:09 +03:00
|
|
|
if (this.eventSource) {
|
2019-01-09 01:44:03 +03:00
|
|
|
return;
|
2019-01-11 03:35:09 +03:00
|
|
|
}
|
2019-01-09 01:44:03 +03:00
|
|
|
|
|
|
|
this.eventSource = new EventSource(this.channelURL(), {withCredentials:true});
|
|
|
|
this.eventSource.onmessage = e => {
|
2019-01-12 02:18:53 +03:00
|
|
|
this.lastEventId = e.lastEventId;
|
2019-01-10 02:43:43 +03:00
|
|
|
|
2019-09-25 23:41:54 +03:00
|
|
|
let obj = JSON.parse(e.data);
|
2019-01-09 01:44:03 +03:00
|
|
|
if (obj.response == "poke") {
|
2019-09-25 23:41:54 +03:00
|
|
|
let funcs = this.outstandingPokes.get(obj.id);
|
2019-06-14 00:53:04 +03:00
|
|
|
if (obj.hasOwnProperty("ok")) {
|
2019-09-25 23:41:54 +03:00
|
|
|
funcs["success"]();
|
2019-06-14 00:53:04 +03:00
|
|
|
} else if (obj.hasOwnProperty("err")) {
|
2019-09-25 23:41:54 +03:00
|
|
|
funcs["fail"](obj.err);
|
2019-06-14 00:53:04 +03:00
|
|
|
} else {
|
2019-09-25 23:41:54 +03:00
|
|
|
console.error("Invalid poke response: ", obj);
|
2019-06-14 00:53:04 +03:00
|
|
|
}
|
2019-01-09 01:44:03 +03:00
|
|
|
this.outstandingPokes.delete(obj.id);
|
2019-01-10 02:43:43 +03:00
|
|
|
|
|
|
|
} else if (obj.response == "subscribe") {
|
|
|
|
// on a response to a subscribe, we only notify the caller on err
|
|
|
|
//
|
2019-09-25 23:41:54 +03:00
|
|
|
let funcs = this.outstandingSubscriptions.get(obj.id);
|
2019-01-10 02:43:43 +03:00
|
|
|
if (obj.hasOwnProperty("err")) {
|
|
|
|
funcs["err"](obj.err);
|
|
|
|
this.outstandingSubscriptions.delete(obj.id);
|
|
|
|
}
|
|
|
|
} else if (obj.response == "diff") {
|
2019-09-25 23:41:54 +03:00
|
|
|
let funcs = this.outstandingSubscriptions.get(obj.id);
|
2019-01-10 02:43:43 +03:00
|
|
|
funcs["event"](obj.json);
|
|
|
|
} else if (obj.response == "quit") {
|
2019-09-25 23:41:54 +03:00
|
|
|
let funcs = this.outstandingSubscriptions.get(obj.id);
|
|
|
|
funcs["quit"](obj);
|
2019-01-10 02:43:43 +03:00
|
|
|
this.outstandingSubscriptions.delete(obj.id);
|
2019-01-09 01:44:03 +03:00
|
|
|
} else {
|
|
|
|
console.log("Unrecognized response: ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.eventSource.onerror = e => {
|
2019-09-25 23:41:54 +03:00
|
|
|
console.error("eventSource error:", e);
|
|
|
|
this.delete();
|
2019-01-09 01:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
channelURL() {
|
|
|
|
return "/~/channel/" + this.uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextId() {
|
|
|
|
return this.requestId++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
'''
|
2018-10-10 21:51:52 +03:00
|
|
|
:: +format-ud-as-integer: prints a number for consumption outside urbit
|
|
|
|
::
|
|
|
|
++ format-ud-as-integer
|
|
|
|
|= a=@ud
|
2019-01-17 02:24:30 +03:00
|
|
|
^- tape
|
|
|
|
?: =(0 a) ['0' ~]
|
2018-10-10 21:51:52 +03:00
|
|
|
%- flop
|
|
|
|
|- ^- tape
|
|
|
|
?:(=(0 a) ~ [(add '0' (mod a 10)) $(a (div a 10))])
|
2019-06-04 00:34:01 +03:00
|
|
|
:: +host-matches: %.y if the site :binding should be used to handle :host
|
|
|
|
::
|
|
|
|
++ host-matches
|
|
|
|
|= [binding=(unit @t) host=(unit @t)]
|
|
|
|
^- ?
|
|
|
|
:: if the binding allows for matching anything, match
|
|
|
|
::
|
|
|
|
?~ binding
|
|
|
|
%.y
|
|
|
|
:: if the host is ~, that means we're trying to bind nothing to a real
|
|
|
|
:: binding. fail.
|
|
|
|
::
|
|
|
|
?~ host
|
|
|
|
%.n
|
|
|
|
:: otherwise, do a straight comparison
|
|
|
|
::
|
|
|
|
=(u.binding u.host)
|
2018-10-11 21:28:27 +03:00
|
|
|
:: +path-matches: returns %.y if :prefix is a prefix of :full
|
|
|
|
::
|
|
|
|
++ path-matches
|
|
|
|
|= [prefix=path full=path]
|
|
|
|
^- ?
|
|
|
|
?~ prefix
|
|
|
|
%.y
|
|
|
|
?~ full
|
|
|
|
%.n
|
|
|
|
?. =(i.prefix i.full)
|
|
|
|
%.n
|
|
|
|
$(prefix t.prefix, full t.full)
|
2018-10-04 00:05:36 +03:00
|
|
|
:: +simplified-url-parser: returns [(each @if @t) (unit port=@ud)]
|
|
|
|
::
|
|
|
|
++ simplified-url-parser
|
|
|
|
;~ plug
|
|
|
|
;~ pose
|
|
|
|
%+ stag %ip
|
|
|
|
=+ tod=(ape:ag ted:ab)
|
|
|
|
%+ bass 256
|
|
|
|
;~(plug tod (stun [3 3] ;~(pfix dot tod)))
|
|
|
|
::
|
|
|
|
(stag %site (cook crip (star ;~(pose dot alp))))
|
|
|
|
==
|
|
|
|
;~ pose
|
|
|
|
(stag ~ ;~(pfix col dim:ag))
|
|
|
|
(easy ~)
|
|
|
|
==
|
|
|
|
==
|
2018-10-26 02:32:54 +03:00
|
|
|
:: +per-server-event: per-event server core
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
++ per-server-event
|
2018-11-15 21:27:10 +03:00
|
|
|
:: gate that produces the +per-server-event core from event information
|
|
|
|
::
|
2018-09-27 02:18:40 +03:00
|
|
|
|= [[our=@p eny=@ =duct now=@da scry=sley] state=server-state]
|
2018-09-20 02:29:36 +03:00
|
|
|
|%
|
2019-04-30 23:38:40 +03:00
|
|
|
:: +request-local: bypass authentication for local lens connections
|
|
|
|
::
|
|
|
|
++ request-local
|
|
|
|
|= [secure=? =address =request:http]
|
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
|
|
|
=/ act [%app app=%lens]
|
2019-11-13 11:38:35 +03:00
|
|
|
=/ eyre-id (cat 3 'eyre--' (scot %uv eny))
|
2019-11-09 10:31:11 +03:00
|
|
|
::
|
2019-04-30 23:38:40 +03:00
|
|
|
=/ connection=outstanding-connection
|
|
|
|
[act [& secure address request] ~ 0]
|
|
|
|
::
|
|
|
|
=. connections.state
|
|
|
|
(~(put by connections.state) duct connection)
|
|
|
|
::
|
|
|
|
:_ state
|
2019-11-13 11:38:35 +03:00
|
|
|
(subscribe-to-app app.act eyre-id inbound-request.connection)
|
2018-09-21 02:36:04 +03:00
|
|
|
:: +request: starts handling an inbound http request
|
|
|
|
::
|
2018-09-20 02:29:36 +03:00
|
|
|
++ request
|
2019-02-09 00:16:42 +03:00
|
|
|
|= [secure=? =address =request:http]
|
2018-09-20 02:29:36 +03:00
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
=+ host=(get-header:http 'host' header-list.request)
|
|
|
|
=+ action=(get-action-for-binding host url.request)
|
2018-10-10 21:51:52 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
=/ authenticated (request-is-logged-in:authentication request)
|
2018-09-21 02:36:04 +03:00
|
|
|
:: record that we started an asynchronous response
|
|
|
|
::
|
2018-10-25 00:31:19 +03:00
|
|
|
=/ connection=outstanding-connection
|
2019-02-09 00:16:42 +03:00
|
|
|
[action [authenticated secure address request] ~ 0]
|
2018-10-25 00:31:19 +03:00
|
|
|
=. connections.state
|
|
|
|
(~(put by connections.state) duct connection)
|
2018-09-27 21:16:59 +03:00
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
?- -.action
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
%gen
|
2018-09-21 02:36:04 +03:00
|
|
|
::
|
2018-12-15 03:04:13 +03:00
|
|
|
=- [[duct %pass /run-build %f %build live=%.n schematic=-]~ state]
|
2018-10-10 21:51:52 +03:00
|
|
|
::
|
2018-10-11 01:26:51 +03:00
|
|
|
:+ %call
|
|
|
|
:+ %call
|
2019-01-11 03:35:09 +03:00
|
|
|
[%core [[our desk.generator.action] (flop path.generator.action)]]
|
2018-10-11 01:26:51 +03:00
|
|
|
:: TODO: Figure out what goes in generators. We need to slop the
|
|
|
|
:: prelude with the arguments passed in.
|
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
[%$ %noun !>([[now=now eny=eny bek=[our desk.generator.action [%da now]]] ~ ~])]
|
2019-02-09 00:16:42 +03:00
|
|
|
[%$ %noun !>([authenticated request])]
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
%app
|
2019-11-13 11:38:35 +03:00
|
|
|
=/ eyre-id (cat 3 'eyre--' (scot %uv eny))
|
2018-09-21 02:36:04 +03:00
|
|
|
:_ state
|
2019-11-13 11:38:35 +03:00
|
|
|
(subscribe-to-app app.action eyre-id inbound-request.connection)
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
2018-11-15 21:27:10 +03:00
|
|
|
%authentication
|
2019-02-09 00:16:42 +03:00
|
|
|
(handle-request:authentication secure address request)
|
2018-11-13 22:12:59 +03:00
|
|
|
::
|
2018-11-15 21:27:10 +03:00
|
|
|
%channel
|
2019-02-09 00:16:42 +03:00
|
|
|
(handle-request:by-channel secure authenticated address request)
|
2019-01-11 03:35:09 +03:00
|
|
|
::
|
|
|
|
%four-oh-four
|
|
|
|
%^ return-static-data-on-duct 404 'text/html'
|
2019-03-21 23:58:37 +03:00
|
|
|
(error-page 404 authenticated url.request ~)
|
2018-09-27 02:18:40 +03:00
|
|
|
==
|
2019-11-13 11:38:35 +03:00
|
|
|
:: +subscribe-to-app: subscribe to app and poke it with request data
|
2019-11-09 10:31:11 +03:00
|
|
|
::
|
2019-11-13 11:38:35 +03:00
|
|
|
++ subscribe-to-app
|
|
|
|
|= [app=term eyre-id=@ta =inbound-request:eyre]
|
|
|
|
^- (list move)
|
|
|
|
:~ :* duct %pass /watch-response
|
|
|
|
%m %deal [our our] app
|
|
|
|
%watch /http-response/[eyre-id]
|
|
|
|
==
|
|
|
|
::
|
|
|
|
:* duct %pass /run-app-request
|
|
|
|
%m %deal [our our] app
|
|
|
|
%poke %handle-http-request
|
|
|
|
!>([eyre-id inbound-request])
|
|
|
|
==
|
2019-11-09 10:31:11 +03:00
|
|
|
==
|
2018-10-22 21:36:30 +03:00
|
|
|
:: +cancel-request: handles a request being externally aborted
|
|
|
|
::
|
|
|
|
++ cancel-request
|
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
2018-10-24 00:25:20 +03:00
|
|
|
?~ connection=(~(get by connections.state) duct)
|
|
|
|
:: nothing has handled this connection
|
|
|
|
::
|
|
|
|
[~ state]
|
|
|
|
::
|
2018-10-24 01:08:17 +03:00
|
|
|
=. connections.state (~(del by connections.state) duct)
|
|
|
|
::
|
2018-10-24 00:25:20 +03:00
|
|
|
?- -.action.u.connection
|
|
|
|
::
|
|
|
|
%gen
|
2018-10-24 01:08:17 +03:00
|
|
|
:_ state
|
2018-12-15 03:04:13 +03:00
|
|
|
[duct %pass /run-build %f %kill ~]~
|
2018-10-24 00:25:20 +03:00
|
|
|
::
|
|
|
|
%app
|
|
|
|
:_ state
|
|
|
|
:_ ~
|
2019-11-13 11:38:35 +03:00
|
|
|
:* duct %pass /watch-response
|
|
|
|
%m %deal [our our] app.action.u.connection
|
|
|
|
%leave ~
|
2018-10-24 00:25:20 +03:00
|
|
|
==
|
|
|
|
::
|
2018-11-15 21:27:10 +03:00
|
|
|
%authentication
|
2018-10-24 00:25:20 +03:00
|
|
|
[~ state]
|
2018-11-13 22:12:59 +03:00
|
|
|
::
|
2018-11-15 21:27:10 +03:00
|
|
|
%channel
|
2018-11-22 00:37:26 +03:00
|
|
|
on-cancel-request:by-channel
|
2019-01-11 03:35:09 +03:00
|
|
|
::
|
|
|
|
%four-oh-four
|
|
|
|
:: it should be impossible for a 404 page to be asynchronous
|
|
|
|
::
|
|
|
|
!!
|
2018-10-24 00:25:20 +03:00
|
|
|
==
|
2018-09-27 02:18:40 +03:00
|
|
|
:: +return-static-data-on-duct: returns one piece of data all at once
|
|
|
|
::
|
|
|
|
++ return-static-data-on-duct
|
|
|
|
|= [code=@ content-type=@t data=octs]
|
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
%- handle-response
|
|
|
|
:* %start
|
2019-02-09 00:16:42 +03:00
|
|
|
:- status-code=code
|
2018-09-27 02:18:40 +03:00
|
|
|
^= headers
|
2018-10-01 21:37:30 +03:00
|
|
|
:~ ['content-type' content-type]
|
2019-01-17 02:24:30 +03:00
|
|
|
['content-length' (crip (format-ud-as-integer p.data))]
|
2018-09-27 02:18:40 +03:00
|
|
|
==
|
|
|
|
data=[~ data]
|
|
|
|
complete=%.y
|
2018-09-20 02:29:36 +03:00
|
|
|
==
|
2018-09-27 02:18:40 +03:00
|
|
|
:: +authentication: per-event authentication as this Urbit's owner
|
|
|
|
::
|
|
|
|
:: Right now this hard codes the authentication page using the old +code
|
|
|
|
:: system, but in the future should be pluggable so we can use U2F or
|
|
|
|
:: WebAuthn or whatever is more secure than passwords.
|
|
|
|
::
|
|
|
|
++ authentication
|
|
|
|
|%
|
2019-08-15 23:58:23 +03:00
|
|
|
:: +handle-request: handles an http request for the
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
|
|
|
++ handle-request
|
2019-02-09 00:16:42 +03:00
|
|
|
|= [secure=? =address =request:http]
|
2018-09-27 02:18:40 +03:00
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
|
|
|
:: if we received a simple get, just return the page
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?: =('GET' method.request)
|
2018-09-27 02:18:40 +03:00
|
|
|
:: parse the arguments out of request uri
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
=+ request-line=(parse-request-line url.request)
|
2018-09-27 02:18:40 +03:00
|
|
|
%^ return-static-data-on-duct 200 'text/html'
|
2019-07-02 01:16:52 +03:00
|
|
|
(login-page (get-header:http 'redirect' args.request-line) our)
|
2018-09-27 02:18:40 +03:00
|
|
|
:: if we are not a post, return an error
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?. =('POST' method.request)
|
2019-07-02 01:16:52 +03:00
|
|
|
(return-static-data-on-duct 400 'text/html' (login-page ~ our))
|
2018-09-27 02:18:40 +03:00
|
|
|
:: we are a post, and must process the body type as form data
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?~ body.request
|
2019-07-02 01:16:52 +03:00
|
|
|
(return-static-data-on-duct 400 'text/html' (login-page ~ our))
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
|
|
|
=/ parsed=(unit (list [key=@t value=@t]))
|
2019-02-09 00:16:42 +03:00
|
|
|
(rush q.u.body.request yquy:de-purl:html)
|
2018-09-27 02:18:40 +03:00
|
|
|
?~ parsed
|
2019-07-02 01:16:52 +03:00
|
|
|
(return-static-data-on-duct 400 'text/html' (login-page ~ our))
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?~ password=(get-header:http 'password' u.parsed)
|
2019-07-02 01:16:52 +03:00
|
|
|
(return-static-data-on-duct 400 'text/html' (login-page ~ our))
|
2018-09-27 02:18:40 +03:00
|
|
|
:: check that the password is correct
|
|
|
|
::
|
|
|
|
?. =(u.password code)
|
2019-07-02 01:16:52 +03:00
|
|
|
(return-static-data-on-duct 400 'text/html' (login-page ~ our))
|
2018-09-27 02:18:40 +03:00
|
|
|
:: mint a unique session cookie
|
|
|
|
::
|
|
|
|
=/ session=@uv
|
|
|
|
|-
|
|
|
|
=/ candidate=@uv (~(raw og eny) 128)
|
|
|
|
?. (~(has by sessions.authentication-state.state) candidate)
|
|
|
|
candidate
|
|
|
|
$(eny (shas %try-again candidate))
|
|
|
|
:: record cookie and record expiry time
|
|
|
|
::
|
2019-08-27 13:10:36 +03:00
|
|
|
=/ expires-in=@dr ~d7
|
2018-09-27 02:18:40 +03:00
|
|
|
=. sessions.authentication-state.state
|
2019-08-27 13:10:36 +03:00
|
|
|
(~(put by sessions.authentication-state.state) session (add now expires-in))
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
2019-08-27 13:10:36 +03:00
|
|
|
=/ max-age=tape (format-ud-as-integer `@ud`(div (msec:milly expires-in) 1.000))
|
2018-09-27 02:18:40 +03:00
|
|
|
=/ cookie-line
|
|
|
|
%- crip
|
2019-10-05 01:06:01 +03:00
|
|
|
"urbauth-{<our>}={<session>}; Path=/; Max-Age={max-age}"
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
2019-03-26 02:52:11 +03:00
|
|
|
?~ redirect=(get-header:http 'redirect' u.parsed)
|
|
|
|
%- handle-response
|
|
|
|
:* %start
|
|
|
|
:- status-code=200
|
|
|
|
^= headers
|
|
|
|
:~ ['set-cookie' cookie-line]
|
|
|
|
==
|
|
|
|
data=~
|
|
|
|
complete=%.y
|
|
|
|
==
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
%- handle-response
|
|
|
|
:* %start
|
2019-02-09 00:16:42 +03:00
|
|
|
:- status-code=307
|
2018-09-27 02:18:40 +03:00
|
|
|
^= headers
|
2019-03-26 02:52:11 +03:00
|
|
|
:~ ['location' u.redirect]
|
2018-10-01 21:37:30 +03:00
|
|
|
['set-cookie' cookie-line]
|
2018-09-27 02:18:40 +03:00
|
|
|
==
|
|
|
|
data=~
|
|
|
|
complete=%.y
|
|
|
|
==
|
|
|
|
:: +request-is-logged-in: checks to see if the request is authenticated
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
:: We are considered logged in if this request has an urbauth
|
2018-09-27 21:16:59 +03:00
|
|
|
:: Cookie which is not expired.
|
|
|
|
::
|
2018-09-27 02:18:40 +03:00
|
|
|
++ request-is-logged-in
|
2019-02-09 00:16:42 +03:00
|
|
|
|= =request:http
|
2018-09-27 02:18:40 +03:00
|
|
|
^- ?
|
2018-09-27 21:16:59 +03:00
|
|
|
:: are there cookies passed with this request?
|
|
|
|
::
|
|
|
|
:: TODO: In HTTP2, the client is allowed to put multiple 'Cookie'
|
|
|
|
:: headers.
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?~ cookie-header=(get-header:http 'cookie' header-list.request)
|
2018-09-27 21:16:59 +03:00
|
|
|
%.n
|
|
|
|
:: is the cookie line is valid?
|
|
|
|
::
|
|
|
|
?~ cookies=(rush u.cookie-header cock:de-purl:html)
|
|
|
|
%.n
|
|
|
|
:: is there an urbauth cookie?
|
|
|
|
::
|
2019-10-05 01:06:01 +03:00
|
|
|
?~ urbauth=(get-header:http (crip "urbauth-{<our>}") u.cookies)
|
2018-09-27 21:16:59 +03:00
|
|
|
%.n
|
|
|
|
:: is this formatted like a valid session cookie?
|
|
|
|
::
|
|
|
|
?~ session-id=(rush u.urbauth ;~(pfix (jest '0v') viz:ag))
|
|
|
|
%.n
|
|
|
|
:: is this a session that we know about?
|
|
|
|
::
|
2019-07-18 00:57:01 +03:00
|
|
|
?~ session=(~(get by sessions.authentication-state.state) `@uv`u.session-id)
|
2018-09-27 21:16:59 +03:00
|
|
|
%.n
|
|
|
|
:: is this session still valid?
|
2018-09-27 02:18:40 +03:00
|
|
|
::
|
2018-09-27 21:16:59 +03:00
|
|
|
(lte now expiry-time.u.session)
|
2018-09-27 02:18:40 +03:00
|
|
|
:: +code: returns the same as |code
|
|
|
|
::
|
|
|
|
++ code
|
|
|
|
^- @ta
|
2019-03-18 23:48:00 +03:00
|
|
|
::
|
|
|
|
=+ pax=/(scot %p our)/code/(scot %da now)/(scot %p our)
|
|
|
|
=+ res=((sloy scry) [151 %noun] %j pax)
|
|
|
|
::
|
|
|
|
(rsh 3 1 (scot %p (@ (need (need res)))))
|
2018-09-27 02:18:40 +03:00
|
|
|
--
|
2018-11-15 21:27:10 +03:00
|
|
|
:: +channel: per-event handling of requests to the channel system
|
|
|
|
::
|
|
|
|
:: Eyre offers a remote interface to your Urbit through channels, which
|
2019-03-18 23:48:00 +03:00
|
|
|
:: are persistent connections on the server which can be disconnected and
|
|
|
|
:: reconnected on the client.
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
++ by-channel
|
2018-11-15 21:27:10 +03:00
|
|
|
:: moves: the moves to be sent out at the end of this event, reversed
|
|
|
|
::
|
|
|
|
=| moves=(list move)
|
|
|
|
|%
|
|
|
|
:: +handle-request: handles an http request for the subscription system
|
|
|
|
::
|
|
|
|
++ handle-request
|
2019-02-09 00:16:42 +03:00
|
|
|
|= [secure=? authenticated=? =address =request:http]
|
2018-11-15 21:27:10 +03:00
|
|
|
^- [(list move) server-state]
|
|
|
|
:: if we're not authenticated error, but don't redirect.
|
|
|
|
::
|
|
|
|
:: We don't redirect because subscription stuff is never the toplevel
|
|
|
|
:: page; issuing a redirect won't help.
|
|
|
|
::
|
|
|
|
?. authenticated
|
2019-03-21 23:58:37 +03:00
|
|
|
%^ return-static-data-on-duct 403 'text/html'
|
|
|
|
(error-page 403 authenticated url.request "unauthenticated channel usage")
|
2018-11-15 21:27:10 +03:00
|
|
|
:: parse out the path key the subscription is on
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
=+ request-line=(parse-request-line url.request)
|
2018-11-15 21:27:10 +03:00
|
|
|
?. ?=([@t @t @t ~] site.request-line)
|
2019-01-09 01:44:03 +03:00
|
|
|
:: url is not of the form '/~/channel/'
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
%^ return-static-data-on-duct 400 'text/html'
|
2019-03-21 23:58:37 +03:00
|
|
|
(error-page 400 authenticated url.request "malformed channel url")
|
2018-11-21 04:06:04 +03:00
|
|
|
:: channel-id: unique channel id parsed out of url
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
=+ channel-id=i.t.t.site.request-line
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2019-01-09 01:44:03 +03:00
|
|
|
?: ?& =('channel' channel-id)
|
|
|
|
=([~ ~.js] ext.request-line)
|
|
|
|
==
|
|
|
|
:: client is requesting the javascript shim
|
|
|
|
::
|
2019-07-12 21:04:47 +03:00
|
|
|
(return-static-data-on-duct 200 'text/javascript' channel-js)
|
2019-01-09 01:44:03 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?: =('PUT' method.request)
|
2018-11-15 21:27:10 +03:00
|
|
|
:: PUT methods starts/modifies a channel, and returns a result immediately
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
(on-put-request channel-id request)
|
2018-11-21 04:06:04 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?: =('GET' method.request)
|
|
|
|
(on-get-request channel-id request)
|
2019-07-02 22:22:13 +03:00
|
|
|
?: =('POST' method.request)
|
|
|
|
:: POST methods are used solely for deleting channels
|
|
|
|
(on-put-request channel-id request)
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
~& %session-not-a-put
|
|
|
|
[~ state]
|
2018-11-22 00:37:26 +03:00
|
|
|
:: +on-cancel-request: cancels an ongoing subscription
|
|
|
|
::
|
|
|
|
:: One of our long lived sessions just got closed. We put the associated
|
|
|
|
:: session back into the waiting state.
|
|
|
|
::
|
|
|
|
++ on-cancel-request
|
|
|
|
^- [(list move) server-state]
|
|
|
|
:: lookup the session id by duct
|
|
|
|
::
|
|
|
|
?~ maybe-channel-id=(~(get by duct-to-key.channel-state.state) duct)
|
|
|
|
~& [%canceling-nonexistant-channel duct]
|
|
|
|
[~ state]
|
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
~& [%canceling-cancel duct]
|
|
|
|
::
|
2019-10-10 14:33:11 +03:00
|
|
|
=/ maybe-session
|
|
|
|
(~(get by session.channel-state.state) u.maybe-channel-id)
|
|
|
|
?~ maybe-session [~ state]
|
|
|
|
::
|
|
|
|
=/ heartbeat-cancel=(list move)
|
|
|
|
?~ heartbeat.u.maybe-session ~
|
|
|
|
:~ %^ cancel-heartbeat-move
|
|
|
|
u.maybe-channel-id
|
|
|
|
date.u.heartbeat.u.maybe-session
|
|
|
|
duct.u.heartbeat.u.maybe-session
|
|
|
|
==
|
|
|
|
::
|
2018-11-22 00:37:26 +03:00
|
|
|
=/ expiration-time=@da (add now channel-timeout)
|
|
|
|
::
|
2019-10-10 14:33:11 +03:00
|
|
|
:- %+ weld heartbeat-cancel
|
|
|
|
[(set-timeout-move u.maybe-channel-id expiration-time) moves]
|
2018-11-22 00:37:26 +03:00
|
|
|
%_ state
|
|
|
|
session.channel-state
|
|
|
|
%+ ~(jab by session.channel-state.state) u.maybe-channel-id
|
|
|
|
|= =channel
|
|
|
|
:: if we are canceling a known channel, it should have a listener
|
|
|
|
::
|
|
|
|
?> ?=([%| *] state.channel)
|
2019-10-10 14:33:11 +03:00
|
|
|
channel(state [%& [expiration-time duct]], heartbeat ~)
|
2018-11-22 00:37:26 +03:00
|
|
|
::
|
|
|
|
duct-to-key.channel-state
|
|
|
|
(~(del by duct-to-key.channel-state.state) duct)
|
|
|
|
==
|
|
|
|
:: +set-timeout-timer-for: sets a timeout timer on a channel
|
|
|
|
::
|
|
|
|
:: This creates a channel if it doesn't exist, cancels existing timers
|
|
|
|
:: if they're already set (we cannot have duplicate timers), and (if
|
|
|
|
:: necessary) moves channels from the listening state to the expiration
|
|
|
|
:: state.
|
|
|
|
::
|
|
|
|
++ update-timeout-timer-for
|
|
|
|
|= channel-id=@t
|
|
|
|
^+ ..update-timeout-timer-for
|
|
|
|
:: when our callback should fire
|
|
|
|
::
|
|
|
|
=/ expiration-time=@da (add now channel-timeout)
|
|
|
|
:: if the channel doesn't exist, create it and set a timer
|
|
|
|
::
|
|
|
|
?~ maybe-channel=(~(get by session.channel-state.state) channel-id)
|
|
|
|
::
|
|
|
|
%_ ..update-timeout-timer-for
|
|
|
|
session.channel-state.state
|
|
|
|
%+ ~(put by session.channel-state.state) channel-id
|
2019-10-07 02:26:03 +03:00
|
|
|
[[%& expiration-time duct] 0 ~ ~ ~]
|
2018-11-22 00:37:26 +03:00
|
|
|
::
|
|
|
|
moves
|
|
|
|
[(set-timeout-move channel-id expiration-time) moves]
|
|
|
|
==
|
|
|
|
:: if the channel has an active listener, we aren't setting any timers
|
|
|
|
::
|
|
|
|
?: ?=([%| *] state.u.maybe-channel)
|
|
|
|
..update-timeout-timer-for
|
|
|
|
:: we have a previous timer; cancel the old one and set the new one
|
|
|
|
::
|
|
|
|
%_ ..update-timeout-timer-for
|
|
|
|
session.channel-state.state
|
|
|
|
%+ ~(jab by session.channel-state.state) channel-id
|
|
|
|
|= =channel
|
|
|
|
channel(state [%& [expiration-time duct]])
|
|
|
|
::
|
|
|
|
moves
|
|
|
|
:* (cancel-timeout-move channel-id p.state.u.maybe-channel)
|
|
|
|
(set-timeout-move channel-id expiration-time)
|
|
|
|
moves
|
|
|
|
==
|
|
|
|
==
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2019-10-07 02:26:03 +03:00
|
|
|
++ set-heartbeat-move
|
|
|
|
|= [channel-id=@t heartbeat-time=@da]
|
|
|
|
^- move
|
|
|
|
:^ duct %pass /channel/heartbeat/[channel-id]
|
|
|
|
[%b %wait heartbeat-time]
|
|
|
|
::
|
|
|
|
++ cancel-heartbeat-move
|
|
|
|
|= [channel-id=@t heartbeat-time=@da =^duct]
|
|
|
|
^- move
|
|
|
|
:^ duct %pass /channel/heartbeat/[channel-id]
|
|
|
|
[%b %rest heartbeat-time]
|
|
|
|
::
|
2018-11-22 00:37:26 +03:00
|
|
|
++ set-timeout-move
|
|
|
|
|= [channel-id=@t expiration-time=@da]
|
|
|
|
^- move
|
|
|
|
[duct %pass /channel/timeout/[channel-id] %b %wait expiration-time]
|
|
|
|
::
|
|
|
|
++ cancel-timeout-move
|
|
|
|
|= [channel-id=@t expiration-time=@da =^duct]
|
|
|
|
^- move
|
|
|
|
:^ duct %pass /channel/timeout/[channel-id]
|
|
|
|
[%b %rest expiration-time]
|
2018-11-21 04:06:04 +03:00
|
|
|
:: +on-get-request: handles a GET request
|
|
|
|
::
|
|
|
|
:: GET requests open a channel for the server to send events to the
|
|
|
|
:: client in text/event-stream format.
|
|
|
|
::
|
|
|
|
++ on-get-request
|
2019-02-09 00:16:42 +03:00
|
|
|
|= [channel-id=@t =request:http]
|
2018-11-21 04:06:04 +03:00
|
|
|
^- [(list move) server-state]
|
|
|
|
:: if there's no channel-id, we must 404
|
|
|
|
::
|
|
|
|
?~ maybe-channel=(~(get by session.channel-state.state) channel-id)
|
|
|
|
%^ return-static-data-on-duct 404 'text/html'
|
2019-03-21 23:58:37 +03:00
|
|
|
(error-page 404 %.y url.request ~)
|
2018-11-21 04:06:04 +03:00
|
|
|
:: if there's already a duct listening to this channel, we must 400
|
|
|
|
::
|
|
|
|
?: ?=([%| *] state.u.maybe-channel)
|
|
|
|
%^ return-static-data-on-duct 400 'text/html'
|
2019-03-21 23:58:37 +03:00
|
|
|
(error-page 400 %.y url.request "channel already bound")
|
2018-11-21 04:06:04 +03:00
|
|
|
:: when opening an event-stream, we must cancel our timeout timer
|
|
|
|
::
|
|
|
|
=. moves
|
2018-11-22 00:37:26 +03:00
|
|
|
[(cancel-timeout-move channel-id p.state.u.maybe-channel) moves]
|
2019-02-09 00:16:42 +03:00
|
|
|
:: the request may include a 'Last-Event-Id' header
|
2018-11-21 04:06:04 +03:00
|
|
|
::
|
|
|
|
=/ maybe-last-event-id=(unit @ud)
|
2019-02-09 00:16:42 +03:00
|
|
|
?~ maybe-raw-header=(get-header:http 'Last-Event-ID' header-list.request)
|
2018-11-21 04:06:04 +03:00
|
|
|
~
|
|
|
|
(rush u.maybe-raw-header dum:ag)
|
|
|
|
:: flush events older than the passed in 'Last-Event-ID'
|
|
|
|
::
|
|
|
|
=? state ?=(^ maybe-last-event-id)
|
|
|
|
(acknowledge-events channel-id u.maybe-last-event-id)
|
|
|
|
:: combine the remaining queued events to send to the client
|
|
|
|
::
|
|
|
|
=/ event-replay=wall
|
|
|
|
%- zing
|
|
|
|
%- flop
|
|
|
|
=/ queue events.u.maybe-channel
|
|
|
|
=| events=(list wall)
|
|
|
|
|-
|
|
|
|
^+ events
|
|
|
|
?: =(~ queue)
|
|
|
|
events
|
|
|
|
=^ head queue ~(get to queue)
|
|
|
|
$(events [lines.p.head events])
|
|
|
|
:: send the start event to the client
|
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
=^ http-moves state
|
|
|
|
%- handle-response
|
2019-02-09 00:16:42 +03:00
|
|
|
:* %start
|
|
|
|
:- 200
|
2018-11-21 04:06:04 +03:00
|
|
|
:~ ['content-type' 'text/event-stream']
|
|
|
|
['cache-control' 'no-cache']
|
|
|
|
['connection' 'keep-alive']
|
|
|
|
==
|
|
|
|
(wall-to-octs event-replay)
|
|
|
|
complete=%.n
|
|
|
|
==
|
2018-11-22 00:37:26 +03:00
|
|
|
:: associate this duct with this session key
|
|
|
|
::
|
|
|
|
=. duct-to-key.channel-state.state
|
|
|
|
(~(put by duct-to-key.channel-state.state) duct channel-id)
|
2019-10-07 02:26:03 +03:00
|
|
|
:: initialize sse heartbeat
|
|
|
|
::
|
|
|
|
=/ heartbeat-time=@da (add now ~s20)
|
|
|
|
=/ heartbeat (set-heartbeat-move channel-id heartbeat-time)
|
|
|
|
:: clear the event queue, record the duct for future output and
|
|
|
|
:: record heartbeat-time for possible future cancel
|
2018-11-21 04:06:04 +03:00
|
|
|
::
|
|
|
|
=. session.channel-state.state
|
|
|
|
%+ ~(jab by session.channel-state.state) channel-id
|
|
|
|
|= =channel
|
2019-10-07 02:26:03 +03:00
|
|
|
channel(events ~, state [%| duct], heartbeat (some [heartbeat-time duct]))
|
2018-11-21 04:06:04 +03:00
|
|
|
::
|
2019-10-07 02:26:03 +03:00
|
|
|
[[heartbeat (weld http-moves moves)] state]
|
2018-11-21 04:06:04 +03:00
|
|
|
:: +acknowledge-events: removes events before :last-event-id on :channel-id
|
|
|
|
::
|
|
|
|
++ acknowledge-events
|
|
|
|
|= [channel-id=@t last-event-id=@u]
|
|
|
|
^- server-state
|
|
|
|
%_ state
|
|
|
|
session.channel-state
|
|
|
|
%+ ~(jab by session.channel-state.state) channel-id
|
|
|
|
|= =channel
|
|
|
|
^+ channel
|
2019-01-12 01:15:17 +03:00
|
|
|
channel(events (prune-events events.channel last-event-id))
|
2018-11-21 04:06:04 +03:00
|
|
|
==
|
2018-11-15 21:27:10 +03:00
|
|
|
:: +on-put-request: handles a PUT request
|
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
:: PUT requests send commands from the client to the server. We receive
|
|
|
|
:: a set of commands in JSON format in the body of the message.
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
++ on-put-request
|
2019-02-09 00:16:42 +03:00
|
|
|
|= [channel-id=@t =request:http]
|
2018-11-15 21:27:10 +03:00
|
|
|
^- [(list move) server-state]
|
|
|
|
:: error when there's no body
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?~ body.request
|
2018-11-15 21:27:10 +03:00
|
|
|
%^ return-static-data-on-duct 400 'text/html'
|
2019-03-21 23:58:37 +03:00
|
|
|
(error-page 400 %.y url.request "no put body")
|
2018-11-16 02:27:49 +03:00
|
|
|
:: if the incoming body isn't json, this is a bad request, 400.
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?~ maybe-json=(de-json:html q.u.body.request)
|
2018-11-15 21:27:10 +03:00
|
|
|
%^ return-static-data-on-duct 400 'text/html'
|
2019-03-21 23:58:37 +03:00
|
|
|
(error-page 400 %.y url.request "put body not json")
|
2018-11-16 02:27:49 +03:00
|
|
|
:: parse the json into an array of +channel-request items
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2018-11-16 02:27:49 +03:00
|
|
|
?~ maybe-requests=(parse-channel-request u.maybe-json)
|
|
|
|
%^ return-static-data-on-duct 400 'text/html'
|
2019-03-21 23:58:37 +03:00
|
|
|
(error-page 400 %.y url.request "invalid channel json")
|
2018-11-16 02:27:49 +03:00
|
|
|
:: while weird, the request list could be empty
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2018-11-16 02:27:49 +03:00
|
|
|
?: =(~ u.maybe-requests)
|
2018-11-15 21:27:10 +03:00
|
|
|
%^ return-static-data-on-duct 400 'text/html'
|
2019-03-21 23:58:37 +03:00
|
|
|
(error-page 400 %.y url.request "empty list of actions")
|
2018-11-21 04:06:04 +03:00
|
|
|
:: check for the existence of the channel-id
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
:: if we have no session, create a new one set to expire in
|
2018-11-22 00:37:26 +03:00
|
|
|
:: :channel-timeout from now. if we have one which has a timer, update
|
|
|
|
:: that timer.
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2018-11-22 00:37:26 +03:00
|
|
|
=. ..on-put-request (update-timeout-timer-for channel-id)
|
2018-11-15 21:27:10 +03:00
|
|
|
:: for each request, execute the action passed in
|
|
|
|
::
|
2018-11-16 02:27:49 +03:00
|
|
|
=+ requests=u.maybe-requests
|
2018-11-20 01:59:58 +03:00
|
|
|
:: gall-moves: put moves here first so we can flop for ordering
|
|
|
|
::
|
|
|
|
:: TODO: Have an error state where any invalid duplicate subscriptions
|
|
|
|
:: or other errors cause the entire thing to fail with a 400 and a tang.
|
|
|
|
::
|
|
|
|
=| gall-moves=(list move)
|
2018-11-15 21:27:10 +03:00
|
|
|
|-
|
2018-11-17 01:30:23 +03:00
|
|
|
::
|
2018-11-15 21:27:10 +03:00
|
|
|
?~ requests
|
2018-11-17 01:30:23 +03:00
|
|
|
:: this is a PUT request; we must mark it as complete
|
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
=^ http-moves state
|
|
|
|
%- handle-response
|
|
|
|
:* %start
|
2019-02-09 00:16:42 +03:00
|
|
|
[status-code=200 headers=~]
|
2018-11-17 01:30:23 +03:00
|
|
|
data=~
|
|
|
|
complete=%.y
|
|
|
|
==
|
|
|
|
::
|
2019-01-11 03:35:09 +03:00
|
|
|
[:(weld (flop gall-moves) http-moves moves) state]
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
?- -.i.requests
|
|
|
|
%ack
|
2019-01-12 01:15:17 +03:00
|
|
|
:: client acknowledges that they have received up to event-id
|
|
|
|
::
|
|
|
|
%_ $
|
|
|
|
state (acknowledge-events channel-id event-id.i.requests)
|
|
|
|
requests t.requests
|
|
|
|
==
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
%poke
|
|
|
|
::
|
2018-11-20 01:59:58 +03:00
|
|
|
=. gall-moves
|
|
|
|
:_ gall-moves
|
2018-11-17 01:30:23 +03:00
|
|
|
^- move
|
2018-11-21 04:06:04 +03:00
|
|
|
:^ duct %pass /channel/poke/[channel-id]/(scot %ud request-id.i.requests)
|
2018-11-17 01:30:23 +03:00
|
|
|
=, i.requests
|
2019-11-05 10:42:59 +03:00
|
|
|
:* %m %deal `sock`[our ship] app
|
2019-11-09 04:26:47 +03:00
|
|
|
`task:agent:mall`[%poke-as mark %json !>(json)]
|
2019-11-05 10:42:59 +03:00
|
|
|
==
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
$(requests t.requests)
|
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
%watch
|
2018-11-20 01:59:58 +03:00
|
|
|
::
|
2019-10-26 02:04:29 +03:00
|
|
|
=/ channel-wire=wire
|
2019-03-27 01:52:32 +03:00
|
|
|
/channel/subscription/[channel-id]/(scot %ud request-id.i.requests)
|
|
|
|
::
|
2018-11-20 01:59:58 +03:00
|
|
|
=. gall-moves
|
|
|
|
:_ gall-moves
|
|
|
|
^- move
|
2019-03-27 01:52:32 +03:00
|
|
|
:^ duct %pass channel-wire
|
2018-11-20 01:59:58 +03:00
|
|
|
=, i.requests
|
2019-11-05 10:42:59 +03:00
|
|
|
:* %m %deal [our ship] app
|
2019-11-09 04:26:47 +03:00
|
|
|
`task:agent:mall`[%watch-as %json path]
|
2019-11-05 10:42:59 +03:00
|
|
|
==
|
2018-11-20 01:59:58 +03:00
|
|
|
::
|
|
|
|
=. session.channel-state.state
|
2018-11-21 04:06:04 +03:00
|
|
|
%+ ~(jab by session.channel-state.state) channel-id
|
|
|
|
|= =channel
|
2018-11-20 01:59:58 +03:00
|
|
|
=, i.requests
|
2019-06-14 21:37:09 +03:00
|
|
|
channel(subscriptions (~(put by subscriptions.channel) channel-wire [ship app path duct]))
|
2018-11-20 01:59:58 +03:00
|
|
|
::
|
|
|
|
$(requests t.requests)
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
%leave
|
2019-10-26 02:04:29 +03:00
|
|
|
=/ channel-wire=wire
|
2019-03-27 01:52:32 +03:00
|
|
|
/channel/subscription/[channel-id]/(scot %ud subscription-id.i.requests)
|
|
|
|
::
|
2019-07-17 20:51:56 +03:00
|
|
|
=/ usession (~(get by session.channel-state.state) channel-id)
|
|
|
|
?~ usession
|
|
|
|
$(requests t.requests)
|
|
|
|
=/ subscriptions subscriptions:u.usession
|
2019-03-27 01:52:32 +03:00
|
|
|
::
|
|
|
|
?~ maybe-subscription=(~(get by subscriptions) channel-wire)
|
|
|
|
:: the client sent us a weird request referring to a subscription
|
|
|
|
:: which isn't active.
|
|
|
|
::
|
|
|
|
~& [%missing-subscription-in-unsubscribe channel-wire]
|
|
|
|
$(requests t.requests)
|
2019-03-15 21:25:12 +03:00
|
|
|
::
|
|
|
|
=. gall-moves
|
|
|
|
:_ gall-moves
|
|
|
|
^- move
|
2019-06-14 21:37:09 +03:00
|
|
|
:^ duc.u.maybe-subscription %pass channel-wire
|
2019-03-27 01:52:32 +03:00
|
|
|
=, u.maybe-subscription
|
2019-11-05 10:42:59 +03:00
|
|
|
:* %m %deal [our ship] app
|
2019-11-07 09:19:32 +03:00
|
|
|
`task:agent:mall`[%leave ~]
|
2019-11-05 10:42:59 +03:00
|
|
|
==
|
2019-03-15 21:25:12 +03:00
|
|
|
::
|
|
|
|
=. session.channel-state.state
|
|
|
|
%+ ~(jab by session.channel-state.state) channel-id
|
|
|
|
|= =channel
|
2019-03-27 01:52:32 +03:00
|
|
|
channel(subscriptions (~(del by subscriptions.channel) channel-wire))
|
2019-03-15 21:25:12 +03:00
|
|
|
::
|
|
|
|
$(requests t.requests)
|
2019-06-14 00:53:04 +03:00
|
|
|
::
|
|
|
|
%delete
|
2019-06-14 21:37:09 +03:00
|
|
|
=/ unitsession
|
|
|
|
(~(get by session.channel-state.state) channel-id)
|
|
|
|
::
|
|
|
|
?~ unitsession
|
|
|
|
$(requests t.requests)
|
2019-06-14 00:53:04 +03:00
|
|
|
::
|
2019-06-14 21:37:09 +03:00
|
|
|
=/ session u.unitsession
|
2019-06-14 00:53:04 +03:00
|
|
|
=. session.channel-state.state
|
|
|
|
(~(del by session.channel-state.state) channel-id)
|
2019-06-14 21:37:09 +03:00
|
|
|
::
|
2019-06-14 00:53:04 +03:00
|
|
|
=. gall-moves
|
2019-06-14 21:37:09 +03:00
|
|
|
%+ weld gall-moves
|
|
|
|
::
|
|
|
|
:: produce a list of moves which cancels every gall subscription
|
|
|
|
::
|
|
|
|
%+ turn ~(tap by subscriptions.session)
|
2019-10-26 02:04:29 +03:00
|
|
|
|= [channel-wire=wire ship=@p app=term =path duc=^duct]
|
2019-06-14 21:37:09 +03:00
|
|
|
^- move
|
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
[duc %pass channel-wire [%m %deal [our ship] app %leave ~]]
|
2019-06-14 21:37:09 +03:00
|
|
|
::
|
|
|
|
?: ?=([%& *] state.session)
|
|
|
|
=. gall-moves
|
|
|
|
:_ gall-moves
|
|
|
|
::
|
|
|
|
^- move
|
|
|
|
?> ?=([%& *] state.session)
|
|
|
|
:^ duct.p.state.session %pass /channel/timeout/[channel-id]
|
|
|
|
[%b %rest date.p.state.session]
|
2019-06-14 00:53:04 +03:00
|
|
|
::
|
2019-06-14 21:37:09 +03:00
|
|
|
$(requests t.requests)
|
2019-06-14 00:53:04 +03:00
|
|
|
::
|
2019-06-14 21:37:09 +03:00
|
|
|
?> ?=([%| *] state.session)
|
|
|
|
=. duct-to-key.channel-state.state
|
|
|
|
(~(del by duct-to-key.channel-state.state) p.state.session)
|
2019-06-14 00:53:04 +03:00
|
|
|
::
|
2019-10-07 02:26:03 +03:00
|
|
|
?~ heartbeat.session $(requests t.requests)
|
|
|
|
=. gall-moves
|
|
|
|
%+ snoc gall-moves
|
|
|
|
%^ cancel-heartbeat-move
|
|
|
|
channel-id
|
|
|
|
date.u.heartbeat.session
|
|
|
|
duct.u.heartbeat.session
|
|
|
|
::
|
2019-06-14 00:53:04 +03:00
|
|
|
$(requests t.requests)
|
|
|
|
::
|
2018-11-15 21:27:10 +03:00
|
|
|
==
|
2018-11-21 04:06:04 +03:00
|
|
|
:: +on-gall-response: turns a gall response into an event
|
|
|
|
::
|
|
|
|
++ on-gall-response
|
2019-11-09 10:31:11 +03:00
|
|
|
|= [channel-id=@t request-id=@ud =sign:agent:mall]
|
2018-11-21 04:06:04 +03:00
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
2019-11-09 10:31:11 +03:00
|
|
|
?- -.sign
|
2019-11-05 10:42:59 +03:00
|
|
|
%poke-ack
|
2018-11-21 04:06:04 +03:00
|
|
|
=/ =json
|
|
|
|
=, enjs:format
|
|
|
|
%- pairs :~
|
|
|
|
['response' [%s 'poke']]
|
|
|
|
['id' (numb request-id)]
|
2019-11-09 10:31:11 +03:00
|
|
|
?~ p.sign
|
2018-11-21 04:06:04 +03:00
|
|
|
['ok' [%s 'ok']]
|
2019-11-09 10:31:11 +03:00
|
|
|
['err' (wall (render-tang-to-wall 100 u.p.sign))]
|
2018-11-21 04:06:04 +03:00
|
|
|
==
|
|
|
|
::
|
|
|
|
(emit-event channel-id [(en-json:html json)]~)
|
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
%fact
|
2018-11-21 04:06:04 +03:00
|
|
|
=/ =json
|
|
|
|
=, enjs:format
|
|
|
|
%- pairs :~
|
|
|
|
['response' [%s 'diff']]
|
|
|
|
['id' (numb request-id)]
|
|
|
|
:- 'json'
|
2019-11-09 10:31:11 +03:00
|
|
|
?> =(%json p.cage.sign)
|
|
|
|
;;(json q.q.cage.sign)
|
2018-11-21 04:06:04 +03:00
|
|
|
==
|
|
|
|
::
|
|
|
|
(emit-event channel-id [(en-json:html json)]~)
|
2019-01-10 02:43:43 +03:00
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
%kick
|
2019-01-11 03:35:09 +03:00
|
|
|
~& [%recieved-quit-from-gall channel-id]
|
2019-01-10 02:43:43 +03:00
|
|
|
=/ =json
|
|
|
|
=, enjs:format
|
|
|
|
%- pairs :~
|
|
|
|
['response' [%s 'quit']]
|
|
|
|
['id' (numb request-id)]
|
|
|
|
==
|
|
|
|
::
|
|
|
|
(emit-event channel-id [(en-json:html json)]~)
|
2018-11-21 04:06:04 +03:00
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
%watch-ack
|
2018-11-21 04:06:04 +03:00
|
|
|
=/ =json
|
|
|
|
=, enjs:format
|
|
|
|
%- pairs :~
|
|
|
|
['response' [%s 'subscribe']]
|
|
|
|
['id' (numb request-id)]
|
2019-11-09 10:31:11 +03:00
|
|
|
?~ p.sign
|
2018-11-21 04:06:04 +03:00
|
|
|
['ok' [%s 'ok']]
|
2019-11-09 10:31:11 +03:00
|
|
|
['err' (wall (render-tang-to-wall 100 u.p.sign))]
|
2018-11-21 04:06:04 +03:00
|
|
|
==
|
|
|
|
::
|
|
|
|
(emit-event channel-id [(en-json:html json)]~)
|
|
|
|
==
|
|
|
|
:: +emit-event: records an event occurred, possibly sending to client
|
|
|
|
::
|
|
|
|
:: When an event occurs, we need to record it, even if we immediately
|
|
|
|
:: send it to a connected browser so in case of disconnection, we can
|
|
|
|
:: resend it.
|
|
|
|
::
|
|
|
|
:: This function is responsible for taking the raw json lines and
|
|
|
|
:: converting them into a text/event-stream. The :event-stream-lines
|
|
|
|
:: then may get sent, and are stored for later resending until
|
|
|
|
:: acknowledged by the client.
|
|
|
|
::
|
|
|
|
++ emit-event
|
|
|
|
|= [channel-id=@t json-text=wall]
|
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
2019-03-29 20:44:37 +03:00
|
|
|
=/ channel=(unit channel)
|
|
|
|
(~(get by session.channel-state.state) channel-id)
|
|
|
|
?~ channel
|
2019-03-30 02:27:36 +03:00
|
|
|
~& [%received-event-for-nonexistent-channel channel-id]
|
2019-03-29 20:44:37 +03:00
|
|
|
[~ state]
|
2018-11-21 04:06:04 +03:00
|
|
|
::
|
2019-03-29 20:44:37 +03:00
|
|
|
=/ event-id next-id.u.channel
|
2018-11-21 04:06:04 +03:00
|
|
|
::
|
|
|
|
=/ event-stream-lines=wall
|
|
|
|
%- weld :_ [""]~
|
2019-01-17 02:24:30 +03:00
|
|
|
:- (weld "id: " (format-ud-as-integer event-id))
|
2018-11-21 04:06:04 +03:00
|
|
|
%+ turn json-text
|
|
|
|
|= =tape
|
|
|
|
(weld "data: " tape)
|
|
|
|
:: if a client is connected, send this event to them.
|
|
|
|
::
|
2019-03-29 20:44:37 +03:00
|
|
|
=? moves ?=([%| *] state.u.channel)
|
2019-02-09 00:16:42 +03:00
|
|
|
^- (list move)
|
2018-11-21 04:06:04 +03:00
|
|
|
:_ moves
|
2019-03-29 20:44:37 +03:00
|
|
|
:+ p.state.u.channel %give
|
2019-07-04 09:09:44 +03:00
|
|
|
^- gift:able
|
2019-02-06 01:05:06 +03:00
|
|
|
:* %response %continue
|
2018-11-21 04:06:04 +03:00
|
|
|
::
|
|
|
|
^= data
|
|
|
|
:- ~
|
|
|
|
%- as-octs:mimes:html
|
|
|
|
(crip (of-wall:format event-stream-lines))
|
|
|
|
::
|
|
|
|
complete=%.n
|
|
|
|
==
|
|
|
|
::
|
|
|
|
:- moves
|
|
|
|
%_ state
|
2018-11-22 00:37:26 +03:00
|
|
|
session.channel-state
|
2018-11-21 04:06:04 +03:00
|
|
|
%+ ~(jab by session.channel-state.state) channel-id
|
|
|
|
|= =^channel
|
|
|
|
^+ channel
|
|
|
|
::
|
|
|
|
%_ channel
|
|
|
|
next-id +(next-id.channel)
|
|
|
|
events (~(put to events.channel) [event-id event-stream-lines])
|
|
|
|
==
|
|
|
|
==
|
2019-10-07 02:26:03 +03:00
|
|
|
::
|
|
|
|
++ on-channel-heartbeat
|
|
|
|
|= channel-id=@t
|
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
2019-10-10 14:33:11 +03:00
|
|
|
?~ connection-state=(~(get by connections.state) duct)
|
|
|
|
[~ state]
|
|
|
|
::
|
2019-10-07 02:26:03 +03:00
|
|
|
=/ res
|
|
|
|
%- handle-response
|
|
|
|
:* %continue
|
|
|
|
data=(some (as-octs:mimes:html '\0a'))
|
|
|
|
complete=%.n
|
|
|
|
==
|
|
|
|
=/ http-moves -.res
|
|
|
|
=/ new-state +.res
|
|
|
|
=/ heartbeat-time=@da (add now ~s20)
|
|
|
|
:_ %_ new-state
|
|
|
|
session.channel-state
|
|
|
|
%+ ~(jab by session.channel-state.state) channel-id
|
|
|
|
|= =channel
|
|
|
|
channel(heartbeat (some [heartbeat-time duct]))
|
|
|
|
==
|
|
|
|
(snoc http-moves (set-heartbeat-move channel-id heartbeat-time))
|
|
|
|
:: +on-channel-timeout: we received a wake to clear an old session
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
|
|
|
++ on-channel-timeout
|
2018-11-21 04:06:04 +03:00
|
|
|
|= channel-id=@t
|
2018-11-15 21:27:10 +03:00
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
2019-07-17 20:51:56 +03:00
|
|
|
=/ usession=(unit channel)
|
|
|
|
(~(get by session.channel-state.state) channel-id)
|
|
|
|
?~ usession
|
|
|
|
[~ state]
|
|
|
|
=/ session=channel u.usession
|
2018-11-15 21:27:10 +03:00
|
|
|
::
|
2018-11-16 02:27:49 +03:00
|
|
|
:_ %_ state
|
|
|
|
session.channel-state
|
2018-11-21 04:06:04 +03:00
|
|
|
(~(del by session.channel-state.state) channel-id)
|
2018-11-16 02:27:49 +03:00
|
|
|
==
|
2019-10-10 14:33:11 +03:00
|
|
|
=/ heartbeat-cancel=(list move)
|
|
|
|
?~ heartbeat.session ~
|
|
|
|
:~ %^ cancel-heartbeat-move
|
|
|
|
channel-id
|
|
|
|
date.u.heartbeat.session
|
|
|
|
duct.u.heartbeat.session
|
|
|
|
==
|
|
|
|
%+ weld heartbeat-cancel
|
2018-11-15 21:27:10 +03:00
|
|
|
:: produce a list of moves which cancels every gall subscription
|
|
|
|
::
|
2019-03-27 01:52:32 +03:00
|
|
|
%+ turn ~(tap by subscriptions.session)
|
2019-10-26 02:04:29 +03:00
|
|
|
|= [channel-wire=wire ship=@p app=term =path duc=^duct]
|
2018-11-16 02:27:49 +03:00
|
|
|
^- move
|
|
|
|
::
|
2019-11-07 09:19:32 +03:00
|
|
|
[duc %pass channel-wire [%m %deal [our ship] app %leave ~]]
|
2018-11-15 21:27:10 +03:00
|
|
|
--
|
2018-10-10 21:51:52 +03:00
|
|
|
:: +handle-ford-response: translates a ford response for the outside world
|
|
|
|
::
|
|
|
|
++ handle-ford-response
|
|
|
|
|= made-result=made-result:ford
|
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
2019-03-19 23:38:18 +03:00
|
|
|
=+ connection=(~(got by connections.state) duct)
|
|
|
|
::
|
2018-10-10 21:51:52 +03:00
|
|
|
?: ?=(%incomplete -.made-result)
|
|
|
|
%^ return-static-data-on-duct 500 'text/html'
|
2019-03-19 23:38:18 +03:00
|
|
|
::
|
|
|
|
%- internal-server-error :*
|
|
|
|
authenticated.inbound-request.connection
|
|
|
|
url.request.inbound-request.connection
|
|
|
|
tang.made-result
|
|
|
|
==
|
2018-10-10 21:51:52 +03:00
|
|
|
::
|
|
|
|
?: ?=(%error -.build-result.made-result)
|
|
|
|
%^ return-static-data-on-duct 500 'text/html'
|
2019-03-19 23:38:18 +03:00
|
|
|
::
|
|
|
|
%- internal-server-error :*
|
|
|
|
authenticated.inbound-request.connection
|
|
|
|
url.request.inbound-request.connection
|
|
|
|
message.build-result.made-result
|
|
|
|
==
|
2018-10-10 21:51:52 +03:00
|
|
|
::
|
|
|
|
=/ =cage (result-to-cage:ford build-result.made-result)
|
|
|
|
::
|
2019-06-19 21:09:53 +03:00
|
|
|
=/ result=simple-payload:http ;;(simple-payload:http q.q.cage)
|
2019-03-21 01:19:36 +03:00
|
|
|
:: ensure we have a valid content-length header
|
|
|
|
::
|
|
|
|
:: We pass on the response and the headers the generator produces, but
|
|
|
|
:: ensure that we have a single content-length header set correctly in
|
|
|
|
:: the returned if this has a body, and has no content-length if there
|
|
|
|
:: is no body returned to the client.
|
|
|
|
::
|
|
|
|
=. headers.response-header.result
|
|
|
|
?~ data.result
|
|
|
|
(delete-header:http 'content-length' headers.response-header.result)
|
|
|
|
::
|
|
|
|
%^ set-header:http 'content-length'
|
|
|
|
(crip (format-ud-as-integer p.u.data.result))
|
|
|
|
headers.response-header.result
|
|
|
|
::
|
2018-10-10 21:51:52 +03:00
|
|
|
%- handle-response
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
^- http-event:http
|
2018-10-10 21:51:52 +03:00
|
|
|
:* %start
|
2019-03-21 01:19:36 +03:00
|
|
|
response-header.result
|
|
|
|
data.result
|
2018-10-10 21:51:52 +03:00
|
|
|
complete=%.y
|
|
|
|
==
|
2019-03-19 23:38:18 +03:00
|
|
|
:: +handle-gall-error: a call to +poke-http-response resulted in a %coup
|
|
|
|
::
|
|
|
|
++ handle-gall-error
|
|
|
|
|= =tang
|
|
|
|
^- [(list move) server-state]
|
|
|
|
::
|
|
|
|
=+ connection=(~(got by connections.state) duct)
|
|
|
|
::
|
|
|
|
%^ return-static-data-on-duct 500 'text/html'
|
|
|
|
::
|
|
|
|
%- internal-server-error :*
|
|
|
|
authenticated.inbound-request.connection
|
|
|
|
url.request.inbound-request.connection
|
|
|
|
tang
|
|
|
|
==
|
2018-10-10 21:51:52 +03:00
|
|
|
:: +handle-response: check a response for correctness and send to earth
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2019-02-09 02:03:46 +03:00
|
|
|
:: All outbound responses including %http-server generated responses need to go
|
2019-01-12 01:15:17 +03:00
|
|
|
:: through this interface because we want to have one centralized place
|
|
|
|
:: where we perform logging and state cleanup for connections that we're
|
|
|
|
:: done with.
|
|
|
|
::
|
2018-09-24 21:48:19 +03:00
|
|
|
++ handle-response
|
2019-02-09 00:16:42 +03:00
|
|
|
|= =http-event:http
|
2018-09-24 21:48:19 +03:00
|
|
|
^- [(list move) server-state]
|
|
|
|
:: verify that this is a valid response on the duct
|
|
|
|
::
|
|
|
|
?~ connection-state=(~(get by connections.state) duct)
|
|
|
|
~& [%invalid-outstanding-connection duct]
|
|
|
|
[~ state]
|
|
|
|
::
|
|
|
|
|^ ^- [(list move) server-state]
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?- -.http-event
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
|
|
|
%start
|
2019-02-09 00:16:42 +03:00
|
|
|
?^ response-header.u.connection-state
|
2018-09-24 21:48:19 +03:00
|
|
|
~& [%http-multiple-start duct]
|
|
|
|
error-connection
|
|
|
|
::
|
|
|
|
=. connections.state
|
|
|
|
%+ ~(jab by connections.state) duct
|
|
|
|
|= connection=outstanding-connection
|
|
|
|
%_ connection
|
2019-02-09 00:16:42 +03:00
|
|
|
response-header `response-header.http-event
|
|
|
|
bytes-sent ?~(data.http-event 0 p.u.data.http-event)
|
2018-09-24 21:48:19 +03:00
|
|
|
==
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
=? state complete.http-event
|
2018-09-24 21:48:19 +03:00
|
|
|
log-complete-request
|
|
|
|
::
|
|
|
|
pass-response
|
|
|
|
::
|
|
|
|
%continue
|
2019-02-09 00:16:42 +03:00
|
|
|
?~ response-header.u.connection-state
|
2018-09-24 21:48:19 +03:00
|
|
|
~& [%http-continue-without-start duct]
|
|
|
|
error-connection
|
|
|
|
::
|
|
|
|
=. connections.state
|
|
|
|
%+ ~(jab by connections.state) duct
|
|
|
|
|= connection=outstanding-connection
|
2019-02-09 00:16:42 +03:00
|
|
|
=+ size=?~(data.http-event 0 p.u.data.http-event)
|
2018-09-24 21:48:19 +03:00
|
|
|
connection(bytes-sent (add bytes-sent.connection size))
|
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
=? state complete.http-event
|
2018-09-24 21:48:19 +03:00
|
|
|
log-complete-request
|
|
|
|
::
|
|
|
|
pass-response
|
|
|
|
::
|
|
|
|
%cancel
|
|
|
|
:: todo: log this differently from an ise.
|
|
|
|
::
|
2019-11-09 10:31:11 +03:00
|
|
|
:: maybe should also send %leave to app?
|
|
|
|
::
|
2018-09-24 21:48:19 +03:00
|
|
|
error-connection
|
|
|
|
==
|
|
|
|
::
|
|
|
|
++ pass-response
|
|
|
|
^- [(list move) server-state]
|
2019-02-09 00:16:42 +03:00
|
|
|
[[duct %give %response http-event]~ state]
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
|
|
|
++ log-complete-request
|
|
|
|
:: todo: log the complete request
|
|
|
|
::
|
|
|
|
:: remove all outstanding state for this connection
|
|
|
|
::
|
|
|
|
=. connections.state
|
|
|
|
(~(del by connections.state) duct)
|
|
|
|
state
|
|
|
|
::
|
|
|
|
++ error-connection
|
|
|
|
:: todo: log application error
|
|
|
|
::
|
|
|
|
:: remove all outstanding state for this connection
|
|
|
|
::
|
|
|
|
=. connections.state
|
|
|
|
(~(del by connections.state) duct)
|
|
|
|
:: respond to outside with %error
|
|
|
|
::
|
|
|
|
^- [(list move) server-state]
|
2019-02-09 00:16:42 +03:00
|
|
|
[[duct %give %response %cancel ~]~ state]
|
2018-09-24 21:48:19 +03:00
|
|
|
--
|
2018-09-20 02:29:36 +03:00
|
|
|
:: +add-binding: conditionally add a pairing between binding and action
|
|
|
|
::
|
|
|
|
:: Adds =binding =action if there is no conflicting bindings.
|
|
|
|
::
|
|
|
|
++ add-binding
|
|
|
|
|= [=binding =action]
|
|
|
|
::
|
|
|
|
=/ to-search bindings.state
|
|
|
|
|-
|
|
|
|
^- [(list move) server-state]
|
|
|
|
?~ to-search
|
2019-02-09 00:16:42 +03:00
|
|
|
:- [duct %give %bound %.y binding]~
|
2018-09-20 02:29:36 +03:00
|
|
|
=. bindings.state
|
2018-10-11 21:28:27 +03:00
|
|
|
:: store in reverse alphabetical order so that longer paths are first
|
|
|
|
::
|
|
|
|
%- flop
|
2018-09-20 02:29:36 +03:00
|
|
|
%+ sort [[binding duct action] bindings.state]
|
|
|
|
|= [[a=^binding *] [b=^binding *]]
|
|
|
|
::
|
|
|
|
?: =(site.a site.b)
|
|
|
|
(aor path.a path.b)
|
|
|
|
:: alphabetize based on site
|
|
|
|
::
|
|
|
|
(aor ?~(site.a '' u.site.a) ?~(site.b '' u.site.b))
|
|
|
|
state
|
|
|
|
::
|
|
|
|
?: =(binding binding.i.to-search)
|
2019-02-09 00:16:42 +03:00
|
|
|
:- [duct %give %bound %.n binding]~
|
2018-09-20 02:29:36 +03:00
|
|
|
state
|
|
|
|
::
|
|
|
|
$(to-search t.to-search)
|
|
|
|
:: +remove-binding: removes a binding if it exists and is owned by this duct
|
|
|
|
::
|
|
|
|
++ remove-binding
|
|
|
|
|= =binding
|
|
|
|
::
|
|
|
|
^- server-state
|
|
|
|
%_ state
|
|
|
|
bindings
|
|
|
|
%+ skip bindings.state
|
|
|
|
|= [item-binding=^binding item-duct=^duct =action]
|
|
|
|
^- ?
|
|
|
|
&(=(item-binding binding) =(item-duct duct))
|
|
|
|
==
|
|
|
|
:: +get-action-for-binding: finds an action for an incoming web request
|
|
|
|
::
|
|
|
|
++ get-action-for-binding
|
|
|
|
|= [raw-host=(unit @t) url=@t]
|
2019-01-11 03:35:09 +03:00
|
|
|
^- action
|
2018-09-20 02:29:36 +03:00
|
|
|
:: process :raw-host
|
|
|
|
::
|
|
|
|
:: If we are missing a 'Host:' header, if that header is a raw IP
|
|
|
|
:: address, or if the 'Host:' header refers to [our].urbit.org, we want
|
2019-06-04 00:34:01 +03:00
|
|
|
:: to return ~ which means we're unidentified and will match against any
|
|
|
|
:: wildcard matching.
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
:: Otherwise, return the site given.
|
|
|
|
::
|
|
|
|
=/ host=(unit @t)
|
|
|
|
?~ raw-host
|
|
|
|
~
|
2018-10-03 21:36:45 +03:00
|
|
|
:: Parse the raw-host so that we can ignore ports, usernames, etc.
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
2018-10-04 00:05:36 +03:00
|
|
|
=+ parsed=(rush u.raw-host simplified-url-parser)
|
|
|
|
?~ parsed
|
2018-09-20 02:29:36 +03:00
|
|
|
~
|
2018-10-03 21:36:45 +03:00
|
|
|
:: if the url is a raw IP, assume default site.
|
|
|
|
::
|
2018-10-04 00:05:36 +03:00
|
|
|
?: ?=([%ip *] -.u.parsed)
|
|
|
|
~
|
|
|
|
:: if the url is "localhost", assume default site.
|
|
|
|
::
|
|
|
|
?: =([%site 'localhost'] -.u.parsed)
|
|
|
|
~
|
|
|
|
:: render our as a tape, and cut off the sig in front.
|
|
|
|
::
|
|
|
|
=/ with-sig=tape (scow %p our)
|
|
|
|
?> ?=(^ with-sig)
|
|
|
|
?: =(u.raw-host (crip t.with-sig))
|
|
|
|
:: [our].urbit.org is the default site
|
|
|
|
::
|
|
|
|
~
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
raw-host
|
|
|
|
:: url is the raw thing passed over the 'Request-Line'.
|
|
|
|
::
|
2018-09-27 02:18:40 +03:00
|
|
|
:: todo: this is really input validation, and we should return a 500 to
|
|
|
|
:: the client.
|
|
|
|
::
|
|
|
|
=/ request-line (parse-request-line url)
|
|
|
|
=/ parsed-url=(list @t) site.request-line
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
=/ bindings bindings.state
|
|
|
|
|-
|
|
|
|
::
|
|
|
|
?~ bindings
|
2019-01-11 03:35:09 +03:00
|
|
|
[%four-oh-four ~]
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
2019-06-04 00:34:01 +03:00
|
|
|
?: ?& (host-matches site.binding.i.bindings raw-host)
|
|
|
|
(path-matches path.binding.i.bindings parsed-url)
|
|
|
|
==
|
2019-01-11 03:35:09 +03:00
|
|
|
action.i.bindings
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
$(bindings t.bindings)
|
|
|
|
--
|
2019-06-04 00:34:01 +03:00
|
|
|
::
|
|
|
|
::
|
|
|
|
++ parse-request-line
|
|
|
|
|= url=@t
|
|
|
|
^- [[ext=(unit @ta) site=(list @t)] args=(list [key=@t value=@t])]
|
|
|
|
(fall (rush url ;~(plug apat:de-purl:html yque:de-purl:html)) [[~ ~] ~])
|
2018-09-20 02:29:36 +03:00
|
|
|
--
|
|
|
|
:: end the =~
|
|
|
|
::
|
|
|
|
. ==
|
|
|
|
:: begin with a default +axle as a blank slate
|
|
|
|
::
|
|
|
|
=| ax=axle
|
|
|
|
:: a vane is activated with current date, entropy, and a namespace function
|
|
|
|
::
|
2018-12-15 03:04:13 +03:00
|
|
|
|= [our=ship now=@da eny=@uvJ scry-gate=sley]
|
2018-09-20 02:29:36 +03:00
|
|
|
:: allow jets to be registered within this core
|
|
|
|
::
|
2019-02-09 02:03:46 +03:00
|
|
|
~% %http-server ..is ~
|
2018-09-20 02:29:36 +03:00
|
|
|
|%
|
|
|
|
++ call
|
|
|
|
|= [=duct type=* wrapped-task=(hobo task:able)]
|
2019-02-09 02:03:46 +03:00
|
|
|
^- [(list move) _http-server-gate]
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
|
|
|
=/ task=task:able
|
|
|
|
?. ?=(%soft -.wrapped-task)
|
|
|
|
wrapped-task
|
2018-10-01 21:37:30 +03:00
|
|
|
~| [%p-wrapped-task p.wrapped-task]
|
2019-06-19 21:09:53 +03:00
|
|
|
;;(task:able p.wrapped-task)
|
2019-03-13 23:29:22 +03:00
|
|
|
:: %crud: notifies us of an event failure
|
|
|
|
::
|
|
|
|
?: ?=(%crud -.task)
|
|
|
|
=/ moves=(list move)
|
|
|
|
[[duct %slip %d %flog task] ~]
|
|
|
|
[moves http-server-gate]
|
2019-02-09 00:16:42 +03:00
|
|
|
:: %init: tells us what our ship name is
|
2018-09-20 02:29:36 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?: ?=(%init -.task)
|
2018-09-27 02:18:40 +03:00
|
|
|
:: initial value for the login handler
|
|
|
|
::
|
|
|
|
=. bindings.server-state.ax
|
2018-11-15 21:27:10 +03:00
|
|
|
:~ [[~ /~/login] duct [%authentication ~]]
|
|
|
|
[[~ /~/channel] duct [%channel ~]]
|
2018-09-27 02:18:40 +03:00
|
|
|
==
|
2019-02-09 02:03:46 +03:00
|
|
|
[~ http-server-gate]
|
2019-09-27 23:00:37 +03:00
|
|
|
:: %trim: in response to memory pressure
|
|
|
|
::
|
2019-10-01 21:34:22 +03:00
|
|
|
:: Cancel all inactive channels
|
|
|
|
:: XX cancel active too if =(0 trim-priority) ?
|
|
|
|
::
|
2019-09-27 23:00:37 +03:00
|
|
|
?: ?=(%trim -.task)
|
2019-10-01 21:34:22 +03:00
|
|
|
=/ event-args [[our eny duct now scry-gate] server-state.ax]
|
|
|
|
=* by-channel by-channel:(per-server-event event-args)
|
|
|
|
=* channel-state channel-state.server-state.ax
|
|
|
|
::
|
|
|
|
=/ inactive=(list @t)
|
|
|
|
=/ full=(set @t) ~(key by session.channel-state)
|
|
|
|
=/ live=(set @t)
|
|
|
|
(~(gas in *(set @t)) ~(val by duct-to-key.channel-state))
|
|
|
|
~(tap in (~(dif in full) live))
|
|
|
|
::
|
|
|
|
?: =(~ inactive)
|
|
|
|
[~ http-server-gate]
|
|
|
|
::
|
|
|
|
=/ len=tape (scow %ud (lent inactive))
|
|
|
|
~> %slog.[0 leaf+"eyre: trim: closing {len} inactive channels"]
|
|
|
|
::
|
|
|
|
=| moves=(list (list move))
|
|
|
|
|- ^- [(list move) _http-server-gate]
|
|
|
|
=* channel-id i.inactive
|
|
|
|
?~ inactive
|
|
|
|
[(zing (flop moves)) http-server-gate]
|
|
|
|
:: discard channel state, and cancel any active gall subscriptions
|
|
|
|
::
|
|
|
|
=^ mov server-state.ax (on-channel-timeout:by-channel channel-id)
|
|
|
|
:: cancel channel timer
|
|
|
|
::
|
|
|
|
=/ channel (~(got by session.channel-state) channel-id)
|
|
|
|
=? mov ?=([%& *] state.channel)
|
|
|
|
:_ mov
|
|
|
|
(cancel-timeout-move:by-channel channel-id p.state.channel)
|
|
|
|
$(moves [mov moves], inactive t.inactive)
|
|
|
|
::
|
2019-03-13 23:35:14 +03:00
|
|
|
:: %vega: notifies us of a completed kernel upgrade
|
2019-02-12 01:41:50 +03:00
|
|
|
::
|
|
|
|
?: ?=(%vega -.task)
|
|
|
|
[~ http-server-gate]
|
2019-02-09 00:16:42 +03:00
|
|
|
:: %born: new unix process
|
2019-03-13 23:35:14 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
?: ?=(%born -.task)
|
2018-10-24 21:25:55 +03:00
|
|
|
:: close previously open connections
|
2018-09-28 21:24:41 +03:00
|
|
|
::
|
2018-10-24 21:25:55 +03:00
|
|
|
:: When we have a new unix process, every outstanding open connection is
|
|
|
|
:: dead. For every duct, send an implicit close connection.
|
|
|
|
::
|
|
|
|
=^ closed-connections=(list move) server-state.ax
|
|
|
|
=/ connections=(list [=^duct *])
|
|
|
|
~(tap by connections.server-state.ax)
|
|
|
|
::
|
|
|
|
=| closed-connections=(list move)
|
|
|
|
|-
|
|
|
|
?~ connections
|
|
|
|
[closed-connections server-state.ax]
|
|
|
|
::
|
|
|
|
=/ event-args
|
2019-01-08 02:17:02 +03:00
|
|
|
[[our eny duct.i.connections now scry-gate] server-state.ax]
|
2018-10-24 21:25:55 +03:00
|
|
|
=/ cancel-request cancel-request:(per-server-event event-args)
|
|
|
|
=^ moves server-state.ax cancel-request
|
|
|
|
::
|
|
|
|
$(closed-connections (weld moves closed-connections), connections t.connections)
|
2019-04-10 00:03:21 +03:00
|
|
|
:: save duct for future %give to unix
|
|
|
|
::
|
|
|
|
=. outgoing-duct.server-state.ax duct
|
2018-10-24 21:25:55 +03:00
|
|
|
::
|
2019-02-09 02:03:46 +03:00
|
|
|
:_ http-server-gate
|
2018-10-24 21:25:55 +03:00
|
|
|
;: weld
|
|
|
|
:: hand back default configuration for now
|
|
|
|
::
|
2019-03-21 08:28:32 +03:00
|
|
|
[duct %give %set-config http-config.server-state.ax]~
|
2018-10-24 21:25:55 +03:00
|
|
|
::
|
|
|
|
closed-connections
|
|
|
|
==
|
2019-02-09 00:16:42 +03:00
|
|
|
:: all other commands operate on a per-server-event
|
2018-09-28 21:24:41 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
=/ event-args [[our eny duct now scry-gate] server-state.ax]
|
|
|
|
=/ server (per-server-event event-args)
|
2018-10-26 02:32:54 +03:00
|
|
|
::
|
2019-03-21 08:28:32 +03:00
|
|
|
?- -.task
|
|
|
|
:: %live: notifies us of the ports of our live http servers
|
2018-10-26 02:32:54 +03:00
|
|
|
::
|
2019-02-09 00:16:42 +03:00
|
|
|
%live
|
2019-03-21 08:28:32 +03:00
|
|
|
=. ports.server-state.ax +.task
|
2019-02-09 02:03:46 +03:00
|
|
|
[~ http-server-gate]
|
2019-03-21 08:28:32 +03:00
|
|
|
:: %rule: updates our http configuration
|
|
|
|
::
|
|
|
|
%rule
|
|
|
|
?- -.http-rule.task
|
|
|
|
:: %cert: install tls certificate
|
|
|
|
::
|
|
|
|
%cert
|
|
|
|
=* config http-config.server-state.ax
|
|
|
|
?: =(secure.config cert.http-rule.task)
|
|
|
|
[~ http-server-gate]
|
|
|
|
=. secure.config cert.http-rule.task
|
|
|
|
:_ http-server-gate
|
2019-04-10 00:03:21 +03:00
|
|
|
=* out-duct outgoing-duct.server-state.ax
|
|
|
|
?~ out-duct ~
|
|
|
|
[out-duct %give %set-config config]~
|
2019-03-21 08:28:32 +03:00
|
|
|
:: %turf: add or remove domain name
|
|
|
|
::
|
|
|
|
%turf
|
|
|
|
=* domains domains.server-state.ax
|
|
|
|
=/ mod/(set turf)
|
|
|
|
?: ?=(%put action.http-rule.task)
|
|
|
|
(~(put in domains) turf.http-rule.task)
|
|
|
|
(~(del in domains) turf.http-rule.task)
|
|
|
|
?: =(domains mod)
|
|
|
|
[~ http-server-gate]
|
|
|
|
=. domains mod
|
|
|
|
:_ http-server-gate
|
|
|
|
=/ cmd
|
|
|
|
[%acme %poke `cage`[%acme-order !>(mod)]]
|
2019-11-05 10:42:59 +03:00
|
|
|
[duct %pass /acme/order %m %deal [our our] cmd]~
|
2019-03-21 08:28:32 +03:00
|
|
|
==
|
2019-02-09 00:16:42 +03:00
|
|
|
::
|
2019-09-27 20:40:22 +03:00
|
|
|
%request-foreign
|
2019-02-09 00:16:42 +03:00
|
|
|
=^ moves server-state.ax (request:server +.task)
|
2019-02-09 02:03:46 +03:00
|
|
|
[moves http-server-gate]
|
2019-04-30 23:38:40 +03:00
|
|
|
::
|
|
|
|
%request-local
|
|
|
|
=^ moves server-state.ax (request-local:server +.task)
|
|
|
|
[moves http-server-gate]
|
2019-02-09 00:16:42 +03:00
|
|
|
::
|
|
|
|
%cancel-request
|
|
|
|
=^ moves server-state.ax cancel-request:server
|
2019-02-09 02:03:46 +03:00
|
|
|
[moves http-server-gate]
|
2019-02-09 00:16:42 +03:00
|
|
|
::
|
|
|
|
%connect
|
|
|
|
=^ moves server-state.ax
|
|
|
|
%+ add-binding:server binding.task
|
|
|
|
[%app app.task]
|
2019-02-09 02:03:46 +03:00
|
|
|
[moves http-server-gate]
|
2019-02-09 00:16:42 +03:00
|
|
|
::
|
|
|
|
%serve
|
|
|
|
=^ moves server-state.ax
|
|
|
|
%+ add-binding:server binding.task
|
|
|
|
[%gen generator.task]
|
2019-02-09 02:03:46 +03:00
|
|
|
[moves http-server-gate]
|
2019-02-09 00:16:42 +03:00
|
|
|
::
|
|
|
|
%disconnect
|
|
|
|
=. server-state.ax (remove-binding:server binding.task)
|
2019-02-09 02:03:46 +03:00
|
|
|
[~ http-server-gate]
|
2019-03-13 04:13:25 +03:00
|
|
|
::
|
|
|
|
%wegh
|
|
|
|
:_ http-server-gate
|
|
|
|
:~ :* duct
|
|
|
|
%give
|
|
|
|
%mass
|
|
|
|
^- mass
|
2019-07-04 02:01:45 +03:00
|
|
|
:+ %eyre %|
|
2019-03-13 04:13:25 +03:00
|
|
|
:~ bindings+&+bindings.server-state.ax
|
|
|
|
auth+&+authentication-state.server-state.ax
|
|
|
|
connections+&+connections.server-state.ax
|
|
|
|
channels+&+channel-state.server-state.ax
|
|
|
|
axle+&+ax
|
|
|
|
== == ==
|
2018-09-20 02:29:36 +03:00
|
|
|
==
|
|
|
|
::
|
2018-09-24 21:48:19 +03:00
|
|
|
++ take
|
|
|
|
|= [=wire =duct wrapped-sign=(hypo sign)]
|
2019-02-09 02:03:46 +03:00
|
|
|
^- [(list move) _http-server-gate]
|
2018-09-24 21:48:19 +03:00
|
|
|
:: unwrap :sign, ignoring unneeded +type in :p.wrapped-sign
|
|
|
|
::
|
|
|
|
=/ =sign q.wrapped-sign
|
2019-11-05 10:42:59 +03:00
|
|
|
=> %= .
|
|
|
|
sign
|
|
|
|
?: ?=(%m -.sign)
|
|
|
|
?> ?=(%unto +<.sign)
|
|
|
|
sign
|
|
|
|
sign
|
|
|
|
==
|
2018-09-24 21:48:19 +03:00
|
|
|
:: :wire must at least contain two parts, the type and the build
|
|
|
|
::
|
2018-10-11 01:26:51 +03:00
|
|
|
?> ?=([@ *] wire)
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2019-02-09 02:03:46 +03:00
|
|
|
|^ ^- [(list move) _http-server-gate]
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2018-10-10 21:51:52 +03:00
|
|
|
?+ i.wire
|
|
|
|
~|([%bad-take-wire wire] !!)
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2019-07-26 00:12:44 +03:00
|
|
|
%run-app-request run-app-request
|
2019-11-09 10:31:11 +03:00
|
|
|
%watch-response watch-response
|
2019-07-26 00:12:44 +03:00
|
|
|
%run-build run-build
|
|
|
|
%channel channel
|
|
|
|
%acme acme-ack
|
2018-10-10 21:51:52 +03:00
|
|
|
==
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2019-07-26 00:12:44 +03:00
|
|
|
++ run-app-request
|
2018-10-03 21:36:45 +03:00
|
|
|
::
|
2019-11-05 10:42:59 +03:00
|
|
|
?> ?=([%m %unto *] sign)
|
2019-03-19 23:38:18 +03:00
|
|
|
::
|
|
|
|
::
|
2019-11-09 10:31:11 +03:00
|
|
|
?> ?=([%poke-ack *] p.sign)
|
|
|
|
?~ p.p.sign
|
|
|
|
:: received a positive acknowledgment: take no action
|
|
|
|
::
|
|
|
|
[~ http-server-gate]
|
|
|
|
:: we have an error; propagate it to the client
|
|
|
|
::
|
|
|
|
=/ event-args [[our eny duct now scry-gate] server-state.ax]
|
|
|
|
=/ handle-gall-error
|
|
|
|
handle-gall-error:(per-server-event event-args)
|
|
|
|
=^ moves server-state.ax
|
|
|
|
(handle-gall-error u.p.p.sign)
|
|
|
|
[moves http-server-gate]
|
|
|
|
::
|
|
|
|
++ watch-response
|
|
|
|
::
|
|
|
|
=/ event-args [[our eny duct now scry-gate] server-state.ax]
|
|
|
|
::
|
|
|
|
?: ?=([%m %unto %watch-ack *] sign)
|
2019-03-19 23:38:18 +03:00
|
|
|
?~ p.p.sign
|
|
|
|
:: received a positive acknowledgment: take no action
|
|
|
|
::
|
|
|
|
[~ http-server-gate]
|
|
|
|
:: we have an error; propagate it to the client
|
2018-10-03 21:36:45 +03:00
|
|
|
::
|
2019-03-19 23:38:18 +03:00
|
|
|
=/ handle-gall-error
|
|
|
|
handle-gall-error:(per-server-event event-args)
|
|
|
|
=^ moves server-state.ax (handle-gall-error u.p.p.sign)
|
|
|
|
[moves http-server-gate]
|
|
|
|
::
|
2019-11-09 10:31:11 +03:00
|
|
|
?: ?=([%m %unto %kick ~] sign)
|
|
|
|
=/ handle-response handle-response:(per-server-event event-args)
|
|
|
|
=^ moves server-state.ax
|
|
|
|
(handle-response %continue ~ &)
|
|
|
|
[moves http-server-gate]
|
2018-09-24 21:48:19 +03:00
|
|
|
::
|
2019-11-09 10:31:11 +03:00
|
|
|
?> ?=([%m %unto %fact *] sign)
|
|
|
|
=/ =mark p.cage.p.sign
|
|
|
|
=/ =vase q.cage.p.sign
|
|
|
|
?. ?= ?(%http-response-header %http-response-data %http-response-cancel)
|
|
|
|
mark
|
|
|
|
=/ handle-gall-error
|
|
|
|
handle-gall-error:(per-server-event event-args)
|
|
|
|
=^ moves server-state.ax
|
|
|
|
(handle-gall-error leaf+"eyre bad mark {<mark>}" ~)
|
|
|
|
[moves http-server-gate]
|
|
|
|
::
|
|
|
|
=/ =http-event:http
|
|
|
|
?- mark
|
|
|
|
%http-response-header [%start !<(response-header:http vase) ~ |]
|
|
|
|
%http-response-data [%continue !<((unit octs) vase) |]
|
|
|
|
%http-response-cancel [%cancel ~]
|
|
|
|
==
|
2018-09-24 21:48:19 +03:00
|
|
|
=/ handle-response handle-response:(per-server-event event-args)
|
2019-11-09 10:31:11 +03:00
|
|
|
=^ moves server-state.ax
|
|
|
|
(handle-response http-event)
|
2019-02-09 02:03:46 +03:00
|
|
|
[moves http-server-gate]
|
2018-10-10 21:51:52 +03:00
|
|
|
::
|
|
|
|
++ run-build
|
|
|
|
::
|
|
|
|
?> ?=([%f %made *] sign)
|
|
|
|
::
|
2019-01-08 02:17:02 +03:00
|
|
|
=/ event-args [[our eny duct now scry-gate] server-state.ax]
|
2018-10-10 21:51:52 +03:00
|
|
|
=/ handle-ford-response handle-ford-response:(per-server-event event-args)
|
|
|
|
=^ moves server-state.ax (handle-ford-response result.sign)
|
2019-02-09 02:03:46 +03:00
|
|
|
[moves http-server-gate]
|
2018-11-20 01:59:58 +03:00
|
|
|
::
|
|
|
|
++ channel
|
|
|
|
::
|
2019-01-08 02:17:02 +03:00
|
|
|
=/ event-args [[our eny duct now scry-gate] server-state.ax]
|
2018-11-20 01:59:58 +03:00
|
|
|
:: channel callback wires are triples.
|
|
|
|
::
|
|
|
|
?> ?=([@ @ @t *] wire)
|
|
|
|
::
|
|
|
|
?+ i.t.wire
|
|
|
|
~|([%bad-channel-wire wire] !!)
|
|
|
|
::
|
|
|
|
%timeout
|
2019-06-28 02:33:16 +03:00
|
|
|
?> ?=([%b %wake *] sign)
|
|
|
|
?^ error.sign
|
|
|
|
[[duct %slip %d %flog %crud %wake u.error.sign]~ http-server-gate]
|
2018-11-20 01:59:58 +03:00
|
|
|
=/ on-channel-timeout
|
2018-11-21 04:06:04 +03:00
|
|
|
on-channel-timeout:by-channel:(per-server-event event-args)
|
2018-11-20 01:59:58 +03:00
|
|
|
=^ moves server-state.ax
|
|
|
|
(on-channel-timeout i.t.t.wire)
|
2019-02-09 02:03:46 +03:00
|
|
|
[moves http-server-gate]
|
2019-10-07 02:26:03 +03:00
|
|
|
::
|
|
|
|
%heartbeat
|
|
|
|
=/ on-channel-heartbeat
|
|
|
|
on-channel-heartbeat:by-channel:(per-server-event event-args)
|
|
|
|
=^ moves server-state.ax
|
|
|
|
(on-channel-heartbeat i.t.t.wire)
|
|
|
|
[moves http-server-gate]
|
2018-11-20 01:59:58 +03:00
|
|
|
::
|
2018-11-21 04:06:04 +03:00
|
|
|
?(%poke %subscription)
|
2019-11-05 10:42:59 +03:00
|
|
|
?> ?=([%m %unto *] sign)
|
2018-11-21 04:06:04 +03:00
|
|
|
?> ?=([@ @ @t @ *] wire)
|
|
|
|
=/ on-gall-response
|
|
|
|
on-gall-response:by-channel:(per-server-event event-args)
|
2019-01-11 03:35:09 +03:00
|
|
|
:: ~& [%gall-response sign]
|
2018-11-21 04:06:04 +03:00
|
|
|
=^ moves server-state.ax
|
|
|
|
(on-gall-response i.t.t.wire `@ud`(slav %ud i.t.t.t.wire) p.sign)
|
2019-02-09 02:03:46 +03:00
|
|
|
[moves http-server-gate]
|
2018-11-20 01:59:58 +03:00
|
|
|
==
|
2019-04-08 23:17:19 +03:00
|
|
|
::
|
|
|
|
++ acme-ack
|
2019-11-05 10:42:59 +03:00
|
|
|
?> ?=([%m %unto *] sign)
|
2019-04-08 23:17:19 +03:00
|
|
|
::
|
2019-11-05 10:42:59 +03:00
|
|
|
?> ?=([%poke-ack *] p.sign)
|
2019-04-08 23:17:19 +03:00
|
|
|
?~ p.p.sign
|
|
|
|
:: received a positive acknowledgment: take no action
|
|
|
|
::
|
|
|
|
[~ http-server-gate]
|
|
|
|
:: received a negative acknowledgment: XX do something
|
|
|
|
::
|
|
|
|
[((slog u.p.p.sign) ~) http-server-gate]
|
2018-09-24 21:48:19 +03:00
|
|
|
--
|
|
|
|
::
|
2019-02-09 02:03:46 +03:00
|
|
|
++ http-server-gate ..$
|
2018-09-28 21:24:41 +03:00
|
|
|
:: +load: migrate old state to new state (called on vane reload)
|
|
|
|
::
|
|
|
|
++ load
|
2019-10-07 02:26:03 +03:00
|
|
|
=> |%
|
|
|
|
+$ channel-old
|
|
|
|
$: state=(each timer duct)
|
|
|
|
next-id=@ud
|
|
|
|
events=(qeu [id=@ud lines=wall])
|
|
|
|
subscriptions=(map wire [ship=@p app=term =path duc=duct])
|
|
|
|
==
|
|
|
|
+$ channel-state-old
|
|
|
|
$: session=(map @t channel-old)
|
|
|
|
duct-to-key=(map duct @t)
|
|
|
|
==
|
|
|
|
++ axle-old
|
|
|
|
%+ cork
|
|
|
|
axle
|
|
|
|
|= =axle
|
|
|
|
axle(date %~2019.1.7, channel-state.server-state (channel-state-old))
|
|
|
|
--
|
|
|
|
|= old=$%(axle axle-old)
|
2018-09-28 21:24:41 +03:00
|
|
|
^+ ..^$
|
|
|
|
::
|
|
|
|
~! %loading
|
2019-10-07 02:26:03 +03:00
|
|
|
?- -.old
|
|
|
|
%~2019.1.7
|
|
|
|
=/ add-heartbeat
|
|
|
|
%- ~(run by session.channel-state.server-state.old)
|
|
|
|
|= [c=channel-old]
|
|
|
|
^- channel
|
|
|
|
[state.c next-id.c events.c subscriptions.c ~]
|
|
|
|
::
|
|
|
|
=/ new
|
|
|
|
%= old
|
|
|
|
date %~2019.10.6
|
|
|
|
session.channel-state.server-state add-heartbeat
|
|
|
|
==
|
|
|
|
$(old new)
|
|
|
|
::
|
|
|
|
%~2019.10.6 ..^$(ax old)
|
|
|
|
==
|
|
|
|
|
2018-09-28 21:24:41 +03:00
|
|
|
:: +stay: produce current state
|
|
|
|
::
|
|
|
|
++ stay `axle`ax
|
|
|
|
:: +scry: request a path in the urbit namespace
|
|
|
|
::
|
|
|
|
++ scry
|
2019-03-21 09:01:38 +03:00
|
|
|
|= [fur=(unit (set monk)) ren=@tas why=shop syd=desk lot=coin tyl=path]
|
|
|
|
^- (unit (unit cage))
|
|
|
|
?. ?=(%& -.why)
|
|
|
|
~
|
|
|
|
=* who p.why
|
|
|
|
?. ?=(%$ ren)
|
|
|
|
[~ ~]
|
|
|
|
?. ?=(%$ -.lot)
|
|
|
|
[~ ~]
|
|
|
|
?. ?=(%host syd)
|
|
|
|
[~ ~]
|
|
|
|
%- (lift (lift |=(a=hart:eyre [%hart !>(a)])))
|
|
|
|
^- (unit (unit hart:eyre))
|
|
|
|
?. =(our who)
|
|
|
|
?. =([%da now] p.lot)
|
|
|
|
[~ ~]
|
|
|
|
~& [%r %scry-foreign-host who]
|
|
|
|
~
|
|
|
|
=. p.lot ?.(=([%da now] p.lot) p.lot [%tas %real])
|
|
|
|
?+ p.lot
|
|
|
|
[~ ~]
|
|
|
|
::
|
|
|
|
[%tas %fake]
|
|
|
|
``[& [~ 8.443] %& /localhost]
|
|
|
|
::
|
|
|
|
[%tas %real]
|
|
|
|
=* domains domains.server-state.ax
|
|
|
|
=* ports ports.server-state.ax
|
|
|
|
=/ =host:eyre [%& ?^(domains n.domains /localhost)]
|
|
|
|
=/ secure=? &(?=(^ secure.ports) !?=(hoke:eyre host))
|
|
|
|
=/ port=(unit @ud)
|
|
|
|
?. secure
|
|
|
|
?:(=(80 insecure.ports) ~ `insecure.ports)
|
|
|
|
?> ?=(^ secure.ports)
|
|
|
|
?:(=(443 u.secure.ports) ~ secure.ports)
|
|
|
|
``[secure port host]
|
|
|
|
==
|
2018-09-20 02:29:36 +03:00
|
|
|
--
|