Dillo v3.2.0-88-g47ab7c70
Loading...
Searching...
No Matches
disposition.c
Go to the documentation of this file.
1/*
2 * File: disposition.c
3 *
4 * Copyright 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "dlib/dlib.h"
21#include "src/misc.h"
22
23#include <stdlib.h>
24
25struct testcase {
26 const char *disposition;
27 const char *type;
28 const char *filename;
29};
30
31struct testcase cases[] = {
32 { "attachment; filename=/foo", "attachment", "_foo" },
33 { "attachment; filename=./../foo", "attachment", "_.._foo" },
34 { "attachment; filename=", "attachment", NULL },
35 { "attachment; filename=\"foo", "attachment", NULL },
36 { "attachment; filename= ", "attachment", NULL },
37 { "attachment; filename=\"", "attachment", NULL },
38 { "attachment; filename=\"\"", "attachment", NULL },
39 { "attachment; filename=\"a\"", "attachment", "a" },
40 { "attachment; filename=\"foo", "attachment", NULL },
41 { "attachment; filename=~/foo", "attachment", "__foo" },
42
43 /* Note that due to the rules for implied linear whitespace (Section 2.1 of
44 * [RFC2616]), OPTIONAL whitespace can appear between words (token or
45 * quoted-string) and separator characters. */
46 { "attachment ; filename=foo", "attachment", "foo" },
47 { "attachment; filename =foo", "attachment", "foo" },
48 { "attachment; filename= foo", "attachment", "foo" },
49 { "attachment; filename = foo", "attachment", "foo" },
50 { "attachment ; filename = foo", "attachment", "foo" },
51
52 /* See http://test.greenbytes.de/tech/tc2231/ */
53 { "inline", "inline", NULL },
54 { "\"inline\"", NULL, NULL },
55 { "inline; filename=\"foo.html\"", "inline", "foo.html" },
56 { "inline; filename=\"Not an attachment!\"", "inline", "Not an attachment!" },
57 { "inline; filename=\"foo.pdf\"", "inline", "foo.pdf" },
58 { "\"attachment\"", NULL, NULL },
59 { "attachment", "attachment", NULL },
60 { "ATTACHMENT", "ATTACHMENT", NULL },
61 { "attachment; filename=\"foo.html\"", "attachment", "foo.html" },
62 { "attachment; filename=\"0000000000111111111122222\"", "attachment", "0000000000111111111122222" },
63 { "attachment; filename=\"00000000001111111111222222222233333\"", "attachment", "00000000001111111111222222222233333" },
64 { "attachment; filename=\"f\\oo.html\"", "attachment", "f_oo.html" },
65 { "attachment; filename=\"\\\"quoting\\\" tested.html\"", "attachment", "\"quoting\" tested.html" },
66 { "attachment; filename=\"Here's a semicolon;.html\"", "attachment", "Here's a semicolon;.html" },
67 { "attachment; foo=\"bar\"; filename=\"foo.html\"", "attachment", "foo.html" },
68 { "attachment; foo=\"\\\"\\\\\";filename=\"foo.html\"", "attachment", "foo.html" },
69 { "attachment; FILENAME=\"foo.html\"", "attachment", "foo.html" },
70 { "attachment; filename=foo.html", "attachment", "foo.html" },
71 { "attachment; filename=foo,bar.html", "attachment", "foo,bar.html" },
72 { "attachment; filename=foo.html ;", "attachment", "foo.html" },
73 { "attachment; ;filename=foo", "attachment", "foo" },
74 { "attachment; filename=foo bar.html", "attachment", "foo" },
75 { "attachment; filename='foo.bar'", "attachment", "'foo.bar'" },
76 { "attachment; filename=\"foo-ä.html\"", "attachment", "foo-ä.html" },
77 { "attachment; filename=\"foo-ä.html\"", "attachment", "foo-ä.html" },
78 { "attachment; filename=\"foo-%41.html\"", "attachment", "foo-%41.html" },
79 { "attachment; filename=\"50%.html\"", "attachment", "50%.html" },
80 { "attachment; filename=\"foo-%\\41.html\"", "attachment", "foo-%_41.html" },
81 { "attachment; name=\"foo-%41.html\"", "attachment", NULL },
82 { "attachment; filename=\"ä-%41.html\"", "attachment", "ä-%41.html" },
83 { "attachment; filename=\"foo-%c3%a4-%e2%82%ac.html\"", "attachment", "foo-%c3%a4-%e2%82%ac.html" },
84 { "attachment; filename =\"foo.html\"", "attachment", "foo.html" },
85 { "attachment; filename=\"foo.html\"; filename=\"bar.html\"", "attachment", "foo.html" },
86 { "attachment; filename=foo[1](2).html", "attachment", "foo[1](2).html" },
87 { "attachment; filename=foo-ä.html", "attachment", NULL },
88 { "attachment; filename=filename=foo-ä.html", "attachment", NULL },
89 { "filename=foo.html", NULL, NULL },
90 { "x=y; filename=foo.html", NULL, NULL },
91 {"\"foo; filename=bar;baz\"; filename=qux", NULL, NULL },
92 { "filename=foo.html, filename=bar.html", NULL, NULL },
93 { "; filename=foo.html", NULL, NULL },
94 { ": inline; attachment; filename=foo.html", NULL, NULL },
95 { "inline; attachment; filename=foo.html", "inline", "foo.html" },
96 { "attachment; inline; filename=foo.html", "attachment", "foo.html" },
97 { "attachment; filename=\"foo.html\".txt", "attachment", "foo.html" },
98 { "attachment; filename=\"bar", "attachment", NULL },
99 { "attachment; filename=foo\"bar;baz\"qux", "attachment", "foo\"bar" },
100 { "attachment; filename=foo.html, attachment; filename=bar.html", "attachment", "foo.html," },
101 { "attachment; foo=foo filename=bar", "attachment", "bar" },
102 { "attachment; filename=bar foo=foo ", "attachment", "bar" },
103 { "attachment filename=bar", "attachment", NULL },
104 { "filename=foo.html; attachment", NULL, NULL },
105 { "attachment; xfilename=foo.html", "attachment", NULL },
106 { "attachment; filename=\"/foo.html\"", "attachment", "_foo.html" },
107 { "attachment; filename=\"\\\\foo.html\"", "attachment", "__foo.html" },
108 { "foobar", "foobar", NULL },
109 { "attachment; example=\"filename=example.txt\"", "attachment", NULL },
110 { "attachment; filename=\"foo-ae.html\"; filename*=UTF-8''foo-%c3%a4.html", "attachment", "foo-ae.html" },
111 { "attachment; filename==?ISO-8859-1?Q?foo-=E4.html?=", "attachment", NULL },
112 { "attachment; filename=\"=?ISO-8859-1?Q?foo-=E4.html?=\"", "attachment", "=?ISO-8859-1?Q?foo-=E4.html?=" },
113};
114
115static int equal(const char *a, const char *b)
116{
117 if (a == NULL && b == NULL)
118 return 1;
119
120 if (a == NULL || b == NULL)
121 return 0;
122
123 return strcmp(a, b) == 0;
124}
125
126int main(void)
127{
128 char dummy[] = "dummy";
129 int ncases = sizeof(cases) / sizeof(cases[0]);
130 int rc = 0;
131
132 for (int i = 0; i < ncases; i++) {
133 struct testcase *t = &cases[i];
134 char *type = dummy;
135 char *filename = dummy;
136 a_Misc_parse_content_disposition(t->disposition, &type, &filename);
137 if (!equal(type, t->type)) {
138 fprintf(stderr, "test %d failed, type mismatch:\n", i);
139 fprintf(stderr, " Content-Dispsition: %s\n", t->disposition);
140 fprintf(stderr, " Expected type: %s\n", t->type);
141 fprintf(stderr, " Computed type: %s\n", type);
142 rc = 1;
143 }
144
145 if (!equal(filename, t->filename)) {
146 fprintf(stderr, "test %d failed, filename mismatch:\n", i);
147 fprintf(stderr, " Content-Dispsition: %s\n", t->disposition);
148 fprintf(stderr, " Expected filename: %s\n", t->filename);
149 fprintf(stderr, " Computed filename: %s\n", filename);
150 rc = 1;
151 }
152
153 dFree(type);
154 dFree(filename);
155 }
156
157 return rc;
158}
static int equal(const char *a, const char *b)
struct testcase cases[]
Definition disposition.c:31
int main(void)
void dFree(void *mem)
Definition dlib.c:68
static void a_Misc_parse_content_disposition(const char *disposition, char **type, char **filename)
Parse Content-Disposition string, e.g., "attachment; filename="file name.jpg"".
Definition misc.h:30