factor/library/httpd/httpd.factor

70 lines
1.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2003, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-07-16 02:26:21 -04:00
IN: httpd
USING: errors httpd-responder kernel lists namespaces
stdio streams strings threads url-encoding ;
2004-07-16 02:26:21 -04:00
: (url>path) ( uri -- path )
url-decode "http://" ?string-head [
2004-12-18 23:35:20 -05:00
"/" split1 dup "" ? nip
] when ;
2004-07-16 02:26:21 -04:00
: url>path ( uri -- path )
"?" split1 dup [
>r (url>path) "?" r> cat3
] [
drop (url>path)
] ifte ;
: secure-path ( path -- path )
".." over string-contains? [ drop f ] when ;
2004-07-16 02:26:21 -04:00
: request-method ( cmd -- method )
[
[[ "GET" "get" ]]
[[ "POST" "post" ]]
[[ "HEAD" "head" ]]
] assoc [ "bad" ] unless* ;
: (handle-request) ( arg cmd -- url method )
request-method dup "method" set swap
prepare-url prepare-header ;
: handle-request ( arg cmd -- )
[ (handle-request) serve-responder ] with-scope ;
2004-07-16 02:26:21 -04:00
: parse-request ( request -- )
dup log
" " split1 dup [
" HTTP" split1 drop url>path secure-path dup [
swap handle-request
] [
2drop bad-request
] ifte
2004-07-16 02:26:21 -04:00
] [
2drop bad-request
2004-07-16 02:26:21 -04:00
] ifte ;
: httpd-client ( socket -- )
[
[
2005-02-14 22:15:02 -05:00
stdio get log-client read-line [ parse-request ] when*
] with-stream
2004-12-25 21:28:47 -05:00
] try ;
2004-07-16 02:26:21 -04:00
2004-08-23 20:44:58 -04:00
: httpd-connection ( socket -- )
"http-server" get accept [ httpd-client ] in-thread drop ;
2004-07-16 02:26:21 -04:00
: httpd-loop ( -- ) httpd-connection httpd-loop ;
2004-07-16 02:26:21 -04:00
: httpd ( port -- )
<server> "http-server" set [
httpd-loop
] [
2005-02-14 22:15:02 -05:00
"http-server" get stream-close rethrow
] catch ;
2005-04-17 18:34:09 -04:00
: stop-httpd ( -- )
#! Stop the server.
"http-server" get stream-close ;