#!/usr/bin/env rune # Copyright 2004-2006 Kevin Reid, under the terms of the MIT X license # found at http://www.opensource.org/licenses/mit-license.html ................ # Testing makeHTTPWriter. pragma.enable("easy-return") pragma.disable("explicit-result-guard") pragma.enable("accumulator") pragma.enable("unary-star") pragma.enable("dot-props") def int8 := def network := (privilegedScope) def makeListStream := def makeHTTPWriter := def makeInCharDecoder := def unpackVow(n, listVow) { return accum [] for i in 0..!n { _.with(listVow <- get(i)) } } def tick() { timer.whenPast(timer.now() + 100, fn{ stderr.println("%% main vat tick") tick() }) }() # --- Making connection --- def [host, portStr] := interp::args def remote := network.getRemoteTCPEndpoint(host, __makeInt(portStr, 10)) when (def cr := remote.connect()) -> { def [rawIns, outs] := cr def ins := makeInCharDecoder(rawIns, "UTF-8", => stderr) def handler() { stderr.println(`%% handler: ${ins.isTerminated()} ${ins.available()}`) if (ins.isTerminated()) { interp.exitAtTop() } else { print(__makeTwine.fromChars(ins.read(0, 2**16), null)) ins <- whenAvailable(1, handler) } } ins <- whenAvailable(1, handler) def connWriter := makeHTTPWriter(def _ extends outs {to close() {super.terminates()}}, => stderr) def requestWriter := connWriter.next() requestWriter.request("GET", "/", "1.0") requestWriter.header("Host", `$host:$portStr`) requestWriter.body(makeListStream([0x30,0x31,0x32,0x33,13,10], int8)) requestWriter.finish() } catch p { interp.exitAtTop(p) } interp.blockAtTop()