-
Notifications
You must be signed in to change notification settings - Fork 1k
fix(xml): close DTD resolver resources #3334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
w3lld1
wants to merge
5
commits into
testng-team:master
Choose a base branch
from
w3lld1:fix-testng-content-handler-resources
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−22
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ef7591
fix(xml): close DTD resolver resources
w3lld1 bd7e949
fix(xml): address resolver review feedback
w3lld1 bd6175b
test(xml): verify resolver source closure
w3lld1 eab30ce
fix(xml): address maintainer review
w3lld1 c76d12c
docs(xml): explain buffered DTD ownership
w3lld1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| import static org.testng.internal.Utils.isStringBlank; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
@@ -16,7 +17,6 @@ | |
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Stack; | ||
| import org.testng.ITestObjectFactory; | ||
|
|
@@ -43,6 +43,8 @@ | |
| */ | ||
| // TODO move to internal | ||
| public class TestNGContentHandler extends DefaultHandler { | ||
| private static final int DTD_CONNECTION_TIMEOUT_MILLIS = 10_000; | ||
|
|
||
| private XmlSuite m_currentSuite = null; | ||
| private XmlTest m_currentTest = null; | ||
| private XmlDefine m_currentDefine = null; | ||
|
|
@@ -71,27 +73,49 @@ public class TestNGContentHandler extends DefaultHandler { | |
| "Failed to read [%s] from CLASSPATH. " + "Attempting to read from [%s].", | ||
| url.getPath(), systemId); | ||
| Logger.getLogger(getClass()).warn(msg); | ||
| URLConnection urlConnection = url.openConnection(); | ||
| if (urlConnection instanceof HttpURLConnection) { | ||
| HttpURLConnection conn = (HttpURLConnection) urlConnection; | ||
|
|
||
| int status = conn.getResponseCode(); | ||
| if (status == HttpURLConnection.HTTP_MOVED_TEMP | ||
| || status == HttpURLConnection.HTTP_MOVED_PERM | ||
| || status == HttpURLConnection.HTTP_SEE_OTHER) { | ||
|
|
||
| String newUrl = conn.getHeaderField("Location"); | ||
| conn = (HttpURLConnection) new URL(newUrl).openConnection(); | ||
| } | ||
| stream = conn.getInputStream(); | ||
| } else { | ||
| stream = urlConnection.getInputStream(); | ||
| } | ||
| return readUrlAsInputSource(url); | ||
| } | ||
| try (InputStream input = stream) { | ||
| return new InputSource(new ByteArrayInputStream(input.readAllBytes())); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return new InputSource( | ||
| Objects.requireNonNull(stream, "Failed to load DTD from " + systemId)); | ||
| }; | ||
|
|
||
| static byte[] readUrl(URL url, boolean followRedirect) throws IOException { | ||
| URLConnection connection = url.openConnection(); | ||
| configureConnection(connection); | ||
| if (!(connection instanceof HttpURLConnection)) { | ||
| try (InputStream input = connection.getInputStream()) { | ||
| return input.readAllBytes(); | ||
| } | ||
| } | ||
|
|
||
| HttpURLConnection httpConnection = (HttpURLConnection) connection; | ||
| httpConnection.setInstanceFollowRedirects(false); | ||
| try { | ||
| int status = httpConnection.getResponseCode(); | ||
| if (followRedirect | ||
| && (status == HttpURLConnection.HTTP_MOVED_TEMP | ||
| || status == HttpURLConnection.HTTP_MOVED_PERM | ||
| || status == HttpURLConnection.HTTP_SEE_OTHER)) { | ||
| return readUrl(new URL(url, httpConnection.getHeaderField("Location")), false); | ||
| } | ||
| try (InputStream input = httpConnection.getInputStream()) { | ||
| return input.readAllBytes(); | ||
| } | ||
| } finally { | ||
| httpConnection.disconnect(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| static InputSource readUrlAsInputSource(URL url) throws IOException { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only used by tests, move it into the test workspace |
||
| return new InputSource(new ByteArrayInputStream(readUrl(url, true))); | ||
| } | ||
|
|
||
| static void configureConnection(URLConnection connection) { | ||
| connection.setConnectTimeout(DTD_CONNECTION_TIMEOUT_MILLIS); | ||
| connection.setReadTimeout(DTD_CONNECTION_TIMEOUT_MILLIS); | ||
| } | ||
|
|
||
| enum Location { | ||
| SUITE, | ||
| TEST, | ||
|
|
@@ -130,9 +154,11 @@ public InputSource resolveEntity(String publicId, String systemId) | |
|
|
||
| if (skipConsideringSystemId(systemId)) { | ||
| m_validate = true; | ||
| InputStream is = loadDtdUsingClassLoader(); | ||
| if (is != null) { | ||
| return new InputSource(is); | ||
| InputStream stream = loadDtdUsingClassLoader(); | ||
| if (stream != null) { | ||
| try (InputStream input = stream) { | ||
| return new InputSource(new ByteArrayInputStream(input.readAllBytes())); | ||
| } | ||
| } | ||
| // If the classpath loading of DTD fails, then we try to load it from "https" TestNG site. | ||
| System.out.println( | ||
|
|
||
22 changes: 22 additions & 0 deletions
22
testng-core/src/test/java/org/testng/xml/TestNGContentHandlerTestSupport.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package org.testng.xml; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.net.URLConnection; | ||
| import org.xml.sax.InputSource; | ||
|
|
||
| public final class TestNGContentHandlerTestSupport { | ||
| private TestNGContentHandlerTestSupport() {} | ||
|
|
||
| public static void configureConnection(URLConnection connection) { | ||
| TestNGContentHandler.configureConnection(connection); | ||
| } | ||
|
|
||
| public static byte[] readUrl(URL url) throws IOException { | ||
| return TestNGContentHandler.readUrl(url, true); | ||
| } | ||
|
|
||
| public static InputSource readUrlAsInputSource(URL url) throws IOException { | ||
| return TestNGContentHandler.readUrlAsInputSource(url); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment to explain why bytes are read instead of
new InputSource(input)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same for the second usage