|
Packit |
875988 |
/*
|
|
Packit |
875988 |
This file is part of libmicrohttpd
|
|
Packit |
875988 |
Copyright (C) 2007 Christian Grothoff (and other contributing authors)
|
|
Packit |
875988 |
|
|
Packit |
875988 |
This library is free software; you can redistribute it and/or
|
|
Packit |
875988 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
875988 |
License as published by the Free Software Foundation; either
|
|
Packit |
875988 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
This library is distributed in the hope that it will be useful,
|
|
Packit |
875988 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
875988 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
875988 |
Lesser General Public License for more details.
|
|
Packit |
875988 |
|
|
Packit |
875988 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
875988 |
License along with this library; if not, write to the Free Software
|
|
Packit |
875988 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
/**
|
|
Packit |
875988 |
* @file fileserver_example_dirs.c
|
|
Packit |
875988 |
* @brief example for how to use libmicrohttpd to serve files (with directory support)
|
|
Packit |
875988 |
* @author Christian Grothoff
|
|
Packit |
875988 |
*/
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#include "platform.h"
|
|
Packit |
875988 |
#include <dirent.h>
|
|
Packit |
875988 |
#include <microhttpd.h>
|
|
Packit |
875988 |
#include <unistd.h>
|
|
Packit |
875988 |
|
|
Packit |
875988 |
#define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>"
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static ssize_t
|
|
Packit |
875988 |
file_reader (void *cls, uint64_t pos, char *buf, size_t max)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
FILE *file = cls;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
(void) fseek (file, pos, SEEK_SET);
|
|
Packit |
875988 |
return fread (buf, 1, max, file);
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static void
|
|
Packit |
875988 |
file_free_callback (void *cls)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
FILE *file = cls;
|
|
Packit |
875988 |
fclose (file);
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static void
|
|
Packit |
875988 |
dir_free_callback (void *cls)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
DIR *dir = cls;
|
|
Packit |
875988 |
if (dir != NULL)
|
|
Packit |
875988 |
closedir (dir);
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static ssize_t
|
|
Packit |
875988 |
dir_reader (void *cls, uint64_t pos, char *buf, size_t max)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
DIR *dir = cls;
|
|
Packit |
875988 |
struct dirent *e;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (max < 512)
|
|
Packit |
875988 |
return 0;
|
|
Packit |
875988 |
(void)pos; /* 'pos' is ignored as function return next one single entry per call. */
|
|
Packit |
875988 |
do
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
e = readdir (dir);
|
|
Packit |
875988 |
if (e == NULL)
|
|
Packit |
875988 |
return MHD_CONTENT_READER_END_OF_STREAM;
|
|
Packit |
875988 |
} while (e->d_name[0] == '.');
|
|
Packit |
875988 |
return snprintf (buf, max,
|
|
Packit |
875988 |
"%s ",
|
|
Packit |
875988 |
e->d_name,
|
|
Packit |
875988 |
e->d_name);
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
static int
|
|
Packit |
875988 |
ahc_echo (void *cls,
|
|
Packit |
875988 |
struct MHD_Connection *connection,
|
|
Packit |
875988 |
const char *url,
|
|
Packit |
875988 |
const char *method,
|
|
Packit |
875988 |
const char *version,
|
|
Packit |
875988 |
const char *upload_data,
|
|
Packit |
875988 |
size_t *upload_data_size, void **ptr)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
static int aptr;
|
|
Packit |
875988 |
struct MHD_Response *response;
|
|
Packit |
875988 |
int ret;
|
|
Packit |
875988 |
FILE *file;
|
|
Packit |
875988 |
int fd;
|
|
Packit |
875988 |
DIR *dir;
|
|
Packit |
875988 |
struct stat buf;
|
|
Packit |
875988 |
char emsg[1024];
|
|
Packit |
875988 |
(void)cls; /* Unused. Silent compiler warning. */
|
|
Packit |
875988 |
(void)version; /* Unused. Silent compiler warning. */
|
|
Packit |
875988 |
(void)upload_data; /* Unused. Silent compiler warning. */
|
|
Packit |
875988 |
(void)upload_data_size; /* Unused. Silent compiler warning. */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
|
|
Packit |
875988 |
return MHD_NO; /* unexpected method */
|
|
Packit |
875988 |
if (&aptr != *ptr)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
/* do never respond on first call */
|
|
Packit |
875988 |
*ptr = &apt;;
|
|
Packit |
875988 |
return MHD_YES;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
*ptr = NULL; /* reset when done */
|
|
Packit |
875988 |
|
|
Packit |
875988 |
file = fopen (&url[1], "rb");
|
|
Packit |
875988 |
if (NULL != file)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fd = fileno (file);
|
|
Packit |
875988 |
if (-1 == fd)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
(void) fclose (file);
|
|
Packit |
875988 |
return MHD_NO; /* internal error */
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
if ( (0 != fstat (fd, &buf)) ||
|
|
Packit |
875988 |
(! S_ISREG (buf.st_mode)) )
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
/* not a regular file, refuse to serve */
|
|
Packit |
875988 |
fclose (file);
|
|
Packit |
875988 |
file = NULL;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (NULL == file)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
dir = opendir (".");
|
|
Packit |
875988 |
if (NULL == dir)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
/* most likely cause: more concurrent requests than
|
|
Packit |
875988 |
available file descriptors / 2 */
|
|
Packit |
875988 |
snprintf (emsg,
|
|
Packit |
875988 |
sizeof (emsg),
|
|
Packit |
875988 |
"Failed to open directory `.': %s\n",
|
|
Packit |
875988 |
strerror (errno));
|
|
Packit |
875988 |
response = MHD_create_response_from_buffer (strlen (emsg),
|
|
Packit |
875988 |
emsg,
|
|
Packit |
875988 |
MHD_RESPMEM_MUST_COPY);
|
|
Packit |
875988 |
if (NULL == response)
|
|
Packit |
875988 |
return MHD_NO;
|
|
Packit |
875988 |
ret = MHD_queue_response (connection,
|
|
Packit |
875988 |
MHD_HTTP_SERVICE_UNAVAILABLE,
|
|
Packit |
875988 |
response);
|
|
Packit |
875988 |
MHD_destroy_response (response);
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
else
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
|
|
Packit |
875988 |
32 * 1024,
|
|
Packit |
875988 |
&dir_reader,
|
|
Packit |
875988 |
dir,
|
|
Packit |
875988 |
&dir_free_callback);
|
|
Packit |
875988 |
if (NULL == response)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
closedir (dir);
|
|
Packit |
875988 |
return MHD_NO;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
|
|
Packit |
875988 |
MHD_destroy_response (response);
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
else
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k page size */
|
|
Packit |
875988 |
&file_reader,
|
|
Packit |
875988 |
file,
|
|
Packit |
875988 |
&file_free_callback);
|
|
Packit |
875988 |
if (NULL == response)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
fclose (file);
|
|
Packit |
875988 |
return MHD_NO;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
|
|
Packit |
875988 |
MHD_destroy_response (response);
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
return ret;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
|
|
Packit |
875988 |
|
|
Packit |
875988 |
int
|
|
Packit |
875988 |
main (int argc, char *const *argv)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
struct MHD_Daemon *d;
|
|
Packit |
875988 |
|
|
Packit |
875988 |
if (argc != 2)
|
|
Packit |
875988 |
{
|
|
Packit |
875988 |
printf ("%s PORT\n", argv[0]);
|
|
Packit |
875988 |
return 1;
|
|
Packit |
875988 |
}
|
|
Packit |
875988 |
d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
|
|
Packit |
875988 |
atoi (argv[1]),
|
|
Packit |
875988 |
NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
|
|
Packit |
875988 |
if (NULL == d)
|
|
Packit |
875988 |
return 1;
|
|
Packit |
875988 |
(void) getc (stdin);
|
|
Packit |
875988 |
MHD_stop_daemon (d);
|
|
Packit |
875988 |
return 0;
|
|
Packit |
875988 |
}
|