11/*
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.
33 *
44 * This program and the accompanying materials are made available under the
55 * terms of the Eclipse Public License v. 2.0, which is available at
2828import org .glassfish .jersey .message .internal .HttpHeaderReader ;
2929import org .glassfish .jersey .uri .UriComponent ;
3030
31- import javax .ws .rs .core .HttpHeaders ;
32-
3331/**
3432 * A content disposition header.
3533 *
@@ -60,10 +58,13 @@ public class ContentDisposition {
6058 private static final Pattern FILENAME_VALUE_CHARS_PATTERN =
6159 Pattern .compile ("(%[a-f0-9]{2}|[a-z0-9!#$&+.^_`|~-])+" , Pattern .CASE_INSENSITIVE );
6260
61+ private static final char QUOTE = '"' ;
62+ private static final char BACK_SLASH = '\\' ;
63+
6364 protected ContentDisposition (final String type , final String fileName , final Date creationDate ,
6465 final Date modificationDate , final Date readDate , final long size ) {
6566 this .type = type ;
66- this .fileName = fileName ;
67+ this .fileName = encodeAsciiFileName ( fileName ) ;
6768 this .creationDate = creationDate ;
6869 this .modificationDate = modificationDate ;
6970 this .readDate = readDate ;
@@ -211,6 +212,23 @@ protected void addLongParameter(final StringBuilder sb, final String name, final
211212 }
212213 }
213214
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+
214232 private void createParameters () throws ParseException {
215233 defineFileName ();
216234
@@ -229,7 +247,7 @@ private void defineFileName() throws ParseException {
229247 final String fileNameExt = parameters .get ("filename*" );
230248
231249 if (fileNameExt == null ) {
232- this .fileName = fileName ;
250+ this .fileName = encodeAsciiFileName ( fileName ) ;
233251 return ;
234252 }
235253
0 commit comments