0 | module HTTP.FormData
 1 |
 2 | import Data.Buffer
 3 | import Data.ByteString
 4 | import Data.List
 5 | import Data.SortedMap
 6 | import HTTP.Header
 7 | import System.File
 8 |
 9 | %default total
10 |
11 | public export
12 | record FDPart where
13 |   constructor FDP
14 |   headers : Headers
15 |   name    : String
16 |   content : ByteString
17 |
18 | public export
19 | 0 FormData : Type
20 | FormData = List FDPart
21 |
22 | crlf : ByteString
23 | crlf = "\r\n"
24 |
25 | crlf2 : ByteString
26 | crlf2 = "\r\n\r\n"
27 |
28 | part : ByteString -> Maybe FDPart
29 | part bs = Prelude.do
30 |   guard (not $ isPrefixOf "--" bs || size bs == 0)
31 |   let (_,r1) := breakDropAtSubstring crlf bs
32 |       n      := substringIndex crlf2.repr r1.repr
33 |   (rh,r2)    <- splitAt (n.fst + crlf2.size) r1
34 |   h          <- parseHeadersMay rh
35 |   cd         <- contentDisposition h
36 |   nm         <- parameter "name" cd.params
37 |   Just $ FDP h nm r2
38 |
39 | export
40 | multipart : (sep : ByteString) -> ByteString -> FormData
41 | multipart sep bs =
42 |   let sepBS := "--" <+> sep
43 |    in mapMaybe part (splitAtSubstring sepBS bs)
44 |