aboutsummaryrefslogtreecommitdiff
path: root/src/misc.c
diff options
context:
space:
mode:
authorRodrigo Arias Mallo <rodarima@gmail.com>2025-04-05 02:03:12 +0200
committerRodrigo Arias Mallo <rodarima@gmail.com>2025-05-01 00:56:42 +0200
commitfcfec5c1bc9e786ab240f4b8d198b2e8ea15a9d5 (patch)
tree5efca97e0dac316d2702cafdfaf67900687c4bd2 /src/misc.c
parentcaa7c4a4bebae237ceaf7f4a8badaf10c5c680e7 (diff)
Move a_Misc_parse_content_disposition() to misc.h
For now it allows building the unit test without linking problems. A more long-term solution is to split the code into separate modules that can be tested more easily.
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c95
1 files changed, 0 insertions, 95 deletions
diff --git a/src/misc.c b/src/misc.c
index 1791e4c7..9129f819 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -354,101 +354,6 @@ int a_Misc_content_type_check(const char *EntryType, const char *DetectedType)
return st;
}
-
-/**
- * Parse Content-Disposition string, e.g., "attachment; filename="file name.jpg"".
- * Content-Disposition is defined in RFC 6266
- */
-void a_Misc_parse_content_disposition(const char *disposition, char **type, char **filename)
-{
- static const char tspecials_space[] = "()<>@,;:\\\"/[]?= ";
- const char terminators[] = " ;\t";
- const char *str, *s;
-
- if (type)
- *type = NULL;
- if (filename)
- *filename = NULL;
- if (!(str = disposition))
- return;
-
- for (s = str; *s && d_isascii((uchar_t)*s) && !iscntrl((uchar_t)*s) &&
- !strchr(tspecials_space, *s); s++) ;
- if (type && !(s == str))
- *type = dStrndup(str, s - str);
-
- if (!*type) {
- return;
- }
-
- if (!strchr(terminators, *s)) {
- *type = NULL;
- return;
- }
-
- if (*s == ';') {
- bool_t quoted = FALSE;
- const char key[] = "filename";
-
- if ((s = dStriAsciiStr(str, key)) &&
- (s == str || strchr(terminators, s[-1]))) {
- s += sizeof(key) - 1;
- for ( ; *s == ' ' || *s == '\t'; ++s);
- if (*s == '=') {
- size_t len = 0;
- for (++s; *s == ' ' || *s == '\t'; ++s);
- if (*s == '"') {
- quoted = TRUE;
- s++;
- for ( ; *s == '.'; ++s);
- bool_t escaped = FALSE;
- const char *c;
- unsigned int maxlen = strlen(s);
- for (c = s; !(*c == '"' && !escaped); c++) {
- if ((len = c - s + 1) > maxlen) {
- return;
- }
- escaped = *c == '\\';
- }
- *filename = dStrndup(s, len);
- } else {
- for ( ; *s == '.'; ++s);
- if ((len = strcspn(s, terminators))) {
- *filename = dStrndup(s, len);
- }
- }
-
- const char invalid_characters[] = "/\\|~";
- char *s = *filename, *d = *filename;
-
- for ( ; s < *filename + len; s++) {
- if (strchr(invalid_characters, *s)) {
- // If this is a backslash preceding a quote, we want to just
- // skip past it without advancing the destination pointer or
- // copying anything.
- if (!(*s == '\\' && *(s+1) == '"')) {
- *d = '_';
- d++;
- }
- } else if (!quoted && (!d_isascii((uchar_t)*s) || *s == '=')) {
- *filename = NULL;
- return;
- } else {
- *d = *s;
- d++;
- }
- }
-
- // Truncate filename to deal with the string being shorter if we
- // skipped over any backslash characters in the above loop
- if (s != d) {
- *d = '\0';
- }
- }
- }
- }
-}
-
/**
* Parse a geometry string.
*/