Skip to content

Commit

Permalink
initial checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Oct 25, 2023
0 parents commit d7fcf52
Show file tree
Hide file tree
Showing 42 changed files with 2,754 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.check.overrideCommand": ["cargo", "component", "check", "--message-format=json"]
}
15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "hello-wasi-http"
version = "0.0.0"
edition = "2021"
publish = false

[package.metadata.component]
package = "component:hello-wasi-http"

[package.metadata.component.dependencies]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cargo-component-bindings = "0.3.0"
40 changes: 40 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cargo_component_bindings::generate!();

/* TODO: these are the bindings we hope to get
pub mod bindings {
use super::T;
wit_bindgen::generate!({
path: "../wasi-http/wit",
world: "wasi:http/proxy",
exports: {
"wasi:http/incoming-handler": T,
},
});
}
*/

use bindings::wasi::http::types::{IncomingRequest, ResponseOutparam};

struct T;

impl bindings::exports::wasi::http::incoming_handler::Guest for T {
fn handle(_request: IncomingRequest, outparam: ResponseOutparam) {
let hdrs = bindings::wasi::http::types::Headers::new(&[]);
let resp = bindings::wasi::http::types::OutgoingResponse::new(200, &hdrs);
let body = resp.write().expect("outgoing response");

bindings::wasi::http::types::ResponseOutparam::set(outparam, Ok(resp));

let out = body.write().expect("outgoing stream");
out.blocking_write_and_flush(b"hello, world!")
.expect("writing response");

drop(out);
bindings::wasi::http::types::OutgoingBody::finish(body, None);
}
}

// Technically this should not be here for a proxy, but given the current
// framework for tests it's required since this file is built as a `bin`
fn main() {}
37 changes: 37 additions & 0 deletions wit/command-extended.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// All of the same imports and exports available in the wasi:cli/command world
// with addition of HTTP proxy related imports:
world command-extended {
import wasi:clocks/wall-clock@0.2.0-rc-2023-10-18;
import wasi:clocks/monotonic-clock@0.2.0-rc-2023-10-18;
import wasi:clocks/timezone@0.2.0-rc-2023-10-18;
import wasi:filesystem/types@0.2.0-rc-2023-10-18;
import wasi:filesystem/preopens@0.2.0-rc-2023-10-18;
import wasi:sockets/instance-network@0.2.0-rc-2023-10-18;
import wasi:sockets/ip-name-lookup@0.2.0-rc-2023-10-18;
import wasi:sockets/network@0.2.0-rc-2023-10-18;
import wasi:sockets/tcp-create-socket@0.2.0-rc-2023-10-18;
import wasi:sockets/tcp@0.2.0-rc-2023-10-18;
import wasi:sockets/udp-create-socket@0.2.0-rc-2023-10-18;
import wasi:sockets/udp@0.2.0-rc-2023-10-18;
import wasi:random/random@0.2.0-rc-2023-10-18;
import wasi:random/insecure@0.2.0-rc-2023-10-18;
import wasi:random/insecure-seed@0.2.0-rc-2023-10-18;
import wasi:io/poll@0.2.0-rc-2023-10-18;
import wasi:io/streams@0.2.0-rc-2023-10-18;
import wasi:cli/environment@0.2.0-rc-2023-10-18;
import wasi:cli/exit@0.2.0-rc-2023-10-18;
import wasi:cli/stdin@0.2.0-rc-2023-10-18;
import wasi:cli/stdout@0.2.0-rc-2023-10-18;
import wasi:cli/stderr@0.2.0-rc-2023-10-18;
import wasi:cli/terminal-input@0.2.0-rc-2023-10-18;
import wasi:cli/terminal-output@0.2.0-rc-2023-10-18;
import wasi:cli/terminal-stdin@0.2.0-rc-2023-10-18;
import wasi:cli/terminal-stdout@0.2.0-rc-2023-10-18;
import wasi:cli/terminal-stderr@0.2.0-rc-2023-10-18;

// We should replace all others with `include self.command`
// as soon as the unioning of worlds is available:
// https://github.com/WebAssembly/component-model/issues/169
import wasi:logging/logging@0.2.0-rc-2023-10-18;
import wasi:http/outgoing-handler@0.2.0-rc-2023-10-18;
}
7 changes: 7 additions & 0 deletions wit/deps/cli/command.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wasi:cli@0.2.0-rc-2023-10-18;

