1
1
/*
2
- * Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2010, 2024 Oracle and/or its affiliates. All rights reserved.
3
3
*
4
4
* This program and the accompanying materials are made available under the
5
5
* terms of the Eclipse Public License v. 2.0, which is available at
28
28
import org .glassfish .jersey .message .internal .HttpHeaderReader ;
29
29
import org .glassfish .jersey .uri .UriComponent ;
30
30
31
- import javax .ws .rs .core .HttpHeaders ;
32
-
33
31
/**
34
32
* A content disposition header.
35
33
*
@@ -60,10 +58,13 @@ public class ContentDisposition {
60
58
private static final Pattern FILENAME_VALUE_CHARS_PATTERN =
61
59
Pattern .compile ("(%[a-f0-9]{2}|[a-z0-9!#$&+.^_`|~-])+" , Pattern .CASE_INSENSITIVE );
62
60
61
+ private static final char QUOTE = '"' ;
62
+ private static final char BACK_SLASH = '\\' ;
63
+
63
64
protected ContentDisposition (final String type , final String fileName , final Date creationDate ,
64
65
final Date modificationDate , final Date readDate , final long size ) {
65
66
this .type = type ;
66
- this .fileName = fileName ;
67
+ this .fileName = encodeAsciiFileName ( fileName ) ;
67
68
this .creationDate = creationDate ;
68
69
this .modificationDate = modificationDate ;
69
70
this .readDate = readDate ;
@@ -211,6 +212,23 @@ protected void addLongParameter(final StringBuilder sb, final String name, final
211
212
}
212
213
}
213
214
215
+ protected String encodeAsciiFileName (String fileName ) {
216
+ if (fileName == null
217
+ || (fileName .indexOf (QUOTE ) == -1
218
+ && fileName .indexOf (BACK_SLASH ) == -1 )) {
219
+ return fileName ;
220
+ }
221
+ final char [] chars = fileName .toCharArray ();
222
+ final StringBuilder encodedBuffer = new StringBuilder ();
223
+ for (char c : chars ) {
224
+ if (c == QUOTE || c == BACK_SLASH ) {
225
+ encodedBuffer .append (BACK_SLASH );
226
+ }
227
+ encodedBuffer .append (c );
228
+ }
229
+ return encodedBuffer .toString ();
230
+ }
231
+
214
232
private void createParameters () throws ParseException {
215
233
defineFileName ();
216
234
@@ -229,7 +247,7 @@ private void defineFileName() throws ParseException {
229
247
final String fileNameExt = parameters .get ("filename*" );
230
248
231
249
if (fileNameExt == null ) {
232
- this .fileName = fileName ;
250
+ this .fileName = encodeAsciiFileName ( fileName ) ;
233
251
return ;
234
252
}
235
253
0 commit comments