The root httpd daemon class. More...
#include <httpd.h>


Public Member Functions | |
| httpd (const string &path, int mint=2, int maxt=4) | |
| httpd (int listenport, int inmint=2, int inmaxt=4) | |
| httpd (void) | |
| void | listento (int port) |
| void | listento (ipaddress addr, int port) |
| void | listento (const string &unixsock) |
| ~httpd (void) | |
| void | start (void) |
| int | getload (void) |
| void | setdefaultdocument (int sti, const string &file) |
| int | minthreads (void) |
| int | maxthreads (void) |
| int | maxpostsize (void) |
| const string & | systempath (void) |
| void | minthreads (int i) |
| void | maxthreads (int i) |
| void | maxpostsize (int i) |
| void | systempath (const string &str) |
| virtual void | run (void) |
| void | addobject (httpdobject *) |
| void | addeventhandler (httpdeventhandler *) |
| void | handle (string &uri, string &postbody, value &inhdr, const string &method, const string &httpver, tcpsocket &s, bool &keepalive) |
| void | eventhandle (const value &) |
| unsigned int | sendfile (tcpsocket &s, const string &fn) |
| bool | havedefault (int sti) |
| const string & | defaultdocument (int sti) |
| void | shutdown (void) |
Public Attributes | |
| value | defaultdocuments |
| tcplistener | listener |
| lock< int > | load |
| lock< int > | tcplock |
| threadgroup | workers |
| int | eventmask |
Protected Attributes | |
| httpdobject * | first |
| httpdeventhandler * | firsthandler |
| int | minthr |
| int | maxthr |
| string | syspath |
| int | _maxpostsize |
| bool | _shutdown |
| conditional | shutdowndone |
This will only return generic 404 errors if no httpdobjects are linked to implement some server behavior. Here's a typical example of a httpd being used together with a small chain of httpdobjects to create a basic static webserver:
#include <grace/daemon.h>
#include <grace/httpd.h>
/// Web Server.
/// Implements the HTTP protocol on port 8080.
/// Serves files from /var/www/sites.
class webserverApp : public daemon
{
public:
webserverApp (void) : daemon ("tld.example.app.webserver")
{
}
~webserverApp (void)
{
}
int main (void);
};
bool _shouldShutDown; ///< True if a SIGTERM was received.
APPOBJECT(webserverApp);
/// Signal handler for SIGTERM.
void termHandler (int sig)
{
_shouldShutDown = true;
}
int webserverApp::main (void)
{
value vhosts;
vhosts["*"] = "default"; // /var/www/sites/default
vhosts["www.example.tld"] = "example"; // /var/www/sites/example
// Spawn to background.
daemonize();
// Set handler for SIGTERM.
signal (SIGTERM, termHandler);
// Set up the httpd object.
httpd server (8080);
server.minthreads (2);
server.maxthreads (16);
server.systempath ("/var/www");
// Set up http logging.
new httpdlogger (server, "/var/www/log/access.log", "/var/www/log/error.log");
// Set up vhosts.
new httpdvhost (server, vhosts);
// Set up file serving.
new httpdfileshare (server, "*", "/var/www/sites");
// Start http server.
server.start ();
_shouldShutDown = false;
while (! _shouldShutDown)
{
sleep (1);
}
server.shutdown ();
return 0;
}
| httpd::httpd | ( | const string & | path, | |
| int | mint = 2, |
|||
| int | maxt = 4 | |||
| ) |
Constructor, create httpd object.
communication
| path | listening socket path | |
| mint | Minimum number of threads | |
| maxt | Maximum number of threads |
References _maxpostsize, _shutdown, eventmask, first, firsthandler, load, maxthr, minthr, and defaults::lim::httpd::postsize.
| httpd::httpd | ( | int | listenport, | |
| int | inmint = 2, |
|||
| int | inmaxt = 4 | |||
| ) |
Constructor.
| listenport | The tcp port to listen to | |
| inmint | Minimum number of threads. | |
| inmaxt | Maximum number of threads. |
References _maxpostsize, _shutdown, eventmask, first, firsthandler, load, maxthr, minthr, and defaults::lim::httpd::postsize.
| httpd::httpd | ( | void | ) |
Delayed initialization constructor.
For situations where you want to define the listenport later.
References _maxpostsize, _shutdown, eventmask, first, firsthandler, load, maxthr, minthr, and defaults::lim::httpd::postsize.
| void httpd::addobject | ( | httpdobject * | obj | ) |
Link a httpdobject to the end of the chain.
Called from the constructor of the httpdobject base class.
References first, and httpdobject::next.
Referenced by httpdobject::httpdobject().
| const string & httpd::defaultdocument | ( | int | sti | ) |
Default document path.
| sti | The http status code. |
References defaultdocuments, and value::sval().
Referenced by handle(), httpdfileshare::run(), httpdvhost::run(), and httpdbasicauth::run().

