1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: multiline kernel sequences io splitting fry namespaces
4 http.parsers hashtables assocs combinators ascii io.files.unique
5 accessors io.encodings.binary io.files byte-arrays math
6 io.streams.string combinators.short-circuit strings math.order ;
9 CONSTANT: buffer-size 65536
10 CONSTANT: separator-prefix "\r\n--"
14 current-separator mime-separator
16 content-disposition bytes
21 TUPLE: mime-file headers filename temporary-path ;
22 TUPLE: mime-variable headers key value ;
24 : <multipart> ( mime-separator -- multipart )
27 H{ } clone >>mime-parts ;
29 ERROR: bad-header bytes ;
31 : mime-write ( sequence -- )
34 : parse-headers ( string -- hashtable )
35 string-lines harvest [ parse-header-line ] map >hashtable ;
37 ERROR: end-of-stream multipart ;
39 : fill-bytes ( multipart -- multipart )
41 [ '[ _ append ] change-bytes ]
42 [ t >>end-of-stream? ] if* ;
44 : maybe-fill-bytes ( multipart -- multipart )
45 dup bytes>> [ fill-bytes ] unless ;
47 : split-bytes ( bytes separator -- leftover-bytes safe-to-dump )
48 dupd [ length ] bi@ 1- - short cut-slice swap ;
50 : dump-until-separator ( multipart -- multipart )
52 [ current-separator>> ] [ bytes>> ] bi
53 [ nip ] [ start ] 2bi [
56 [ over current-separator>> length short tail-slice >>bytes ] bi*
59 dup [ bytes>> ] [ current-separator>> ] bi split-bytes mime-write
60 >>bytes fill-bytes dup end-of-stream?>> [ dump-until-separator ] unless
63 : dump-string ( multipart separator -- multipart string )
65 [ dump-until-separator ] with-string-writer ;
67 : read-header ( multipart -- multipart )
68 dup bytes>> "--\r\n" sequence= [
71 "\r\n\r\n" dump-string parse-headers >>header
74 : empty-name? ( string -- ? )
75 { "''" "\"\"" "" f } member? ;
77 : quote? ( ch -- ? ) "'\"" member? ;
79 : quoted? ( str -- ? )
83 [ [ first ] [ peek ] bi = ]
86 : unquote ( str -- newstr )
87 dup quoted? [ but-last-slice rest-slice >string ] when ;
89 : save-uploaded-file ( multipart -- )
90 dup filename>> empty-name? [
93 [ [ header>> ] [ filename>> ] [ temp-file>> ] tri mime-file boa ]
94 [ content-disposition>> "name" swap at unquote ]
95 [ mime-parts>> set-at ] tri
98 : save-mime-part ( multipart -- )
99 dup name>> empty-name? [
102 [ [ header>> ] [ name>> unquote ] [ name-content>> ] tri mime-variable boa ]
104 [ mime-parts>> set-at ] tri
107 : dump-mime-file ( multipart filename -- multipart )
108 binary <file-writer> [
109 dup mime-separator>> >>current-separator dump-until-separator
110 ] with-output-stream ;
112 : dump-file ( multipart -- multipart )
113 "factor-" "-upload" make-unique-file
114 [ >>temp-file ] [ dump-mime-file ] bi ;
116 : parse-content-disposition-form-data ( string -- hashtable )
118 [ "=" split1 [ [ blank? ] trim ] bi@ ] H{ } map>assoc ;
120 : lookup-disposition ( multipart string -- multipart value/f )
121 over content-disposition>> at ;
123 ERROR: unknown-content-disposition multipart ;
125 : parse-form-data ( multipart -- multipart )
126 "filename" lookup-disposition [
129 [ dump-file ] [ save-uploaded-file ] bi
131 "name" lookup-disposition [
132 [ dup mime-separator>> dump-string >>name-content ] dip
133 >>name dup save-mime-part
135 unknown-content-disposition
139 ERROR: no-content-disposition multipart ;
141 : process-header ( multipart -- multipart )
142 "content-disposition" over header>> at ";" split1 swap {
144 parse-content-disposition-form-data >>content-disposition
147 [ no-content-disposition ]
150 : assert-sequence= ( a b -- )
151 2dup sequence= [ 2drop ] [ assert ] if ;
153 : read-assert-sequence= ( sequence -- )
154 [ length read ] keep assert-sequence= ;
156 : parse-beginning ( multipart -- multipart )
157 "--" read-assert-sequence=
159 [ read-assert-sequence= ]
160 [ separator-prefix prepend >>mime-separator ] bi ;
162 : parse-multipart-loop ( multipart -- multipart )
164 dup end-of-stream?>> [ process-header parse-multipart-loop ] unless ;
166 : parse-multipart ( separator -- mime-parts )
167 <multipart> parse-beginning fill-bytes parse-multipart-loop