libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 527700a01e67e35781a4ba789a43e4d982dd5b25
parent 3ddb4e562b4422a7d7664b83af4e3a2febf4014d
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Fri, 29 Sep 2017 21:54:05 +0300

Added ability to compile demos without libmagic,
added more accurate check for libmagic in configure.

Diffstat:
Mconfigure.ac | 36++++++++++++++++++++++++++++++------
Msrc/examples/Makefile.am | 12++++++++----
Msrc/examples/demo.c | 16+++++++++++++++-
Msrc/examples/demo_https.c | 16+++++++++++++++-
4 files changed, 68 insertions(+), 12 deletions(-)

diff --git a/configure.ac b/configure.ac @@ -1195,12 +1195,36 @@ then fi AM_CONDITIONAL([HAVE_CURL], [test "x$enable_curl" = "xyes"]) -mhd_have_magic_open='no' -AC_CHECK_HEADERS([magic.h], - [ AC_CHECK_LIB([[magic]], [[magic_open]], [[mhd_have_magic_open='yes']]) ],[], - [AC_INCLUDES_DEFAULT]) - -AM_CONDITIONAL([HAVE_MAGIC], [[test "x$mhd_have_magic_open" = "xyes"]]) +mhd_have_libmagic="no" +SAVE_LIBS="$LIBS" +LIBS="$LIBS -lmagic" +AC_MSG_CHECKING([[for suitable libmagic]]) +AC_LINK_IFELSE( + [AC_LANG_PROGRAM( + [[ +#include <magic.h> + ]], + [[ + char var_data[256]; + const char *var_mime; + magic_t var_magic = magic_open (MAGIC_MIME_TYPE); + (void)magic_load (var_magic, NULL); + var_data[0] = 0; + var_mime = magic_buffer (var_magic, var_data, 1); + magic_close (var_magic); + ]] + ) + ], + [ + AC_DEFINE([HAVE_LIBMAGIC], [1], [Define to 1 if you have suitable libmagic.]) + mhd_have_libmagic="yes" + AC_MSG_RESULT([[yes]]) + ], + [AC_MSG_RESULT([[no]]) + ] +) +LIBS="$SAVE_LIBS" +AM_CONDITIONAL([HAVE_LIBMAGIC], [[test "x$mhd_have_libmagic" = "xyes"]]) # large file support (> 4 GB) AC_SYS_LARGEFILE diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am @@ -38,7 +38,6 @@ endif if HAVE_POSTPROCESSOR noinst_PROGRAMS += \ post_example -if HAVE_MAGIC if HAVE_POSIX_THREADS noinst_PROGRAMS += demo if ENABLE_HTTPS @@ -46,7 +45,6 @@ noinst_PROGRAMS += demo_https endif endif endif -endif if ENABLE_DAUTH noinst_PROGRAMS += \ @@ -100,7 +98,10 @@ demo_CPPFLAGS = \ $(AM_CPPFLAGS) $(CPU_COUNT_DEF) demo_LDADD = \ $(top_builddir)/src/microhttpd/libmicrohttpd.la \ - $(PTHREAD_LIBS) -lmagic + $(PTHREAD_LIBS) +if HAVE_LIBMAGIC +demo_LDADD += -lmagic +endif demo_https_SOURCES = \ demo_https.c @@ -110,7 +111,10 @@ demo_https_CPPFLAGS = \ $(AM_CPPFLAGS) $(CPU_COUNT_DEF) demo_https_LDADD = \ $(top_builddir)/src/microhttpd/libmicrohttpd.la \ - $(PTHREAD_LIBS) -lmagic + $(PTHREAD_LIBS) +if HAVE_LIBMAGIC +demo_https_LDADD += -lmagic +endif benchmark_SOURCES = \ benchmark.c diff --git a/src/examples/demo.c b/src/examples/demo.c @@ -35,7 +35,9 @@ #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> +#ifdef MHD_HAVE_LIBMAGIC #include <magic.h> +#endif /* MHD_HAVE_LIBMAGIC */ #include <limits.h> #include <ctype.h> @@ -52,12 +54,14 @@ */ #define NUMBER_OF_THREADS CPU_COUNT +#ifdef MHD_HAVE_LIBMAGIC /** * How many bytes of a file do we give to libmagic to determine the mime type? * 16k might be a bit excessive, but ought not hurt performance much anyway, * and should definitively be on the safe side. */ #define MAGIC_HEADER_SIZE (16 * 1024) +#endif /* MHD_HAVE_LIBMAGIC */ /** @@ -183,10 +187,12 @@ static struct MHD_Response *request_refused_response; */ static pthread_mutex_t mutex; +#ifdef MHD_HAVE_LIBMAGIC /** * Global handle to MAGIC data. */ static magic_t magic; +#endif /* MHD_HAVE_LIBMAGIC */ /** @@ -686,8 +692,10 @@ generate_page (void *cls, if (0 != strcmp (url, "/")) { /* should be file download */ +#ifdef MHD_HAVE_LIBMAGIC char file_data[MAGIC_HEADER_SIZE]; ssize_t got; +#endif /* MHD_HAVE_LIBMAGIC */ const char *mime; if ( (0 != strcmp (method, MHD_HTTP_METHOD_GET)) && @@ -710,13 +718,15 @@ generate_page (void *cls, return MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, file_not_found_response); +#ifdef MHD_HAVE_LIBMAGIC /* read beginning of the file to determine mime type */ got = read (fd, file_data, sizeof (file_data)); + (void) lseek (fd, 0, SEEK_SET); if (-1 != got) mime = magic_buffer (magic, file_data, got); else +#endif /* MHD_HAVE_LIBMAGIC */ mime = NULL; - (void) lseek (fd, 0, SEEK_SET); if (NULL == (response = MHD_create_response_from_fd (buf.st_size, fd))) @@ -866,8 +876,10 @@ main (int argc, char *const *argv) #ifndef MINGW ignore_sigpipe (); #endif +#ifdef MHD_HAVE_LIBMAGIC magic = magic_open (MAGIC_MIME_TYPE); (void) magic_load (magic, NULL); +#endif /* MHD_HAVE_LIBMAGIC */ (void) pthread_mutex_init (&mutex, NULL); file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), @@ -905,7 +917,9 @@ main (int argc, char *const *argv) MHD_destroy_response (internal_error_response); update_cached_response (NULL); (void) pthread_mutex_destroy (&mutex); +#ifdef MHD_HAVE_LIBMAGIC magic_close (magic); +#endif /* MHD_HAVE_LIBMAGIC */ return 0; } diff --git a/src/examples/demo_https.c b/src/examples/demo_https.c @@ -36,7 +36,9 @@ #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> +#ifdef MHD_HAVE_LIBMAGIC #include <magic.h> +#endif /* MHD_HAVE_LIBMAGIC */ #include <limits.h> #include <ctype.h> @@ -53,12 +55,14 @@ */ #define NUMBER_OF_THREADS CPU_COUNT +#ifdef MHD_HAVE_LIBMAGIC /** * How many bytes of a file do we give to libmagic to determine the mime type? * 16k might be a bit excessive, but ought not hurt performance much anyway, * and should definitively be on the safe side. */ #define MAGIC_HEADER_SIZE (16 * 1024) +#endif /* MHD_HAVE_LIBMAGIC */ /** @@ -184,10 +188,12 @@ static struct MHD_Response *request_refused_response; */ static pthread_mutex_t mutex; +#ifdef MHD_HAVE_LIBMAGIC /** * Global handle to MAGIC data. */ static magic_t magic; +#endif /* MHD_HAVE_LIBMAGIC */ /** @@ -687,8 +693,10 @@ generate_page (void *cls, if (0 != strcmp (url, "/")) { /* should be file download */ +#ifdef MHD_HAVE_LIBMAGIC char file_data[MAGIC_HEADER_SIZE]; ssize_t got; +#endif /* MHD_HAVE_LIBMAGIC */ const char *mime; if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) @@ -710,13 +718,15 @@ generate_page (void *cls, return MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, file_not_found_response); +#ifdef MHD_HAVE_LIBMAGIC /* read beginning of the file to determine mime type */ got = read (fd, file_data, sizeof (file_data)); + (void) lseek (fd, 0, SEEK_SET); if (-1 != got) mime = magic_buffer (magic, file_data, got); else +#endif /* MHD_HAVE_LIBMAGIC */ mime = NULL; - (void) lseek (fd, 0, SEEK_SET); if (NULL == (response = MHD_create_response_from_fd (buf.st_size, fd))) @@ -915,8 +925,10 @@ main (int argc, char *const *argv) #ifndef MINGW ignore_sigpipe (); #endif +#ifdef MHD_HAVE_LIBMAGIC magic = magic_open (MAGIC_MIME_TYPE); (void) magic_load (magic, NULL); +#endif /* MHD_HAVE_LIBMAGIC */ (void) pthread_mutex_init (&mutex, NULL); file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), @@ -956,7 +968,9 @@ main (int argc, char *const *argv) MHD_destroy_response (internal_error_response); update_cached_response (NULL); (void) pthread_mutex_destroy (&mutex); +#ifdef MHD_HAVE_LIBMAGIC magic_close (magic); +#endif /* MHD_HAVE_LIBMAGIC */ return 0; }