@@ -222,7 +222,7 @@ public String toString() {
222
222
if (this .filename != null ) {
223
223
if (this .charset == null || StandardCharsets .US_ASCII .equals (this .charset )) {
224
224
sb .append ("; filename=\" " );
225
- sb .append (this .filename ).append ('\"' );
225
+ sb .append (escapeQuotationsInFilename ( this .filename ) ).append ('\"' );
226
226
}
227
227
else {
228
228
sb .append ("; filename*=" );
@@ -428,6 +428,23 @@ private static boolean isRFC5987AttrChar(byte c) {
428
428
c == '.' || c == '^' || c == '_' || c == '`' || c == '|' || c == '~' ;
429
429
}
430
430
431
+ private static String escapeQuotationsInFilename (String filename ) {
432
+ if (filename .indexOf ('"' ) == -1 && filename .indexOf ('\\' ) == -1 ) {
433
+ return filename ;
434
+ }
435
+ boolean escaped = false ;
436
+ StringBuilder sb = new StringBuilder ();
437
+ for (char c : filename .toCharArray ()) {
438
+ sb .append ((c == '"' && !escaped ) ? "\\ \" " : c );
439
+ escaped = (!escaped && c == '\\' );
440
+ }
441
+ // Remove backslash at the end..
442
+ if (escaped ) {
443
+ sb .deleteCharAt (sb .length () - 1 );
444
+ }
445
+ return sb .toString ();
446
+ }
447
+
431
448
/**
432
449
* Encode the given header field param as describe in RFC 5987.
433
450
* @param input the header field param
@@ -437,13 +454,10 @@ private static boolean isRFC5987AttrChar(byte c) {
437
454
* @see <a href="https://tools.ietf.org/html/rfc5987">RFC 5987</a>
438
455
*/
439
456
private static String encodeFilename (String input , Charset charset ) {
440
- Assert .notNull (input , "Input String should not be null" );
441
- Assert .notNull (charset , "Charset should not be null" );
442
- if (StandardCharsets .US_ASCII .equals (charset )) {
443
- return input ;
444
- }
445
- Assert .isTrue (UTF_8 .equals (charset ) || ISO_8859_1 .equals (charset ),
446
- "Charset should be UTF-8 or ISO-8859-1" );
457
+ Assert .notNull (input , "`input` is required" );
458
+ Assert .notNull (charset , "`charset` is required" );
459
+ Assert .isTrue (!StandardCharsets .US_ASCII .equals (charset ), "ASCII does not require encoding" );
460
+ Assert .isTrue (UTF_8 .equals (charset ) || ISO_8859_1 .equals (charset ), "Only UTF-8 and ISO-8859-1 supported." );
447
461
byte [] source = input .getBytes (charset );
448
462
int len = source .length ;
449
463
StringBuilder sb = new StringBuilder (len << 1 );
@@ -578,25 +592,13 @@ public Builder name(String name) {
578
592
@ Override
579
593
public Builder filename (String filename ) {
580
594
Assert .hasText (filename , "No filename" );
581
- this .filename = escapeQuotationMarks ( filename ) ;
595
+ this .filename = filename ;
582
596
return this ;
583
597
}
584
598
585
- private static String escapeQuotationMarks (String filename ) {
586
- if (filename .indexOf ('"' ) == -1 ) {
587
- return filename ;
588
- }
589
- boolean escaped = false ;
590
- StringBuilder sb = new StringBuilder ();
591
- for (char c : filename .toCharArray ()) {
592
- sb .append ((c == '"' && !escaped ) ? "\\ \" " : c );
593
- escaped = (!escaped && c == '\\' );
594
- }
595
- return sb .toString ();
596
- }
597
-
598
599
@ Override
599
600
public Builder filename (String filename , Charset charset ) {
601
+ Assert .hasText (filename , "No filename" );
600
602
this .filename = filename ;
601
603
this .charset = charset ;
602
604
return this ;
0 commit comments