Skip to content
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

"The connection was reset." in chrome based browsers #155

Open
Infiziert90 opened this issue Aug 28, 2024 · 0 comments
Open

"The connection was reset." in chrome based browsers #155

Infiziert90 opened this issue Aug 28, 2024 · 0 comments

Comments

@Infiziert90
Copy link

Infiziert90 commented Aug 28, 2024

This error happens every time i try to redirect the browser without reading the request body.
Simply calling _ = ctx.Request.DataAsString; before the .Send() fixes it.
Task.Delay(100) didn't fix it on the other hand, so i assume the bug is somewhere in WebserverLite.
Use-case is rate limits without much processing.

Test code:

namespace WatsonLiteTest;

class Run
{
    public static async Task Main(string[] _)
    {
        await new Webserver().Run();
    }
}

class Webserver
{
    private WebserverLite Host;

    public async Task Run()
    {
        Host = new WebserverLite(new WebserverSettings("localhost", 9000), DefaultRoute);

        Host.Routes.PreAuthentication.Static.Add(WatsonWebserver.Core.HttpMethod.GET, "/", AuthRoute);
        Host.Routes.PreAuthentication.Static.Add(WatsonWebserver.Core.HttpMethod.POST, "/auth", AuthenticateClient);
        Host.Routes.PreAuthentication.Static.Add(WatsonWebserver.Core.HttpMethod.POST, "/workingAuth", AuthenticateWorking);

        await Host.StartAsync();
    }

    private async Task AuthRoute(HttpContextBase ctx)
    {
        await ctx.Response.Send("""
                                <!DOCTYPE html>
                                <html lang="en">
                                <head>
                                    <title>Authentication</title>
                                
                                    <meta charset="utf-8">
                                    <meta name="viewport" content="width=device-width, initial-scale=1">
                                </head>

                                <body>
                                    <main class="auth">
                                        <h1>Authcode</h1>
                                        <form action="/auth" method="POST">
                                            <label>Not working: <input type="password" name="authcode"></label>
                                            <button type="submit">Submit</button>
                                        </form>
                                        <form action="/workingAuth" method="POST">
                                            <label>Working: <input type="password" name="authcode"></label>
                                            <button type="submit">Submit</button>
                                        </form>
                                    </main>
                                </body>
                                </html>
                                """);
    }

    private async Task AuthenticateClient(HttpContextBase ctx)
    {
        ctx.Response.Headers.Add("Location", "/");
        ctx.Response.StatusCode = 303;
        await ctx.Response.Send();
    }

    private async Task AuthenticateWorking(HttpContextBase ctx)
    {
        _ = ctx.Request.DataAsString; // reading the body
        ctx.Response.Headers.Add("Location", "/");
        ctx.Response.StatusCode = 303;
        await ctx.Response.Send();
    }

    private async Task<bool> DefaultRoute(HttpContextBase ctx)
    {
        return await ctx.Response.Send("Nothing to see here.");
    }
}

msedge_fTsSK4KUMZ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant