Skip to content

Commit b92f85a

Browse files
committed
Fix recognition of network paths in UrlUtil
DEVSIX-1668 DEVSIX-1670
1 parent adb0d2c commit b92f85a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

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(Uri.UnescapeDataString(url.AbsoluteUri));
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ public static Uri ToURL(String filename) {
7171
public static Stream OpenStream(Uri url) {
7272
Stream isp;
7373
if (url.IsFile) {
74-
isp = new FileStream(Uri.UnescapeDataString(url.AbsolutePath), FileMode.Open, FileAccess.Read);
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.
78+
isp = new FileStream(url.LocalPath, FileMode.Open, FileAccess.Read);
7579
} else {
7680
#if !NETSTANDARD1_6
7781
WebRequest req = WebRequest.Create(url);

0 commit comments

Comments
 (0)