diff options
author | Christian Grothoff <christian@grothoff.org> | 2008-04-20 22:59:49 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2008-04-20 22:59:49 +0000 |
commit | e4f1f8fc4a0485b37f581b422746a6a620367329 (patch) | |
tree | 8e6a50a23dce81a11f9641d693422672922e40c3 /src | |
parent | 0cfd955fd733f1cf32d207cdc08718184528d871 (diff) | |
download | libmicrohttpd-e4f1f8fc4a0485b37f581b422746a6a620367329.tar.gz libmicrohttpd-e4f1f8fc4a0485b37f581b422746a6a620367329.zip |
another example
Diffstat (limited to 'src')
-rw-r--r-- | src/examples/Makefile.am | 6 | ||||
-rw-r--r-- | src/examples/fileserver_example_external_select.c | 142 |
2 files changed, 148 insertions, 0 deletions
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am index 4111fa1e..3b87ba5a 100644 --- a/src/examples/Makefile.am +++ b/src/examples/Makefile.am | |||
@@ -21,3 +21,9 @@ fileserver_example_SOURCES = \ | |||
21 | fileserver_example_LDADD = \ | 21 | fileserver_example_LDADD = \ |
22 | $(top_builddir)/src/daemon/libmicrohttpd.la | 22 | $(top_builddir)/src/daemon/libmicrohttpd.la |
23 | 23 | ||
24 | |||
25 | fileserver_example_external_select_SOURCES = \ | ||
26 | fileserver_example_external_select.c | ||
27 | fileserver_example_external_select_LDADD = \ | ||
28 | $(top_builddir)/src/daemon/libmicrohttpd.la | ||
29 | |||
diff --git a/src/examples/fileserver_example_external_select.c b/src/examples/fileserver_example_external_select.c new file mode 100644 index 00000000..9c3119ea --- /dev/null +++ b/src/examples/fileserver_example_external_select.c | |||
@@ -0,0 +1,142 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007, 2008 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_external_select.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 | |||
40 | static int | ||
41 | file_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 | |||
49 | static int | ||
50 | ahc_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 | |||
95 | int | ||
96 | main (int argc, char *const *argv) | ||
97 | { | ||
98 | struct MHD_Daemon *d; | ||
99 | time_t end; | ||
100 | time_t t; | ||
101 | struct timeval tv; | ||
102 | fd_set rs; | ||
103 | fd_set ws; | ||
104 | fd_set es; | ||
105 | int max; | ||
106 | unsigned long long mhd_timeout; | ||
107 | |||
108 | if (argc != 3) | ||
109 | { | ||
110 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | ||
111 | return 1; | ||
112 | } | ||
113 | d = MHD_start_daemon (MHD_USE_DEBUG, | ||
114 | atoi (argv[1]), | ||
115 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); | ||
116 | if (d == NULL) | ||
117 | return 1; | ||
118 | end = time(NULL) + atoi (argv[2]); | ||
119 | while ( (t = time(NULL)) < end) | ||
120 | { | ||
121 | tv.tv_sec = end - t; | ||
122 | tv.tv_usec = 0; | ||
123 | max = 0; | ||
124 | FD_ZERO(&rs); | ||
125 | FD_ZERO(&ws); | ||
126 | FD_ZERO(&es); | ||
127 | MHD_get_fdset(d, &rs, &ws, &es, &max); | ||
128 | if (MHD_get_timeout(d, &mhd_timeout) == MHD_YES) | ||
129 | |||
130 | { | ||
131 | if (tv.tv_sec * 1000 < mhd_timeout) | ||
132 | { | ||
133 | tv.tv_sec = mhd_timeout / 1000; | ||
134 | tv.tv_usec = (mhd_timout - (tv.tv_sec * 1000)) * 1000; | ||
135 | } | ||
136 | } | ||
137 | SELECT(max+1, &rs, &ws, &es, &tv); | ||
138 | MHD_run(d); | ||
139 | } | ||
140 | MHD_stop_daemon (d); | ||
141 | return 0; | ||
142 | } | ||