emptygif
Sprache: Deutsch

Alle Sprachen und Frameworks

Mongoose C

Ein C-Snippet für Mongoose, das Anfragen an /pixel.gif mit dem leeren GIF beantwortet, das einmal beim Start dekodiert wird.

Mongoose C

Installieren
curl -LO https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c -LO https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.h
Datei
main.c
Ausführen
cc -O2 main.c mongoose.c -o pixel && ./pixel
#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);
}

Wir haben dieses Snippet ausgeführt und die zurückgegebenen Bytes verglichen.

Prüfen

curl -sI http://localhost:8080/pixel.gif

Offizielle Website: Mongoose

Mehr in C