Skip to content

Commit 1c9653b

Browse files
authored
Merge branch 'master' into cors-from-client-cfg
2 parents be899c3 + 78fe760 commit 1c9653b

File tree

4 files changed

+89
-17
lines changed

4 files changed

+89
-17
lines changed

java/src/main/java/com/genexus/internet/HttpResponse.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.genexus.com.IHttpResponse;
1212
import com.genexus.webpanels.FileItemCollection;
1313
import com.genexus.webpanels.HttpContextWeb;
14+
import com.genexus.webpanels.WebUtils;
1415

1516
/**
1617
* Esta clase esta disponible en los webprocs para grabar informacion en el response
@@ -41,7 +42,7 @@ public void addHeader(String name, String value)
4142
{
4243
if(name.equalsIgnoreCase("Content-Disposition"))
4344
{
44-
value = getEncodedContentDisposition(value);
45+
value = WebUtils.getEncodedContentDisposition(value, httpContext.getBrowserType());
4546
}
4647

4748
httpContext.setHeader(name, value);
@@ -56,21 +57,6 @@ else if (name.equalsIgnoreCase("Content-length"))
5657
httpContext.getResponse().setContentLength((int) CommonUtil.val(value));
5758
}
5859
}
59-
60-
private String getEncodedContentDisposition(String value)
61-
{
62-
int filenameIdx = value.toLowerCase().indexOf("filename");
63-
if(filenameIdx != -1)
64-
{
65-
int eqIdx = value.toLowerCase().indexOf("=", filenameIdx);
66-
if (eqIdx != -1)
67-
{
68-
String filename = value.substring(eqIdx + 1).trim();
69-
value = value.substring(0, eqIdx + 1) + PrivateUtilities.URLEncode(filename, "UTF8");
70-
}
71-
}
72-
return value;
73-
}
7460

7561
public boolean isText()
7662
{

java/src/main/java/com/genexus/webpanels/WebUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,21 @@ private static void htmlDecode(String str, StringBuffer buffer)
321321
}
322322
}
323323

324+
public static String getEncodedContentDisposition(String value, int browserType)
325+
{
326+
int filenameIdx = value.toLowerCase().indexOf("filename");
327+
int eqIdx = value.toLowerCase().indexOf("=", filenameIdx);
328+
329+
if(filenameIdx == -1 || eqIdx == -1 || browserType == HttpContext.BROWSER_SAFARI) { //Safari does not support ContentDisposition Header encoded
330+
return value;
331+
}
332+
333+
String filename = value.substring(eqIdx + 1).trim();
334+
value = value.substring(0, filenameIdx) + String.format("filename*=UTF-8''%1$s; filename=\"%1$s\"", PrivateUtilities.URLEncode(filename, "UTF8"));
335+
336+
return value;
337+
}
338+
324339
public static GXWebComponent getWebComponent(Class caller, String name, int remoteHandle, com.genexus.ModelContext context)
325340
{
326341
try
@@ -562,4 +577,6 @@ public static Class processRestService(XMLReader reader) {
562577
return null;
563578
}
564579
}
580+
581+
565582
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.genexus.webpanels;
2+
3+
import com.genexus.specific.java.Connect;
4+
import org.junit.Assert;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
public class TestWebUtils {
9+
10+
@Before
11+
public void setUp() throws Exception {
12+
Connect.init();
13+
}
14+
15+
@Test
16+
public void TestContentDispositionHeaderEncoding1() {
17+
String contentDisposition = "attachment; filename=file.pdf";
18+
String expectedContentDisposition = "attachment; filename*=UTF-8''file.pdf; filename=\"file.pdf\"";
19+
doTest(contentDisposition, expectedContentDisposition);
20+
}
21+
22+
@Test
23+
public void TestContentDispositionHeaderEncoding2() {
24+
String contentDisposition = "attachment; filename=file.pdf";
25+
String expectedContentDisposition = contentDisposition;
26+
doTest(contentDisposition, expectedContentDisposition, HttpContextWeb.BROWSER_SAFARI);
27+
}
28+
29+
@Test
30+
public void TestContentDispositionHeaderEncoding3() {
31+
String contentDisposition = "attachment; filename=注文詳細.xlsx";
32+
String expectedContentDisposition = "attachment; filename*=UTF-8''%E6%B3%A8%E6%96%87%E8%A9%B3%E7%B4%B0.xlsx; filename=\"%E6%B3%A8%E6%96%87%E8%A9%B3%E7%B4%B0.xlsx\"";
33+
doTest(contentDisposition, expectedContentDisposition);
34+
}
35+
36+
@Test
37+
public void TestContentDispositionHeaderEncoding4() {
38+
String contentDisposition = "attachment; filename=注文詳細.xlsx";
39+
String expectedContentDisposition = contentDisposition;
40+
//Safari does not support rfc5987
41+
doTest(contentDisposition, expectedContentDisposition, HttpContextWeb.BROWSER_SAFARI);
42+
}
43+
44+
@Test
45+
public void TestContentDispositionHeaderEncoding5() {
46+
String contentDisposition = "form-data; filename=file.pdf";
47+
String expectedContentDisposition = "form-data; filename*=UTF-8''file.pdf; filename=\"file.pdf\"";
48+
doTest(contentDisposition, expectedContentDisposition);
49+
}
50+
51+
@Test
52+
public void TestContentDispositionHeaderEncoding6() {
53+
String contentDisposition = "ATTACHMENT; FILEname=注文詳細.xlsx";
54+
String expectedContentDisposition = "ATTACHMENT; filename*=UTF-8''%E6%B3%A8%E6%96%87%E8%A9%B3%E7%B4%B0.xlsx; filename=\"%E6%B3%A8%E6%96%87%E8%A9%B3%E7%B4%B0.xlsx\"";
55+
doTest(contentDisposition, expectedContentDisposition);
56+
}
57+
58+
59+
private void doTest(String contentDisposition, String expectedContentDisposition) {
60+
doTest(contentDisposition, expectedContentDisposition, HttpContextWeb.BROWSER_CHROME);
61+
}
62+
63+
private void doTest(String contentDisposition, String expectedContentDisposition, int browserType) {
64+
String encodedValue = WebUtils.getEncodedContentDisposition(contentDisposition, browserType);
65+
Assert.assertEquals(expectedContentDisposition, encodedValue);
66+
}
67+
}
68+
69+

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<url>https://github.com/genexuslabs/JavaClasses</url>
1515

1616
<properties>
17-
<revision>2.7</revision>
17+
<revision>2.8</revision>
1818
<changelist>-SNAPSHOT</changelist>
1919
<sha1/>
2020

0 commit comments

Comments
 (0)