///|
/// The content type of a page.
let html : String = "text/html; charset=utf-8"

///|
/// The element of the icon. The picture is in the page, because a browser without a
/// session also gets pages.
let icon_link : String = ""

///|
/// The rule of a policy that permits `icon_link`.
let icon_rule : String = "img-src data:"

///|
/// The title of each page. An instance name has only letters, digits and hyphens,
/// so it is safe in HTML and in an attribute.
fn title_of(instance : @app.InstanceName) -> String {
  match instance.to_string() {
    "stable" => "Mission Control"
    name => "Mission Control (\{name})"
  }
}

///|
/// The page of the UI. It has no data of the user. The name of the instance is there
/// for a view that cannot connect.
fn ui_page(
  instance : @app.InstanceName,
  port : @core.Port,
  surface : @app.Surface,
) -> Reply {
  let attribute = match surface {
    Browser => ""
    Desktop => " data-surface=\"desktop\""
  }
  let page =
    $|
    $|
    $|
    $|
    $|
    $|\{title_of(instance)}
    $|\{icon_link}
    $|
    $|
    $|
$| $| $| $| $| // The UI sets inline styles. Some browsers do not read 'self' as the WebSocket of // the same address, so the policy names it. let policy = [ "default-src 'none'", "script-src 'self'", "style-src 'self' 'unsafe-inline'", "connect-src 'self' ws://127.0.0.1:\{port.to_int()}", icon_rule, closed_rules, ].join("; ") Reply(Success, kind=html, body=@utf8.encode(page), policy~) } ///| /// The page that a link opens. It gives the cookie to the browser, keeps the view /// key in the storage of the origin, and replaces itself with the UI, so that the /// address has no token. /// /// A key and a nonce have only the characters of base64url, and an ID is a number, /// so they are safe in the script and in the attribute. fn entrance_page( instance : @app.InstanceName, entrance : Entrance, ) -> Reply raise NoRandomBytes { let nonce = new_secret().to_text() let target = match entrance.project { Some(id) => "/#/projects/\{id.to_int()}" None => "/" } // With a browser that keeps nothing, the UI says what to do. let page = $| $| $| $| $|\{title_of(instance)} $|\{icon_link} $| $| $| $| $| $| $| let policy = [ "default-src 'none'", "script-src 'nonce-\{nonce}'", icon_rule, closed_rules, ].join("; ") Reply( Success, kind=html, body=@utf8.encode(page), policy~, cookie=entrance.cookie, ) } ///| /// The page for a browser with no session, and for a link that does not work. fn no_session_page(instance : @app.InstanceName) -> Reply { let command = @app.Surface::Browser.open_command(instance) let page = $| $| $| $| $|\{title_of(instance)} $|\{icon_link} $| $| $|

Mission Control

$|

This browser has no session, or the link is too old. To get a new link, run \{command} in a terminal.

$| $| $| let policy = ["default-src 'none'", icon_rule, closed_rules].join("; ") Reply(Forbidden, kind=html, body=@utf8.encode(page), policy~) }