1 ! Copyright (C) 2003, 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: errors hashtables kernel namespaces io strings
5 threads http sequences prettyprint ;
7 : (url>path) ( uri -- path )
8 url-decode "http://" ?head [
9 "/" split1 dup "" ? nip
12 : url>path ( uri -- path )
14 >r (url>path) "?" r> 3append
19 : secure-path ( path -- path )
20 ".." over subseq? [ drop f ] when ;
22 : request-method ( cmd -- method )
27 } hash [ "bad" ] unless* ;
30 #! The host the current responder was called from.
31 "Host" "header" get hash ":" split1 drop ;
33 : (handle-request) ( arg cmd -- method path host )
34 request-method dup "method" set swap
35 prepare-url prepare-header host ;
37 : handle-request ( arg cmd -- )
38 [ (handle-request) serve-responder ] with-scope ;
40 : parse-request ( request -- )
43 " HTTP" split1 drop url>path secure-path dup [
53 "Starting HTTP server on port " write dup . flush
55 60000 stdio get set-timeout
56 readln [ parse-request ] when*
61 \ httpd get stream-close ;