     request get url>> url-addr ascii <client> drop
     1 minutes over set-timeout ;
 
-: (with-http-request) ( request quot: ( chunk -- ) -- response )
+: with-http-request ( request quot: ( chunk -- ) -- response )
     swap
     request [
         <request-socket> [


<PRIVATE

DEFER: (with-http-request)

SYMBOL: redirects



: success? ( code -- ? ) 200 299 between? ;

ERROR: download-failed response data ;

M: download-failed error.
    "HTTP request failed:" print nl
    [ response>> . ] [ data>> . ] bi ;

: check-response* ( response data -- response data )
    over code>> success? [ download-failed ] unless ;

: check-response ( response -- response )
    f check-response* drop ;

: http-request ( request -- response data )
    [ [ % ] with-http-request ] B{ } make
    over content-charset>> decode check-response* ;

: <client-request> ( url -- request )
    <request> swap >url ensure-port >>url ;
