/*
This file is part of GNUnet
(C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file transport/plugin_transport_http.c
* @brief http transport service plugin
* @author Matthias Wachs
*/
#include "plugin_transport_http.h"
#define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
#define _RECEIVE 0
#define _SEND 1
static struct Plugin * p;
/**
* Function that queries MHD's select sets and
* starts the task waiting for them.
* @param plugin plugin
* @param daemon_handle the MHD daemon handle
* @param now schedule now or with MHD delay
* @return gnunet task identifier
*/
static GNUNET_SCHEDULER_TaskIdentifier
server_schedule (struct Plugin *plugin,
struct MHD_Daemon *daemon_handle,
int now);
static void
server_log (void *arg, const char *fmt, va_list ap)
{
char text[1024];
vsnprintf (text, sizeof (text), fmt, ap);
va_end (ap);
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: %s\n", text);
}
/**
* Check if incoming connection is accepted.
* NOTE: Here every connection is accepted
* @param cls plugin as closure
* @param addr address of incoming connection
* @param addr_len address length of incoming connection
* @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
*
*/
static int
server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
{
struct Plugin *plugin = cls;
if (plugin->cur_connections <= plugin->max_connections)
return MHD_YES;
else
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Server: Cannot accept new connections\n");
return MHD_NO;
}
}
#if BUILD_HTTPS
static char *
server_load_file (const char *file)
{
struct GNUNET_DISK_FileHandle *gn_file;
uint64_t fsize;
char *text = NULL;
if (GNUNET_OK != GNUNET_DISK_file_size (file,
&fsize, GNUNET_NO, GNUNET_YES))
return NULL;
text = GNUNET_malloc (fsize + 1);
gn_file =
GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
GNUNET_DISK_PERM_USER_READ);
if (gn_file == NULL)
{
GNUNET_free (text);
return NULL;
}
if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
{
GNUNET_free (text);
GNUNET_DISK_file_close (gn_file);
return NULL;
}
text[fsize] = '\0';
GNUNET_DISK_file_close (gn_file);
return text;
}
#endif
#if BUILD_HTTPS
static int
server_load_certificate (struct Plugin *plugin)
{
int res = GNUNET_OK;
char *key_file;
char *cert_file;
/* Get crypto init str