httpd runs in native factor with no responders

cvs
Slava Pestov 2004-08-11 02:11:48 +00:00
parent d94e0bb97d
commit cda61358bf
6 changed files with 49 additions and 21 deletions

View File

@ -29,11 +29,11 @@ IN: httpd
USE: arithmetic USE: arithmetic
USE: combinators USE: combinators
USE: errors USE: errors
USE: kernel
USE: lists USE: lists
USE: logging USE: logging
USE: logic USE: logic
USE: namespaces USE: namespaces
USE: regexp
USE: stack USE: stack
USE: stdio USE: stdio
USE: streams USE: streams
@ -46,37 +46,38 @@ USE: url-encoding
"400 Bad request" httpd-error ; "400 Bad request" httpd-error ;
: url>path ( uri -- path ) : url>path ( uri -- path )
url-decode dup "http://.*?(/.*)" group1 dup [ url-decode dup "http://" str-head? dup [
nip "/" split1 f "" replace nip nip
] [ ] [
drop drop
] ifte ; ] ifte ;
: secure-path ( request -- path ) : secure-path ( path -- path )
dup [ ".." over str-contains? [ drop f ] when ;
"(.*?)( HTTP.*|)" group1 dup [
dup #".*\.\.+" re-matches [ drop f ] when
] when
] when ;
: httpd-request ( request -- ) : handle-request ( arg cmd -- )
[
[ "GET" = ] [ drop "get" serve-responder ]
[ "POST" = ] [ drop "post" serve-responder ]
[ drop t ] [ 2drop bad-request ]
] cond ;
: parse-request ( request -- )
dup log dup log
secure-path dup [ " " split1 dup [
url>path " HTTP" split1 drop url>path secure-path dup [
swap handle-request
[ ] [
[ "GET (.+)" | [ car "get" serve-responder ] ] 2drop bad-request
[ "POST (.+)" | [ car "post" serve-responder ] ] ] ifte
[ t | [ drop bad-request ] ]
] re-cond
] [ ] [
drop bad-request 2drop bad-request
] ifte ; ] ifte ;
: httpd-client ( socket -- ) : httpd-client ( socket -- )
[ [
"stdio" get "client" set log-client "stdio" get "client" set log-client
read [ httpd-request ] when* read [ parse-request ] when*
] with-stream ; ] with-stream ;
: quit-flag ( -- ? ) : quit-flag ( -- ? )

View File

@ -56,7 +56,7 @@ USE: httpd
"200 Document follows" "text/plain" response print ; "200 Document follows" "text/plain" response print ;
: get-responder ( name -- responder ) : get-responder ( name -- responder )
"httpd-responders" get [ get ] bind ; "httpd-responders" get get* ;
: responder-argument ( argument -- argument ) : responder-argument ( argument -- argument )
dup f-or-"" [ drop "default-argument" get ] when ; dup f-or-"" [ drop "default-argument" get ] when ;

View File

@ -29,6 +29,7 @@ IN: init
USE: combinators USE: combinators
USE: compiler USE: compiler
USE: continuations USE: continuations
USE: httpd-responder
USE: kernel USE: kernel
USE: lists USE: lists
USE: interpreter USE: interpreter
@ -68,6 +69,7 @@ USE: strings
init-environment init-environment
init-search-path init-search-path
init-scratchpad init-scratchpad
default-responders
"args" get parse-command-line "args" get parse-command-line
run-user-init run-user-init

View File

@ -82,6 +82,7 @@ primitives,
"/library/httpd/url-encoding.factor" "/library/httpd/url-encoding.factor"
"/library/httpd/http-common.factor" "/library/httpd/http-common.factor"
"/library/httpd/responder.factor" "/library/httpd/responder.factor"
"/library/httpd/httpd.factor"
"/library/math/arc-trig-hyp.factor" "/library/math/arc-trig-hyp.factor"
"/library/math/arithmetic.factor" "/library/math/arithmetic.factor"
"/library/math/list-math.factor" "/library/math/list-math.factor"

View File

@ -31,6 +31,7 @@ USE: arithmetic
USE: errors USE: errors
USE: combinators USE: combinators
USE: hashtables USE: hashtables
USE: httpd-responder
USE: kernel USE: kernel
USE: lists USE: lists
USE: logic USE: logic
@ -74,6 +75,7 @@ USE: unparser
init-scratchpad init-scratchpad
init-styles init-styles
init-vocab-styles init-vocab-styles
! default-responders
run-user-init run-user-init

View File

@ -41,6 +41,28 @@ USE: url-encoding
] with-logging ] with-logging
] unit-test ] unit-test
[ "index.html" ]
[ "http://www.jedit.org/index.html" url>path ] unit-test
[ "foo/bar" ]
[ "http://www.jedit.org/foo/bar" url>path ] unit-test
[ "" ]
[ "http://www.jedit.org/" url>path ] unit-test
[ "" ]
[ "http://www.jedit.org" url>path ] unit-test
[ "foobar" ]
[ "foobar" secure-path ] unit-test
[ f ]
[ "foobar/../baz" secure-path ] unit-test
[ ] [ "GET /index.html" parse-request ] unit-test
[ ] [ "GET ../index.html" parse-request ] unit-test
[ ] [ "POO" parse-request ] unit-test
[ ] [ "/" "get" ] [ serve-responder ] test-word [ ] [ "/" "get" ] [ serve-responder ] test-word
[ ] [ "" "get" ] [ serve-responder ] test-word [ ] [ "" "get" ] [ serve-responder ] test-word
[ ] [ "test" "get" ] [ serve-responder ] test-word [ ] [ "test" "get" ] [ serve-responder ] test-word