aboutsummaryrefslogtreecommitdiff
path: root/src/examples/https_fileserver_example.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/examples/https_fileserver_example.c')
-rw-r--r--src/examples/https_fileserver_example.c203
1 files changed, 203 insertions, 0 deletions
diff --git a/src/examples/https_fileserver_example.c b/src/examples/https_fileserver_example.c
new file mode 100644
index 00000000..55201ce2
--- /dev/null
+++ b/src/examples/https_fileserver_example.c
@@ -0,0 +1,203 @@
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 https_server_example.c
21 * @brief a simple HTTPS file server using TLS.
22 *
23 * Usage :
24 *
25 * 'http_fileserver_example HTTP-PORT SECONDS-TO-RUN [CERTIFICATE PATH, KEY PATH]'
26 *
27 * The certificate & key are required by the server to operate, Omitting the
28 * path arguments will cause the server to use the hard coded example certificate & key.
29 *
30 * 'certtool' may be used to generate these if required.
31 *
32 * @author Sagie Amir
33 */
34
35#include "platform.h"
36#include <microhttpd.h>
37#include <sys/stat.h>
38
39#include "gnutls.h"
40#include <gcrypt.h>
41
42#define BUF_SIZE 1024
43#define MAX_URL_LEN 255
44
45// TODO remove if unused
46#define CAFILE "ca.pem"
47#define CRLFILE "crl.pem"
48
49#define EMPTY_PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>"
50
51/* Test Certificate */
52const char cert_pem[] =
53 "-----BEGIN CERTIFICATE-----\n"
54 "MIICpjCCAZ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Mlq30N0e3Q61YK9LxMdU1vsluXxNq2rfK1Scb1oOlOOtlbV3zA3VRF\n"
88 "hV3t1nOA9tFmUrwZi0CUMWJE/zbPAyhwWotKyZkCgYEAh0kFicPdbABdrCglXVae\n"
89 "SnfSjVwYkVuGd5Ze0WADvjYsVkYBHTvhgRNnRJMg+/vWz3Sf4Ps4rgUbqK8Vc20b\n"
90 "AU5G6H6tlCvPRGm0ZxrwTWDHTcuKRVs+pJE8C/qWoklE/AAhjluWVoGwUMbPGuiH\n"
91 "6Gf1bgHF6oj/Sq7rv/VLZ8ECgYBeq7ml05YyLuJutuwa4yzQ/MXfghzv4aVyb0F3\n"
92 "QCdXR6o2IYgR6jnSewrZKlA9aPqFJrwHNR6sNXlnSmt5Fcf/RWO/qgJQGLUv3+rG\n"
93 "7kuLTNDR05azSdiZc7J89ID3Bkb+z2YkV+6JUiPq/Ei1+nDBEXb/m+/HqALU/nyj\n"
94 "P3gXeQKBgBusb8Rbd+KgxSA0hwY6aoRTPRt8LNvXdsB9vRcKKHUFQvxUWiUSS+L9\n"
95 "/Qu1sJbrUquKOHqksV5wCnWnAKyJNJlhHuBToqQTgKXjuNmVdYSe631saiI7PHyC\n"
96 "eRJ6DxULPxABytJrYCRrNqmXi5TCiqR2mtfalEMOPxz8rUU8dYyx\n"
97 "-----END RSA PRIVATE KEY-----\n";
98
99static int
100file_reader (void *cls, size_t pos, char *buf, int max)
101{
102 FILE *file = cls;
103
104 fseek (file, pos, SEEK_SET);
105 return fread (buf, 1, max, file);
106}
107
108/* HTTP access handler call back */
109static int
110http_ahc (void *cls,
111 struct MHD_Connection *connection,
112 const char *url,
113 const char *method,
114 const char *upload_data,
115 const char *version, unsigned int *upload_data_size, void **ptr)
116{
117 static int aptr;
118 struct MHD_Response *response;
119 int ret;
120 FILE *file;
121 struct stat buf;
122
123 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
124 return MHD_NO; /* unexpected method */
125 if (&aptr != *ptr)
126 {
127 /* do never respond on first call */
128 *ptr = &aptr;
129 return MHD_YES;
130 }
131 *ptr = NULL; /* reset when done */
132
133 file = fopen (url, "r");
134 if (file == NULL)
135 {
136 response = MHD_create_response_from_data (strlen (EMPTY_PAGE),
137 (void *) EMPTY_PAGE,
138 MHD_NO, MHD_NO);
139 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
140 MHD_destroy_response (response);
141 }
142 else
143 {
144 stat (url, &buf);
145 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
146 &file_reader, file,
147 (MHD_ContentReaderFreeCallback)
148 & fclose);
149 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
150 MHD_destroy_response (response);
151 }
152 return ret;
153}
154
155int
156main (int argc, char *const *argv)
157{
158 struct MHD_Daemon *TLS_daemon;
159
160 if (argc == 3){
161 /* TODO check if this is truly necessary - disallow usage of the blocking /dev/random */
162 /* gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); */
163 TLS_daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG
164 | MHD_USE_SSL, atoi (argv[1]),
165 NULL,
166 NULL, &http_ahc,
167 NULL, MHD_OPTION_CONNECTION_TIMEOUT, 256,
168 MHD_OPTION_HTTPS_MEM_KEY, key_pem,
169 MHD_OPTION_HTTPS_MEM_CERT, cert_pem,
170
171 MHD_OPTION_END);
172 }
173 else if (argc == 5){
174 TLS_daemon = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG
175 | MHD_USE_SSL, atoi (argv[1]),
176 NULL,
177 NULL, &http_ahc,
178 NULL, MHD_OPTION_CONNECTION_TIMEOUT, 256,
179 MHD_OPTION_HTTPS_CERT_PATH, argv[3],
180 MHD_OPTION_HTTPS_KEY_PATH, argv[4],
181 MHD_OPTION_END);
182 }
183 else {
184 printf
185 ("Usage : %s HTTP-PORT SECONDS-TO-RUN [CERTIFICATE PATH, KEY PATH]\n", argv[0]);
186 return 1;
187 }
188
189 if (TLS_daemon == NULL)
190 {
191 printf ("Error: failed to start TLS_daemon");
192 return 1;
193 }
194 else {
195 printf ("MHD daemon listening on port %d\n", atoi (argv[1]));
196 }
197
198 sleep (atoi (argv[2]));
199
200 MHD_stop_daemon (TLS_daemon);
201
202 return 0;
203}