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.c114
1 files changed, 58 insertions, 56 deletions
diff --git a/src/examples/https_fileserver_example.c b/src/examples/https_fileserver_example.c
index 7e3e6d43..cd7aa355 100644
--- a/src/examples/https_fileserver_example.c
+++ b/src/examples/https_fileserver_example.c
@@ -43,7 +43,8 @@
43#define CAFILE "ca.pem" 43#define CAFILE "ca.pem"
44#define CRLFILE "crl.pem" 44#define CRLFILE "crl.pem"
45 45
46#define EMPTY_PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>" 46#define EMPTY_PAGE \
47 "<html><head><title>File not found</title></head><body>File not found</body></html>"
47 48
48/* Test Certificate */ 49/* Test Certificate */
49const char cert_pem[] = 50const char cert_pem[] =
@@ -117,7 +118,7 @@ http_ahc (void *cls,
117 const char *method, 118 const char *method,
118 const char *version, 119 const char *version,
119 const char *upload_data, 120 const char *upload_data,
120 size_t *upload_data_size, void **ptr) 121 size_t *upload_data_size, void **ptr)
121{ 122{
122 static int aptr; 123 static int aptr;
123 struct MHD_Response *response; 124 struct MHD_Response *response;
@@ -125,60 +126,60 @@ http_ahc (void *cls,
125 FILE *file; 126 FILE *file;
126 int fd; 127 int fd;
127 struct stat buf; 128 struct stat buf;
128 (void)cls; /* Unused. Silent compiler warning. */ 129 (void) cls; /* Unused. Silent compiler warning. */
129 (void)version; /* Unused. Silent compiler warning. */ 130 (void) version; /* Unused. Silent compiler warning. */
130 (void)upload_data; /* Unused. Silent compiler warning. */ 131 (void) upload_data; /* Unused. Silent compiler warning. */
131 (void)upload_data_size; /* Unused. Silent compiler warning. */ 132 (void) upload_data_size; /* Unused. Silent compiler warning. */
132 133
133 if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) 134 if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
134 return MHD_NO; /* unexpected method */ 135 return MHD_NO; /* unexpected method */
135 if (&aptr != *ptr) 136 if (&aptr != *ptr)
136 { 137 {
137 /* do never respond on first call */ 138 /* do never respond on first call */
138 *ptr = &aptr; 139 *ptr = &aptr;
139 return MHD_YES; 140 return MHD_YES;
140 } 141 }
141 *ptr = NULL; /* reset when done */ 142 *ptr = NULL; /* reset when done */
142 143
143 file = fopen (&url[1], "rb"); 144 file = fopen (&url[1], "rb");
144 if (NULL != file) 145 if (NULL != file)
146 {
147 fd = fileno (file);
148 if (-1 == fd)
145 { 149 {
146 fd = fileno (file); 150 (void) fclose (file);
147 if (-1 == fd) 151 return MHD_NO; /* internal error */
148 {
149 (void) fclose (file);
150 return MHD_NO; /* internal error */
151 }
152 if ( (0 != fstat (fd, &buf)) ||
153 (! S_ISREG (buf.st_mode)) )
154 {
155 /* not a regular file, refuse to serve */
156 fclose (file);
157 file = NULL;
158 }
159 } 152 }
160 153 if ( (0 != fstat (fd, &buf)) ||
161 if (NULL == file) 154 (! S_ISREG (buf.st_mode)) )
162 { 155 {
163 response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE), 156 /* not a regular file, refuse to serve */
164 (void *) EMPTY_PAGE, 157 fclose (file);
165 MHD_RESPMEM_PERSISTENT); 158 file = NULL;
166 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
167 MHD_destroy_response (response);
168 } 159 }
160 }
161
162 if (NULL == file)
163 {
164 response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE),
165 (void *) EMPTY_PAGE,
166 MHD_RESPMEM_PERSISTENT);
167 ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
168 MHD_destroy_response (response);
169 }
169 else 170 else
171 {
172 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */
173 &file_reader, file,
174 &file_free_callback);
175 if (NULL == response)
170 { 176 {
171 response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */ 177 fclose (file);
172 &file_reader, file, 178 return MHD_NO;
173 &file_free_callback);
174 if (NULL == response)
175 {
176 fclose (file);
177 return MHD_NO;
178 }
179 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
180 MHD_destroy_response (response);
181 } 179 }
180 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
181 MHD_destroy_response (response);
182 }
182 return ret; 183 return ret;
183} 184}
184 185
@@ -190,22 +191,23 @@ main (int argc, char *const *argv)
190 int port; 191 int port;
191 192
192 if (argc != 2) 193 if (argc != 2)
193 { 194 {
194 printf ("%s PORT\n", argv[0]); 195 printf ("%s PORT\n", argv[0]);
195 return 1; 196 return 1;
196 } 197 }
197 port = atoi (argv[1]); 198 port = atoi (argv[1]);
198 if ( (1 > port) || 199 if ( (1 > port) ||
199 (port > UINT16_MAX) ) 200 (port > UINT16_MAX) )
200 { 201 {
201 fprintf (stderr, 202 fprintf (stderr,
202 "Port must be a number between 1 and 65535\n"); 203 "Port must be a number between 1 and 65535\n");
203 return 1; 204 return 1;
204 } 205 }
205 206
206 TLS_daemon = 207 TLS_daemon =
207 MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | 208 MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
208 MHD_USE_TLS, 209 | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
210 | MHD_USE_TLS,
209 (uint16_t) port, 211 (uint16_t) port,
210 NULL, NULL, 212 NULL, NULL,
211 &http_ahc, NULL, 213 &http_ahc, NULL,
@@ -214,10 +216,10 @@ main (int argc, char *const *argv)
214 MHD_OPTION_HTTPS_MEM_CERT, cert_pem, 216 MHD_OPTION_HTTPS_MEM_CERT, cert_pem,
215 MHD_OPTION_END); 217 MHD_OPTION_END);
216 if (NULL == TLS_daemon) 218 if (NULL == TLS_daemon)
217 { 219 {
218 fprintf (stderr, "Error: failed to start TLS_daemon\n"); 220 fprintf (stderr, "Error: failed to start TLS_daemon\n");
219 return 1; 221 return 1;
220 } 222 }
221 printf ("MHD daemon listening on port %u\n", 223 printf ("MHD daemon listening on port %u\n",
222 (unsigned int) port); 224 (unsigned int) port);
223 225