ಎಲ್ಲಾ ಭಾಷೆಗಳು ಮತ್ತು ಫ್ರೇಮ್ವರ್ಕ್ಗಳು
libevent (evhttp) C
/pixel.gif ಗೆ ಬರುವ ವಿನಂತಿಗಳಿಗೆ ಖಾಲಿ GIF ನೊಂದಿಗೆ ಉತ್ತರಿಸುವ, libevent (evhttp) ಗಾಗಿ ಒಂದು C ಸ್ನಿಪೆಟ್; GIF ಅನ್ನು ಆರಂಭದಲ್ಲಿ ಒಮ್ಮೆ ಮಾತ್ರ ಡಿಕೋಡ್ ಮಾಡಲಾಗುತ್ತದೆ.
libevent (evhttp) C
#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);
}
ನಾವು ಈ ಸ್ನಿಪೆಟ್ ಅನ್ನು ರನ್ ಮಾಡಿ, ಅದು ಹಿಂತಿರುಗಿಸಿದ ಬೈಟ್ಗಳನ್ನು ಹೋಲಿಸಿದ್ದೇವೆ.
ಪರಿಶೀಲಿಸಿ
curl -sI http://localhost:8080/pixel.gif
ಅಧಿಕೃತ ಸೈಟ್: libevent (evhttp)