world command {
include reactor;

export run;
}
18 changes: 18 additions & 0 deletions wit/deps/cli/environment.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interface environment {
/// Get the POSIX-style environment variables.
///
/// Each environment variable is provided as a pair of string variable names
/// and string value.
///
/// Morally, these are a value import, but until value imports are available
/// in the component model, this import function should return the same
/// values each time it is called.
get-environment: func() -> list<tuple<string, string>>;

/// Get the POSIX-style arguments to the program.
get-arguments: func() -> list<string>;

/// Return a path that programs should use as their initial current working
/// directory, interpreting `.` as shorthand for this.
initial-cwd: func() -> option<string>;
}
4 changes: 4 additions & 0 deletions wit/deps/cli/exit.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface exit {
/// Exit the current instance and any linked instances.
exit: func(status: result);
}
32 changes: 32 additions & 0 deletions wit/deps/cli/reactor.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package wasi:cli@0.2.0-rc-2023-10-18;

world reactor {
import wasi:clocks/wall-clock@0.2.0-rc-2023-10-18;
import wasi:clocks/monotonic-clock@0.2.0-rc-2023-10-18;
import wasi:clocks/timezone@0.2.0-rc-2023-10-18;
import wasi:filesystem/types@0.2.0-rc-2023-10-18;
import wasi:filesystem/preopens@0.2.0-rc-2023-10-18;
import wasi:sockets/instance-network@0.2.0-rc-2023-10-18;
import wasi:sockets/ip-name-lookup@0.2.0-rc-2023-10-18;
import wasi:sockets/network@0.2.0-rc-2023-10-18;
import wasi:sockets/tcp-create-socket@0.2.0-rc-2023-10-18;
import wasi:sockets/tcp@0.2.0-rc-2023-10-18;
import wasi:sockets/udp-create-socket@0.2.0-rc-2023-10-18;
import wasi:sockets/udp@0.2.0-rc-2023-10-18;
import wasi:random/random@0.2.0-rc-2023-10-18;
import wasi:random/insecure@0.2.0-rc-2023-10-18;
import wasi:random/insecure-seed@0.2.0-rc-2023-10-18;
import wasi:io/poll@0.2.0-rc-2023-10-18;
import wasi:io/streams@0.2.0-rc-2023-10-18;

import environment;
import exit;
import stdin;
import stdout;
import stderr;
import terminal-input;
import terminal-output;
import terminal-stdin;
import terminal-stdout;
import terminal-stderr;
}
4 changes: 4 additions & 0 deletions wit/deps/cli/run.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface run {
/// Run the program.
run: func() -> result;
}
17 changes: 17 additions & 0 deletions wit/deps/cli/stdio.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
interface stdin {
use wasi:io/streams@0.2.0-rc-2023-10-18.{input-stream};

get-stdin: func() -> input-stream;
}

interface stdout {
use wasi:io/streams@0.2.0-rc-2023-10-18.{output-stream};

get-stdout: func() -> output-stream;
}

interface stderr {
use wasi:io/streams@0.2.0-rc-2023-10-18.{output-stream};

get-stderr: func() -> output-stream;
}
47 changes: 47 additions & 0 deletions wit/deps/cli/terminal.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
interface terminal-input {
/// The input side of a terminal.
resource terminal-input;

// In the future, this may include functions for disabling echoing,
// disabling input buffering so that keyboard events are sent through
// immediately, querying supported features, and so on.
}

interface terminal-output {
/// The output side of a terminal.
resource terminal-output;

// In the future, this may include functions for querying the terminal
// size, being notified of terminal size changes, querying supported
// features, and so on.
}

/// An interface providing an optional `terminal-input` for stdin as a
/// link-time authority.
interface terminal-stdin {
use terminal-input.{terminal-input};

/// If stdin is connected to a terminal, return a `terminal-input` handle
/// allowing further interaction with it.
get-terminal-stdin: func() -> option<terminal-input>;
}

/// An interface providing an optional `terminal-output` for stdout as a
/// link-time authority.
interface terminal-stdout {
use terminal-output.{terminal-output};

/// If stdout is connected to a terminal, return a `terminal-output` handle
/// allowing further interaction with it.
get-terminal-stdout: func() -> option<terminal-output>;
}

