http: clean up and fix some content encoding logic

db4
Slava Pestov 2010-03-13 22:09:56 +13:00
parent cb0e99a962
commit 937b05f1de
4 changed files with 18 additions and 7 deletions

View 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.pathnames io.encodings io.encodings.string io.encodings.ascii
io.encodings.utf8 io.encodings.binary io.encodings.iana io.crlf io.encodings.utf8 io.encodings.binary io.encodings.iana io.crlf
io.streams.duplex fry ascii urls urls.encoding present locals 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 IN: http.client
ERROR: too-many-redirects ; ERROR: too-many-redirects ;
@ -51,13 +51,18 @@ ERROR: too-many-redirects ;
read-crlf parse-response-line first3 read-crlf parse-response-line first3
[ >>version ] [ >>code ] [ >>message ] tri* ; [ >>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-response-header ( response -- response )
read-header >>header read-header >>header
dup "set-cookie" header parse-set-cookie >>cookies dup "set-cookie" header parse-set-cookie >>cookies
dup "content-type" header [ dup "content-type" header [
parse-content-type parse-content-type
[ >>content-type ] [ >>content-charset ] bi* [ >>content-type ] [ >>content-charset ] bi*
dup content-charset>> name>encoding >>content-encoding dup detect-encoding >>content-encoding
] when* ; ] when* ;
: read-response ( -- response ) : read-response ( -- response )

View File

@ -5,7 +5,7 @@ sequences splitting sorting sets strings vectors hashtables
quotations arrays byte-arrays math.parser calendar quotations arrays byte-arrays math.parser calendar
calendar.format present urls fry io io.encodings calendar.format present urls fry io io.encodings
io.encodings.iana io.encodings.binary io.encodings.utf8 io.crlf 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 IN: http
CONSTANT: max-redirects 10 CONSTANT: max-redirects 10
@ -223,4 +223,4 @@ TUPLE: post-data data params content-type content-encoding ;
: parse-content-type ( content-type -- type encoding ) : parse-content-type ( content-type -- type encoding )
";" split1 ";" split1
parse-content-type-attributes "charset" swap at parse-content-type-attributes "charset" swap at
[ dup "text/" head? "UTF-8" and ] unless* ; [ dup mime-type-encoding encoding>name ] unless* ;

View File

@ -26,6 +26,7 @@ http.server.remapping
html.templates html.templates
html.streams html.streams
html html
mime.types
xml.writer ; xml.writer ;
FROM: mime.multipart => parse-multipart ; FROM: mime.multipart => parse-multipart ;
IN: http.server IN: http.server
@ -102,7 +103,7 @@ GENERIC: write-full-response ( request response -- )
: unparse-content-type ( request -- content-type ) : unparse-content-type ( request -- content-type )
[ content-type>> ] [ content-charset>> ] bi [ content-type>> ] [ content-charset>> ] bi
over "text/" head? [ "UTF-8" or ] when over mime-type-encoding encoding>name or
[ "application/octet-stream" or ] dip [ "application/octet-stream" or ] dip
[ "; charset=" glue ] when* ; [ "; charset=" glue ] when* ;

View 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. ! 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 ; splitting kernel namespaces fry memoize ;
IN: mime.types IN: mime.types
@ -23,3 +24,7 @@ MEMO: mime-types ( -- assoc )
: mime-type ( filename -- mime-type ) : mime-type ( filename -- mime-type )
file-extension mime-types at "application/octet-stream" or ; file-extension mime-types at "application/octet-stream" or ;
: mime-type-encoding ( mime-type -- encoding )
"text/" head? utf8 binary ? ;