diff --git a/basis/http/client/client.factor b/basis/http/client/client.factor index d69b1ed1f3..2ce0ec9dfc 100644 --- a/basis/http/client/client.factor +++ b/basis/http/client/client.factor @@ -7,7 +7,7 @@ io io.sockets io.streams.string io.files io.timeouts io.pathnames io.encodings io.encodings.string io.encodings.ascii io.encodings.utf8 io.encodings.binary io.encodings.iana io.crlf io.streams.duplex fry ascii urls urls.encoding present locals -http http.parsers http.client.post-data ; +http http.parsers http.client.post-data mime.types ; IN: http.client ERROR: too-many-redirects ; @@ -51,13 +51,18 @@ ERROR: too-many-redirects ; read-crlf parse-response-line first3 [ >>version ] [ >>code ] [ >>message ] tri* ; +: detect-encoding ( response -- encoding ) + [ content-charset>> name>encoding ] + [ content-type>> mime-type-encoding ] bi + or ; + : read-response-header ( response -- response ) read-header >>header dup "set-cookie" header parse-set-cookie >>cookies dup "content-type" header [ parse-content-type [ >>content-type ] [ >>content-charset ] bi* - dup content-charset>> name>encoding >>content-encoding + dup detect-encoding >>content-encoding ] when* ; : read-response ( -- response ) diff --git a/basis/http/http.factor b/basis/http/http.factor index 54866c745b..46b67b5321 100644 --- a/basis/http/http.factor +++ b/basis/http/http.factor @@ -5,7 +5,7 @@ sequences splitting sorting sets strings vectors hashtables quotations arrays byte-arrays math.parser calendar calendar.format present urls fry io io.encodings io.encodings.iana io.encodings.binary io.encodings.utf8 io.crlf -ascii io.encodings.8-bit.latin1 http.parsers base64 ; +ascii io.encodings.8-bit.latin1 http.parsers base64 mime.types ; IN: http CONSTANT: max-redirects 10 @@ -223,4 +223,4 @@ TUPLE: post-data data params content-type content-encoding ; : parse-content-type ( content-type -- type encoding ) ";" split1 parse-content-type-attributes "charset" swap at - [ dup "text/" head? "UTF-8" and ] unless* ; + [ dup mime-type-encoding encoding>name ] unless* ; diff --git a/basis/http/server/server.factor b/basis/http/server/server.factor index bfb724ca9a..acdd71d10d 100644 --- a/basis/http/server/server.factor +++ b/basis/http/server/server.factor @@ -26,6 +26,7 @@ http.server.remapping html.templates html.streams html +mime.types xml.writer ; FROM: mime.multipart => parse-multipart ; IN: http.server @@ -102,7 +103,7 @@ GENERIC: write-full-response ( request response -- ) : unparse-content-type ( request -- content-type ) [ content-type>> ] [ content-charset>> ] bi - over "text/" head? [ "UTF-8" or ] when + over mime-type-encoding encoding>name or [ "application/octet-stream" or ] dip [ "; charset=" glue ] when* ; diff --git a/basis/mime/types/types.factor b/basis/mime/types/types.factor index 5693aa9162..90b856ef23 100644 --- a/basis/mime/types/types.factor +++ b/basis/mime/types/types.factor @@ -1,6 +1,7 @@ -! Copyright (C) 2004, 2008 Slava Pestov. +! Copyright (C) 2004, 2010 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: io.pathnames io.files io.encodings.ascii assocs sequences +USING: io.pathnames io.files io.encodings.ascii +io.encodings.binary io.encodings.utf8 assocs sequences splitting kernel namespaces fry memoize ; IN: mime.types @@ -23,3 +24,7 @@ MEMO: mime-types ( -- assoc ) : mime-type ( filename -- mime-type ) file-extension mime-types at "application/octet-stream" or ; + +: mime-type-encoding ( mime-type -- encoding ) + "text/" head? utf8 binary ? ; +