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
10 CONSTANT: buffer-size 65536
11 CONSTANT: separator-prefix "\r\n--"
15 current-separator mime-separator
17 content-disposition bytes
22 TUPLE: mime-file headers filename temporary-path ;
23 TUPLE: mime-variable headers key value ;
25 : <multipart> ( mime-separator -- multipart )
28 H{ } clone >>mime-parts ;
30 ERROR: bad-header bytes ;
32 : mime-write ( sequence -- )
35 : parse-headers ( string -- hashtable )
36 string-lines harvest [ parse-header-line ] map >hashtable ;
38 ERROR: end-of-stream multipart ;
40 : fill-bytes ( multipart -- multipart )
42 [ '[ _ append ] change-bytes ]
43 [ t >>end-of-stream? ] if* ;
45 : maybe-fill-bytes ( multipart -- multipart )
46 dup bytes>> length 256 < [ fill-bytes ] when ;
48 : split-bytes ( bytes separator -- leftover-bytes safe-to-dump )
49 dupd [ length ] bi@ 1 - - short cut-slice swap ;
51 : dump-until-separator ( multipart -- multipart )
53 [ current-separator>> ] [ bytes>> ] bi
54 [ nip ] [ start ] 2bi [
57 [ over current-separator>> length short tail-slice >>bytes ] bi*
60 dup [ bytes>> ] [ current-separator>> ] bi split-bytes mime-write
61 >>bytes fill-bytes dup end-of-stream?>> [ dump-until-separator ] unless
64 : dump-string ( multipart separator -- multipart string )
66 [ dump-until-separator ] with-string-writer ;
68 : read-header ( multipart -- multipart )
70 dup bytes>> "--\r\n" sequence= [
73 "\r\n\r\n" dump-string parse-headers >>header
76 : empty-name? ( string -- ? )
77 { "''" "\"\"" "" f } member? ;
79 : save-uploaded-file ( multipart -- )
80 dup filename>> empty-name? [
83 [ [ header>> ] [ filename>> ] [ temp-file>> ] tri mime-file boa ]
84 [ content-disposition>> "name" swap at unquote ]
85 [ mime-parts>> set-at ] tri
88 : save-mime-part ( multipart -- )
89 dup name>> empty-name? [
94 [ mime-parts>> set-at ] tri
97 : dump-mime-file ( multipart filename -- multipart )
98 binary <file-writer> [
99 dup mime-separator>> >>current-separator dump-until-separator
100 ] with-output-stream ;
102 : dump-file ( multipart -- multipart )
103 "factor-" "-upload" make-unique-file
104 [ >>temp-file ] [ dump-mime-file ] bi ;
106 : parse-content-disposition-form-data ( string -- hashtable )
108 [ "=" split1 [ [ blank? ] trim ] bi@ ] H{ } map>assoc ;
110 : lookup-disposition ( multipart string -- multipart value/f )
111 over content-disposition>> at ;
113 ERROR: unknown-content-disposition multipart ;
115 : parse-form-data ( multipart -- multipart )
116 "filename" lookup-disposition [
119 [ dump-file ] [ save-uploaded-file ] bi
121 "name" lookup-disposition [
122 [ dup mime-separator>> dump-string >>name-content ] dip
123 >>name dup save-mime-part
125 unknown-content-disposition
129 ERROR: no-content-disposition multipart ;
131 : process-header ( multipart -- multipart )
132 "content-disposition" over header>> at ";" split1 swap {
134 parse-content-disposition-form-data >>content-disposition
137 [ no-content-disposition ]
140 : read-assert-sequence= ( sequence -- )
141 [ length read ] keep assert-sequence= ;
143 : parse-beginning ( multipart -- multipart )
144 "--" read-assert-sequence=
146 [ read-assert-sequence= ]
147 [ separator-prefix prepend >>mime-separator ] bi ;
149 : parse-multipart-loop ( multipart -- multipart )
151 dup end-of-stream?>> [ process-header parse-multipart-loop ] unless ;
153 : parse-multipart ( separator -- mime-parts )
154 <multipart> parse-beginning fill-bytes parse-multipart-loop