aboutsummaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2008-08-24 18:20:40 +0000
committerChristian Grothoff <christian@grothoff.org>2008-08-24 18:20:40 +0000
commit337922f27f5f7368e0f3cb674acacbb23736d2be (patch)
tree28dc1440a4625a6fc0e1787653ca1734e95dbfe2 /src/examples
parent9181dd0a072670d61a2e6839f23d7e33165ca089 (diff)
downloadlibmicrohttpd-337922f27f5f7368e0f3cb674acacbb23736d2be.tar.gz
libmicrohttpd-337922f27f5f7368e0f3cb674acacbb23736d2be.zip
example
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/Makefile.am8
-rw-r--r--src/examples/refuse_post_example.c95
2 files changed, 102 insertions, 1 deletions
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am
index aed468b7..bd25184d 100644
--- a/src/examples/Makefile.am
+++ b/src/examples/Makefile.am
@@ -10,7 +10,8 @@ authorization_example \
10minimal_example \ 10minimal_example \
11querystring_example \ 11querystring_example \
12fileserver_example \ 12fileserver_example \
13fileserver_example_external_select 13fileserver_example_external_select \
14refuse_post_example
14 15
15if ENABLE_HTTPS 16if ENABLE_HTTPS
16noinst_PROGRAMS += https_server_example 17noinst_PROGRAMS += https_server_example
@@ -26,6 +27,11 @@ authorization_example_SOURCES = \
26authorization_example_LDADD = \ 27authorization_example_LDADD = \
27 $(top_builddir)/src/daemon/libmicrohttpd.la 28 $(top_builddir)/src/daemon/libmicrohttpd.la
28 29
30refuse_post_example_SOURCES = \
31 refuse_post_example.c
32refuse_post_example_LDADD = \
33 $(top_builddir)/src/daemon/libmicrohttpd.la
34
29querystring_example_SOURCES = \ 35querystring_example_SOURCES = \
30 querystring_example.c 36 querystring_example.c
31querystring_example_LDADD = \ 37querystring_example_LDADD = \
diff --git a/src/examples/refuse_post_example.c b/src/examples/refuse_post_example.c
new file mode 100644
index 00000000..5a2b1eb1
--- /dev/null
+++ b/src/examples/refuse_post_example.c
@@ -0,0 +1,95 @@
1/*
2 This file is part of libmicrohttpd
3 (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 refuse_post_example.c
21 * @brief example for how to refuse a POST request properly
22 * @author Christian Grothoff and Sebastian Gerhardt
23 */
24#include "platform.h"
25#include <microhttpd.h>
26
27const char* askpage = "<html><body>\n\
28 Upload a file, please!<br>\n\
29 <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\
30 <input name=\"file\" type=\"file\">\n\
31 <input type=\"submit\" value=\" Send \"></form>\n\
32 </body></html>";
33
34#define BUSYPAGE "<html><head><title>Webserver busy</title></head><body>We are too busy to process POSTs right now.</body></html>"
35
36static int
37ahc_echo (void *cls,
38 struct MHD_Connection *connection,
39 const char *url,
40 const char *method,
41 const char *version,
42 const char *upload_data, unsigned int *upload_data_size, void **ptr)
43{
44 static int aptr;
45 const char *me = cls;
46 struct MHD_Response *response;
47 int ret;
48
49 if ((0 != strcmp (method, "GET")) && (0 != strcmp (method, "POST")))
50 return MHD_NO; /* unexpected method */
51
52 if (&aptr != *ptr)
53 {
54 *ptr = &aptr;
55
56 /* always to busy for POST requests */
57 if (0 == strcmp (method, "POST"))
58 {
59 response = MHD_create_response_from_data (strlen (BUSYPAGE),
60 (void *) BUSYPAGE, MHD_NO, MHD_NO);
61 ret = MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, response);
62 MHD_destroy_response (response);
63 return ret;
64 }
65 }
66
67 *ptr = NULL; /* reset when done */
68 response = MHD_create_response_from_data (strlen (me),
69 (void *) me, MHD_NO, MHD_NO);
70 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
71 MHD_destroy_response (response);
72 return ret;
73}
74
75int
76main (int argc, char *const *argv)
77{
78 struct MHD_Daemon *d;
79
80 if (argc != 3)
81 {
82 printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
83 return 1;
84 }
85 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
86 atoi (argv[1]),
87 NULL, NULL, &ahc_echo, (void *) askpage, MHD_OPTION_END);
88 if (d == NULL)
89 return 1;
90 sleep (atoi (argv[2]));
91 MHD_stop_daemon (d);
92 return 0;
93}
94
95/* end of refuse_post_example.c */