aboutsummaryrefslogtreecommitdiff
path: root/src/examples/demo.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/demo.c')
-rw-r--r--src/examples/demo.c16
1 files changed, 15 insertions, 1 deletions
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 */
184static pthread_mutex_t mutex; 188static 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 */
189static magic_t magic; 194static 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