qse/samples/http/httpd01.c

165 lines
3.0 KiB
C
Raw Normal View History

2016-04-29 03:55:42 +00:00
#include <qse/http/stdhttpd.h>
#include <qse/si/sio.h>
2011-07-12 10:31:33 +00:00
#include <qse/cmn/main.h>
2011-08-02 09:43:48 +00:00
#include <qse/cmn/str.h>
2011-12-28 14:26:02 +00:00
#include <qse/cmn/mem.h>
2012-01-11 14:28:59 +00:00
#include <qse/cmn/mbwc.h>
#include <signal.h>
2012-01-11 14:28:59 +00:00
#include <locale.h>
2012-06-20 15:12:18 +00:00
#if defined(_WIN32)
2012-01-11 14:28:59 +00:00
# include <windows.h>
2012-09-07 15:33:44 +00:00
# include <tchar.h>
# include <process.h>
#elif defined(__OS2__)
# define INCL_DOSPROCESS
# define INCL_DOSEXCEPTIONS
# define INCL_ERRORS
# include <os2.h>
#elif defined(__DOS__)
# include <dos.h>
2012-02-09 12:54:00 +00:00
#else
# include <unistd.h>
# include <errno.h>
2012-04-27 14:33:14 +00:00
#endif
#if defined(HAVE_SSL)
# include <openssl/ssl.h>
# include <openssl/err.h>
# include <openssl/engine.h>
#endif
2012-02-08 12:59:59 +00:00
static qse_httpd_t* g_httpd = QSE_NULL;
static void sigint (int sig)
{
if (g_httpd) qse_httpd_stop (g_httpd);
}
2012-09-12 15:47:41 +00:00
static int httpd_main (int argc, qse_char_t* argv[])
2011-06-23 10:17:35 +00:00
{
2012-02-08 12:59:59 +00:00
qse_httpd_t* httpd = QSE_NULL;
qse_ntime_t tmout;
2012-02-08 12:59:59 +00:00
int ret = -1, i;
2011-06-23 10:17:35 +00:00
if (argc <= 1)
2011-06-22 09:58:50 +00:00
{
2016-04-29 03:55:42 +00:00
qse_fprintf (QSE_STDERR, QSE_T("Usage: %s binding_address:port ...\n"), argv[0]);
2012-02-08 12:59:59 +00:00
goto oops;
2011-06-22 09:58:50 +00:00
}
2011-06-23 10:17:35 +00:00
2016-04-29 03:55:42 +00:00
httpd = qse_httpd_openstd (0, QSE_NULL);
2011-07-12 10:31:33 +00:00
if (httpd == QSE_NULL)
{
2011-07-12 10:31:33 +00:00
qse_fprintf (QSE_STDERR, QSE_T("Cannot open httpd\n"));
2012-02-08 12:59:59 +00:00
goto oops;
}
2012-02-08 12:59:59 +00:00
for (i = 1; i < argc; i++)
{
qse_httpd_server_t* server;
2016-04-29 03:55:42 +00:00
qse_httpd_server_dope_t dope;
2016-04-29 03:55:42 +00:00
qse_memset(&dope, 0, QSE_SIZEOF(dope));
qse_strtonwad (argv[i], &dope.nwad);
server = qse_httpd_attachserverstd (httpd, &dope, 0);
if (server == QSE_NULL)
{
2012-06-20 15:12:18 +00:00
qse_fprintf (QSE_STDERR,
2012-02-08 12:59:59 +00:00
QSE_T("Failed to add httpd listener - %s\n"), argv[i]);
goto oops;
}
}
2012-02-08 12:59:59 +00:00
g_httpd = httpd;
2011-07-12 10:31:33 +00:00
signal (SIGINT, sigint);
2012-12-04 16:44:59 +00:00
#if defined(SIGPIPE)
2011-07-12 10:31:33 +00:00
signal (SIGPIPE, SIG_IGN);
2012-12-04 16:44:59 +00:00
#endif
2011-06-22 09:58:50 +00:00
2016-04-29 03:55:42 +00:00
{
int trait = QSE_HTTPD_CGIERRTONUL;
qse_httpd_setopt (httpd, QSE_HTTPD_TRAIT, &trait);
qse_init_ntime (&tmout, 10, 0);
2016-04-29 03:55:42 +00:00
qse_httpd_setopt (httpd, QSE_HTTPD_TMOUT, &tmout);
}
2016-04-29 03:55:42 +00:00
ret = qse_httpd_loopstd (httpd, QSE_NULL, QSE_NULL);
2011-06-23 10:17:35 +00:00
2011-07-12 10:31:33 +00:00
signal (SIGINT, SIG_DFL);
2012-12-04 16:44:59 +00:00
#if defined(SIGPIPE)
2011-07-12 10:31:33 +00:00
signal (SIGPIPE, SIG_DFL);
2012-12-04 16:44:59 +00:00
#endif
2012-02-08 12:59:59 +00:00
g_httpd = QSE_NULL;
2011-06-22 09:58:50 +00:00
2012-02-08 12:59:59 +00:00
if (ret <= -1) qse_fprintf (QSE_STDERR, QSE_T("Httpd error\n"));
2012-01-11 14:28:59 +00:00
2012-02-08 12:59:59 +00:00
oops:
if (httpd) qse_httpd_close (httpd);
return ret;
2011-06-22 09:58:50 +00:00
}
2011-07-12 10:31:33 +00:00
int qse_main (int argc, qse_achar_t* argv[])
2011-06-22 09:58:50 +00:00
{
int ret;
2017-09-16 08:54:25 +00:00
qse_open_stdsios ();
#if defined(_WIN32)
2012-01-11 14:28:59 +00:00
{
char locale[100];
UINT codepage;
WSADATA wsadata;
codepage = GetConsoleOutputCP();
if (codepage == CP_UTF8)
{
/*SetConsoleOUtputCP (CP_UTF8);*/
qse_setdflcmgrbyid (QSE_CMGR_UTF8);
}
else
{
sprintf (locale, ".%u", (unsigned int)codepage);
setlocale (LC_ALL, locale);
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
}
if (WSAStartup (MAKEWORD(2,0), &wsadata) != 0)
{
qse_fprintf (QSE_STDERR, QSE_T("Failed to start up winsock\n"));
return -1;
}
}
2012-01-11 14:28:59 +00:00
#else
setlocale (LC_ALL, "");
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
2012-01-11 14:28:59 +00:00
#endif
#if defined(HAVE_SSL)
SSL_load_error_strings ();
SSL_library_init ();
#endif
ret = qse_run_main (argc, argv, httpd_main);
#if defined(HAVE_SSL)
/*ERR_remove_state ();*/
ENGINE_cleanup ();
ERR_free_strings ();
EVP_cleanup ();
CRYPTO_cleanup_all_ex_data ();
#endif
#if defined(_WIN32)
WSACleanup ();
#endif
2017-09-16 08:54:25 +00:00
qse_close_stdsios ();
return ret;
2011-06-22 09:58:50 +00:00
}