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 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 27 const 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 36 static int 37 ahc_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, size_t *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_buffer (strlen (BUSYPAGE), 60 (void *) BUSYPAGE, 61 MHD_RESPMEM_PERSISTENT); 62 ret = 63 MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, 64 response); 65 MHD_destroy_response (response); 66 return ret; 67 } 68 } 69 70 *ptr = NULL; /* reset when done */ 71 response = MHD_create_response_from_buffer (strlen (me), 72 (void *) me, 73 MHD_RESPMEM_PERSISTENT); 74 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 75 MHD_destroy_response (response); 76 return ret; 77 } 78 79 int 80 main (int argc, char *const *argv) 81 { 82 struct MHD_Daemon *d; 83 84 if (argc != 2) 85 { 86 printf ("%s PORT\n", argv[0]); 87 return 1; 88 } 89 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, 90 atoi (argv[1]), 91 NULL, NULL, &ahc_echo, (void *) askpage, 92 MHD_OPTION_END); 93 if (d == NULL) 94 return 1; 95 (void) getc (stdin); 96 MHD_stop_daemon (d); 97 return 0; 98 } 99 100 /* end of refuse_post_example.c */ 101