GNU libmicrohttpd C
/pixel.gif కు వచ్చే రిక్వెస్ట్లకు empty GIF తో జవాబిచ్చే, GNU libmicrohttpd కోసం C స్నిపెట్. GIF స్టార్టప్లో ఒక్కసారే డీకోడ్ అవుతుంది.
GNU libmicrohttpd C
#include <microhttpd.h>
#include <string.h>
#include <unistd.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 enum MHD_Result handler(void *cls, struct MHD_Connection *conn, const char *url,
const char *method, const char *version, const char *upload_data,
size_t *upload_data_size, void **req_cls) {
(void) cls; (void) version; (void) upload_data; (void) upload_data_size; (void) req_cls;
int found = strcmp(method, "GET") == 0 && strcmp(url, "/pixel.gif") == 0;
struct MHD_Response *res = found
? MHD_create_response_from_buffer_static(sizeof GIF, GIF)
: MHD_create_response_from_buffer_static(0, "");
if (found) {
MHD_add_response_header(res, "Content-Type", "image/gif");
MHD_add_response_header(res, "Cache-Control", "no-store");
}
enum MHD_Result ret = MHD_queue_response(conn, found ? MHD_HTTP_OK : MHD_HTTP_NOT_FOUND, res);
MHD_destroy_response(res);
return ret;
}
int main(void) {
struct MHD_Daemon *daemon = MHD_start_daemon(
MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, 8080, NULL, NULL,
&handler, NULL, MHD_OPTION_END);
if (daemon == NULL) return 1;
pause(); // the daemon serves requests on its own thread
MHD_stop_daemon(daemon);
return 0;
}
మేము ఈ స్నిపెట్ను రన్ చేసి, అది తిరిగి ఇచ్చిన బైట్లను పోల్చి చూశాం.
చెక్ చేయండి
curl -sI http://localhost:8080/pixel.gif
అధికారిక సైట్: GNU libmicrohttpd