factor/library/httpd/httpd.factor

71 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 httpd-responder kernel lists logging namespaces
stdio streams strings threads url-encoding ;
2004-07-16 02:26:21 -04:00
: httpd-log-stream ( -- stream )
#! Set httpd-log-file to save httpd log to a file.
"httpd-log-file" get dup [
<file-reader>
] [
2004-11-29 23:14:12 -05:00
drop stdio get
] ifte ;
2004-07-16 02:26:21 -04:00
: url>path ( uri -- path )
url-decode "http://" ?str-head [
2004-12-18 23:35:20 -05:00
"/" split1 dup "" ? nip
] when ;
2004-07-16 02:26:21 -04:00
: secure-path ( path -- path )
".." over str-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 -- )
[
[
stdio get log-client read [ 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 ] forever ;
2004-07-16 02:26:21 -04:00
: (httpd) ( port -- )
<server> "http-server" set [
httpd-loop
] [
"http-server" get fclose rethrow
] catch ;
2004-07-16 02:26:21 -04:00
: httpd ( port -- )
[ httpd-log-stream "log" set (httpd) ] with-scope ;