| void httpd::eventhandle | ( | const value & | ev | ) |
Handle an event through the chain of event handlers.
Some example events:
<!-- An access event --> <event class="access"> <string id="method">GET</string> <string id="httpver">1.1</string> <string id="uri">/index.html</string> <string id="file">/web/site1/index.html</string> <string id="ip">10.42.69.5</string> <string id="user"/> <string id="referrer">http:site1/test.html</string> <string id="useragent">Wget/1.8.2</string> <integer id="status">200</integer> <integer id="bytes">194</integer> </event> <!-- an error event --> <event class="error"> <string id="ip">10.42.69.5</string> <string id="text">Client tried to authenticate as user root</string> </event> <!-- a thread-related info event --> <event class="info"> <!-- the counterpart is threadstopped --> <string id="type">threadstarted</string> <string id="thread">httpdworker/10827</string> </event> <!-- a connection-related info event --> <event class="info"> <!-- the counterpart is connectionclosed --> <string id="type">connectionaccepted</string> <string id="thread">httpdworker/10827</string> <!-- connectionclosed messages don't have this part --> <integer id="load">3</integer> <string id="ip">10.42.69.5</string> </event>
References httpdeventhandler::classmatch, firsthandler, httpdeventhandler::handle(), and httpdeventhandler::next.
Referenced by handle(), httpdfileshare::run(), httpdfiletypehandler::run(), httpdvhost::run(), httpdbasicauth::run(), and httpdworker::run().

| int httpd::getload | ( | void | ) |
| void httpd::handle | ( | string & | uri, | |
| string & | postbody, | |||
| value & | inhdr, | |||
| const string & | method, | |||
| const string & | httpver, | |||
| tcpsocket & | s, | |||
| bool & | keepalive | |||
| ) |
Handle a request through the chain.
| uri | The uri of the request | |
| postbody | The posted body data | |
| inhdr | The received headers | |
| method | The HTTP method | |
| httpver | The HTTP version | |
| s | The tcpsocket handling the request | |
| keepalive | Whether HTTP keepalive should be used. |
References value::bval(), threadgroup::count(), string::cropat(), defaultdocument(), eventhandle(), eventmask, value::exists(), first, string::globcmp(), havedefault(), value::ival(), load, httpdobject::next, tcpsocket::peer_name, file::printf(), file::puts(), httpdobject::run(), sendfile(), string::strlen(), tune::httpd::keepalive::trigger, httpdobject::urimatch, and workers.
Referenced by httpdworker::run().

| bool httpd::havedefault | ( | int | sti | ) |
Default document checker.
| sti | The http status code. |
References defaultdocuments, and value::exists().
Referenced by handle(), httpdfileshare::run(), httpdvhost::run(), and httpdbasicauth::run().

| void httpd::listento | ( | ipaddress | addr, | |
| int | port | |||
| ) |
Set listen-port and address post-facto.
If the httpd object was created through the constructor without a listenport, this function binds the listening socket to a specific ip address and port.
| addr | The listening address. | |
| port | The listening port. |
References listener, and tcplistener::listento().

| void httpd::listento | ( | int | port | ) |
Set listen-port post-facto.
If the httpd object was created through the constructor without a listenport, this function binds the listening socket to a specific port.
| port | The port number. |
References listener, and tcplistener::listento().

Use sendfile symantics to send a disk file.
References filesystem::exists(), file::puts(), tcpsocket::sendfile(), and filesystem::size().
Referenced by handle(), and httpdfileshare::run().

| void httpd::setdefaultdocument | ( | int | sti, | |
| const string & | file | |||
| ) |
Set a default document.
Links a file path to a HTTP status code.
References defaultdocuments, string::strcat(), string::strchr(), and systempath().

| void httpd::start | ( | void | ) | [inline] |
Start the server process.
Threads are not started on construction, to allow a full chain of httpdobjects to be built before serving requests.
Reimplemented from thread.
References thread::spawn().

1.6.1