0

This code is taken from the example program in Mongoose embedded web server.

The event MG_NEW_REQUEST is called twice. Is this the expected behavior? Why would it process the same request twice? How would you prevent this from doing so?

int i =0;
static void *callback(enum mg_event event,
                      struct mg_connection *conn,
                      const struct mg_request_info *request_info)
{
    if (event == MG_NEW_REQUEST)
    {    
        i++;
        printf("%d \n", i);

        // Echo requested URI back to the client
        mg_printf(conn, "HTTP/1.1 200 OK\r\n"
              "Content-Type: text/html\r\n\r\n"
              "%s", request_info->uri);    
        mg_printf(conn, "Hello World!!"
                  "%s", "");

        //request_info->query_string
        return const_cast<char *>("done");  // Mark as processed
    }
    else
    {
        return NULL;
    }
}

int main(void) {
    struct mg_context *ctx;
    const char *options[] = {"listening_ports", "8080", NULL};

    printf("Test web server, open browser to http://localhost:8080 ");
    ctx = mg_start(&callback, NULL, options);
    getchar();  // Wait until user hits "enter"
    mg_stop(ctx);

    return 0;
}
mpromonet
  • 11,326
  • 43
  • 62
  • 91

1 Answers1

1

I assume that the browser does in fact two requests. One for the url / and another for /favicon.ico.

René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293