Smarter download word, old download word renamed to download-to for Ed
parent
f77526714f
commit
14481db63f
|
@ -7,3 +7,8 @@ USING: http.client tools.test ;
|
||||||
[ 404 ] [ "404 File not found" parse-response ] unit-test
|
[ 404 ] [ "404 File not found" parse-response ] unit-test
|
||||||
[ 200 ] [ "HTTP/1.0 200" parse-response ] unit-test
|
[ 200 ] [ "HTTP/1.0 200" parse-response ] unit-test
|
||||||
[ 200 ] [ "HTTP/1.0 200 Success" 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
|
||||||
|
|
|
@ -59,9 +59,23 @@ DEFER: http-get-stream
|
||||||
http-get-stream [ stdio get contents ] with-stream
|
http-get-stream [ stdio get contents ] with-stream
|
||||||
] with-scope ;
|
] 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.
|
#! 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 -- )
|
: post-request ( content-type content host resource -- )
|
||||||
#! Note: It is up to the caller to url encode the content if
|
#! Note: It is up to the caller to url encode the content if
|
||||||
|
|
Loading…
Reference in New Issue