From fcf35c1315773edb2061b6bbd87b0963eecbd097 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 27 Oct 2016 11:26:42 -0700 Subject: 3604 --- html/092socket.mu.html | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'html/092socket.mu.html') diff --git a/html/092socket.mu.html b/html/092socket.mu.html index 78321183..e3c179eb 100644 --- a/html/092socket.mu.html +++ b/html/092socket.mu.html @@ -54,6 +54,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color memory-should-contain [ 10:array:character <- [abc] ] + socket <- $close-socket socket ] # helper just for this scenario def example-handler query:text -> response:text [ @@ -85,18 +86,18 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color type request-handler = (recipe text -> text) -def serve-one-request socket:num, request-handler:request-handler [ +def serve-one-request socket:num, request-handler:request-handler -> socket:num [ local-scope load-ingredients session:num <- $accept socket assert session, [ F - example-server-test: $accept failed] contents:&:source:char, sink:&:sink:char <- new-channel 30 - sink <- start-running receive-from-socket session, sink + start-running receive-from-socket session, sink query:text <- drain contents response:text <- call request-handler, query write-to-socket session, response - $close-socket session + session <- $close-socket session ] def start-reading-from-network resources:&:resources, uri:text -> contents:&:source:char [ @@ -120,7 +121,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color req:text <- interpolate [GET _ HTTP/1.1], path request-socket socket, req contents:&:source:char, sink:&:sink:char <- new-channel 10000 - start-running receive-from-socket socket, sink + start-running receive-from-client-socket-and-close socket, sink ] def request-socket socket:num, s:text -> socket:num [ @@ -134,33 +135,34 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color $write-to-socket socket, 10/lf ] -def receive-from-socket socket:num, sink:&:sink:char -> sink:&:sink:char [ +def receive-from-socket socket:num, sink:&:sink:char -> sink:&:sink:char, socket:num [ local-scope load-ingredients { +next-attempt - req:text, found?:bool, eof?:bool, error:num <- $read-from-socket socket, 4096/bytes + c:char, found?:bool, eof?:bool, error:num <- $read-from-socket socket + break-if eof? break-if error { - break-if found? - switch - loop +next-attempt + break-unless found? + sink <- write sink, c } - bytes-read:num <- length *req - i:num <- copy 0 { - done?:bool <- greater-or-equal i, bytes-read - break-if done? - c:char <- index *req, i # todo: unicode - sink <- write sink, c - i <- add i, 1 - loop + break-if found? + switch } - loop-unless eof? + loop } sink <- close sink ] +def receive-from-client-socket-and-close socket:num, sink:&:sink:char -> sink:&:sink:char, socket:num [ + local-scope + load-ingredients + sink <- receive-from-socket socket, sink + socket <- $close-socket socket +] + def write-to-socket socket:num, s:text [ local-scope load-ingredients -- cgit 1.4.1-2-gfad0