-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontent_type.mbt
More file actions
99 lines (91 loc) · 1.9 KB
/
content_type.mbt
File metadata and controls
99 lines (91 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
///|
priv struct ContentType {
media_type : StringView
subtype : StringView
params : Map[StringView, StringView]
} derive(Show)
///|
fn parse_content_type(s : StringView) -> ContentType? {
fn dequote(value : StringView) -> StringView {
if value is ['"', .. rest, '"'] {
rest
} else {
value
}
}
let params = Map::new()
let (media_type, subtype, rest) = lexmatch s with longest {
(
"[ \t]*"
("[^ \t/;=]+" as media_type)
"[ \t]*"
"/"
"[ \t]*"
("[^ \t/;=]+" as subtype)
"[ \t]*",
rest
) => (media_type, subtype, rest)
_ => return None
}
for curr = rest {
lexmatch curr with longest {
("[ \t]+", rest) => continue rest
(
";"
"[ \t]*"
("[^ \t=;]+" as key)
"[ \t]*"
"="
"[ \t]*"
("(\"[^\"]*\"|[^ \t;]*)" as value)
"[ \t]*",
rest
) => {
params.set(key, dequote(value))
continue rest
}
(";" "[ \t]*", rest) => continue rest
"" => break
_ => break
}
}
Some({ media_type, subtype, params })
}
///|
test "parse_content_type" {
inspect(
parse_content_type("application/json; charset=utf-8"),
content=(
#|Some({media_type: "application", subtype: "json", params: {"charset": "utf-8"}})
),
)
}
///|
test "parse_content_type_with_quoted_params" {
inspect(
parse_content_type(
"multipart/form-data; boundary=\"foo;bar\"; charset = utf-8",
),
content=(
#|Some({media_type: "multipart", subtype: "form-data", params: {"boundary": "foo;bar", "charset": "utf-8"}})
),
)
}
///|
test "parse_content_type_invalid" {
inspect(
parse_content_type("application"),
content=(
#|None
),
)
}
///|
test "parse_form_data" {
inspect(
parse_form_data(b"name=John+Doe&age=30"),
content=(
#|{"name": "John Doe", "age": "30"}
),
)
}