Skip to content

Commit 6d1bcdd

Browse files
committed
[GTK] Fix NPE on POST requests when response has no Content-Type
The NPE was output to System.err because nothing catches it, but its running on a thread. The code worked, but System.err would have ``` Exception in thread "Thread-2" java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "content_type" is null at org.eclipse.swt.browser.WebKit.lambda$14(WebKit.java:2186) at java.base/java.lang.Thread.run(Thread.java:1583) ``` or similar.
1 parent 819875e commit 6d1bcdd

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

bundles/org.eclipse.swt/Eclipse SWT WebKit/gtk/org/eclipse/swt/browser/WebKit.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,19 +2200,21 @@ public boolean setUrl (String url, String postData, String[] headers) {
22002200
{ // Extract result meta data
22012201
// Get Media Type from Content-Type
22022202
String content_type = conn.getContentType();
2203-
int parameterSeparatorIndex = content_type.indexOf(';');
2204-
mime_type = parameterSeparatorIndex > 0 ? content_type.substring(0, parameterSeparatorIndex) : content_type;
2205-
2206-
// Get Encoding if defined
2207-
if (content_type.indexOf(';') > 0) {
2208-
String [] attrs = content_type.split(";");
2209-
for (String attr : attrs) {
2210-
int i = attr.indexOf('=');
2211-
if (i > 0) {
2212-
String key = attr.substring(0, i).trim();
2213-
String value = attr.substring(i + 1).trim();
2214-
if ("charset".equalsIgnoreCase(key)) { //$NON-NLS-1$
2215-
encoding_type = value;
2203+
if (content_type != null) {
2204+
int parameterSeparatorIndex = content_type.indexOf(';');
2205+
mime_type = parameterSeparatorIndex > 0 ? content_type.substring(0, parameterSeparatorIndex) : content_type;
2206+
2207+
// Get Encoding if defined
2208+
if (content_type.indexOf(';') > 0) {
2209+
String [] attrs = content_type.split(";");
2210+
for (String attr : attrs) {
2211+
int i = attr.indexOf('=');
2212+
if (i > 0) {
2213+
String key = attr.substring(0, i).trim();
2214+
String value = attr.substring(i + 1).trim();
2215+
if ("charset".equalsIgnoreCase(key)) { //$NON-NLS-1$
2216+
encoding_type = value;
2217+
}
22162218
}
22172219
}
22182220
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_browser_Browser.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,43 @@ public void test_setUrl_remote_with_post() throws IOException {
12841284
}
12851285
}
12861286

1287+
@Test
1288+
public void test_setUrl_remote_with_post_no_content_type() throws IOException {
1289+
try (var server = new EchoHttpServer() {
1290+
@Override
1291+
protected void setResponseHeaders(HttpExchange exchange) {
1292+
// omit calling super so that no content type is set
1293+
}
1294+
}) {
1295+
try (var capture = new CapturedOutput()) {
1296+
String url = server.postEchoUrl();
1297+
String postData = "test_setUrl_remote_with_post";
1298+
1299+
Runnable browserSetFunc = () -> {
1300+
testLog.append("Setting Browser url to:" + url);
1301+
boolean opSuccess = browser.setUrl(url, postData, null);
1302+
assertTrue(opSuccess);
1303+
};
1304+
1305+
final AtomicReference<Boolean> completed = new AtomicReference<>(false);
1306+
browser.addProgressListener(completedAdapter(event -> {
1307+
testLog.append("ProgressListener fired");
1308+
completed.set(true);
1309+
}));
1310+
browserSetFunc.run();
1311+
shell.open();
1312+
1313+
boolean hasFinished = waitForPassCondition(() -> completed.get().booleanValue());
1314+
assertTrue(hasFinished);
1315+
1316+
String lowerCase = browser.getText().toLowerCase();
1317+
assertTrue(lowerCase.contains("<title>test_setUrl_remote_with_post</title>".toLowerCase()), "Browser getText was: " + browser.getText());
1318+
assertTrue(lowerCase.contains("</html>"), "Browser getText was: " + browser.getText());
1319+
capture.assertNoOutput();
1320+
}
1321+
}
1322+
}
1323+
12871324
@Test
12881325
public void test_setUrl_post_invalid_url() {
12891326
assumeTrue(SwtTestUtil.isLinux, "Handling of invalid URLs is platform dependent");

0 commit comments

Comments
 (0)