emptygif
Harshe: Hausa

Duk harsuna da frameworks

libevent (evhttp) C

Snippet na C don libevent (evhttp) da ke amsa requests zuwa /pixel.gif da empty GIF, wanda ake decoding ɗinsa sau ɗaya lokacin farawa.

libevent (evhttp) C

Girkawa
sudo apt install libevent-dev
Fayil
main.c
Gudanar
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);
}

Mun gudanar da wannan snippet kuma mun kwatanta bytes ɗin da ya mayar.

Duba shi

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

Shafin hukuma: libevent (evhttp)

Ƙari a C