1 /*
2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2008 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 * @file fileserver_example_external_select.c
21 * @brief minimal example for how to use libmicrohttpd to server files
22 * @author Christian Grothoff
23 */
24
25 #include "platform.h"
26 #include <microhttpd.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29
30 #define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>"
31
32 static ssize_t
file_reader(void * cls,uint64_t pos,char * buf,size_t max)33 file_reader (void *cls, uint64_t pos, char *buf, size_t max)
34 {
35 FILE *file = cls;
36
37 (void) fseek (file, pos, SEEK_SET);
38 return fread (buf, 1, max, file);
39 }
40
41 static void
free_callback(void * cls)42 free_callback (void *cls)
43 {
44 FILE *file = cls;
45 fclose (file);
46 }
47
48 static int
ahc_echo(void * cls,struct MHD_Connection * connection,const char * url,const char * method,const char * version,const char * upload_data,size_t * upload_data_size,void ** ptr)49 ahc_echo (void *cls,
50 struct MHD_Connection *connection,
51 const char *url,
52 const char *method,
53 const char *version,
54 const char *upload_data,
55 size_t *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, MHD_HTTP_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 if ( (0 == stat (&url[1], &buf)) &&
73 (S_ISREG (buf.st_mode)) )
74 file = fopen (&url[1], "rb");
75 else
76 file = NULL;
77 if (file == NULL)
78 {
79 response = MHD_create_response_from_buffer (strlen (PAGE),
80 (void *) PAGE,
81 MHD_RESPMEM_PERSISTENT);
82 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
83 MHD_destroy_response (response);
84 }
85 else
86 {
87 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */
88 &file_reader,
89 file,
90 &free_callback);
91 if (response == NULL)
92 {
93 fclose (file);
94 return MHD_NO;
95 }
96 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
97 MHD_destroy_response (response);
98 }
99 return ret;
100 }
101
102 int
main(int argc,char * const * argv)103 main (int argc, char *const *argv)
104 {
105 struct MHD_Daemon *d;
106 time_t end;
107 time_t t;
108 struct timeval tv;
109 fd_set rs;
110 fd_set ws;
111 fd_set es;
112 MHD_socket max;
113 MHD_UNSIGNED_LONG_LONG mhd_timeout;
114
115 if (argc != 3)
116 {
117 printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
118 return 1;
119 }
120 d = MHD_start_daemon (MHD_USE_DEBUG,
121 atoi (argv[1]),
122 NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
123 if (d == NULL)
124 return 1;
125 end = time (NULL) + atoi (argv[2]);
126 while ((t = time (NULL)) < end)
127 {
128 tv.tv_sec = end - t;
129 tv.tv_usec = 0;
130 max = 0;
131 FD_ZERO (&rs);
132 FD_ZERO (&ws);
133 FD_ZERO (&es);
134 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
135 break; /* fatal internal error */
136 if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)
137 {
138 if (((MHD_UNSIGNED_LONG_LONG)tv.tv_sec) < mhd_timeout / 1000LL)
139 {
140 tv.tv_sec = mhd_timeout / 1000LL;
141 tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000LL)) * 1000LL;
142 }
143 }
144 select (max + 1, &rs, &ws, &es, &tv);
145 MHD_run (d);
146 }
147 MHD_stop_daemon (d);
148 return 0;
149 }
150