Serving HTML page with images

Hi,

I am using mongoose libs to serve a HTML page displaying two images (without filesystem, files are stored as binaries). The device is the ESP32 using WiFi.

To do so I have this mongoose event handler :

void mongoose_event_handler(struct mg_connection *nc, int ev, void *evData) {
 switch (ev) {
     case MG_EV_HTTP_REQUEST: {
         struct http_message *hm = (struct http_message *) evData;
         if ( mg_vcmp(&hm->uri , "/") == 0 )
         {
         mg_send_head(nc, 200, HTML_index_html_len, "Content-Type: text/html");
         mg_printf(nc, "%s", HTML_index_html);
         }
         else if ( mg_vcmp(&hm->uri , "/logo.png") == 0 )
         {
             mg_send_head(nc, 200, HTML_logo_png_len, "Content-Type: image/png");
             mg_printf(nc, "%s", HTML_logo_png);
         }
         else if ( mg_vcmp(&hm->uri , "/a.png") == 0 )
         {
             mg_send_head(nc, 200, HTML_a_png_len, "Content-Type: image/png");
             mg_printf(nc, "%s", HTML_a_png);
         }
         else
         {
              mg_send_head(nc, 404, 0, "Content-Type: text/plain");
         }
         nc->flags |= MG_F_SEND_AND_CLOSE;
         break;
     }
 }
}

And I set up a task like this :

void mongooseTask(void *data) {
    struct mg_mgr mgr;
    mg_mgr_init(&mgr, NULL);
    struct mg_connection *c = mg_bind(&mgr, ":80", mongoose_event_handler);
    if (c == NULL) {
        vTaskDelete(NULL);
        return;
    }
    mg_set_protocol_http_websocket(c);
    while (serv_queue == NULL){
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }
    while (1) {
        mg_mgr_poll(&mgr, 1000);
    }
}

The HTML page loads as it should, but both images wont display. With wireshark, I have noticed that both images responses are here but only http header and nothing else. So, Google chrome console notice me from a "CONTENT_LENGTH_MISMATCH" error.

Is it that mongoose can't deal with requests that are very close to each other ?

Best regards.

Comments

  • SergeySergey Dublin, Ireland

    Your code is faulty.
    mg_printf(nc, "%s", HTML_a_png); is going to output until the very first \x00 character, that's what %s does. And obviously PNG data would have that zero byte somewhere.
    Use mg_send() instead. Note that the whole file will be buffered into the output buffer, therefore you won't be able to send large files.
    For large files, either implement rate limiting yourself, or resort to a filesystem.
    You're implementing the wheel. Use https://mongoose-iot.com/, don't spend your time on solved problems.

  • Hi Sergey,

    Thank you for your answer !

    My bad for my mistake in printf, I switched to mg_send.

    But indeed, big files serving is too heavy for the heap memory.
    I choosed to store my files as gzip in order to use the "content-encoding" HTTP flag.
    My problem is now solved.

    Thank your for your help !

  • SergeySergey Dublin, Ireland

    You're welcome Louis

Sign In or Register to comment.