diff options
Diffstat (limited to 'src/examples/fileserver_example_external_select.c')
-rw-r--r-- | src/examples/fileserver_example_external_select.c | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/src/examples/fileserver_example_external_select.c b/src/examples/fileserver_example_external_select.c index 7a270613..705d4b0b 100644 --- a/src/examples/fileserver_example_external_select.c +++ b/src/examples/fileserver_example_external_select.c | |||
@@ -38,6 +38,7 @@ file_reader (void *cls, uint64_t pos, char *buf, size_t max) | |||
38 | return fread (buf, 1, max, file); | 38 | return fread (buf, 1, max, file); |
39 | } | 39 | } |
40 | 40 | ||
41 | |||
41 | static void | 42 | static void |
42 | free_callback (void *cls) | 43 | free_callback (void *cls) |
43 | { | 44 | { |
@@ -45,6 +46,7 @@ free_callback (void *cls) | |||
45 | fclose (file); | 46 | fclose (file); |
46 | } | 47 | } |
47 | 48 | ||
49 | |||
48 | static int | 50 | static int |
49 | ahc_echo (void *cls, | 51 | ahc_echo (void *cls, |
50 | struct MHD_Connection *connection, | 52 | struct MHD_Connection *connection, |
@@ -58,6 +60,7 @@ ahc_echo (void *cls, | |||
58 | struct MHD_Response *response; | 60 | struct MHD_Response *response; |
59 | int ret; | 61 | int ret; |
60 | FILE *file; | 62 | FILE *file; |
63 | int fd; | ||
61 | struct stat buf; | 64 | struct stat buf; |
62 | 65 | ||
63 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) | 66 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) |
@@ -69,12 +72,23 @@ ahc_echo (void *cls, | |||
69 | return MHD_YES; | 72 | return MHD_YES; |
70 | } | 73 | } |
71 | *ptr = NULL; /* reset when done */ | 74 | *ptr = NULL; /* reset when done */ |
72 | if ( (0 == stat (&url[1], &buf)) && | 75 | |
73 | (S_ISREG (buf.st_mode)) ) | 76 | file = fopen (&url[1], "rb"); |
74 | file = fopen (&url[1], "rb"); | 77 | if (NULL != file) |
75 | else | 78 | { |
76 | file = NULL; | 79 | fd = fileno (file); |
77 | if (file == NULL) | 80 | if (-1 == fd) |
81 | return MHD_NO; /* internal error */ | ||
82 | if ( (0 != fstat (fd, &buf)) || | ||
83 | (! S_ISREG (buf.st_mode)) ) | ||
84 | { | ||
85 | /* not a regular file, refuse to serve */ | ||
86 | fclose (file); | ||
87 | file = NULL; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | if (NULL == file) | ||
78 | { | 92 | { |
79 | response = MHD_create_response_from_buffer (strlen (PAGE), | 93 | response = MHD_create_response_from_buffer (strlen (PAGE), |
80 | (void *) PAGE, | 94 | (void *) PAGE, |
@@ -88,7 +102,7 @@ ahc_echo (void *cls, | |||
88 | &file_reader, | 102 | &file_reader, |
89 | file, | 103 | file, |
90 | &free_callback); | 104 | &free_callback); |
91 | if (response == NULL) | 105 | if (NULL == response) |
92 | { | 106 | { |
93 | fclose (file); | 107 | fclose (file); |
94 | return MHD_NO; | 108 | return MHD_NO; |
@@ -99,6 +113,7 @@ ahc_echo (void *cls, | |||
99 | return ret; | 113 | return ret; |
100 | } | 114 | } |
101 | 115 | ||
116 | |||
102 | int | 117 | int |
103 | main (int argc, char *const *argv) | 118 | main (int argc, char *const *argv) |
104 | { | 119 | { |