factor/libs/httpd/httpd.factor

62 lines
1.5 KiB
Factor
Raw Permalink Normal View History

2006-10-19 22:41:20 -04:00
! Copyright (C) 2003, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2004-07-16 02:26:21 -04:00
IN: httpd
2006-05-25 00:06:50 -04:00
USING: errors hashtables kernel namespaces io strings
2006-10-19 22:41:20 -04:00
threads http sequences prettyprint ;
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 [
>r (url>path) "?" r> 3append
] [
drop (url>path)
2005-09-24 15:21:17 -04:00
] if ;
: 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 )
2005-11-29 23:49:59 -05:00
H{
{ "GET" "get" }
{ "POST" "post" }
{ "HEAD" "head" }
} hash [ "bad" ] unless* ;
2005-05-23 19:14:29 -04:00
: host ( -- string )
#! The host the current responder was called from.
2005-11-29 23:49:59 -05:00
"Host" "header" get hash ":" split1 drop ;
2005-05-23 19:14:29 -04:00
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-message
" " split1 dup [
" HTTP" split1 drop url>path secure-path dup [
swap handle-request
] [
2drop bad-request
2005-09-24 15:21:17 -04:00
] if
2004-07-16 02:26:21 -04:00
] [
2drop bad-request
2005-09-24 15:21:17 -04:00
] if ;
2004-07-16 02:26:21 -04:00
: httpd ( port -- )
2006-10-19 22:41:20 -04:00
"Starting HTTP server on port " write dup . flush
\ httpd [
2005-06-18 21:15:07 -04:00
60000 stdio get set-timeout
readln [ parse-request ] when*
] with-server ;
2005-04-17 18:34:09 -04:00
: stop-httpd ( -- )
#! Stop the server.
\ httpd get stream-close ;