diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2017-09-29 21:54:05 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2017-09-29 22:07:44 +0300 |
commit | 527700a01e67e35781a4ba789a43e4d982dd5b25 (patch) | |
tree | 3baef651e66417d9ec5246d187ffdd7e1ccaa85f | |
parent | 3ddb4e562b4422a7d7664b83af4e3a2febf4014d (diff) | |
download | libmicrohttpd-527700a01e67e35781a4ba789a43e4d982dd5b25.tar.gz libmicrohttpd-527700a01e67e35781a4ba789a43e4d982dd5b25.zip |
Added ability to compile demos without libmagic,
added more accurate check for libmagic in configure.
-rw-r--r-- | configure.ac | 36 | ||||
-rw-r--r-- | src/examples/Makefile.am | 12 | ||||
-rw-r--r-- | src/examples/demo.c | 16 | ||||
-rw-r--r-- | src/examples/demo_https.c | 16 |
4 files changed, 68 insertions, 12 deletions
diff --git a/configure.ac b/configure.ac index a5a48210..85da74cd 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -1195,12 +1195,36 @@ then | |||
1195 | fi | 1195 | fi |
1196 | AM_CONDITIONAL([HAVE_CURL], [test "x$enable_curl" = "xyes"]) | 1196 | AM_CONDITIONAL([HAVE_CURL], [test "x$enable_curl" = "xyes"]) |
1197 | 1197 | ||
1198 | mhd_have_magic_open='no' | 1198 | mhd_have_libmagic="no" |
1199 | AC_CHECK_HEADERS([magic.h], | 1199 | SAVE_LIBS="$LIBS" |
1200 | [ AC_CHECK_LIB([[magic]], [[magic_open]], [[mhd_have_magic_open='yes']]) ],[], | 1200 | LIBS="$LIBS -lmagic" |
1201 | [AC_INCLUDES_DEFAULT]) | 1201 | AC_MSG_CHECKING([[for suitable libmagic]]) |
1202 | 1202 | AC_LINK_IFELSE( | |
1203 | AM_CONDITIONAL([HAVE_MAGIC], [[test "x$mhd_have_magic_open" = "xyes"]]) | 1203 | [AC_LANG_PROGRAM( |
1204 | [[ | ||
1205 | #include <magic.h> | ||
1206 | ]], | ||
1207 | [[ | ||
1208 | char var_data[256]; | ||
1209 | const char *var_mime; | ||
1210 | magic_t var_magic = magic_open (MAGIC_MIME_TYPE); | ||
1211 | (void)magic_load (var_magic, NULL); | ||
1212 | var_data[0] = 0; | ||
1213 | var_mime = magic_buffer (var_magic, var_data, 1); | ||
1214 | magic_close (var_magic); | ||
1215 | ]] | ||
1216 | ) | ||
1217 | ], | ||
1218 | [ | ||
1219 | AC_DEFINE([HAVE_LIBMAGIC], [1], [Define to 1 if you have suitable libmagic.]) | ||
1220 | mhd_have_libmagic="yes" | ||
1221 | AC_MSG_RESULT([[yes]]) | ||
1222 | ], | ||
1223 | [AC_MSG_RESULT([[no]]) | ||
1224 | ] | ||
1225 | ) | ||
1226 | LIBS="$SAVE_LIBS" | ||
1227 | AM_CONDITIONAL([HAVE_LIBMAGIC], [[test "x$mhd_have_libmagic" = "xyes"]]) | ||
1204 | 1228 | ||
1205 | # large file support (> 4 GB) | 1229 | # large file support (> 4 GB) |
1206 | AC_SYS_LARGEFILE | 1230 | AC_SYS_LARGEFILE |
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am index aae7ff48..58f4b4aa 100644 --- a/src/examples/Makefile.am +++ b/src/examples/Makefile.am | |||
@@ -38,7 +38,6 @@ endif | |||
38 | if HAVE_POSTPROCESSOR | 38 | if HAVE_POSTPROCESSOR |
39 | noinst_PROGRAMS += \ | 39 | noinst_PROGRAMS += \ |
40 | post_example | 40 | post_example |
41 | if HAVE_MAGIC | ||
42 | if HAVE_POSIX_THREADS | 41 | if HAVE_POSIX_THREADS |
43 | noinst_PROGRAMS += demo | 42 | noinst_PROGRAMS += demo |
44 | if ENABLE_HTTPS | 43 | if ENABLE_HTTPS |
@@ -46,7 +45,6 @@ noinst_PROGRAMS += demo_https | |||
46 | endif | 45 | endif |
47 | endif | 46 | endif |
48 | endif | 47 | endif |
49 | endif | ||
50 | 48 | ||
51 | if ENABLE_DAUTH | 49 | if ENABLE_DAUTH |
52 | noinst_PROGRAMS += \ | 50 | noinst_PROGRAMS += \ |
@@ -100,7 +98,10 @@ demo_CPPFLAGS = \ | |||
100 | $(AM_CPPFLAGS) $(CPU_COUNT_DEF) | 98 | $(AM_CPPFLAGS) $(CPU_COUNT_DEF) |
101 | demo_LDADD = \ | 99 | demo_LDADD = \ |
102 | $(top_builddir)/src/microhttpd/libmicrohttpd.la \ | 100 | $(top_builddir)/src/microhttpd/libmicrohttpd.la \ |
103 | $(PTHREAD_LIBS) -lmagic | 101 | $(PTHREAD_LIBS) |
102 | if HAVE_LIBMAGIC | ||
103 | demo_LDADD += -lmagic | ||
104 | endif | ||
104 | 105 | ||
105 | demo_https_SOURCES = \ | 106 | demo_https_SOURCES = \ |
106 | demo_https.c | 107 | demo_https.c |
@@ -110,7 +111,10 @@ demo_https_CPPFLAGS = \ | |||
110 | $(AM_CPPFLAGS) $(CPU_COUNT_DEF) | 111 | $(AM_CPPFLAGS) $(CPU_COUNT_DEF) |
111 | demo_https_LDADD = \ | 112 | demo_https_LDADD = \ |
112 | $(top_builddir)/src/microhttpd/libmicrohttpd.la \ | 113 | $(top_builddir)/src/microhttpd/libmicrohttpd.la \ |
113 | $(PTHREAD_LIBS) -lmagic | 114 | $(PTHREAD_LIBS) |
115 | if HAVE_LIBMAGIC | ||
116 | demo_https_LDADD += -lmagic | ||
117 | endif | ||
114 | 118 | ||
115 | benchmark_SOURCES = \ | 119 | benchmark_SOURCES = \ |
116 | benchmark.c | 120 | benchmark.c |
diff --git a/src/examples/demo.c b/src/examples/demo.c index f6187676..68b18ce2 100644 --- a/src/examples/demo.c +++ b/src/examples/demo.c | |||
@@ -35,7 +35,9 @@ | |||
35 | #include <sys/types.h> | 35 | #include <sys/types.h> |
36 | #include <sys/stat.h> | 36 | #include <sys/stat.h> |
37 | #include <dirent.h> | 37 | #include <dirent.h> |
38 | #ifdef MHD_HAVE_LIBMAGIC | ||
38 | #include <magic.h> | 39 | #include <magic.h> |
40 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
39 | #include <limits.h> | 41 | #include <limits.h> |
40 | #include <ctype.h> | 42 | #include <ctype.h> |
41 | 43 | ||
@@ -52,12 +54,14 @@ | |||
52 | */ | 54 | */ |
53 | #define NUMBER_OF_THREADS CPU_COUNT | 55 | #define NUMBER_OF_THREADS CPU_COUNT |
54 | 56 | ||
57 | #ifdef MHD_HAVE_LIBMAGIC | ||
55 | /** | 58 | /** |
56 | * How many bytes of a file do we give to libmagic to determine the mime type? | 59 | * How many bytes of a file do we give to libmagic to determine the mime type? |
57 | * 16k might be a bit excessive, but ought not hurt performance much anyway, | 60 | * 16k might be a bit excessive, but ought not hurt performance much anyway, |
58 | * and should definitively be on the safe side. | 61 | * and should definitively be on the safe side. |
59 | */ | 62 | */ |
60 | #define MAGIC_HEADER_SIZE (16 * 1024) | 63 | #define MAGIC_HEADER_SIZE (16 * 1024) |
64 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
61 | 65 | ||
62 | 66 | ||
63 | /** | 67 | /** |
@@ -183,10 +187,12 @@ static struct MHD_Response *request_refused_response; | |||
183 | */ | 187 | */ |
184 | static pthread_mutex_t mutex; | 188 | static pthread_mutex_t mutex; |
185 | 189 | ||
190 | #ifdef MHD_HAVE_LIBMAGIC | ||
186 | /** | 191 | /** |
187 | * Global handle to MAGIC data. | 192 | * Global handle to MAGIC data. |
188 | */ | 193 | */ |
189 | static magic_t magic; | 194 | static magic_t magic; |
195 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
190 | 196 | ||
191 | 197 | ||
192 | /** | 198 | /** |
@@ -686,8 +692,10 @@ generate_page (void *cls, | |||
686 | if (0 != strcmp (url, "/")) | 692 | if (0 != strcmp (url, "/")) |
687 | { | 693 | { |
688 | /* should be file download */ | 694 | /* should be file download */ |
695 | #ifdef MHD_HAVE_LIBMAGIC | ||
689 | char file_data[MAGIC_HEADER_SIZE]; | 696 | char file_data[MAGIC_HEADER_SIZE]; |
690 | ssize_t got; | 697 | ssize_t got; |
698 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
691 | const char *mime; | 699 | const char *mime; |
692 | 700 | ||
693 | if ( (0 != strcmp (method, MHD_HTTP_METHOD_GET)) && | 701 | if ( (0 != strcmp (method, MHD_HTTP_METHOD_GET)) && |
@@ -710,13 +718,15 @@ generate_page (void *cls, | |||
710 | return MHD_queue_response (connection, | 718 | return MHD_queue_response (connection, |
711 | MHD_HTTP_NOT_FOUND, | 719 | MHD_HTTP_NOT_FOUND, |
712 | file_not_found_response); | 720 | file_not_found_response); |
721 | #ifdef MHD_HAVE_LIBMAGIC | ||
713 | /* read beginning of the file to determine mime type */ | 722 | /* read beginning of the file to determine mime type */ |
714 | got = read (fd, file_data, sizeof (file_data)); | 723 | got = read (fd, file_data, sizeof (file_data)); |
724 | (void) lseek (fd, 0, SEEK_SET); | ||
715 | if (-1 != got) | 725 | if (-1 != got) |
716 | mime = magic_buffer (magic, file_data, got); | 726 | mime = magic_buffer (magic, file_data, got); |
717 | else | 727 | else |
728 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
718 | mime = NULL; | 729 | mime = NULL; |
719 | (void) lseek (fd, 0, SEEK_SET); | ||
720 | 730 | ||
721 | if (NULL == (response = MHD_create_response_from_fd (buf.st_size, | 731 | if (NULL == (response = MHD_create_response_from_fd (buf.st_size, |
722 | fd))) | 732 | fd))) |
@@ -866,8 +876,10 @@ main (int argc, char *const *argv) | |||
866 | #ifndef MINGW | 876 | #ifndef MINGW |
867 | ignore_sigpipe (); | 877 | ignore_sigpipe (); |
868 | #endif | 878 | #endif |
879 | #ifdef MHD_HAVE_LIBMAGIC | ||
869 | magic = magic_open (MAGIC_MIME_TYPE); | 880 | magic = magic_open (MAGIC_MIME_TYPE); |
870 | (void) magic_load (magic, NULL); | 881 | (void) magic_load (magic, NULL); |
882 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
871 | 883 | ||
872 | (void) pthread_mutex_init (&mutex, NULL); | 884 | (void) pthread_mutex_init (&mutex, NULL); |
873 | file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), | 885 | file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), |
@@ -905,7 +917,9 @@ main (int argc, char *const *argv) | |||
905 | MHD_destroy_response (internal_error_response); | 917 | MHD_destroy_response (internal_error_response); |
906 | update_cached_response (NULL); | 918 | update_cached_response (NULL); |
907 | (void) pthread_mutex_destroy (&mutex); | 919 | (void) pthread_mutex_destroy (&mutex); |
920 | #ifdef MHD_HAVE_LIBMAGIC | ||
908 | magic_close (magic); | 921 | magic_close (magic); |
922 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
909 | return 0; | 923 | return 0; |
910 | } | 924 | } |
911 | 925 | ||
diff --git a/src/examples/demo_https.c b/src/examples/demo_https.c index 51574fd7..f9376de9 100644 --- a/src/examples/demo_https.c +++ b/src/examples/demo_https.c | |||
@@ -36,7 +36,9 @@ | |||
36 | #include <sys/types.h> | 36 | #include <sys/types.h> |
37 | #include <sys/stat.h> | 37 | #include <sys/stat.h> |
38 | #include <dirent.h> | 38 | #include <dirent.h> |
39 | #ifdef MHD_HAVE_LIBMAGIC | ||
39 | #include <magic.h> | 40 | #include <magic.h> |
41 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
40 | #include <limits.h> | 42 | #include <limits.h> |
41 | #include <ctype.h> | 43 | #include <ctype.h> |
42 | 44 | ||
@@ -53,12 +55,14 @@ | |||
53 | */ | 55 | */ |
54 | #define NUMBER_OF_THREADS CPU_COUNT | 56 | #define NUMBER_OF_THREADS CPU_COUNT |
55 | 57 | ||
58 | #ifdef MHD_HAVE_LIBMAGIC | ||
56 | /** | 59 | /** |
57 | * How many bytes of a file do we give to libmagic to determine the mime type? | 60 | * How many bytes of a file do we give to libmagic to determine the mime type? |
58 | * 16k might be a bit excessive, but ought not hurt performance much anyway, | 61 | * 16k might be a bit excessive, but ought not hurt performance much anyway, |
59 | * and should definitively be on the safe side. | 62 | * and should definitively be on the safe side. |
60 | */ | 63 | */ |
61 | #define MAGIC_HEADER_SIZE (16 * 1024) | 64 | #define MAGIC_HEADER_SIZE (16 * 1024) |
65 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
62 | 66 | ||
63 | 67 | ||
64 | /** | 68 | /** |
@@ -184,10 +188,12 @@ static struct MHD_Response *request_refused_response; | |||
184 | */ | 188 | */ |
185 | static pthread_mutex_t mutex; | 189 | static pthread_mutex_t mutex; |
186 | 190 | ||
191 | #ifdef MHD_HAVE_LIBMAGIC | ||
187 | /** | 192 | /** |
188 | * Global handle to MAGIC data. | 193 | * Global handle to MAGIC data. |
189 | */ | 194 | */ |
190 | static magic_t magic; | 195 | static magic_t magic; |
196 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
191 | 197 | ||
192 | 198 | ||
193 | /** | 199 | /** |
@@ -687,8 +693,10 @@ generate_page (void *cls, | |||
687 | if (0 != strcmp (url, "/")) | 693 | if (0 != strcmp (url, "/")) |
688 | { | 694 | { |
689 | /* should be file download */ | 695 | /* should be file download */ |
696 | #ifdef MHD_HAVE_LIBMAGIC | ||
690 | char file_data[MAGIC_HEADER_SIZE]; | 697 | char file_data[MAGIC_HEADER_SIZE]; |
691 | ssize_t got; | 698 | ssize_t got; |
699 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
692 | const char *mime; | 700 | const char *mime; |
693 | 701 | ||
694 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) | 702 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) |
@@ -710,13 +718,15 @@ generate_page (void *cls, | |||
710 | return MHD_queue_response (connection, | 718 | return MHD_queue_response (connection, |
711 | MHD_HTTP_NOT_FOUND, | 719 | MHD_HTTP_NOT_FOUND, |
712 | file_not_found_response); | 720 | file_not_found_response); |
721 | #ifdef MHD_HAVE_LIBMAGIC | ||
713 | /* read beginning of the file to determine mime type */ | 722 | /* read beginning of the file to determine mime type */ |
714 | got = read (fd, file_data, sizeof (file_data)); | 723 | got = read (fd, file_data, sizeof (file_data)); |
724 | (void) lseek (fd, 0, SEEK_SET); | ||
715 | if (-1 != got) | 725 | if (-1 != got) |
716 | mime = magic_buffer (magic, file_data, got); | 726 | mime = magic_buffer (magic, file_data, got); |
717 | else | 727 | else |
728 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
718 | mime = NULL; | 729 | mime = NULL; |
719 | (void) lseek (fd, 0, SEEK_SET); | ||
720 | 730 | ||
721 | if (NULL == (response = MHD_create_response_from_fd (buf.st_size, | 731 | if (NULL == (response = MHD_create_response_from_fd (buf.st_size, |
722 | fd))) | 732 | fd))) |
@@ -915,8 +925,10 @@ main (int argc, char *const *argv) | |||
915 | #ifndef MINGW | 925 | #ifndef MINGW |
916 | ignore_sigpipe (); | 926 | ignore_sigpipe (); |
917 | #endif | 927 | #endif |
928 | #ifdef MHD_HAVE_LIBMAGIC | ||
918 | magic = magic_open (MAGIC_MIME_TYPE); | 929 | magic = magic_open (MAGIC_MIME_TYPE); |
919 | (void) magic_load (magic, NULL); | 930 | (void) magic_load (magic, NULL); |
931 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
920 | 932 | ||
921 | (void) pthread_mutex_init (&mutex, NULL); | 933 | (void) pthread_mutex_init (&mutex, NULL); |
922 | file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), | 934 | file_not_found_response = MHD_create_response_from_buffer (strlen (FILE_NOT_FOUND_PAGE), |
@@ -956,7 +968,9 @@ main (int argc, char *const *argv) | |||
956 | MHD_destroy_response (internal_error_response); | 968 | MHD_destroy_response (internal_error_response); |
957 | update_cached_response (NULL); | 969 | update_cached_response (NULL); |
958 | (void) pthread_mutex_destroy (&mutex); | 970 | (void) pthread_mutex_destroy (&mutex); |
971 | #ifdef MHD_HAVE_LIBMAGIC | ||
959 | magic_close (magic); | 972 | magic_close (magic); |
973 | #endif /* MHD_HAVE_LIBMAGIC */ | ||
960 | return 0; | 974 | return 0; |
961 | } | 975 | } |
962 | 976 | ||