Smarter download word, old download word renamed to download-to for Ed

db4
Slava Pestov 2008-01-31 01:15:28 -06:00
parent f77526714f
commit 14481db63f
2 changed files with 21 additions and 2 deletions

5
extra/http/client/client-tests.factor Normal file → Executable file
View File

@ -7,3 +7,8 @@ USING: http.client tools.test ;
[ 404 ] [ "404 File not found" parse-response ] unit-test
[ 200 ] [ "HTTP/1.0 200" parse-response ] unit-test
[ 200 ] [ "HTTP/1.0 200 Success" parse-response ] unit-test
[ "foo.txt" ] [ "http://www.paulgraham.com/foo.txt" download-name ] unit-test
[ "foo.txt" ] [ "http://www.arcsucks.com/foo.txt?xxx" download-name ] unit-test
[ "foo.txt" ] [ "http://www.arcsucks.com/foo.txt/" download-name ] unit-test
[ "www.arcsucks.com" ] [ "http://www.arcsucks.com////" download-name ] unit-test

View File

@ -59,9 +59,23 @@ DEFER: http-get-stream
http-get-stream [ stdio get contents ] with-stream
] with-scope ;
: download ( url file -- )
: download-name ( url -- name )
file-name "?" split1 drop "/" ?tail drop ;
: default-timeout 60 1000 * over set-timeout ;
: success? ( code -- ? ) 200 = ;
: download-to ( url file -- )
#! Downloads the contents of a URL to a file.
>r http-get 2nip r> <file-writer> [ write ] with-stream ;
>r http-get-stream nip default-timeout swap success? [
r> <file-writer> stream-copy
] [
r> drop dispose "HTTP download failed" throw
] if ;
: download ( url -- )
dup download-name download-to ;
: post-request ( content-type content host resource -- )
#! Note: It is up to the caller to url encode the content if