Skip to content

Commit

Permalink
Implement windows, mac and linux
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Oct 10, 2023
1 parent 57485da commit 03d0fa5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 36 deletions.
3 changes: 2 additions & 1 deletion html-redirect/redirect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ std::string redirect(std::string inputPath, const std::string& basePath) {
// 2. Try appending a `/` to the path and checking for an index.html
fs::path indexPath = fullPath / fs::path("index.html");
if (fs::is_regular_file(indexPath)) {
// Also redirect
return "/" + fs::relative(indexPath, basePath).string();
}

Expand All @@ -36,7 +37,7 @@ std::string redirect(std::string inputPath, const std::string& basePath) {

int main() {
std::string basePath = "/Users/bret/Developer/ssc/socket/html-redirect/test-cases/1";
std::string testPath = "/an-index-file/a-html-file.html";
std::string testPath = "/";
std::string resolvedPath = redirect(testPath, basePath);
std::cout << resolvedPath << std::endl;
}
83 changes: 60 additions & 23 deletions src/ipc/bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1751,14 +1751,6 @@ static void registerSchemeHandler (Router *router) {

auto ext = fs::path(path).extension().string();

if (path == "/" || path.size() == 0) {
path = "/index.html";
ext = ".html";
} else if (path.ends_with("/")) {
path += "index.html";
ext = ".html";
}

if (ext.size() > 0 && !ext.starts_with(".")) {
ext = "." + ext;
}
Expand Down Expand Up @@ -1786,8 +1778,13 @@ static void registerSchemeHandler (Router *router) {
return;
}

if (ext.size() == 0) {
auto redirectURL = uri + "/";
auto resolved = Router::resolveURLPathForWebView(path, cwd);
path = resolved.path;

if (path.size() == 0 && userConfig.contains("webview_default_index")) {
path = userConfig["webview_default_index"];
} else if (resolved.redirect) {
auto redirectURL = path;
auto redirectSource = String(
"<meta http-equiv=\"refresh\" content=\"0; url='" + redirectURL + "'\" />"
);
Expand All @@ -1809,9 +1806,11 @@ static void registerSchemeHandler (Router *router) {
return;
}

path = fs::absolute(fs::path(cwd) / path.substr(1)).string();
if (path.size() > 0) {
path = fs::absolute(fs::path(cwd) / path.substr(1)).string();
}

if (!fs::exists(path)) {
if (path.size() == 0 || !fs::exists(path)) {
auto stream = g_memory_input_stream_new_from_data(nullptr, 0, 0);
auto response = webkit_uri_scheme_response_new(stream, 0);

Expand Down Expand Up @@ -1919,6 +1918,7 @@ static void registerSchemeHandler (Router *router) {

NSData* data = nullptr;
bool isModule = false;
auto basePath = String(NSBundle.mainBundle.resourcePath.UTF8String);
auto path = String(components.path.UTF8String);

auto ext = String(
Expand All @@ -1927,14 +1927,6 @@ static void registerSchemeHandler (Router *router) {
: ""
);

if (path == "/" || path.size() == 0) {
path = "/index.html";
ext = ".html";
} else if (path.ends_with("/")) {
path += "index.html";
ext = ".html";
}

if (ext.size() > 0 && !ext.starts_with(".")) {
ext = "." + ext;
}
Expand All @@ -1943,10 +1935,13 @@ static void registerSchemeHandler (Router *router) {
host.UTF8String != nullptr &&
String(host.UTF8String) == bundleIdentifier
) {
if (ext.size() == 0 && userConfig.contains("webview_default_index")) {
auto resolved = Router::resolveURLPathForWebView(path, basePath);
path = resolved.path;

if (path.size() == 0 && userConfig.contains("webview_default_index")) {
path = userConfig["webview_default_index"];
} else if (ext.size() == 0) {
auto redirectURL = String(request.URL.absoluteString.UTF8String) + "/";
} else if (resolved.redirect) {
auto redirectURL = path;
auto redirectSource = String(
"<meta http-equiv=\"refresh\" content=\"0; url='" + redirectURL + "'\" />"
);
Expand Down Expand Up @@ -2564,6 +2559,48 @@ namespace SSC::IPC {
}
}

Router::WebViewURLPathResolution Router::resolveURLPathForWebView (String inputPath, const String& basePath) {
namespace fs = std::filesystem;

if (inputPath.starts_with("/")) {
inputPath = inputPath.substr(1);
}

// Resolve the full path
fs::path fullPath = fs::path(basePath) / fs::path(inputPath);

// 1. Try the given path if it's a file
if (fs::is_regular_file(fullPath)) {
return Router::WebViewURLPathResolution{"/" + fs::relative(fullPath, basePath).string()};
}

// 2. Try appending a `/` to the path and checking for an index.html
fs::path indexPath = fullPath / fs::path("index.html");
if (fs::is_regular_file(indexPath)) {
if (fullPath.string().ends_with("/")) {
return Router::WebViewURLPathResolution{
.path = "/" + fs::relative(indexPath, basePath).string(),
.redirect = false
};
} else {
return Router::WebViewURLPathResolution{
.path = "/" + fs::relative(fullPath, basePath).string() + "/",
.redirect = true
};
}
}

// 3. Check if appending a .html file extension gives a valid file
fs::path htmlPath = fullPath;
htmlPath.replace_extension(".html");
if (fs::is_regular_file(htmlPath)) {
return Router::WebViewURLPathResolution{"/" + fs::relative(htmlPath, basePath).string()};
}

// If no valid path is found, return empty string
return Router::WebViewURLPathResolution{};
};

Router::Router () {
initRouterTable(this);
registerSchemeHandler(this);
Expand Down
7 changes: 7 additions & 0 deletions src/ipc/ipc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ namespace SSC::IPC {
using Table = std::map<String, MessageCallbackContext>;
using Listeners = std::map<String, std::vector<MessageCallbackListenerContext>>;

struct WebViewURLPathResolution {
String path = "";
bool redirect = false;
};

static WebViewURLPathResolution resolveURLPathForWebView (String inputPath, const String& basePath);

private:
Table preserved;

Expand Down
23 changes: 11 additions & 12 deletions src/window/win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -943,14 +943,6 @@ namespace SSC {

auto ext = fs::path(path).extension().string();

if (path == "/" || path.size() == 0) {
path = "/index.html";
ext = ".html";
} else if (path.ends_with("/")) {
path += "index.html";
ext = ".html";
}

if (ext.size() > 0 && !ext.starts_with(".")) {
ext = "." + ext;
}
Expand Down Expand Up @@ -1014,8 +1006,12 @@ namespace SSC {
}
} else {
auto rootPath = this->modulePath.parent_path();
auto resolved = Router::resolveURLPathForWebView(path, rootPath);
path = resolved.path;

if (ext.size() == 0) {
if (path.size() == 0 && userConfig.contains("webview_default_index")) {
path = userConfig["webview_default_index"];
} else if (resolved.redirect) {
uri += "/";
app.dispatch([&, uri, path, args, deferral, env] {
ICoreWebView2WebResourceResponse* res = nullptr;
Expand All @@ -1024,8 +1020,8 @@ namespace SSC {
301,
L"Moved Permanently",
WString(
StringToWString("Location: ") + StringToWString(uri) + L"\n" +
StringToWString("Content-Location: ") + StringToWString(uri) + L"\n"
StringToWString("Location: ") + StringToWString(path) + L"\n" +
StringToWString("Content-Location: ") + StringToWString(path) + L"\n"
).c_str(),
&res
);
Expand All @@ -1035,7 +1031,10 @@ namespace SSC {
return S_OK;
}

path = fs::absolute(rootPath / path.substr(1)).string();
if (path.size() > 0) {
path = fs::absolute(rootPath / path.substr(1)).string();
}

LARGE_INTEGER fileSize;
auto handle = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
auto getSizeResult = GetFileSizeEx(handle, &fileSize);
Expand Down

0 comments on commit 03d0fa5

Please sign in to comment.