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
|
|
|
|
2005-02-12 21:51:33 -05: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
|
2004-11-15 12:33:21 -05:00
|
|
|
] when ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-02-12 21:51:33 -05:00
|
|
|
: url>path ( uri -- path )
|
|
|
|
"?" split1 dup [
|
2006-12-10 15:01:52 -05:00
|
|
|
>r (url>path) "?" r> 3append
|
2005-02-12 21:51:33 -05:00
|
|
|
] [
|
|
|
|
drop (url>path)
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-02-12 21:51:33 -05:00
|
|
|
|
2004-08-10 22:11:48 -04:00
|
|
|
: 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
|
|
|
|
2004-09-02 16:40:34 -04:00
|
|
|
: request-method ( cmd -- method )
|
2005-11-29 23:49:59 -05:00
|
|
|
H{
|
|
|
|
{ "GET" "get" }
|
|
|
|
{ "POST" "post" }
|
|
|
|
{ "HEAD" "head" }
|
|
|
|
} hash [ "bad" ] unless* ;
|
2004-08-29 23:30:54 -04:00
|
|
|
|
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 )
|
2004-09-02 16:40:34 -04:00
|
|
|
request-method dup "method" set swap
|
2005-05-23 19:14:29 -04:00
|
|
|
prepare-url prepare-header host ;
|
2004-08-31 20:31:16 -04:00
|
|
|
|
2004-08-10 22:11:48 -04:00
|
|
|
: 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
|
|
|
|
2004-08-10 22:11:48 -04:00
|
|
|
: parse-request ( request -- )
|
2005-08-31 21:06:13 -04:00
|
|
|
dup log-message
|
2004-08-10 22:11:48 -04:00
|
|
|
" " 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
|
|
|
] [
|
2004-08-10 22:11:48 -04:00
|
|
|
2drop bad-request
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-09-20 20:18:01 -04:00
|
|
|
: httpd ( port -- )
|
2006-10-19 22:41:20 -04:00
|
|
|
"Starting HTTP server on port " write dup . flush
|
2005-09-20 20:18:01 -04:00
|
|
|
\ httpd [
|
2005-06-18 21:15:07 -04:00
|
|
|
60000 stdio get set-timeout
|
2005-07-21 21:43:37 -04:00
|
|
|
readln [ parse-request ] when*
|
2005-09-20 20:18:01 -04:00
|
|
|
] with-server ;
|
2004-08-17 20:44:57 -04:00
|
|
|
|
2005-04-17 18:34:09 -04:00
|
|
|
: stop-httpd ( -- )
|
|
|
|
#! Stop the server.
|
2005-09-20 20:18:01 -04:00
|
|
|
\ httpd get stream-close ;
|