diff --git a/extra/http/client/client.factor b/extra/http/client/client.factor index e4bbf0279f..1c42b174d4 100755 --- a/extra/http/client/client.factor +++ b/extra/http/client/client.factor @@ -3,7 +3,7 @@ USING: assocs http kernel math math.parser namespaces sequences io io.sockets io.streams.string io.files io.timeouts strings splitting calendar continuations accessors vectors -io.encodings.8-bit io.encodings.binary fry ; +io.encodings.8-bit io.encodings.binary fry debugger ; IN: http.client DEFER: http-request @@ -61,20 +61,43 @@ PRIVATE> ] close-on-error ] with-variable ; +: read-chunks ( -- ) + readln ";" split1 drop hex> + dup { f 0 } member? [ drop ] [ read % read-chunks ] if ; + +: do-chunked-encoding ( response stream -- response stream/string ) + over "transfer-encoding" header "chunked" = [ + [ [ read-chunks ] "" make ] with-stream + ] when ; + : ( url -- request ) request-with-url "GET" >>method ; -: http-get-stream ( url -- response stream ) - http-request ; +: string-or-contents ( stream/string -- string ) + dup string? [ contents ] unless ; + +: http-get-stream ( url -- response stream/string ) + http-request do-chunked-encoding ; : success? ( code -- ? ) 200 = ; -: check-response ( response -- ) - code>> success? - [ "HTTP download failed" throw ] unless ; +ERROR: download-failed response body ; + +M: download-failed error. + "HTTP download failed:" print nl + [ + response>> + write-response-code + write-response-message nl + drop + ] + [ body>> write ] bi ; + +: check-response ( response string -- string ) + over code>> success? [ nip ] [ download-failed ] if ; : http-get ( url -- string ) - http-get-stream contents swap check-response ; + http-get-stream string-or-contents check-response ; : download-name ( url -- name ) file-name "?" split1 drop "/" ?tail drop ; @@ -95,4 +118,4 @@ PRIVATE> swap >>post-data-type ; : http-post ( content-type content url -- response string ) - http-request contents ; + http-request do-chunked-encoding string-or-contents ; diff --git a/extra/http/http.factor b/extra/http/http.factor index 9e31855e53..926336cae1 100755 --- a/extra/http/http.factor +++ b/extra/http/http.factor @@ -175,13 +175,17 @@ post-data post-data-type cookies ; +: set-header ( request/response value key -- request/response ) + pick header>> set-at ; + : request new "1.1" >>version http-port >>port H{ } clone >>header H{ } clone >>query - V{ } clone >>cookies ; + V{ } clone >>cookies + "close" "connection" set-header ; : query-param ( request key -- value ) swap query>> at ; @@ -330,9 +334,6 @@ SYMBOL: max-post-request tri ] with-string-writer ; -: set-header ( request/response value key -- request/response ) - pick header>> set-at ; - GENERIC: write-response ( response -- ) GENERIC: write-full-response ( request response -- ) @@ -347,11 +348,11 @@ body ; : response new - "1.1" >>version - H{ } clone >>header - "close" "connection" set-header - now timestamp>http-string "date" set-header - V{ } clone >>cookies ; + "1.1" >>version + H{ } clone >>header + "close" "connection" set-header + now timestamp>http-string "date" set-header + V{ } clone >>cookies ; : read-response-version " \t" read-until