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