cesanta / mongoose

Embedded Web Server

Home Page:https://mongoose.ws

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

7.13 MG_EV_READ event has no data

kylemilz opened this issue · comments

Hi,

We have an application that uses both MG_EV_HTTP_MSG and MG_EV_READ events to process both complete HTTP requests and partial requests depending on the URL. In mongoose 7.12, we have observed that MG_EV_READ is called before MG_EV_HTTP_MSG which makes sense to us because we have a chance to process a partial HTTP request if needed. Also in this case, if we consume all of the data, MG_EV_HTTP_MSG does not fire.

In 7.13 I am seeing MG_EV_HTTP_MSG event before MG_EV_READ and it consumes all of the received data before we have a chance to process it with MG_EV_READ. Look at this example program below, for 7.12:

sitecntrl:~/mongoose$ git checkout 7.12
HEAD is now at 32559f15 fix Github editor line ending creativity
sitecntrl:~/mongoose$ cat test.c
#include <err.h>

#include "mongoose.h"


static void
fn(struct mg_connection *c, int ev, void *ev_data, void *)
{
        if (ev == MG_EV_HTTP_MSG) {
                printf(">>> MG_EV_HTTP_MSG\n");
        }
        else if (ev == MG_EV_READ) {
                printf(">>> MG_EV_READ, c->recv.len = %d\n", c->recv.len);
                c->recv.len = 0;
        }
}

int
main(int argc, char *argv[])
{
        struct mg_mgr mgr;

        mg_mgr_init(&mgr);
        mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, &mgr);

        while (1)
                mg_mgr_poll(&mgr, 1000);

        mg_mgr_free(&mgr);

        return 0;
}
sitecntrl:~/mongoose$ cc -o read test.c mongoose.c
sitecntrl:~/mongoose$ ./read
(switch to other terminal)
sitecntrl:~/mongoose$ curl http://127.0.0.1:8000
(switch back)
>>> MG_EV_READ, c->recv.len = 78

This makes sense to me. MG_EV_READ is called, consumes that data (sets recv.len = 0), then MG_EV_HTTP_MSG is not called because there is no more data.

But look at what happens in 7.13. Example program slightly modified to cope with signature change:

sitecntrl:~/mongoose$ git checkout 7.13
HEAD is now at 52997c6c update version
sitecntrl:~/mongoose$ cat test.c
#include <err.h>

#include "mongoose.h"


static void
fn(struct mg_connection *c, int ev, void *ev_data)
{
        if (ev == MG_EV_HTTP_MSG) {
                printf(">>> MG_EV_HTTP_MSG\n");
        }
        else if (ev == MG_EV_READ) {
                printf(">>> MG_EV_READ, c->recv.len = %d\n", c->recv.len);
                c->recv.len = 0;
        }
}

int
main(int argc, char *argv[])
{
        struct mg_mgr mgr;

        mg_mgr_init(&mgr);
        mg_http_listen(&mgr, "http://0.0.0.0:8000", fn, &mgr);

        while (1)
                mg_mgr_poll(&mgr, 1000);

        mg_mgr_free(&mgr);

        return 0;
}
sitecntrl:~/mongoose$ cc -o read test.c mongoose.c
sitecntrl:~/mongoose$ ./read
(switch to other terminal)
sitecntrl:~/mongoose$ curl http://127.0.0.1:8000
(switch back)
>>> MG_EV_HTTP_MSG
>>> MG_EV_READ, c->recv.len = 0

You can see that MG_EV_HTTP_MSG has fired before MG_EV_READ and presumably consumed all of the recv buffer data. Is this expected behavior?

Environment

  • mongoose version: 7.12/7.13
  • Compiler/IDE and SDK: gcc 11.3.0
  • Target hardware/board: Arm Cortex A7
  • Target RTOS/OS (if applicable): Linux 5.15

That change is intentional #2581
Please see how we changed similar examples in that and related PRs
#2581 (comment)
Let us know if you need more help