unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.46k stars 176 forks source link

opening zipped folder #525

Closed Novotnde closed 3 years ago

Novotnde commented 3 years ago

Hello, I have a question I am trying to use embedio in xamarin application and I need to open up a zip folder so far no luck. Since its been few days I decided to ask for some advice

I have my folder as embedded resource and I am trying to unzip it with .WithZipFileStream - but I am getting 404 exception. Since I am unsure that this method also works with folders I have tried to unzip it and then use static folders. - here I am getting iOS exception

[HandleHttpException] exception. Request route: http://127.0.0.1:8080/index.html. 2021-06-23 11:24:23.508491+0200 EsignoReader.iOS[25534:34280063] 2021-06-23 11:24:23.667350+0200 EsignoReader.iOS[25534:34278886] [Process] 0x7f93743fbc20 - [pageProxyID=5, webPageID=6, PID=25535] WebPageProxy::didFailProvisionalLoadForFrame: frameID = 3, domain = WebKitErrorDomain, code = 102

public App()
{

    Task.Factory.StartNew(async () =>
    {
        using (var server = CreateWebServerSingleIndex("http://*:8080"))
        {
            await server.RunAsync();
        }
    });

MainPage = new NavigationPage(new WebViewPage());

}
 private static WebServer CreateWebServerSingleIndex(string url)
 {

            string[] names = typeof(EmbeddedResourceDataStream).GetTypeInfo().Assembly.GetManifestResourceNames();
            Stream zipFileStream = typeof(EmbeddedResourceDataStream).GetTypeInfo().Assembly
                .GetManifestResourceStream("EsignoReader.test.zip");
            var server = new WebServer(o => o
                .WithUrlPrefix(url)
                .WithMode(HttpListenerMode.EmbedIO)).
                WithZipFileStream("/test/", zipFileStream);

            server.StateChanged += (s, e) => $"WebServer New State - {e.NewState}".Info();

            return server;
  }
private WebServer CreateWebServer(string url)
{
    var webApiModule = new WebApiModule("/api", ResponseSerializer.Json).WithController<TestController>()
        .HandleUnhandledException(ExceptionHandler.DataResponseForException);

    using (var embededResource = typeof(EmbeddedResourceDataStream).GetTypeInfo().Assembly
        .GetManifestResourceStream("EsignoReader.server.zip"))
    {
        using (var fileStream = File.Create(Path.Combine(FileSystem.CacheDirectory, "server.zip")))
        {
            embededResource.CopyTo(fileStream);
        }
    }
        ZipFile.ExtractToDirectory(Path.Combine(FileSystem.CacheDirectory, "server.zip"),
            Path.Combine(FileSystem.CacheDirectory, "server"));

    var server =
        new WebServer(o => o.WithUrlPrefix(url).WithMode(HttpListenerMode.EmbedIO)).WithStaticFolder("/",
            Path.Combine(FileSystem.CacheDirectory, "server"), false);
    server.WithModule(webApiModule).HandleHttpException(HandleHttpException);
    server.StateChanged += ServerOnStateChanged;

    return server;
}

Could you please advise where am I making mistake?

rdeago commented 3 years ago

Hello @Novotnde, thanks for using EmbedIO!

I've never worked with Xamarin, so I hope someone else will help you with platform-specific issues. In the meanwhile, here's a couple things I would do if facing a similar problem:

  1. Knowing that ZipFileProvider is not thread-safe and therefore may have problems with simultaneous requests, I'd try the embedded resource scenario with the simplest possible index.html (<html><body>Hello world!</body></html> isn't exactly valid HTML - no head tag, no title, etc. - but will do the job). If that works, try adding a couple images, or an external stylesheet and an external script, and see if you start getting errors.
  2. I'd google (or duckduckgo) every single word in the iOS exception message (copy-to-directory scenario), in the hope of gaining a better understanding, then I'd post my findings, along with the exception message, here.
  3. I'd have a look at previous Xamarin-related EmbedIO issues for similar / related problems.
  4. While I'm at it, why not take a look at issues in the xamarin-macios repository too.
  5. I'd try a simple Web server, with just an OnAny returning the HTML code above, to rule out problems with anything not fileModule-related.

It's not much, I know, and you probably have already done all of the above - but it's all I can do, sorry.

Also, it may help if you post your version of Xamarin, Mono, iOS, and Visual Studio; while this is always good practice, it is especially with "moving targets" like Xamarin/iOS.

Novotnde commented 3 years ago

Hello, thank you for fast reply!

I have figured the way to do it with WithZipFileStream at the end. Thank you very much!

rdeago commented 3 years ago

@Novotnde you're welcome!

Let me guess:

If it was something else, please take a minute to explain, so others can benefit from your experience. Thanks!

jswigart commented 1 year ago

I worked around a threading issue with a SharpZipLib provider

SharpZipFileProvider.zip