diff options
Diffstat (limited to 'src/examples/demo_https.c')
-rw-r--r-- | src/examples/demo_https.c | 16 |
1 files changed, 15 insertions, 1 deletions
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 | ||