Skip to content

Fix PathToUrl to correctly handle external URLs #1075

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3385,10 +3385,14 @@ public object GetContextProperty(string key)

public string PathToUrl(string path)
{
GXLogging.Debug(Logger, "PathToUrl:", () => GetContextPath() + " relativePath:" + PathToRelativeUrl(path));
#pragma warning disable SYSLIB0013 // EscapeUriString
return Uri.EscapeUriString(GetContextPath()) + PathToRelativeUrl(path, false);
#pragma warning disable SYSLIB0013 // EscapeUriString
string relativeUrl = PathToRelativeUrl(path, false);
GXLogging.Debug(Logger, "PathToUrl:", () => GetContextPath() + " relativePath:" + relativeUrl);
if (Uri.IsWellFormedUriString(relativeUrl, UriKind.Absolute))
return relativeUrl;
else if (Uri.TryCreate(GetContextPath() + relativeUrl, UriKind.Absolute, out Uri uri))
return uri.AbsoluteUri;
else
return relativeUrl;
}

public string PathToRelativeUrl(string path)
Expand Down
51 changes: 51 additions & 0 deletions dotnet/test/DotNetCoreUnitTest/Domain/PathToUrlTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Runtime.InteropServices;
using GeneXus.Application;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Xunit;

namespace xUnitTesting
{

public class PathToUrlTest
{
[Fact]
public void PathToUrl()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}
DefaultHttpContext httpContext = new DefaultHttpContext();

httpContext.Features.Set<IHttpRequestFeature>(new HttpRequestFeature());
httpContext.Features.Set<IHttpResponseFeature>(new HttpResponseFeature());

httpContext.Request.Host = new HostString("localhost");
httpContext.Request.Scheme = "http";
httpContext.Request.PathBase = "/test";

string baseUrl = @"http://localhost/test/";
GxContext context = new GxContext();
context.HttpContext = httpContext;

string imagePath = @"C:\Models\MyKB\NETModel\web\PublicTempStorage\dog6bf667c1-f15b-4af1-9ece-893381a470a0.jpg";
string imageUrl = context.PathToUrl(imagePath);
string expectedImagePath = baseUrl + @"dog6bf667c1-f15b-4af1-9ece-893381a470a0.jpg";
Assert.Equal(expectedImagePath, imageUrl, true, true, false);


imagePath = @"https://testsk3.blob.core.windows.net/skprivate/PublicTempStorage/dogeb918a65-0d8a-41e0-906a-6554457280e3.jpg?sv=2018-03-28&sp=r";
imageUrl = context.PathToUrl(imagePath);
expectedImagePath = imagePath;
Assert.Equal(expectedImagePath, imageUrl, true, true, false);

imagePath = @"file:///C:/Models/MyKB/NETModel/web/PublicTempStorage/dog6bf667c1-f15b-4af1-9ece-893381a470a0.jpg";
imageUrl = context.PathToUrl(imagePath);
expectedImagePath = baseUrl + @"dog6bf667c1-f15b-4af1-9ece-893381a470a0.jpg"; ;
Assert.Equal(expectedImagePath, imageUrl, true, true, false);

}

}
}
Loading