]> gitweb.factorcode.org Git - factor.git/commitdiff
http: clean up and fix some content encoding logic
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 13 Mar 2010 09:09:56 +0000 (22:09 +1300)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 13 Mar 2010 09:10:36 +0000 (22:10 +1300)
basis/http/client/client.factor
basis/http/http.factor
basis/http/server/server.factor
basis/mime/types/types.factor

index d69b1ed1f31e4d13c98daa1fe64bea42aadd45f9..2ce0ec9dfce9dcceea45b3b0a9f20fe84a74fde9 100644 (file)
@@ -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 )
index 54866c745b25f9883e62f5a581cc2e89b97942c0..46b67b53216e76f313a54745907eb212baa1117b 100644 (file)
@@ -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* ;
index bfb724ca9a8b3573de322d29156696d18dd354c5..acdd71d10d2e3541d6f9159e02f60363140f07b7 100644 (file)
@@ -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* ;
 
index 5693aa9162c451e9678bab641928a133a468aff9..90b856ef23c07390e7ee00a24012d08891735b00 100644 (file)
@@ -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 ? ;
+