io/hostname-resolution.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
c4ba555297842d5b34e053e752a4c0f6db7833ab
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.

// Resolve a hostname explicitly, then connect to a chosen numeric address
// (D223-A1, NETWORK.md §14.10).
//
// Run manually with both independent grants:
// bin/protos --allow-network --allow-resolver protos/examples/io/hostname-resolution.protos
// Resolver grants name resolution; Network grants TCP. Neither implies the other.
// External name service and connectivity depend on the host; CI never runs it.
// A self-contained, controlled-fixture version of the same composition runs in
// ProtosResolverAuthorityTest.

IpEndpoint: import("std:network/ip_endpoints").IpEndpoint

originalHostname: "api.github.com"

// Name-service resolution only: a Future of 1..64 distinct IpAddress
// candidates in advisory order. It never connects and never appends a search
// domain; a failure is a ResolutionError with kind "noAddress",
// "unavailable", "denied" or "limit".
// The Resolver is closed once the lookup ends, whether it succeeds or fails.
addresses: (() => {
    resolver.lookupAddresses(originalHostname).value()
}).ensure(() => {
    resolver.close().value()
})

// The application chooses an address; ordering promises no reachability.
endpoint: IpEndpoint(addresses[0], 443)

// Network applies its own connect authorization to the numeric endpoint;
// connectTcp has no hostname form.
connection: network.connectTcp(endpoint).value()

connected: (() => {
    // For HTTPS, the ORIGINAL hostname — never a resolved address or alias — is
    // the TLS reference identity (and SNI), and the HTTP Host authority:
    //
    //     Client: import("std:tls/client").Client
    //     secure: Client.handshake(connection, originalHostname, trust).value()
    //     headers: [["Host", Encoding.Latin1.encode(originalHostname)], ...]
    //
    // `trust` must be selected explicitly, e.g. Trust.javaCacerts(). The
    // complete composition is tls-client-over-tcp.protos.
    true
}).ensure(() => {
    // The connection is ours: close it on every path.
    connection.close().value()
})

connected