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
157
158
159
//! RFC 6266 Content-Disposition header parsing.
//!
//! Parses `Content-Disposition` headers as defined in RFC 6266, extracting the
//! disposition type and filename parameters. Mirrors the C++ state-machine
//! parser in `aria2_original/src/util.cc` (`parse_content_disposition`) while
//! providing a more ergonomic Rust API.
//!
//! # Key rules (from RFC 6266 and the C++ implementation)
//!
//! - `filename*=charset'language'encoded_value` takes priority over `filename=`.
//! - `filename*=UTF-8''value` uses percent-encoding.
//! - `filename="quoted string"` strips surrounding quotes.
//! - `filename=unquoted` is also valid.
//! - Non-ASCII bytes in `filename=` are interpreted as UTF-8 (matching C++
//! `defaultUTF8` mode); ISO-8859-1 bytes are converted to UTF-8 when the
//! charset is explicitly `iso-8859-1`.
//! - Directory-traversal filenames (`..`, `/`, `\`, etc.) are rejected.
//!
//! # Strictness (C++ parity)
//!
//! The C++ state machine is deliberately strict, and so is this port. The
//! following are **rejected outright** — the whole header fails and
//! [`ContentDispositionResult::disposition_type`] comes back empty:
//!
//! - An empty parameter, including a trailing `;`, is rejected. The C++
//! terminal-state behavior is preserved: `attachment; ;filename=foo` and
//! `attachment; filename=foo.html;` both fail parsing.
//! - An empty unquoted value: `attachment; filename=` and
//! `attachment; filename=;` — a token may not be empty. An *empty quoted*
//! value (`filename=""`) is legal and simply yields no filename, as is an
//! empty ext-value (`filename*=UTF-8''`).
//! - A parameter with no `=`: `attachment; filename; x=y`.
//! - Two completed `filename=` parameters. Note that a `filename=` following an
//! already accepted `filename*=` is *ignored* rather than counted, so it can
//! never trigger the duplicate error (C++ leaves `in_file_parm == 0`).
//! - An ext-value whose octets do not match its declared charset: invalid UTF-8
//! under `utf-8`, or non-ISO-8859-1 octets under `iso-8859-1`. This applies to
//! *every* extended parameter, not just `filename*`.
use trace;
// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------
/// Result of parsing a `Content-Disposition` header value.
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/// Parse a `Content-Disposition` header value per RFC 6266.
///
/// Returns a `ContentDispositionResult` with the disposition type and
/// any filename parameters found. The `filename` field prefers `filename*`
/// over `filename=` as required by RFC 6266.
///
/// # Examples
///
/// ```
/// use aria2_protocol::http::content_disposition::parse_content_disposition;
///
/// let result = parse_content_disposition("attachment; filename=\"example.html\"");
/// assert_eq!(result.disposition_type, "attachment");
/// assert_eq!(result.filename.as_deref(), Some("example.html"));
/// ```
/// Parse a `Content-Disposition` value while choosing the legacy decoding rule
/// for the plain `filename=` parameter.
///
/// The extended `filename*=` form always uses the charset declared in the
/// header. `default_utf8` only affects a plain `filename=` value that has no
/// charset marker; `false` preserves aria2's historical ISO-8859-1 default.
/// Convenience function that extracts just the filename from a
/// `Content-Disposition` header value.
///
/// Returns `None` if no valid filename is found, or if the filename
/// contains directory-traversal components.
///
/// # Examples
///
/// ```
/// use aria2_protocol::http::content_disposition::extract_filename;
///
/// assert_eq!(
/// extract_filename("attachment; filename=\"report.pdf\""),
/// Some("report.pdf".to_owned())
/// );
/// assert_eq!(
/// extract_filename("inline"),
/// None
/// );
/// ```