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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use percent_encoding::{percent_encode, PATH_SEGMENT_ENCODE_SET, EncodeSet};
use std::fmt;
use std::borrow::Cow;
#[derive(PartialEq, Debug)]
pub enum DispositionType {
Inline,
Attachment,
}
#[derive(Debug)]
pub enum Filename {
Name(Option<String>),
Extended(String, Option<String>, Vec<u8>)
}
impl Filename {
pub fn new() -> Self {
Filename::Name(None)
}
pub fn with_name(name: String) -> Self {
Filename::Name(Some(name))
}
pub fn with_encoded_name(name: Cow<str>) -> Self {
let is_non_ascii = name.as_bytes().iter().any(|byte| PATH_SEGMENT_ENCODE_SET.contains(*byte));
match is_non_ascii {
false => Self::with_name(name.into_owned()),
true => {
let bytes = match name {
Cow::Borrowed(name) => name.as_bytes().into(),
Cow::Owned(name) => name.into_bytes(),
};
Filename::Extended("utf-8".to_owned(), None, bytes)
}
}
}
pub fn with_extended(charset: String, lang: Option<String>, name: Vec<u8>) -> Self {
Filename::Extended(charset, lang, name)
}
#[inline]
pub fn is_extended(&self) -> bool {
match self {
Filename::Extended(_, _, _) => true,
_ => false
}
}
}
#[derive(Debug)]
pub enum ContentDisposition {
Inline,
Attachment(Filename),
}
impl fmt::Display for ContentDisposition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ContentDisposition::Inline => write!(f, "inline"),
ContentDisposition::Attachment(file) => match file {
Filename::Name(Some(name)) => write!(f, "attachment; filename=\"{}\"", name),
Filename::Name(None) => write!(f, "attachment"),
Filename::Extended(charset, lang, value) => {
write!(f, "attachment; filename*={}'{}'{}",
charset,
lang.as_ref().map(|lang| lang.as_str()).unwrap_or(""),
percent_encode(&value, PATH_SEGMENT_ENCODE_SET).to_string())
},
},
}
}
}
#[cfg(test)]
mod tests {
use super::{ContentDisposition, Filename};
#[test]
fn parse_file_name_extended_ascii() {
const INPUT: &'static str = "rori.mp4";
let file_name = Filename::with_encoded_name(INPUT.into());
assert!(!file_name.is_extended());
}
#[test]
fn parse_file_name_extended_non_ascii() {
const INPUT: &'static str = "ロリへんたい.mp4";
let file_name = Filename::with_encoded_name(INPUT.into());
assert!(file_name.is_extended());
}
#[test]
fn verify_content_disposition_display() {
let cd = ContentDisposition::Inline;
let cd = format!("{}", cd);
assert_eq!(cd, "inline");
let cd = ContentDisposition::Attachment(Filename::new());
let cd = format!("{}", cd);
assert_eq!(cd, "attachment");
let cd = ContentDisposition::Attachment(Filename::with_name("lolka".to_string()));
let cd = format!("{}", cd);
assert_eq!(cd, "attachment; filename=\"lolka\"");
let cd = ContentDisposition::Attachment(Filename::with_encoded_name("lolka".into()));
let cd = format!("{}", cd);
assert_eq!(cd, "attachment; filename=\"lolka\"");
let cd = ContentDisposition::Attachment(Filename::with_encoded_name("ロリ".into()));
let cd = format!("{}", cd);
assert_eq!(cd, "attachment; filename*=utf-8\'\'%E3%83%AD%E3%83%AA");
}
}