! See http://factorcode.org/license.txt for BSD license.
USING: io.encodings.ascii io.files io.files.unique kernel
mime.multipart tools.test io.streams.duplex io multiline
-assocs ;
+assocs accessors ;
IN: mime.multipart.tests
: upload-separator ( -- seq )
[ t ] [
mime-test-stream [ upload-separator parse-multipart ] with-input-stream
- "\"file1\"" swap key?
+ "file1" swap key?
] unit-test
[ t ] [
mime-test-stream [ upload-separator parse-multipart ] with-input-stream
- "\"text1\"" swap key?
+ "file1" swap key?
+] unit-test
+
+[ t ] [
+ mime-test-stream [ upload-separator parse-multipart ] with-input-stream
+ "file1" swap at filename>> "up.txt" =
] unit-test
: empty-name? ( string -- ? )
{ "''" "\"\"" "" f } member? ;
+: quote? ( ch -- ? ) "'\"" member? ;
+
+: quoted? ( str -- ? )
+ {
+ [ length 1 > ]
+ [ first quote? ]
+ [ [ first ] [ peek ] bi = ]
+ } 1&& ;
+
+: unquote ( str -- newstr )
+ dup quoted? [ but-last-slice rest-slice >string ] when ;
+
: save-uploaded-file ( multipart -- )
dup filename>> empty-name? [
drop
] [
[ [ header>> ] [ filename>> ] [ temp-file>> ] tri mime-file boa ]
- [ content-disposition>> "name" swap at ]
+ [ content-disposition>> "name" swap at unquote ]
[ mime-parts>> set-at ] tri
] if ;
drop
] [
[ [ header>> ] [ name>> ] [ name-content>> ] tri mime-variable boa ]
- [ name>> ]
+ [ name>> unquote ]
[ mime-parts>> set-at ] tri
] if ;
: parse-form-data ( multipart -- multipart )
"filename" lookup-disposition [
+ unquote
>>filename
[ dump-file ] [ save-uploaded-file ] bi
] [