Skip to content

Commit 93899a0

Browse files
author
Egor Martsynkovsky
committed
Fix opened connections outside ResourceRetriever
DEVSIX-5037 Autoported commit. Original commit hash: [0847044] Manual files: io/src/main/java/com/itextpdf/io/util/UrlUtil.java io/src/test/java/com/itextpdf/io/util/UrlUtilTest.java
1 parent fe8e672 commit 93899a0

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

itext.tests/itext.io.tests/itext/io/util/UrlUtilTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ source product.
4242
*/
4343
using System;
4444
using System.IO;
45+
using System.Net;
4546
using System.Text;
4647
using iText.Commons.Utils;
4748
using iText.Test;
49+
using NUnit.Framework;
4850

4951
namespace iText.IO.Util {
5052
public class UrlUtilTest : ExtendedITextTest {
@@ -64,6 +66,27 @@ public virtual void GetFinalURLDoesNotLockFileTest() {
6466
NUnit.Framework.Assert.IsTrue(FileUtil.DeleteFile(tempFile));
6567
}
6668

69+
// This test checks that when we pass invalid url and trying get stream related to final redirected url,exception
70+
// would be thrown.
71+
[NUnit.Framework.Test]
72+
public virtual void GetInputStreamOfFinalConnectionThrowExceptionTest() {
73+
Uri invalidUrl = new Uri("http://itextpdf");
74+
75+
NUnit.Framework.Assert.That(() => {
76+
UrlUtil.GetInputStreamOfFinalConnection(invalidUrl);
77+
}, NUnit.Framework.Throws.InstanceOf<WebException>());
78+
}
79+
80+
// This test checks that when we pass valid url and trying get stream related to final redirected url, it would
81+
// not be null.
82+
[NUnit.Framework.Test]
83+
public virtual void GetInputStreamOfFinalConnectionTest() {
84+
Uri initialUrl = new Uri("http://itextpdf.com");
85+
Stream streamOfFinalConnectionOfInvalidUrl = UrlUtil.GetInputStreamOfFinalConnection(initialUrl);
86+
87+
NUnit.Framework.Assert.NotNull(streamOfFinalConnectionOfInvalidUrl);
88+
}
89+
6790
[NUnit.Framework.Test]
6891
public void GetBaseUriTest() {
6992
String absolutePathRoot = new Uri(new Uri("file://"), destinationFolder).AbsoluteUri;

itext/itext.io/itext/io/util/UrlUtil.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ public static Uri ToNormalizedURI(FileInfo file) {
114114
/// <summary>
115115
/// This method gets the last redirected url.
116116
/// </summary>
117-
/// <param name="uri">an initial url</param>
118-
/// <returns>the last redirected url</returns>
117+
/// <param name="uri">an initial url.</param>
118+
///
119+
/// <returns>the last redirected url.</returns>
120+
[Obsolete]
119121
public static Uri GetFinalURL(Uri uri) {
120122
return uri;
121123
}
@@ -139,5 +141,16 @@ public static String GetNormalizedFileUriString(String filename)
139141
{
140142
return "file://" + UrlUtil.ToNormalizedURI(filename).AbsolutePath;
141143
}
144+
145+
/// <summary>
146+
/// Gets the input stream of connection related to last redirected url. You should manually close input stream after
147+
/// calling this method to not hold any open resources.
148+
/// </summary>
149+
/// <param name="url">an initial URL.</param>
150+
///
151+
/// <returns>an input stream of connection related to the last redirected url.</returns>
152+
public static Stream GetInputStreamOfFinalConnection(Uri url) {
153+
return UrlUtil.OpenStream(url);
154+
}
142155
}
143156
}

itext/itext.styledxmlparser/itext/styledxmlparser/resolver/resource/DefaultResourceRetriever.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public virtual Stream GetInputStreamByUrl(Uri url) {
9696
, url));
9797
return null;
9898
}
99-
return new LimitedInputStream(UrlUtil.OpenStream(url), resourceSizeByteLimit);
99+
return new LimitedInputStream(UrlUtil.GetInputStreamOfFinalConnection(url), resourceSizeByteLimit);
100100
}
101101

102102
/// <summary>Gets the byte array that are retrieved from the source URL.</summary>

itext/itext.styledxmlparser/itext/styledxmlparser/resolver/resource/ResourceResolver.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ source product.
4646
using iText.Commons;
4747
using iText.Commons.Utils;
4848
using iText.IO.Image;
49-
using iText.IO.Util;
5049
using iText.Kernel.Pdf.Xobject;
5150

5251
namespace iText.StyledXmlParser.Resolver.Resource {
@@ -278,7 +277,6 @@ protected internal virtual PdfXObject TryResolveBase64ImageSource(String src) {
278277
protected internal virtual PdfXObject TryResolveUrlImageSource(String uri) {
279278
try {
280279
Uri url = uriResolver.ResolveAgainstBaseUri(uri);
281-
url = UrlUtil.GetFinalURL(url);
282280
String imageResolvedSrc = url.ToExternalForm();
283281
PdfXObject imageXObject = imageCache.GetImage(imageResolvedSrc);
284282
if (imageXObject == null) {
@@ -295,11 +293,11 @@ protected internal virtual PdfXObject TryResolveUrlImageSource(String uri) {
295293
}
296294

297295
/// <summary>Create a iText XObject based on the image stored at the passed location.</summary>
298-
/// <param name="url">location of the Image file</param>
296+
/// <param name="url">location of the Image file.</param>
299297
/// <returns>
300298
///
301299
/// <see cref="iText.Kernel.Pdf.Xobject.PdfXObject"/>
302-
/// containing the Image loaded in
300+
/// containing the Image loaded in.
303301
/// </returns>
304302
protected internal virtual PdfXObject CreateImageByUrl(Uri url) {
305303
byte[] bytes = retriever.GetByteArrayByUrl(url);

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
22758a9a14a188fe166c90befcc99fdbde473415
1+
0847044410e8b1c1eb0f1536d1ff2e282bc9d62d

0 commit comments

Comments
 (0)