ESP8266 Webserver Project

Moderator: Sprite_tm

User avatar
By tve
#17484 Is there a clean way to abort a long POST request? E.g. a request where the cgi determines in the first call that the POST is invalid and an error should be returned? I put the following crappy code into my handler and am wondering whether something else could work better:
Code: Select allint ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) {
        int offset = connData->post->received - connData->post->buffLen;
        if (offset == 0) {
                connData->cgiPrivData = NULL;
        } else if (connData->cgiPrivData != NULL) {
                // we have an error condition, do nothing
                return HTTPD_CGI_DONE;
        }

...
        // return an error if there is one
        if (err != NULL) {
                os_printf("Error %d: %s\n", code, err);
                ...
                connData->cgiPrivData = (void *)1;
                return HTTPD_CGI_DONE;
        }

It seems to me that as soon as the handler returns HTTPD_CGI_DONE it should not be called again, but that's not what happens.
User avatar
By Sprite_tm
#17610 Hmm, I wouldn't mind coding something up that can abort a http request, but I have no idea how. As far as I know, the only way to 'abort' such a request is to bluntly close the socket which it's being received on, and that's not the most user-friendly way ever: it causes the webbrowser to display a non-relevant error message or a blank page. As far as I can see, the only way to error out nicely is to take the data and ignore it, and finally send a body with an error message. If you know of any other way, I'd be happy to hear and maybe implement it.
User avatar
By ficeto
#17636 @BySprite_tm exactly :) that is the best way. In HTTP at least... if you kill the connection prematurely, you have no way of sending the browser the cause for the error. Leaving it run might waste time if the content is large (megabytes), so killing it in those cases might prove better, but not recommended.
User avatar
By tve
#17713 I didn't mean a way to close the connection prematurely, I just meant a way to tell the http layer not to continue calling the handler. Right now, the handler has to remember "I already returned an error, I need to just drop what I got on the floor". Instead, the http layer could do this. Or put differently, when the handler returns HTTPD_CGI_DONE it should not be called again.