| 1 |
/* |
| 2 |
This file is part of libmicrohttpd |
| 3 |
Copyright (C) 2007, 2008 Christian Grothoff (and other contributing authors) |
| 4 |
Copyright (C) 2016-2022 Evgeny Grin (Karlson2k) |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Lesser General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2.1 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public |
| 17 |
License along with this library; if not, write to the Free Software |
| 18 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
*/ |
| 20 |
/** |
| 21 |
* @file refuse_post_example.c |
| 22 |
* @brief example for how to refuse a POST request properly |
| 23 |
* @author Christian Grothoff and Sebastian Gerhardt |
| 24 |
* @author Karlson2k (Evgeny Grin) |
| 25 |
*/ |
| 26 |
#include "platform.h" |
| 27 |
#include <microhttpd.h> |
| 28 |
|
| 29 |
struct handler_param |
| 30 |
{ |
| 31 |
const char *response_page; |
| 32 |
}; |
| 33 |
|
| 34 |
static const char *askpage = |
| 35 |
"<html><body>\n\ |
| 36 |
Upload a file, please!<br>\n\ |
| 37 |
<form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\ |
| 38 |
<input name=\"file\" type=\"file\">\n\ |
| 39 |
<input type=\"submit\" value=\" Send \"></form>\n\ |
| 40 |
</body></html>"; |
| 41 |
|
| 42 |
#define BUSYPAGE \ |
| 43 |
"<html><head><title>Webserver busy</title></head>" \ |
| 44 |
"<body>We are too busy to process POSTs right now.</body></html>" |
| 45 |
|
| 46 |
static enum MHD_Result |
| 47 |
ahc_echo (void *cls, |
| 48 |
struct MHD_Connection *connection, |
| 49 |
const char *url, |
| 50 |
const char *method, |
| 51 |
const char *version, |
| 52 |
const char *upload_data, size_t *upload_data_size, void **req_cls) |
| 53 |
{ |
| 54 |
static int aptr; |
| 55 |
struct handler_param *param = (struct handler_param *) cls; |
| 56 |
struct MHD_Response *response; |
| 57 |
enum MHD_Result ret; |
| 58 |
(void) cls; /* Unused. Silent compiler warning. */ |
| 59 |
(void) url; /* Unused. Silent compiler warning. */ |
| 60 |
(void) version; /* Unused. Silent compiler warning. */ |
| 61 |
(void) upload_data; /* Unused. Silent compiler warning. */ |
| 62 |
(void) upload_data_size; /* Unused. Silent compiler warning. */ |
| 63 |
|
| 64 |
if ((0 != strcmp (method, "GET")) && (0 != strcmp (method, "POST"))) |
| 65 |
return MHD_NO; /* unexpected method */ |
| 66 |
|
| 67 |
if (&aptr != *req_cls) |
| 68 |
{ |
| 69 |
*req_cls = &aptr; |
| 70 |
|
| 71 |
/* always to busy for POST requests */ |
| 72 |
if (0 == strcmp (method, "POST")) |
| 73 |
{ |
| 74 |
response = |
| 75 |
MHD_create_response_from_buffer_static (strlen (BUSYPAGE), |
| 76 |
(const void *) BUSYPAGE); |
| 77 |
ret = |
| 78 |
MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, |
| 79 |
response); |
| 80 |
MHD_destroy_response (response); |
| 81 |
return ret; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
*req_cls = NULL; /* reset when done */ |
| 86 |
response = |
| 87 |
MHD_create_response_from_buffer_static (strlen (param->response_page), |
| 88 |
(const void *) |
| 89 |
param->response_page); |
| 90 |
ret = MHD_queue_response (connection, MHD_HTTP_OK, response); |
| 91 |
MHD_destroy_response (response); |
| 92 |
return ret; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
int |
| 97 |
main (int argc, char *const *argv) |
| 98 |
{ |
| 99 |
struct MHD_Daemon *d; |
| 100 |
struct handler_param data_for_handler; |
| 101 |
int port; |
| 102 |
|
| 103 |
if (argc != 2) |
| 104 |
{ |
| 105 |
printf ("%s PORT\n", argv[0]); |
| 106 |
return 1; |
| 107 |
} |
| 108 |
port = atoi (argv[1]); |
| 109 |
if ( (1 > port) || (port > 65535) ) |
| 110 |
{ |
| 111 |
fprintf (stderr, |
| 112 |
"Port must be a number between 1 and 65535.\n"); |
| 113 |
return 1; |
| 114 |
} |
| 115 |
data_for_handler.response_page = askpage; |
| 116 |
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION |
| 117 |
| MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, |
| 118 |
(uint16_t) port, |
| 119 |
NULL, NULL, &ahc_echo, &data_for_handler, |
| 120 |
MHD_OPTION_END); |
| 121 |
if (d == NULL) |
| 122 |
return 1; |
| 123 |
(void) getc (stdin); |
| 124 |
MHD_stop_daemon (d); |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
/* end of refuse_post_example.c */ |