#include #include #include #include #include #define MHD_DIR "~/libmicrohttpd/" /* change it to your MHD directory */ static int compress_buf(void **buf, size_t *size) { Bytef *cbuf; uLongf csize; int ret; csize = compressBound(*size); cbuf = malloc(csize); if (!cbuf) return MHD_NO; ret = compress2(cbuf, &csize, *buf, *size, Z_BEST_COMPRESSION); if ((ret != Z_OK) || (csize >= *size)) { free(cbuf); return MHD_NO; } free(*buf); *buf = cbuf; *size = csize; return MHD_YES; } ssize_t read_cb(void *cls, uint64_t pos, char *buf, size_t size) { size_t zsize; void *zbuf = malloc(size); zsize = fread(zbuf, 1, size, cls); if (zsize == 0) { free(zbuf); return MHD_CONTENT_READER_END_OF_STREAM; } compress_buf(&zbuf, &zsize); memcpy(buf, zbuf, zsize); free(zbuf); return zsize; } void free_cb(void *cls) { fclose(cls); } int ahc_echo(void *cls, struct MHD_Connection *con, const char *url, const char *method, const char *version, const char *data, size_t *size, void **ptr) { struct MHD_Response *res; FILE *file; int ret; if (!*ptr) { *ptr = (void *) 1; return MHD_YES; } *ptr = NULL; file = fopen(MHD_DIR "ChangeLog", "rb"); /*file = fopen(MHD_DIR "README", "rb");*/ res = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 1024, &read_cb, file, &free_cb); MHD_add_response_header(res, MHD_HTTP_HEADER_CONTENT_ENCODING, "deflate"); MHD_add_response_header(res, MHD_HTTP_HEADER_CONTENT_TYPE, "text/plain"); ret = MHD_queue_response(con, MHD_HTTP_OK, res); MHD_destroy_response(res); return ret; } int main() { struct MHD_Daemon *d; d = MHD_start_daemon(MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, 8080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END); getchar(); MHD_stop_daemon(d); return 0; }