working on http server, word of the day

cvs
Slava Pestov 2004-08-11 01:32:10 +00:00
parent 4d036d397a
commit d94e0bb97d
10 changed files with 69 additions and 58 deletions

View File

@ -22,9 +22,11 @@
- vector-each/map examples - vector-each/map examples
- string construction examples - string construction examples
- string construction ackward - string construction ackward
- read#
+ tests: + tests:
- finish split
- java factor: equal numbers have non-equal hashcodes! - java factor: equal numbers have non-equal hashcodes!
- sbuf= - sbuf=
- vector-hashcode - vector-hashcode
@ -52,7 +54,6 @@ ERROR: I/O error: [ "primitive_read_line_fd_8" "Resource temporarily unavailable
- errors: don't show .factor-rc - errors: don't show .factor-rc
- handle division by zero - handle division by zero
- decide if overflow is a fatal error - decide if overflow is a fatal error
- f >n: crashes
- parsing should be parsing - parsing should be parsing
- describe-word - describe-word
- contains ==> contains? - contains ==> contains?

View File

@ -42,21 +42,23 @@ USE: vectors
: save-error ( error -- ) : save-error ( error -- )
#! Save the stacks and parser state for post-mortem #! Save the stacks and parser state for post-mortem
#! inspection after an error. #! inspection after an error.
"pos" get namespace [
"line" get "pos" get
"line-number" get "line" get
"parse-name" get "line-number" get
global [ "parse-name" get
"error-parse-name" set global [
"error-line-number" set "error-parse-name" set
"error-line" set "error-line-number" set
"error-pos" set "error-line" set
"error" set "error-pos" set
datastack >pop> "error-datastack" set "error" set
callstack >pop> >pop> "error-callstack" set datastack >pop> "error-datastack" set
namestack "error-namestack" set callstack >pop> >pop> "error-callstack" set
catchstack "error-catchstack" set namestack "error-namestack" set
] bind ; catchstack "error-catchstack" set
] bind
] when* ;
: catch ( try catch -- ) : catch ( try catch -- )
#! Call the try quotation. If an error occurs restore the #! Call the try quotation. If an error occurs restore the

View File

@ -45,7 +45,7 @@ USE: vectors
#! array index is determined using a hash function, and the #! array index is determined using a hash function, and the
#! buckets are associative lists which are searched #! buckets are associative lists which are searched
#! linearly. The number of buckets must be a power of two. #! linearly. The number of buckets must be a power of two.
dup <vector> dup >r set-vector-length r> ; empty-vector ;
: (hashcode) ( key table -- index ) : (hashcode) ( key table -- index )
#! Compute the index of the bucket for a key. #! Compute the index of the bucket for a key.

View File

@ -33,7 +33,6 @@ USE: lists
USE: logging USE: logging
USE: namespaces USE: namespaces
USE: parser USE: parser
USE: regexp
USE: stack USE: stack
USE: stdio USE: stdio
USE: streams USE: streams
@ -55,16 +54,15 @@ USE: url-encoding
dup log-error dup log-error
<% dup "text/html" response % error-body % %> print ; <% dup "text/html" response % error-body % %> print ;
: read-header-iter ( alist -- alist ) : header-line ( alist line -- alist )
read dup "" = [ ": " split1 dup [ transp acons ] [ 2drop ] ifte ;
drop
] [ : (read-header) ( alist -- alist )
"(.+?): (.+)" groups [ uncons car cons swons ] when* read dup
read-header-iter f-or-"" [ drop ] [ header-line (read-header) ] ifte ;
] ifte ;
: read-header ( -- alist ) : read-header ( -- alist )
[ ] read-header-iter ; [ ] (read-header) ;
: content-length ( alist -- length ) : content-length ( alist -- length )
"Content-Length" swap assoc dec> ; "Content-Length" swap assoc dec> ;

View File

@ -67,9 +67,6 @@ USE: httpd
: no-such-responder ( name -- ) : no-such-responder ( name -- )
"404 no such responder: " swap cat2 httpd-error ; "404 no such responder: " swap cat2 httpd-error ;
: bad-responder-query ( argument -- )
"404 missing parameter" httpd-error ;
: trim-/ ( url -- url ) : trim-/ ( url -- url )
#! Trim a leading /, if there is one. #! Trim a leading /, if there is one.
dup "/" str-head? dup [ nip ] [ drop ] ifte ; dup "/" str-head? dup [ nip ] [ drop ] ifte ;
@ -78,14 +75,9 @@ USE: httpd
"Calling responder " swap cat2 log ; "Calling responder " swap cat2 log ;
: serve-responder ( argument method -- ) : serve-responder ( argument method -- )
swap over log-responder
trim-/ swap trim-/ "/" split1 over get-responder dup [
dup "/" split1 dup [ rot drop call-responder
nip unswons dup get-responder dup [
swap log-responder call-responder
] [
drop nip nip no-such-responder
] ifte
] [ ] [
3drop bad-responder-query 2drop no-such-responder drop
] ifte ; ] ifte ;

View File

@ -80,6 +80,8 @@ primitives,
"/library/words.factor" "/library/words.factor"
"/library/httpd/html.factor" "/library/httpd/html.factor"
"/library/httpd/url-encoding.factor" "/library/httpd/url-encoding.factor"
"/library/httpd/http-common.factor"
"/library/httpd/responder.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"
@ -93,7 +95,6 @@ primitives,
"/library/platform/native/errors.factor" "/library/platform/native/errors.factor"
"/library/platform/native/io-internals.factor" "/library/platform/native/io-internals.factor"
"/library/platform/native/stream.factor" "/library/platform/native/stream.factor"
"/library/platform/native/kernel.factor"
"/library/platform/native/namespaces.factor" "/library/platform/native/namespaces.factor"
"/library/platform/native/strings.factor" "/library/platform/native/strings.factor"
"/library/platform/native/parse-numbers.factor" "/library/platform/native/parse-numbers.factor"
@ -105,6 +106,7 @@ primitives,
"/library/platform/native/stack.factor" "/library/platform/native/stack.factor"
"/library/platform/native/words.factor" "/library/platform/native/words.factor"
"/library/platform/native/vectors.factor" "/library/platform/native/vectors.factor"
"/library/platform/native/kernel.factor"
"/library/platform/native/vocabularies.factor" "/library/platform/native/vocabularies.factor"
"/library/platform/native/unparser.factor" "/library/platform/native/unparser.factor"
"/library/platform/native/cross-compiler.factor" "/library/platform/native/cross-compiler.factor"

View File

@ -127,18 +127,14 @@ USE: stack
swap [ str// ] dip split cons swap [ str// ] dip split cons
] ifte ; ] ifte ;
: split1 ( string split -- pair ) : split1 ( string split -- before after )
#! The car of the pair is the string up to the first #! The car of the pair is the string up to the first
#! occurrence of split; the cdr is the remainder of #! occurrence of split; the cdr is the remainder of
#! the string. #! the string.
dupd index-of dup -1 = [ 2dup index-of dup -1 = [
drop dup str-length 0 = [ 2drop f
drop f
] [
unit
] ifte
] [ ] [
str// cons swapd str/ rot str-length str/ nip
] ifte ; ] ifte ;
: max-str-length ( list -- len ) : max-str-length ( list -- len )

