Serve a dynamic index #2659
Replies: 1 comment 1 reply
-
I don't know what to say. Your code looks fine at birdseye view. We don't check user code, btw. https://mongoose.ws/documentation/#http Please follow the guidelines in the documentation, examples and tutorials available. PS: you can do whatever you want, it is C. You just need to know how to do it. |
Beta Was this translation helpful? Give feedback.
-
Hello, I have been using the library for a hobby project with great success so far so kudos for providing this for free. I have been stuck with this problem for a while and have not been able to come up with a solution so far, hence posting. Although appreciate that this might also be a broader web development question but would appreciate any pointers that can be provided. I am not looking for a ready made solution, only some guidance. Also, I am a newbie in this area.
Use case - I want my landing/home page to dynamically be updated for all images within a folder. Every time the page loads it should display all images in the said folder.
Environment - I am using a raspberry pi with Diet-pi
Approaches tried so far - I was initially trying to understand if I can use php to dynamically update index.html and serve it using the standard mg_http_serve_dir function. But I am not clear if there is a way to use php on mongoose.
I then tried to create a page dynamically (using code from the examples and the code that is used in the listdir function. The relevant code is below (this is for the final else section in the event handler). I have skipped the rest of the code because I think the remaining parts work well.
else{
char tmp[10];
size_t off, n;
mg_printf(c,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Content-Length: \r\n\r\n");
off=c->send.len;
mg_printf(c,
"<!DOCTYPE html><html><head><title>My Image</title>"
"<body>\n");
mg_printf(c,
"<img src=\"images/test.gif\" alt=\"test\">");
mg_printf(c,
"</body></html>\n");
n = mg_snprintf(tmp, sizeof(tmp), "%lu", (unsigned long) (c->send.len - off));
if (n > sizeof(tmp)) n = 0;
memcpy(c->send.buf + off - 12, tmp, n); // Set content length
c->is_resp = 0;
}
The above code works but the image does not get loaded. I am not sure what the problem here is. The images folder is located in the same directory as the final compiled file. Also, if I manually update an "index.html" to add the img src and use mg_http_serve_dir, the thing works as expected. But I need this to dynamically add all image file.
Again, appreciate that a lot of this might be because of my sheer lack of experience so would really appreciate any help/direction here.
Thanks,
Nitin
Beta Was this translation helpful? Give feedback.
All reactions