From f1527f57cfb54577d606ef358d9e2a1860c64ba1 Mon Sep 17 00:00:00 2001 From: Graham Chiu Date: Mon, 17 Apr 2017 21:14:13 +1200 Subject: [PATCH] Update shttpd.r some fixes to make with work with ren-c ** Build: 15-Apr-2017/4:17:49 ** ** Commit: 6d69673177c9cfc73f7e1b630199d86eecd187a0 --- scripts/shttpd.r | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/scripts/shttpd.r b/scripts/shttpd.r index 9f68f0d..f332c32 100644 --- a/scripts/shttpd.r +++ b/scripts/shttpd.r @@ -1,12 +1,14 @@ REBOL [title: "A tiny static HTTP server" author: 'abolka date: 2009-11-04] +crlf2bin: to binary! join-of crlf crlf code-map: make map! [200 "OK" 400 "Forbidden" 404 "Not Found"] mime-map: make map! [ "html" "text/html" "css" "text/css" "js" "application/javascript" "gif" "image/gif" "jpg" "image/jpeg" "png" "image/png" "r" "text/plain" "r3" "text/plain" "reb" "text/plain" + ] -error-template: trim/auto { +error-template: trim/auto copy { $code $text

$text

Requested URI: $uri


shttpd.r on REBOL 3 $r3 @@ -37,11 +39,11 @@ send-chunk: func [port] [ unless empty? port/locals [write port take/part port/locals 32'000] ] -handle-request: func [config req /local uri type file data] [ +handle-request: func [config req /local uri type file data ext] [ parse to-string req ["get " ["/ " | copy uri to " "]] - default 'uri "index.html" - parse uri [some [thru "."] copy ext to end (type: mime-map/:ext)] - default 'type "application/octet-stream" + uri: default ["index.html"] + parse uri [some [thru "."] copy ext: to end (type: select mime-map ext)] + type: default ["application/octet-stream"] if not exists? file: config/root/:uri [return error-response 404 uri] if error? try [data: read file] [return error-response 400 uri] reduce [200 type data] @@ -49,16 +51,23 @@ handle-request: func [config req /local uri type file data] [ awake-client: func [event /local port res] [ port: event/port + print ["Event type: " event/type] switch event/type [ read [ - either find port/data to-binary join crlf crlf [ + either find port/data crlf2bin [ res: handle-request port/locals/config port/data start-response port res ] [ read port ] ] - wrote [unless send-chunk port [close port]] + wrote [ + either empty? port/locals [ + close port + ][ + send-chunk port + ] + ] close [close port] ] ] @@ -72,9 +81,10 @@ awake-server: func [event /local client] [ ] serve: func [web-port web-root /local listen-port] [ - listen-port: open join tcp://: web-port - listen-port/locals: construct compose/deep [config: [root: (web-root)]] + listen-port: open rejoin [tcp://: web-port] + listen-port/locals: make object! compose/deep [config: [root: (web-root)]] listen-port/awake: :awake-server + print ajoin ["serving on port " web-port "..."] wait listen-port ]