diff options
author | Christian Grothoff <christian@grothoff.org> | 2008-04-12 20:45:58 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2008-04-12 20:45:58 +0000 |
commit | 5e9c5b7621d550d92f5565622ad0bf147d4d8a3f (patch) | |
tree | 39a50077b389cfea554cdc8c9ce94adaa8c05f96 /src/examples/minimal_example.c | |
parent | a7cd9f029a61823c9285318a1dfd283ffac1912b (diff) | |
download | libmicrohttpd-5e9c5b7621d550d92f5565622ad0bf147d4d8a3f.tar.gz libmicrohttpd-5e9c5b7621d550d92f5565622ad0bf147d4d8a3f.zip |
moving stuff around
Diffstat (limited to 'src/examples/minimal_example.c')
-rw-r--r-- | src/examples/minimal_example.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/examples/minimal_example.c b/src/examples/minimal_example.c new file mode 100644 index 00000000..647a4776 --- /dev/null +++ b/src/examples/minimal_example.c | |||
@@ -0,0 +1,85 @@ | |||
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 minimal_example.c | ||
23 | * @brief minimal example for how to use libmicrohttpd | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "config.h" | ||
28 | #include <microhttpd.h> | ||
29 | #include <stdlib.h> | ||
30 | #ifndef MINGW | ||
31 | #include <unistd.h> | ||
32 | #endif | ||
33 | #include <string.h> | ||
34 | #include <stdio.h> | ||
35 | |||
36 | #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" | ||
37 | |||
38 | static int | ||
39 | ahc_echo (void *cls, | ||
40 | struct MHD_Connection *connection, | ||
41 | const char *url, | ||
42 | const char *method, | ||
43 | const char *version, | ||
44 | const char *upload_data, unsigned int *upload_data_size, void **ptr) | ||
45 | { | ||
46 | static int aptr; | ||
47 | const char *me = cls; | ||
48 | struct MHD_Response *response; | ||
49 | int ret; | ||
50 | |||
51 | if (0 != strcmp (method, "GET")) | ||
52 | return MHD_NO; /* unexpected method */ | ||
53 | if (&aptr != *ptr) | ||
54 | { | ||
55 | /* do never respond on first call */ | ||
56 | *ptr = &aptr; | ||
57 | return MHD_YES; | ||
58 | } | ||
59 | *ptr = NULL; /* reset when done */ | ||
60 | response = MHD_create_response_from_data (strlen (me), | ||
61 | (void *) me, MHD_NO, MHD_NO); | ||
62 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
63 | MHD_destroy_response (response); | ||
64 | return ret; | ||
65 | } | ||
66 | |||
67 | int | ||
68 | main (int argc, char *const *argv) | ||
69 | { | ||
70 | struct MHD_Daemon *d; | ||
71 | |||
72 | if (argc != 3) | ||
73 | { | ||
74 | printf ("%s PORT SECONDS-TO-RUN\n", argv[0]); | ||
75 | return 1; | ||
76 | } | ||
77 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, | ||
78 | atoi (argv[1]), | ||
79 | NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END); | ||
80 | if (d == NULL) | ||
81 | return 1; | ||
82 | sleep (atoi (argv[2])); | ||
83 | MHD_stop_daemon (d); | ||
84 | return 0; | ||
85 | } | ||