/// An interface providing an optional `terminal-output` for stderr as a
/// link-time authority.
interface terminal-stderr {
use terminal-output.{terminal-output};

/// If stderr is connected to a terminal, return a `terminal-output` handle
/// allowing further interaction with it.
get-terminal-stderr: func() -> option<terminal-output>;
}
32 changes: 32 additions & 0 deletions wit/deps/clocks/monotonic-clock.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// WASI Monotonic Clock is a clock API intended to let users measure elapsed
/// time.
///
/// It is intended to be portable at least between Unix-family platforms and
/// Windows.
///
/// A monotonic clock is a clock which has an unspecified initial value, and
/// successive reads of the clock will produce non-decreasing values.
///
/// It is intended for measuring elapsed time.
interface monotonic-clock {
use wasi:io/poll@0.2.0-rc-2023-10-18.{pollable};

/// A timestamp in nanoseconds.
type instant = u64;

/// Read the current value of the clock.
///
/// The clock is monotonic, therefore calling this function repeatedly will
/// produce a sequence of non-decreasing values.
now: func() -> instant;

/// Query the resolution of the clock.
resolution: func() -> instant;

/// Create a `pollable` which will resolve once the specified time has been
/// reached.
subscribe: func(
when: instant,
absolute: bool
) -> pollable;
}
48 changes: 48 additions & 0 deletions wit/deps/clocks/timezone.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
interface timezone {
use wall-clock.{datetime};

/// Return information needed to display the given `datetime`. This includes
/// the UTC offset, the time zone name, and a flag indicating whether
/// daylight saving time is active.
///
/// If the timezone cannot be determined for the given `datetime`, return a
/// `timezone-display` for `UTC` with a `utc-offset` of 0 and no daylight
/// saving time.
display: func(when: datetime) -> timezone-display;

/// The same as `display`, but only return the UTC offset.
utc-offset: func(when: datetime) -> s32;

/// Information useful for displaying the timezone of a specific `datetime`.
///
/// This information may vary within a single `timezone` to reflect daylight
/// saving time adjustments.
record timezone-display {
/// The number of seconds difference between UTC time and the local
/// time of the timezone.
///
/// The returned value will always be less than 86400 which is the
/// number of seconds in a day (24*60*60).
///
/// In implementations that do not expose an actual time zone, this
/// should return 0.
utc-offset: s32,

/// The abbreviated name of the timezone to display to a user. The name
/// `UTC` indicates Coordinated Universal Time. Otherwise, this should
/// reference local standards for the name of the time zone.
///
/// In implementations that do not expose an actual time zone, this
/// should be the string `UTC`.
///
/// In time zones that do not have an applicable name, a formatted
/// representation of the UTC offset may be returned, such as `-04:00`.
name: string,

/// Whether daylight saving time is active.
///
/// In implementations that do not expose an actual time zone, this
/// should return false.
in-daylight-saving-time: bool,
}
}
41 changes: 41 additions & 0 deletions wit/deps/clocks/wall-clock.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// WASI Wall Clock is a clock API intended to let users query the current
/// time. The name "wall" makes an analogy to a "clock on the wall", which
/// is not necessarily monotonic as it may be reset.
///
/// It is intended to be portable at least between Unix-family platforms and
/// Windows.
///
/// A wall clock is a clock which measures the date and time according to
/// some external reference.
///
/// External references may be reset, so this clock is not necessarily
/// monotonic, making it unsuitable for measuring elapsed time.
///
/// It is intended for reporting the current date and time for humans.
interface wall-clock {
/// A time and date in seconds plus nanoseconds.
record datetime {
seconds: u64,
nanoseconds: u32,
}

/// Read the current value of the clock.
///
/// This clock is not monotonic, therefore calling this function repeatedly
/// will not necessarily produce a sequence of non-decreasing values.
///
/// The returned timestamps represent the number of seconds since
/// 1970-01-01T00:00:00Z, also known as [POSIX's Seconds Since the Epoch],
/// also known as [Unix Time].
///
/// The nanoseconds field of the output is always less than 1000000000.
///
/// [POSIX's Seconds Since the Epoch]: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_16
/// [Unix Time]: https://en.wikipedia.org/wiki/Unix_time
now: func() -> datetime;

/// Query the resolution of the clock.
///
/// The nanoseconds field of the output is always less than 1000000000.
resolution: func() -> datetime;
}
7 changes: 7 additions & 0 deletions wit/deps/clocks/world.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package wasi:clocks@0.2.0-rc-2023-10-18;

world imports {
import monotonic-clock;
import wall-clock;
import timezone;
}
6 changes: 6 additions & 0 deletions wit/deps/filesystem/preopens.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface preopens {
use types.{descriptor};

/// Return the set of preopened directories, and their path.
get-directories: func() -> list<tuple<descriptor, string>>;
}
Loading

0 comments on commit d7fcf52

Please sign in to comment.