emptygif
Dil: Türkçe

Tüm diller ve framework'ler

libevent (evhttp) C

libevent (evhttp) için, /pixel.gif isteklerine başlangıçta bir kez çözülen boş GIF ile yanıt veren bir C örneği.

libevent (evhttp) C

Kurulum
sudo apt install libevent-dev
Dosya
main.c
Çalıştır
cc -O2 main.c -o pixel -levent && ./pixel
#include <event2/buffer.h>
#include <event2/event.h>
#include <event2/http.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 pixel(struct evhttp_request *req, void *arg) {
    (void) arg;
    struct evkeyvalq *headers = evhttp_request_get_output_headers(req);
    evhttp_add_header(headers, "Content-Type", "image/gif");
    evhttp_add_header(headers, "Cache-Control", "no-store");

    struct evbuffer *body = evbuffer_new();
    evbuffer_add_reference(body, GIF, sizeof GIF, NULL, NULL); /* no copy */
    evhttp_send_reply(req, HTTP_OK, "OK", body);
    evbuffer_free(body);
}

int main(void) {
    struct event_base *base = event_base_new();
    struct evhttp *http = evhttp_new(base);
    evhttp_set_allowed_methods(http, EVHTTP_REQ_GET | EVHTTP_REQ_HEAD);
    evhttp_set_cb(http, "/pixel.gif", pixel, NULL);
    if (evhttp_bind_socket(http, "0.0.0.0", 8080) != 0) return 1;
    return event_base_dispatch(base);
}

Bu örneği çalıştırıp döndürdüğü baytları karşılaştırdık.

Kontrol et

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

Resmî site: libevent (evhttp)

C için daha fazlası