View File

@ -9,7 +9,5 @@ USE: test
! This should run without issue (and tests nothing useful) ! This should run without issue (and tests nothing useful)
! in Java Factor ! in Java Factor
! This was bloody stupid of me
"20 <sbuf> \"foo\" set" eval "20 <sbuf> \"foo\" set" eval
"garbage-collection" eval "garbage-collection" eval

View File

@ -1,24 +1,46 @@
IN: scratchpad IN: scratchpad
USE: httpd USE: httpd
USE: httpd-responder USE: httpd-responder
USE: logging
USE: namespaces
USE: stdio USE: stdio
USE: test USE: test
USE: url-encoding USE: url-encoding
"HTTPD tests" print [ "HTTP/1.0 404\nContent-Type: text/html\n" ]
[ "404" "text/html" response ] unit-test
[ "hello world" ] [ "hello+world" ] [ url-decode ] test-word [ 5430 ]
[ "hello world" ] [ "hello%20world" ] [ url-decode ] test-word [ f "Content-Length: 5430" header-line content-length ] unit-test
[ " ! " ] [ "%20%21%20" ] [ url-decode ] test-word
[ "hello world" ] [ "hello world%" ] [ url-decode ] test-word
[ "hello world" ] [ "hello world%x" ] [ url-decode ] test-word [ "hello world" ] [ "hello+world" url-decode ] unit-test
[ "hello%20world" ] [ "hello world" ] [ url-encode ] test-word [ "hello world" ] [ "hello%20world" url-decode ] unit-test
[ "%20%21%20" ] [ " ! " ] [ url-encode ] test-word [ " ! " ] [ "%20%21%20" url-decode ] unit-test
[ "hello world" ] [ "hello world%" url-decode ] unit-test
[ "hello world" ] [ "hello world%x" url-decode ] unit-test
[ "hello%20world" ] [ "hello world" url-encode ] unit-test
[ "%20%21%20" ] [ " ! " url-encode ] unit-test
! These make sure the words work, and don't leave ! These make sure the words work, and don't leave
! extra crap on the stakc ! extra crap on the stakc
[ ] [ "404 not found" ] [ httpd-error ] test-word [ ] [ "404 not found" ] [ httpd-error ] test-word
[ "arg" ] [
[
"arg" "default-argument" set
"" responder-argument
] with-scope
] unit-test
[ "inspect/global" ] [ "/inspect/global" trim-/ ] unit-test
[ ] [
[
"unit/test" log-responder
] with-logging
] 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

View File

@ -51,7 +51,7 @@ USE: vocabularies
"Running Factor test suite..." print "Running Factor test suite..." print
"vocabularies" get [ f "scratchpad" set ] bind "vocabularies" get [ f "scratchpad" set ] bind
[ [
"garbage-collection" "crashes"
"lists/cons" "lists/cons"
"lists/lists" "lists/lists"
"lists/assoc" "lists/assoc"