diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-11-28 13:36:11 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-12-01 17:42:17 +0300 |
commit | 938b9b8dae70739c6e629bf144b57b5d6212e6b1 (patch) | |
tree | da03387929e65b64a6e48eddde2690fbd82ce724 /src/examples | |
parent | 7f9b98e25631731afd8b853b9f0163dc793a0a57 (diff) | |
download | libmicrohttpd-938b9b8dae70739c6e629bf144b57b5d6212e6b1.tar.gz libmicrohttpd-938b9b8dae70739c6e629bf144b57b5d6212e6b1.zip |
fileserver_example: used MHD built-in function for sending files, added comments
Diffstat (limited to 'src/examples')
-rw-r--r-- | src/examples/fileserver_example.c | 63 |
1 files changed, 25 insertions, 38 deletions
diff --git a/src/examples/fileserver_example.c b/src/examples/fileserver_example.c index 9a46fba1..4d710575 100644 --- a/src/examples/fileserver_example.c +++ b/src/examples/fileserver_example.c | |||
@@ -24,31 +24,21 @@ | |||
24 | 24 | ||
25 | #include "platform.h" | 25 | #include "platform.h" |
26 | #include <microhttpd.h> | 26 | #include <microhttpd.h> |
27 | #ifdef HAVE_UNISTD_H | ||
27 | #include <unistd.h> | 28 | #include <unistd.h> |
29 | #endif /* HAVE_UNISTD_H */ | ||
30 | #ifdef HAVE_SYS_STAT_H | ||
31 | #include <sys/stat.h> | ||
32 | #endif /* HAVE_SYS_STAT_H */ | ||
33 | #ifdef HAVE_FCNTL_H | ||
34 | #include <fcntl.h> | ||
35 | #endif /* HAVE_FCNTL_H */ | ||
28 | 36 | ||
29 | #define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" | 37 | #define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" |
30 | 38 | ||
31 | 39 | #ifndef S_ISREG | |
32 | static ssize_t | 40 | #define S_ISREG(x) (S_IFREG == (x & S_IFREG)) |
33 | file_reader (void *cls, | 41 | #endif /* S_ISREG */ |
34 | uint64_t pos, | ||
35 | char *buf, | ||
36 | size_t max) | ||
37 | { | ||
38 | FILE *file = cls; | ||
39 | |||
40 | (void) fseek (file, pos, SEEK_SET); | ||
41 | return fread (buf, 1, max, file); | ||
42 | } | ||
43 | |||
44 | |||
45 | static void | ||
46 | free_callback (void *cls) | ||
47 | { | ||
48 | FILE *file = cls; | ||
49 | fclose (file); | ||
50 | } | ||
51 | |||
52 | 42 | ||
53 | static int | 43 | static int |
54 | ahc_echo (void *cls, | 44 | ahc_echo (void *cls, |
@@ -62,7 +52,6 @@ ahc_echo (void *cls, | |||
62 | static int aptr; | 52 | static int aptr; |
63 | struct MHD_Response *response; | 53 | struct MHD_Response *response; |
64 | int ret; | 54 | int ret; |
65 | FILE *file; | ||
66 | int fd; | 55 | int fd; |
67 | struct stat buf; | 56 | struct stat buf; |
68 | 57 | ||
@@ -76,24 +65,25 @@ ahc_echo (void *cls, | |||
76 | return MHD_YES; | 65 | return MHD_YES; |
77 | } | 66 | } |
78 | *ptr = NULL; /* reset when done */ | 67 | *ptr = NULL; /* reset when done */ |
79 | file = fopen (&url[1], "rb"); | 68 | /* WARNING: direct usage of url as filename is for example only! |
80 | if (NULL != file) | 69 | * NEVER pass received data directly as parameter to file manipulation |
70 | * functions. Always check validity of data before using. | ||
71 | */ | ||
72 | if (NULL != strstr(url, "../")) /* Very simplified check! */ | ||
73 | fd = -1; /* Do not allow usage of parent directories. */ | ||
74 | else | ||
75 | fd = open (url + 1, O_RDONLY); | ||
76 | if (-1 != fd) | ||
81 | { | 77 | { |
82 | fd = fileno (file); | ||
83 | if (-1 == fd) | ||
84 | { | ||
85 | (void) fclose (file); | ||
86 | return MHD_NO; /* internal error */ | ||
87 | } | ||
88 | if ( (0 != fstat (fd, &buf)) || | 78 | if ( (0 != fstat (fd, &buf)) || |
89 | (! S_ISREG (buf.st_mode)) ) | 79 | (! S_ISREG (buf.st_mode)) ) |
90 | { | 80 | { |
91 | /* not a regular file, refuse to serve */ | 81 | /* not a regular file, refuse to serve */ |
92 | fclose (file); | 82 | close (fd); |
93 | file = NULL; | 83 | fd = -1; |
94 | } | 84 | } |
95 | } | 85 | } |
96 | if (NULL == file) | 86 | if (-1 == fd) |
97 | { | 87 | { |
98 | response = MHD_create_response_from_buffer (strlen (PAGE), | 88 | response = MHD_create_response_from_buffer (strlen (PAGE), |
99 | (void *) PAGE, | 89 | (void *) PAGE, |
@@ -103,13 +93,10 @@ ahc_echo (void *cls, | |||
103 | } | 93 | } |
104 | else | 94 | else |
105 | { | 95 | { |
106 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */ | 96 | response = MHD_create_response_from_fd64(buf.st_size, fd); |
107 | &file_reader, | ||
108 | file, | ||
109 | &free_callback); | ||
110 | if (NULL == response) | 97 | if (NULL == response) |
111 | { | 98 | { |
112 | fclose (file); | 99 | close (fd); |
113 | return MHD_NO; | 100 | return MHD_NO; |
114 | } | 101 | } |
115 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | 102 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |