factor/library/httpd/httpd.factor

72 lines
1.7 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 kernel lists namespaces io strings threads http
sequences ;
2004-07-16 02:26:21 -04:00
: (url>path) ( uri -- path )
2005-05-18 16:26:22 -04:00
url-decode "http://" ?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 [
2005-05-18 16:26:22 -04:00
>r (url>path) "?" r> append3
] [
drop (url>path)
] ifte ;
: secure-path ( path -- path )
2005-05-18 16:26:22 -04:00
".." over subseq? [ drop f ] when ;
2004-07-16 02:26:21 -04:00
: request-method ( cmd -- method )
[
[[ "GET" "get" ]]
[[ "POST" "post" ]]
[[ "HEAD" "head" ]]
] assoc [ "bad" ] unless* ;
2005-05-23 19:14:29 -04:00
: host ( -- string )
#! The host the current responder was called from.
"Host" "header" get assoc ":" split1 drop ;
2005-05-23 01:23:33 -04:00
: (handle-request) ( arg cmd -- method path host )
request-method dup "method" set swap
2005-05-23 19:14:29 -04:00
prepare-url prepare-header host ;
: handle-request ( arg cmd -- )
2005-05-23 01:23:33 -04:00
[ (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-06-18 21:15:07 -04:00
dup log-client [
60000 stdio get set-timeout
readln [ parse-request ] when*
2005-06-18 21:15:07 -04:00
] with-stream ;
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 ]
[ "http-server" get stream-close rethrow ] catch
2005-04-24 23:02:19 -04:00
] with-logging ;
2005-04-17 18:34:09 -04:00
: stop-httpd ( -- )
#! Stop the server.
"http-server" get stream-close ;