Mongoose C
Snippet C kanggo Mongoose sing mangsuli request menyang /pixel.gif nganggo empty GIF, sing didekode sepisan nalika startup.
Mongoose C
#include "mongoose.h"
static const unsigned char GIF[] = {0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b};
static void handler(struct mg_connection *c, int ev, void *ev_data) {
if (ev != MG_EV_HTTP_MSG) return;
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_strcmp(hm->uri, mg_str("/pixel.gif")) == 0) {
mg_printf(c,
"HTTP/1.1 200 OK\r\n"
"Content-Type: image/gif\r\n"
"Content-Length: %d\r\n"
"Cache-Control: no-store\r\n\r\n",
(int) sizeof GIF);
mg_send(c, GIF, sizeof GIF);
} else {
mg_http_reply(c, 404, "", "Not Found\n");
}
}
int main(void) {
struct mg_mgr mgr;
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "http://0.0.0.0:8080", handler, NULL);
for (;;) mg_mgr_poll(&mgr, 1000);
}
Snippet iki wis dilakokake lan byte asile wis dibandhingake.
Priksa
curl -sI http://localhost:8080/pixel.gif