aboutsummaryrefslogtreecommitdiff
path: root/src/examples/fileserver_example_dirs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/fileserver_example_dirs.c')
-rw-r--r--src/examples/fileserver_example_dirs.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/examples/fileserver_example_dirs.c b/src/examples/fileserver_example_dirs.c
new file mode 100644
index 00000000..3f2ca38b
--- /dev/null
+++ b/src/examples/fileserver_example_dirs.c
@@ -0,0 +1,126 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2007 Christian Grothoff (and other contributing authors)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20/**
21 * @file fileserver_example.c
22 * @brief example for how to use libmicrohttpd to serve files (with directory support)
23 * @author Christian Grothoff
24 */
25
26#include "platform.h"
27#include <dirent.h>
28#include <microhttpd.h>
29#include <unistd.h>
30
31#define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>"
32
33static int
34file_reader (void *cls, uint64_t pos, char *buf, int max)
35{
36 FILE *file = cls;
37
38 fseek (file, pos, SEEK_SET);
39 return fread (buf, 1, max, file);
40}
41
42
43static int
44dir_reader (void *cls, uint64_t pos, char *buf, int max)
45{
46 struct dirent *e;
47 if (max < 512)
48 return 0;
49 e= readdir (cls);
50 if (e == NULL)
51 return -1;
52 return snprintf (buf, max,
53 "<a href=\"/%s\">%s</a><br>",
54 e->d_name,
55 e->d_name);
56}
57
58
59static int
60ahc_echo (void *cls,
61 struct MHD_Connection *connection,
62 const char *url,
63 const char *method,
64 const char *version,
65 const char *upload_data,
66 size_t *upload_data_size, void **ptr)
67{
68 static int aptr;
69 struct MHD_Response *response;
70 int ret;
71 FILE *file;
72 struct stat buf;
73
74 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
75 return MHD_NO; /* unexpected method */
76 if (&aptr != *ptr)
77 {
78 /* do never respond on first call */
79 *ptr = &aptr;
80 return MHD_YES;
81 }
82 *ptr = NULL; /* reset when done */
83 file = fopen (&url[1], "r");
84 if (file == NULL)
85 {
86 response = MHD_create_response_from_callback (0,
87 32 * 1024,
88 &dir_reader,
89 opendir ("."),
90 (MHD_ContentReaderFreeCallback) &closedir);
91 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
92 MHD_destroy_response (response);
93 }
94 else
95 {
96 stat (&url[1], &buf);
97 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */
98 &file_reader,
99 file,
100 (MHD_ContentReaderFreeCallback)
101 & fclose);
102 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
103 MHD_destroy_response (response);
104 }
105 return ret;
106}
107
108int
109main (int argc, char *const *argv)
110{
111 struct MHD_Daemon *d;
112
113 if (argc != 2)
114 {
115 printf ("%s PORT\n", argv[0]);
116 return 1;
117 }
118 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
119 atoi (argv[1]),
120 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
121 if (d == NULL)
122 return 1;
123 while (1) sleep (1);
124 MHD_stop_daemon (d);
125 return 0;
126}