diff options
author | Sascha Volkenandt <sascha (at) akv-soft (dot) de> | 2007-01-02 19:18:27 +0000 |
---|---|---|
committer | Sascha Volkenandt <sascha (at) akv-soft (dot) de> | 2007-01-02 19:18:27 +0000 |
commit | 48c46dfdd986ad4a7a0692d05992f7882bef6a88 (patch) | |
tree | 88a3a88a7ab43632850569cba3ab48a1924d9e52 /httpd/tnt | |
download | vdr-plugin-live-48c46dfdd986ad4a7a0692d05992f7882bef6a88.tar.gz vdr-plugin-live-48c46dfdd986ad4a7a0692d05992f7882bef6a88.tar.bz2 |
- initial checkin
Diffstat (limited to 'httpd/tnt')
-rw-r--r-- | httpd/tnt/dispatcher.h | 121 | ||||
-rw-r--r-- | httpd/tnt/gcryptinit.h | 37 | ||||
-rw-r--r-- | httpd/tnt/gnutls.h | 146 | ||||
-rw-r--r-- | httpd/tnt/job.h | 201 | ||||
-rw-r--r-- | httpd/tnt/listener.h | 78 | ||||
-rw-r--r-- | httpd/tnt/openssl.h | 121 | ||||
-rw-r--r-- | httpd/tnt/poller.h | 61 | ||||
-rw-r--r-- | httpd/tnt/regex.h | 79 | ||||
-rw-r--r-- | httpd/tnt/ssl.h | 52 | ||||
-rw-r--r-- | httpd/tnt/tntnet.h | 99 | ||||
-rw-r--r-- | httpd/tnt/worker.h | 91 |
11 files changed, 1086 insertions, 0 deletions
diff --git a/httpd/tnt/dispatcher.h b/httpd/tnt/dispatcher.h new file mode 100644 index 0000000..218efcb --- /dev/null +++ b/httpd/tnt/dispatcher.h @@ -0,0 +1,121 @@ +/* tnt/dispatcher.h + * Copyright (C) 2003-2005 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_DISPATCHER_H +#define TNT_DISPATCHER_H + +#include <cxxtools/thread.h> +#include <tnt/urlmapper.h> +#include <vector> +#include <map> +#include "tnt/regex.h" + +namespace tnt +{ + // Dispatcher - one per host + class Dispatcher : public Urlmapper + { + public: + class CompidentType : public Compident + { + public: + typedef std::vector<std::string> args_type; + + private: + std::string pathinfo; + args_type args; + bool pathinfo_set; + + public: + CompidentType() + : pathinfo_set(false) + { } + + explicit CompidentType(const std::string& ident) + : Compident(ident), + pathinfo_set(false) + { } + + bool hasPathInfo() const + { return pathinfo_set; } + void setPathInfo(const std::string& p) + { pathinfo = p; pathinfo_set = true; } + void setArgs(const args_type& a) + { args = a; } + const std::string& getPathInfo() const + { return pathinfo; } + const args_type& getArgs() const + { return args; } + args_type& getArgsRef() + { return args; } + }; + + private: + typedef std::vector<std::pair<regex, CompidentType> > urlmap_type; + urlmap_type urlmap; // map url to soname/compname + mutable cxxtools::RWLock rwlock; + + typedef std::map<std::pair<std::string, urlmap_type::const_iterator>, + CompidentType> urlMapCacheType; + mutable urlMapCacheType urlMapCache; + static urlMapCacheType::size_type maxUrlMapCache; + + // don't make this public - it's not threadsafe: + CompidentType mapCompNext(const std::string& compUrl, + urlmap_type::const_iterator& pos) const; + + public: + virtual ~Dispatcher() { } + + void addUrlMapEntry(const std::string& url, const CompidentType& ci); + + Compident mapComp(const std::string& compUrl) const; + + static urlMapCacheType::size_type getMaxUrlMapCache() + { return maxUrlMapCache; } + static void setMaxUrlMapCache(urlMapCacheType::size_type s) + { maxUrlMapCache = s; } + + friend class PosType; + + class PosType + { + const Dispatcher& dis; + cxxtools::RdLock lock; + urlmap_type::const_iterator pos; + std::string url; + bool first; + + public: + PosType(const Dispatcher& d, const std::string& u) + : dis(d), + lock(dis.rwlock), + pos(dis.urlmap.begin()), + url(u), + first(true) + { } + + CompidentType getNext(); + }; + }; + +} + +#endif // TNT_DISPATCHER_H + diff --git a/httpd/tnt/gcryptinit.h b/httpd/tnt/gcryptinit.h new file mode 100644 index 0000000..3b6ee33 --- /dev/null +++ b/httpd/tnt/gcryptinit.h @@ -0,0 +1,37 @@ +/* tnt/gcryptinit.h + * Copyright (C) 2003-2005 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_GCRYPTINIT_H +#define TNT_GCRYPTINIT_H + +#include <gcrypt.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +gcry_error_t gcrypt_init(); + +#ifdef __cplusplus +} +#endif + +#endif // TNT_GCRYPTINIT_H + diff --git a/httpd/tnt/gnutls.h b/httpd/tnt/gnutls.h new file mode 100644 index 0000000..8d8811c --- /dev/null +++ b/httpd/tnt/gnutls.h @@ -0,0 +1,146 @@ +/* tnt/gnutls.h + * Copyright (C) 2006 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_GNUTLS_H +#define TNT_GNUTLS_H + +#include <cxxtools/tcpstream.h> +#include <gnutls/gnutls.h> + +namespace tnt +{ + class GnuTlsException : public std::runtime_error + { + unsigned long code; + static std::string formatMessage(const std::string& function, int code_); + + public: + GnuTlsException(const std::string& function, int code_) + : std::runtime_error(formatMessage(function, code_)), + code(code_) + { } + + unsigned long getCode() const + { return code; } + }; + + class GnuTlsInit + { + static unsigned initCount; + static gnutls_dh_params dhParams; + + public: + GnuTlsInit(); + ~GnuTlsInit(); + + gnutls_dh_params getDhParams() const { return dhParams; } + }; + + class GnuTlsX509Cred + { + gnutls_certificate_credentials x509_cred; + GnuTlsInit init; + + public: + GnuTlsX509Cred(const char* certificateFile, const char* privateKeyFile); + ~GnuTlsX509Cred(); + + operator gnutls_certificate_credentials() const { return x509_cred; } + }; + + class GnuTlsServer : public cxxtools::net::Server + { + GnuTlsX509Cred cred; + + public: + GnuTlsServer(const char* certificateFile, const char* privateKeyFile); + + gnutls_certificate_credentials getCred() const { return cred; } + }; + + class GnuTlsStream : public cxxtools::net::Stream + { + gnutls_session session; + + public: + GnuTlsStream() + : session(0) + { } + + explicit GnuTlsStream(int fd) + : cxxtools::net::Stream(fd) + { } + + explicit GnuTlsStream(const GnuTlsServer& server) + { accept(server); } + + ~GnuTlsStream(); + + void accept(const GnuTlsServer& server); + int sslRead(char* buffer, int bufsize) const; + int sslWrite(const char* buffer, int bufsize) const; + void shutdown() const; + }; + + class GnuTls_streambuf : public std::streambuf + { + GnuTlsStream& m_stream; + char_type* m_buffer; + unsigned m_bufsize; + + public: + explicit GnuTls_streambuf(GnuTlsStream& stream, unsigned bufsize = 256, int timeout = -1); + ~GnuTls_streambuf() + { delete[] m_buffer; } + + void setTimeout(int t) { m_stream.setTimeout(t); } + int getTimeout() const { return m_stream.getTimeout(); } + + /// overload std::streambuf + int_type overflow(int_type c); + /// overload std::streambuf + int_type underflow(); + /// overload std::streambuf + int sync(); + }; + + class GnuTls_iostream : public GnuTlsStream, public std::iostream + { + GnuTls_streambuf m_buffer; + + public: + explicit GnuTls_iostream(unsigned bufsize = 256, int timeout = -1) + : GnuTlsStream(-1), + std::iostream(&m_buffer), + m_buffer(*this, bufsize, timeout) + { } + + explicit GnuTls_iostream(const GnuTlsServer& server, unsigned bufsize = 256, int timeout = -1) + : GnuTlsStream(server), + std::iostream(&m_buffer), + m_buffer(*this, bufsize, timeout) + { } + + void setTimeout(int timeout) { m_buffer.setTimeout(timeout); } + int getTimeout() const { return m_buffer.getTimeout(); } + }; +} + +#endif // TNT_GNUTLS_H + diff --git a/httpd/tnt/job.h b/httpd/tnt/job.h new file mode 100644 index 0000000..5e839fd --- /dev/null +++ b/httpd/tnt/job.h @@ -0,0 +1,201 @@ +/* tnt/job.h + * Copyright (C) 2003-2005 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_JOB_H +#define TNT_JOB_H + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include <deque> +#include <cxxtools/thread.h> +#include <cxxtools/tcpstream.h> +#include <tnt/httprequest.h> +#include <tnt/httpparser.h> +#include <tnt/pointer.h> +#include <time.h> +#include "tnt/ssl.h" + +/** +// in tntnet (mainthread): +Jobqueue queue; +void mainloop() +{ + while (1) + { + Jobqueue::JobPtr j = new Tcpjob(); + j->accept(poller.get()); + queue.put(j); + } +} + +// in server (workerthread): +void Server::run() +{ + while (1) + { + Jobqueue::JobPtr j = queue.get(); + std::iostream& socket = j->getStream(); + processRequest(socket); + } +} +*/ + +namespace tnt +{ + /** Job - one per request */ + class Job + { + unsigned keepAliveCounter; + + HttpRequest request; + HttpMessage::Parser parser; + time_t lastAccessTime; + + unsigned refs; + + static unsigned socket_read_timeout; + static unsigned socket_write_timeout; + static unsigned keepalive_max; + static unsigned socket_buffer_size; + + public: + Job() + : keepAliveCounter(keepalive_max), + parser(request), + lastAccessTime(0), + refs(0) + { } + + protected: + virtual ~Job(); + + public: + unsigned addRef() { return ++refs; } + unsigned release() + { + if (--refs == 0) + { + delete this; + return 0; + } + else + return refs; + } + + virtual std::iostream& getStream() = 0; + virtual int getFd() const = 0; + virtual void setRead() = 0; + virtual void setWrite() = 0; + + HttpRequest& getRequest() { return request; } + HttpMessage::Parser& getParser() { return parser; } + + unsigned decrementKeepAliveCounter() + { return keepAliveCounter > 0 ? --keepAliveCounter : 0; } + void clear(); + void touch() { time(&lastAccessTime); } + int msecToTimeout(time_t currentTime) const; + + static void setSocketReadTimeout(unsigned ms) { socket_read_timeout = ms; } + static void setSocketWriteTimeout(unsigned ms) { socket_write_timeout = ms; } + static void setKeepAliveMax(unsigned n) { keepalive_max = n; } + static void setSocketBufferSize(unsigned b) { socket_buffer_size = b; } + + static unsigned getSocketReadTimeout() { return socket_read_timeout; } + static unsigned getSocketWriteTimeout() { return socket_write_timeout; } + static unsigned getKeepAliveTimeout(); + static unsigned getKeepAliveMax() { return keepalive_max; } + static unsigned getSocketBufferSize() { return socket_buffer_size; } + }; + + class Tcpjob : public Job + { + cxxtools::net::iostream socket; + + public: + Tcpjob() + : socket(getSocketBufferSize(), getSocketReadTimeout()) + { } + + void accept(const cxxtools::net::Server& listener); + + std::iostream& getStream(); + int getFd() const; + void setRead(); + void setWrite(); + }; + +#ifdef USE_SSL + class SslTcpjob : public Job + { + ssl_iostream socket; + + public: + SslTcpjob() + : socket(getSocketBufferSize(), getSocketReadTimeout()) + { } + + void accept(const SslServer& listener); + + std::iostream& getStream(); + int getFd() const; + void setRead(); + void setWrite(); + }; +#endif // USE_SSL + + /** Jobqueue - one per process */ + class Jobqueue + { + public: + typedef Pointer<Job> JobPtr; + + cxxtools::Condition noWaitThreads; + + private: + std::deque<JobPtr> jobs; + cxxtools::Mutex mutex; + cxxtools::Condition notEmpty; + cxxtools::Condition notFull; + unsigned waitThreads; + unsigned capacity; + + public: + explicit Jobqueue(unsigned capacity_) + : waitThreads(0), + capacity(capacity_) + { } + + void put(JobPtr j); + JobPtr get(); + + void setCapacity(unsigned c) + { capacity = c; } + unsigned getCapacity() const + { return capacity; } + unsigned getWaitThreadCount() const + { return waitThreads; } + bool empty() const + { return jobs.empty(); } + }; +} + +#endif // TNT_JOB_H + diff --git a/httpd/tnt/listener.h b/httpd/tnt/listener.h new file mode 100644 index 0000000..4991763 --- /dev/null +++ b/httpd/tnt/listener.h @@ -0,0 +1,78 @@ +/* tnt/listener.h + * Copyright (C) 2003 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_LISTENER_H +#define TNT_LISTENER_H + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include <cxxtools/thread.h> +#include "tnt/job.h" + +namespace tnt +{ + class ListenerBase : public cxxtools::AttachedThread + { + std::string ipaddr; + unsigned short int port; + + public: + ListenerBase(const std::string& ipaddr_, unsigned short int port_) + : ipaddr(ipaddr_), + port(port_) + { } + + void doStop(); + }; + + class Listener : public ListenerBase + { + cxxtools::net::Server server; + Jobqueue& queue; + static int backlog; + static unsigned listenRetry; + + public: + Listener(const std::string& ipaddr, unsigned short int port, Jobqueue& q); + virtual void run(); + + static void setBacklog(int backlog_) { backlog = backlog_; } + static int getBacklog() { return backlog; } + static void setListenRetry(unsigned listenRetry_) { listenRetry = listenRetry_; } + static unsigned getListenRetry() { return listenRetry; } + }; + +#ifdef USE_SSL + class Ssllistener : public ListenerBase + { + SslServer server; + Jobqueue& queue; + + public: + Ssllistener(const char* certificateFile, const char* keyFile, + const std::string& ipaddr, unsigned short int port, Jobqueue& q); + virtual void run(); + }; +#endif // USE_SSL + +} + +#endif // TNT_LISTENER_H + diff --git a/httpd/tnt/openssl.h b/httpd/tnt/openssl.h new file mode 100644 index 0000000..863782f --- /dev/null +++ b/httpd/tnt/openssl.h @@ -0,0 +1,121 @@ +/* tnt/openssl.h + * Copyright (C) 2003 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_OPENSSL_H +#define TNT_OPENSSL_H + +#include <cxxtools/tcpstream.h> +#include <openssl/ssl.h> + +namespace tnt +{ + class OpensslException : public std::runtime_error + { + unsigned long code; + + public: + OpensslException(const std::string& what, unsigned long code_) + : std::runtime_error(what), + code(code_) + { } + + unsigned long getCode() const + { return code; } + }; + + class OpensslServer : public cxxtools::net::Server + { + SSL_CTX* ctx; + void installCertificates(const char* certificateFile, const char* privateKeyFile); + + public: + OpensslServer(const char* certificateFile); + OpensslServer(const char* certificateFile, const char* privateKeyFile); + ~OpensslServer(); + + SSL_CTX* getSslContext() const { return ctx; } + }; + + class OpensslStream : public cxxtools::net::Stream + { + SSL* ssl; + + public: + OpensslStream(); + + explicit OpensslStream(int fd) + : cxxtools::net::Stream(fd) + { } + + explicit OpensslStream(const OpensslServer& server); + ~OpensslStream(); + + void accept(const OpensslServer& server); + + int sslRead(char* buffer, int bufsize) const; + int sslWrite(const char* buffer, int bufsize) const; + void shutdown() const; + }; + + class openssl_streambuf : public std::streambuf + { + OpensslStream& m_stream; + char_type* m_buffer; + unsigned m_bufsize; + + public: + explicit openssl_streambuf(OpensslStream& stream, unsigned bufsize = 256, int timeout = -1); + ~openssl_streambuf() + { delete[] m_buffer; } + + void setTimeout(int t) { m_stream.setTimeout(t); } + int getTimeout() const { return m_stream.getTimeout(); } + + /// overload std::streambuf + int_type overflow(int_type c); + /// overload std::streambuf + int_type underflow(); + /// overload std::streambuf + int sync(); + }; + + class openssl_iostream : public OpensslStream, public std::iostream + { + openssl_streambuf m_buffer; + + public: + explicit openssl_iostream(unsigned bufsize = 256, int timeout = -1) + : OpensslStream(-1), + std::iostream(&m_buffer), + m_buffer(*this, bufsize, timeout) + { } + + explicit openssl_iostream(const OpensslServer& server, unsigned bufsize = 256, int timeout = -1) + : OpensslStream(server), + std::iostream(&m_buffer), + m_buffer(*this, bufsize, timeout) + { } + + void setTimeout(int timeout) { m_buffer.setTimeout(timeout); } + int getTimeout() const { return m_buffer.getTimeout(); } + }; +} + +#endif // TNT_OPENSSL_H + diff --git a/httpd/tnt/poller.h b/httpd/tnt/poller.h new file mode 100644 index 0000000..d9b12e9 --- /dev/null +++ b/httpd/tnt/poller.h @@ -0,0 +1,61 @@ +/* tnt/poller.h + * Copyright (C) 2005 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_POLLER_H +#define TNT_POLLER_H + +#include "tnt/job.h" +#include <cxxtools/dynbuffer.h> +#include <cxxtools/thread.h> +#include <sys/poll.h> + +namespace tnt +{ + class Poller : public cxxtools::AttachedThread + { + Jobqueue& queue; + int notify_pipe[2]; + + typedef std::deque<Jobqueue::JobPtr> jobs_type; + + jobs_type current_jobs; + cxxtools::Dynbuffer<pollfd> pollfds; + + jobs_type new_jobs; + + cxxtools::Mutex mutex; + int poll_timeout; + + void append_new_jobs(); + void append(Jobqueue::JobPtr& job); + void dispatch(); + void remove(jobs_type::size_type n); + + public: + Poller(Jobqueue& q); + + virtual void run(); + void doStop(); + void addIdleJob(Jobqueue::JobPtr job); + }; + +} + +#endif // TNT_POLLER_H + diff --git a/httpd/tnt/regex.h b/httpd/tnt/regex.h new file mode 100644 index 0000000..b2a30e8 --- /dev/null +++ b/httpd/tnt/regex.h @@ -0,0 +1,79 @@ +/* tnt/regex.h + * Copyright (C) 2005 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_REGEX_H +#define TNT_REGEX_H + +#include <string> +#include <sys/types.h> +#include <regex.h> + +namespace tnt +{ + /// collects matches in a regex + class regex_smatch + { + friend class regex; + + std::string str; + regmatch_t matchbuf[10]; + + public: + unsigned size() const; + std::string get(unsigned n) const; + std::string format(const std::string& s) const; + }; + + /// regex(3)-wrapper. + /// Warning: incomplete, but sufficient for tntnet. + /// Regular expression is not automatically freed. Tntnet needs to + /// put regex into a stl-container, so it needs to be copyable. + /// For this class to be complete, the regex_t needs to be + /// reference-counted. This is unneeded for tntnet, because the regex is + /// never freed anyway. + class regex + { + regex_t expr; + + void checkerr(int ret) const; + + public: + explicit regex(const char* ex, int cflags = REG_EXTENDED) + { + checkerr(::regcomp(&expr, ex, cflags)); + } + + explicit regex(const std::string& ex, int cflags = REG_EXTENDED) + { + checkerr(::regcomp(&expr, ex.c_str(), cflags)); + } + + bool match(const std::string& str_, regex_smatch& smatch, int eflags = 0) const; + bool match(const std::string& str_, int eflags = 0) const; + + void free(); + + private: + friend class value_type; + }; + +} + +#endif // TNT_REGEX_H + diff --git a/httpd/tnt/ssl.h b/httpd/tnt/ssl.h new file mode 100644 index 0000000..6660d2f --- /dev/null +++ b/httpd/tnt/ssl.h @@ -0,0 +1,52 @@ +/* tnt/ssl.h + * Copyright (C) 2006 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_SSL_H +#define TNT_SSL_H + +#ifdef WITH_GNUTLS +# include "tnt/gnutls.h" +# define USE_SSL +#endif + +#ifdef WITH_OPENSSL +# include "tnt/openssl.h" +# define USE_SSL +#endif + +namespace tnt +{ +#ifdef WITH_GNUTLS + + typedef GnuTlsServer SslServer; + typedef GnuTls_iostream ssl_iostream; + +#endif + +#ifdef WITH_OPENSSL + + typedef OpensslServer SslServer; + typedef openssl_iostream ssl_iostream; + +#endif + +} + +#endif // TNT_SSL_H + diff --git a/httpd/tnt/tntnet.h b/httpd/tnt/tntnet.h new file mode 100644 index 0000000..a83784e --- /dev/null +++ b/httpd/tnt/tntnet.h @@ -0,0 +1,99 @@ +/* tnt/tntnet.h + * Copyright (C) 2003-2005 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_TNTNET_H +#define TNT_TNTNET_H + +#include <cxxtools/arg.h> +#include "tnt/tntconfig.h" +#include "tnt/job.h" +#include "tnt/poller.h" +#include "tnt/dispatcher.h" +#include <tnt/scopemanager.h> +#include <set> + +namespace tnt +{ + class ListenerBase; + + class Tntnet + { + std::string configFile; + Tntconfig config; + cxxtools::Arg<const char*> propertyfilename; + cxxtools::Arg<bool> debug; + bool isDaemon; + + unsigned minthreads; + unsigned maxthreads; + unsigned long threadstartdelay; + + Jobqueue queue; + + static bool stop; + static int ret; + typedef std::set<ListenerBase*> listeners_type; + listeners_type listeners; + + Poller pollerthread; + Dispatcher d_dispatcher; + + static std::string pidFileName; + + ScopeManager scopemanager; + + // helper methods + void setUser() const; + void setGroup() const; + void setDir(const char* def) const; + int mkDaemon() const; // returns pipe + void closeStdHandles() const; + + // noncopyable + Tntnet(const Tntnet&); + Tntnet& operator= (const Tntnet&); + + void initLogging(); + void writePidfile(int pid); + void monitorProcess(int workerPid); + void initWorkerProcess(); + void workerProcess(int filedes = -1); + + void timerTask(); + void loadConfiguration(); + + public: + Tntnet(int& argc, char* argv[]); + int run(); + + static void shutdown(); + static void restart(); + static bool shouldStop() { return stop; } + + Jobqueue& getQueue() { return queue; } + Poller& getPoller() { return pollerthread; } + const Dispatcher& getDispatcher() const { return d_dispatcher; } + const Tntconfig& getConfig() const { return config; } + ScopeManager& getScopemanager() { return scopemanager; } + }; + +} + +#endif // TNT_TNTNET_H + diff --git a/httpd/tnt/worker.h b/httpd/tnt/worker.h new file mode 100644 index 0000000..be42841 --- /dev/null +++ b/httpd/tnt/worker.h @@ -0,0 +1,91 @@ +/* tnt/worker.h + * Copyright (C) 2003-2005 Tommi Maekitalo + * + * This program 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 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and + * NON-INFRINGEMENT. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef TNT_WORKER_H +#define TNT_WORKER_H + +#include <string> +#include <cxxtools/tcpstream.h> +#include <cxxtools/thread.h> +#include <cxxtools/pool.h> +#include <tnt/comploader.h> +#include <tnt/tntnet.h> +#include <tnt/scope.h> + +namespace tnt +{ + class HttpRequest; + class HttpReply; + + class Worker : public cxxtools::DetachedThread + { + static cxxtools::Mutex mutex; + static unsigned nextThreadNumber; + + Tntnet& application; + + typedef cxxtools::Pool<Comploader> ComploaderPoolType; + static ComploaderPoolType comploaderPool; + + ComploaderPoolType::objectptr_type comploaderObject; + Comploader& comploader; + + Scope threadScope; + + pthread_t threadId; + const char* state; + time_t lastWaitTime; + + typedef std::set<Worker*> workers_type; + static workers_type workers; + + static unsigned maxRequestTime; + static unsigned minThreads; + static bool enableCompression; + + bool processRequest(HttpRequest& request, std::iostream& socket, + unsigned keepAliveCount); + void healthCheck(time_t currentTime); + + ~Worker(); + + public: + Worker(Tntnet& app); + + virtual void run(); + + void dispatch(HttpRequest& request, HttpReply& reply); + + static void timer(); + + /// Sets a hard limit for request-time. + /// When the time is exceeded, this process exits. + static void setMaxRequestTime(unsigned sec) { maxRequestTime = sec; } + static unsigned getMaxRequestTime() { return maxRequestTime; } + + static workers_type::size_type getCountThreads(); + static void setMinThreads(unsigned n) { minThreads = n; } + + static void setEnableCompression(bool sw = true) { enableCompression = sw; } + static unsigned getEnableCompression() { return enableCompression; } + }; +} + +#endif // TNT_WORKER_H + |