emptygif
اللغة: العربية

كل اللغات وأطر العمل

Mongoose C

مقتطف بلغة C لـ Mongoose يستجيب لطلبات /pixel.gif بملف GIF الفارغ، الذي يُفك ترميزه مرة واحدة عند بدء التشغيل.

Mongoose C

التثبيت
curl -LO https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c -LO https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.h
الملف
main.c
التشغيل
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);
}

شغّلنا هذا المقتطف وقارنّا البايتات التي أعادها.

التحقق

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

الموقع الرسمي: Mongoose

المزيد بلغة C