|
|
@@ -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;
|
|
|
}
|
|
|
|