Skip to content

Commit c2f8f99

Browse files
Fix web links resolving for RandomAccessSourceFactory
Anything like this didn't work properly (e.g. for image data creation): `https://www.google.by/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png`.
1 parent 37f5875 commit c2f8f99

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

itext.tests/itext.layout.tests/itext/layout/NetWorkPathTest.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,44 +40,55 @@ source product.
4040
For more information, please contact iText Software Corp. at this
4141
4242
*/
43+
4344
using System;
44-
using System.Collections.Generic;
4545
using System.Globalization;
46-
using System.Linq;
47-
using System.Text;
4846
using System.Threading;
4947
using iText.IO.Image;
48+
using iText.Kernel.Pdf;
5049
using iText.Layout.Element;
50+
using iText.Test;
51+
using NUnit.Framework;
5152

52-
namespace iText.Layout
53-
{
53+
namespace iText.Layout {
5454
// This test is present only in c#
5555
// Also this test in only for windows OS
56-
class NetWorkPathTest
57-
{
56+
public class NetWorkPathTest : ExtendedITextTest {
57+
5858
[NUnit.Framework.Test]
59-
public virtual void NetworkPathImageTest()
60-
{
61-
var fullImagePath = @"\\someVeryRandomWords\SomeVeryRandomName.img";
62-
string startOfMsg = null;
59+
public virtual void NetworkPathImageTest() {
60+
String fullImagePath = @"\\someVeryRandomWords\SomeVeryRandomName.img";
61+
String startOfMsg = null;
6362
#if !NETSTANDARD1_6
6463
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
6564
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
6665
#else
6766
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
6867
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
6968
#endif
70-
try
71-
{
69+
try {
7270
Image drawing = new Image(ImageDataFactory.Create(fullImagePath));
73-
}
74-
catch (Exception e)
75-
{
71+
} catch (Exception e) {
7672
if (e.InnerException != null && e.InnerException.Message.Length > 18)
7773
startOfMsg = e.InnerException.Message.Substring(0, 19);
7874
}
7975
NUnit.Framework.Assert.IsNotNull(startOfMsg);
8076
NUnit.Framework.Assert.AreNotEqual("Could not find file", startOfMsg);
8177
}
78+
79+
[NUnit.Framework.Test]
80+
[Ignore("Manual run only")]
81+
public virtual void NetworkPathImageTest02() {
82+
// TODO This test can work only if shared folder exists on some local network computer.
83+
// Suggested apporach is to create such folder on your computer and input corresponding names as variables values below.
84+
String comupterNameAndSharedFolderPath = @"INSERT_YOUR_COMPUTER_NAME"; // e.g. \\DESKTOP-ABCD3TQ\_inbox
85+
String outPath = "INSERT_OUTPUT_PATH";
86+
87+
String fullImagePath = @"\\" + comupterNameAndSharedFolderPath + @"\img.jpg";
88+
Image drawing = new Image(ImageDataFactory.Create(fullImagePath));
89+
Document doc = new Document(new PdfDocument(new PdfWriter(outPath)));
90+
doc.Add(drawing.SetAutoScale(true));
91+
doc.Close();
92+
}
8293
}
83-
}
94+
}

itext/itext.io/itext/io/source/RandomAccessSourceFactory.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ public IRandomAccessSource CreateSource(FileStream raf)
153153
/// <exception cref="System.IO.IOException"/>
154154
public IRandomAccessSource CreateSource(Uri url) {
155155
#if !NETSTANDARD1_6
156-
WebRequest wr = WebRequest.Create(url.LocalPath);
156+
// Creation of web request via url.AbsoluteUri breaks UNC pathes (like \\computer-name\\img.jpg),
157+
// url.LocalPath and url.AbsolutePath - break http links (like https://website.com/img.jpg).
158+
// It seems enough to simply pass Uri instance as is, WebRequest seems to handle both escaping and UNC issues.
159+
WebRequest wr = WebRequest.Create(url);
157160
wr.Credentials = CredentialCache.DefaultCredentials;
158161
Stream isp = wr.GetResponse().GetResponseStream();
159162
#else

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ public static Uri ToURL(String filename) {
7171
public static Stream OpenStream(Uri url) {
7272
Stream isp;
7373
if (url.IsFile) {
74-
// do not replace first argument with Uri.UnescapeDataString(url.AbsolutePath) instead of url.LocalPath
75-
// it breaks the recognition of network paths
74+
// Use url.LocalPath because it's needed for handling UNC pathes (like used in local
75+
// networks, e.g. \\computer\file.ext). It's safe to use #LocalPath because we
76+
// check #IsFile beforehand. On the other hand, the url.AbsolutePath provides escaped string and also breaks
77+
// UNC path.
7678
isp = new FileStream(url.LocalPath, FileMode.Open, FileAccess.Read);
7779
} else {
7880
#if !NETSTANDARD1_6

0 commit comments

Comments
 (0)