http/server/module.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
f836cd27513b845eab1d82ebe46514c3622dde8a
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Standard Library HTTP/1.1 single-request origin server (D215, HTTP.md).
//!
//! `Server.serveOne(duplex, handler, limits)` borrows a caller-supplied duplex
//! byte flow (`ByteReadable` and `ByteWritable`), processes exactly one
//! request, invokes `handler(request)` at most once, emits exactly one final
//! response carrying `Connection: close`, and retires its session.
//! `Server.serveOne.owning(duplex, handler, limits)` additionally requires
//! `Closable` and closes the duplex on every terminal path. Both answer a fresh
//! `Future` resolving to `null`.
//!
//! The server listens on no port and acquires no network, clock, timer, or
//! other ambient authority. It defines no timeout and is not declared safe for
//! direct exposure to hostile Internet traffic (HTTP.md §11).

/// The prototype of every HTTP failure. Each failure is a fresh occurrence
/// whose `kind` is "malformedRequest", "requestLimit", "invalidResponse", or
/// "unsupportedFeature", and whose `status`, when present, is the associated
/// HTTP status Integer.
this.HttpError: Error {}

/// The single-request HTTP/1.1 server.
this.Server: ((facility) => {
    Session: import("../internal/Session.protos")
    Limits: import("../internal/Limits.protos")

    // Validates limits (HTTP.md §10) and answers a private snapshot of every
    // effective limit, or null when limits is not admissible.
    captureLimits: (limits) => {
        Limits.capture(facility, limits)
    }

    // HTTP.md §2.3 and §2.4: semantic validation precedes every effect. An
    // invalid call answers an already failed Future without a Task, a session,
    // ownership, or I/O; an admitted call establishes the session and then
    // creates exactly one service Task.
    admit: (duplex, handler, limits, owning) => {
        valid: facility.provides(duplex, "read") && facility.provides(duplex, "write")
        if(valid && owning) {
            valid = facility.closable(duplex)
        }
        if(valid) {
            valid = facility.provides(handler, "call")
        }
        captured: null
        if(valid) {
            captured = captureLimits(limits)
            valid = captured !== null
        }

        answer: null
        if(valid) {
            answer = facility.startService(
                Session.serviceBody(facility, HttpError, duplex, handler, captured, owning)
            )
        } else {
            answer = facility.failed(InvalidIOArgument {})
        }
        answer
    }

    entry: (duplex, handler, limits) => {
        admit(duplex, handler, limits, false)
    }
    entry.owning: (duplex, handler, limits) => {
        admit(duplex, handler, limits, true)
    }

    server: {
        serveOne: entry
    }
    server
})(_httpAsyncFacility)

// The runtime-provisioned facility is captured privately above; it is not
// module surface.
context.removeSlot("_httpAsyncFacility")