Mongoose Встроенный веб-сервер
Этот код взят из примера программы на встроенном веб-сервере Mongoose.
Событие MG_NEW_REQUEST вызывается дважды. Это ожидаемое поведение? Зачем обрабатывать один и тот же запрос дважды? Как бы вы помешали этому?
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;
}
1 ответ
Я предполагаю, что браузер фактически выполняет два запроса. Один для URL /
и еще один для /favicon.ico
,