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

cgi httpd bug when receiving a HEAD request #367

Open
jonatjano opened this issue Feb 24, 2023 · 6 comments
Open

cgi httpd bug when receiving a HEAD request #367

jonatjano opened this issue Feb 24, 2023 · 6 comments

Comments

@jonatjano
Copy link

jonatjano commented Feb 24, 2023

The bug exists in both embedded_httpd_threads and embedded_httpd_process

context : my program is executed on a shared server were http(s) requests are redirected by a Gateway which I have no control on

when using arsd.cgi to receive a request I found that HEAD requests always responded by 502 Bad Gateway while any other method worked as expected, after investigating I found that the only difference is in Cgi.close where when receiving HEAD, the outputBuffer is not written (as expected for HEAD requests)
But that writing in close is made with isAll = true which seems to be required even for HEAD requests

adding cgi.write("", true, false) to my code solved the issue

void handler(Cgi cgi)
{
        writeln(cgi.requestMethod.to!string, cgi.requestUri);

        switch (cgi.pathInfo)
        {
                case "/":
                        if (cgi.requestMethod == Cgi.RequestMethod.HEAD) {
                                cgi.setResponseStatus(200);
                                cgi.write("", true, false); // this call solves the issue, in the case of other method, it is made inside of cgi.close
                        } else if (cgi.requestMethod == Cgi.RequestMethod.GET) {
                                cgi.setResponseStatus(200);
                                cgi.setResponseContentType("text/html");
                                cgi.write("Hello, World!");
                        } else {
                                cgi.setResponseStatus("404 Not found");
                                cgi.write("Requested page not found.");
                        }
                        break;
                default:
                        cgi.setResponseStatus("404 Not found");
                        cgi.write("Requested page not found.");
                        break;
        }
        cgi.close;
}
@adamdruppe
Copy link
Owner

i'll have to take a look, make sure the Content-Length is set right. Cuz I'm pretty sure HEAD is supposed to set that, but not actually send the content.

@jonatjano
Copy link
Author

Setting the Content-Length manually with cgi.header("Content-Length: 0"); do not solve the issue

@adamdruppe
Copy link
Owner

it needs to flush the buffers with cgi.write does, but the headers still need to be set as if it was allw ritten

i'll get to it later, i gotta fdo soemthing else rn. im p sure the problem is just that the header buffers aren't flushed but they also need to be set right

@adamdruppe
Copy link
Owner

see cgi.close calls write(null, false, false) so swapping to true might work if the other headers are right

adamdruppe added a commit that referenced this issue Mar 6, 2023
@adamdruppe
Copy link
Owner

Sorry, I got distracted and just tested this now but indeed this very small change does seem to do the job:

6b4cc44

adamdruppe added a commit that referenced this issue Apr 16, 2023
@adamdruppe
Copy link
Owner

I did a v10.9.10 hotfix released for this and a couple other small issues.

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

2 participants