Closed vvyoko closed 9 months ago
This could be done in user code, not inside NetCoreServer library. In HttpsServer example this could be implemented like this:
class HttpCacheSession : HttpSession
{
public HttpCacheSession(NetCoreServer.HttpServer server) : base(server) {}
protected override void OnReceivedRequest(HttpRequest request)
{
if (request.Method == "GET")
{
if (request.Url == "/")
{
var response = Cache.Find("/index.html");
if (response.Item1)
SendAsync(response.Item2);
else
SendResponseAsync(Response.MakeErrorResponse(404, "Cannot find index.html"));
}
}
else
SendResponseAsync(Response.MakeErrorResponse("Unsupported HTTP method: " + request.Method));
}
}
thx,it work fine
string key = request.Url;
key = Uri.UnescapeDataString(key);
if (string.IsNullOrEmpty(Path.GetExtension(key)))
{
if (key.EndsWith('/'))
{
var redirect = Cache.Find(key + "index.html");
if (redirect.Item1)
{
SendAsync(redirect.Item2);
return;
}
}
else if (Cache.Find(key + "/index.html").Item1)
{
var r = Response.MakeOkResponse(307);
r.SetBody(Encoding.UTF8.GetBytes("<script>window.location=window.location.href+\"/\";</script>").AsSpan());
//r.SetHeader("Location", key + "/");
SendResponseAsync(r);
return;
}
}
First thanks for fixed the cache update issue
Accessing a directory to redirect to index.html under the directory is a very common behavior like
http://localhost:7777/test/ --> http://localhost:7777/test/index.html
Redirects can be implemented in external code However, cache modification involves some private variables, which is difficult for external code to modify
by the way,Is there a better way to redirect? Manually construct the response of 307 not work (ask gpt)