diff options
Diffstat (limited to 'src/daemon/fileserver_example.c')
-rw-r--r-- | src/daemon/fileserver_example.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/daemon/fileserver_example.c b/src/daemon/fileserver_example.c new file mode 100644 index 00000000..989ab7d5 --- /dev/null +++ b/src/daemon/fileserver_example.c | |||
@@ -0,0 +1,114 @@ | |||
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 | |||
40 | static int file_reader(void * cls, | ||
41 | size_t pos, | ||
42 | char * buf, | ||
43 | int max) { | ||
44 | FILE * file = cls; | ||
45 | |||
46 | fseek(file, pos, SEEK_SET); | ||
47 | return fread(buf, | ||
48 | 1, | ||
49 | max, | ||
50 | file); | ||
51 | } | ||
52 | |||
53 | static int ahc_echo(void * cls, | ||
54 | struct MHD_Connection * connection, | ||
55 | const char * url, | ||
56 | const char * method, | ||
57 | const char * upload_data, | ||
58 | const char * version, | ||
59 | unsigned int * upload_data_size) { | ||
60 | struct MHD_Response * response; | ||
61 | int ret; | ||
62 | FILE * file; | ||
63 | struct stat buf; | ||
64 | |||
65 | if (0 != strcmp(method, "GET")) | ||
66 | return MHD_NO; /* unexpected method */ | ||
67 | file = fopen(&url[1], "r"); | ||
68 | if (file == NULL) { | ||
69 | response = MHD_create_response_from_data(strlen(PAGE), | ||
70 | (void*) PAGE, | ||
71 | MHD_NO, | ||
72 | MHD_NO); | ||
73 | ret = MHD_queue_response(connection, | ||
74 | MHD_HTTP_NOT_FOUND, | ||
75 | response); | ||
76 | MHD_destroy_response(response); | ||
77 | } else { | ||
78 | stat(&url[1], | ||
79 | &buf); | ||
80 | response = MHD_create_response_from_callback(buf.st_size, | ||
81 | &file_reader, | ||
82 | file, | ||
83 | (MHD_ContentReaderFreeCallback) &fclose); | ||
84 | ret = MHD_queue_response(connection, | ||
85 | MHD_HTTP_OK, | ||
86 | response); | ||
87 | MHD_destroy_response(response); | ||
88 | } | ||
89 | return ret; | ||
90 | } | ||
91 | |||
92 | int main(int argc, | ||
93 | char * const * argv) { | ||
94 | struct MHD_Daemon * d; | ||
95 | |||
96 | if (argc != 3) { | ||
97 | printf("%s PORT SECONDS-TO-RUN\n", | ||
98 | argv[0]); | ||
99 | return 1; | ||
100 | } | ||
101 | d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | ||
102 | atoi(argv[1]), | ||
103 | NULL, | ||
104 | NULL, | ||
105 | &ahc_echo, | ||
106 | PAGE, | ||
107 | MHD_OPTION_END); | ||
108 | if (d == NULL) | ||
109 | return 1; | ||
110 | sleep(atoi(argv[2])); | ||
111 | MHD_stop_daemon(d); | ||
112 | return 0; | ||
113 | } | ||
114 | |||