Qore HttpServer Module Reference
0.9.11
|
The HttpServer class implements a multithreaded HTTP server. More...
Public Member Methods | |
addHandlerToListener (softstring bind, string name, hash< HttpHandlerConfigInfo > info) | |
adds a request handler to a listener given the listener's name or bind address | |
addHandlerToListener (softstring bind, string name, HttpServer::AbstractUrlHandler obj) | |
adds a request handler to a listener given the listener's name or bind address | |
deprecated | addHandlerToListener (softstring bind, string name, string path, *softlist< softstring > content_types, HttpServer::AbstractHttpRequestHandler obj, *softlist< softstring > special_headers, bool isregex=True) |
adds a request handler to a listener given the listener's name or bind address More... | |
addHandlerToListenerID (softint id, string name, hash< HttpHandlerConfigInfo > info) | |
adds a request handler to a listener given the listener's id More... | |
addHandlerToListenerID (softint id, string name, HttpServer::AbstractUrlHandler obj) | |
adds a request handler to a listener given the listener's id More... | |
addHandlerToListenerID (softint id, string name, string path, *softlist content_type, HttpServer::AbstractHttpRequestHandler obj, *softlist special_headers, bool isregex=True) | |
adds a request handler to a listener given the listener's id More... | |
addHttpMethod (string m) | |
add a new supported HTTP method More... | |
deprecated list< hash< auto > > | addINETListeners (*string node, softstring service, *string cert_path, *string key_path, *string name, int family=AF_UNSPEC, *string pwd) |
adds one or more global listeners according to the bind address More... | |
hash< auto > | addListener (hash< HttpListenerOptionInfo > opts) |
adds a single global listener to the server More... | |
deprecated hash< auto > | addListener (softstring bind, *string cert_path, *string key_path, *string name, int family=AF_UNSPEC, *string pwd) |
adds a single global listener to the server More... | |
final list< hash< auto > > | addListeners (hash< HttpListenerOptionInfo > info) |
adds one or more dedicated listeners to the server with the given bind address More... | |
deprecated softlist< hash< auto > > | addListeners (softstring bind, *string cert_path, *string key_path, *string name, int family=AF_UNSPEC, *string pwd) |
adds one or more global listeners according to the bind address More... | |
final list< hash< auto > > | addListeners (string bind, hash< auto > lp, *code logger, *code errorlogger, *code stopc, *string name, int family=AF_UNSPEC) |
adds a dedicated listener to the server More... | |
final list< hash< auto > > | addListeners (string bind, hash< HttpListenerOptionInfo > info) |
adds one or more dedicated listeners to the server with the given bind address More... | |
deprecated final list< hash< auto > > | addListenersWithHandler (string hname, HttpServer::AbstractHttpRequestHandler handler, hash< auto > lp, *code logger, *code errorlogger, *code stopc, *string name, int family=AF_UNSPEC) |
adds a dedicated listener to the server with an explicit/dedicated handler for incoming connections More... | |
constructor (*code logfunc, *code errlogfunc, bool dbg=False, string name=HttpServer::HttpServerString, hash< auto > hdr={ 'X-Powered-By':'Qore/'+Qore::VersionString}) | |
creates the HttpServer More... | |
copy () | |
throws an exception; these objects do not support copying | |
destructor () | |
calls stop() and destroys the object | |
bool | getDebug () |
returns the current status of the debug flag | |
int | getListenerCount () |
returns the number of running HTTP listeners | |
hash< auto > | getListenerInfo (softint id) |
returns a hash of information about the listener given the listener ID More... | |
hash< auto > | getListenerInfoName (string name) |
returns a hash of information about the listener given the listener name or bind ID More... | |
int | getListenerLogOptions (softstring bind) |
returns a binary-or'ed product of listener log options corresponding to enabled log options for the given listener More... | |
int | getListenerLogOptionsID (softint id) |
returns a binary-or'ed product of listener log options corresponding to enabled log options for the given listener More... | |
hash< string, hash< auto > > | getListeners () |
returns a hash of listener information More... | |
int | getListenerTID (softint id) |
gets the TID of a listener based on its listener ID | |
listenerStarted (int id, hash< auto > sinfo) | |
called from listener when the actual listener thread is running More... | |
log (string fmt) | |
called to log information to the registered log code | |
logError (string fmt) | |
called to log error information to the registered error log code | |
string | maskData (string msg) |
masks log messages by removing sensitive data More... | |
removeDynamicHandler (string name) | |
remove dynamic handler | |
bool | removeHandlerFromListenerID (softstring id, HttpServer::AbstractHttpRequestHandler handler) |
remove request handler from a listener given the listener's id More... | |
bool | removeHandlerFromListenerID (softstring id, string handler_name) |
remove request handler from a listener given the listener's id More... | |
sendHttpError (HttpListener listener, hash< auto > cx, Socket s, int code, *data msg, *InputStream chunked_msg, *hash< auto > extra_hdrs, *string encoding, bool head) | |
sends an HTTP error message on the socket | |
setDebug (bool dbg=True) | |
turns on or off debugging; when debugging is enabled more verbose error messages are reported | |
setDefaultHandler (string name, HttpServer::AbstractHttpRequestHandler obj) | |
sets the default request handler when no other handler can be matched | |
setDynamicHandler (string name, hash< HttpHandlerConfigInfo > info) | |
sets a dynamic request handler according to the arguments given | |
setDynamicHandler (string name, HttpServer::AbstractUrlHandler obj) | |
sets a dynamic request handler according to the arguments given | |
setDynamicHandler (string name, string path, *softlist< softstring > content_types, HttpServer::AbstractHttpRequestHandler obj, *softlist< softstring > special_headers, bool isregex=True) | |
sets a dynamic request handler according to the arguments given | |
setHandler (string name, hash< HttpHandlerConfigInfo > info) | |
sets a request handler according to the arguments given | |
setHandler (string name, HttpServer::AbstractUrlHandler obj) | |
sets a request handler according to the arguments given | |
setHandler (string name, string path, *softlist< softstring > content_types, HttpServer::AbstractHttpRequestHandler obj, *softlist< softstring > special_headers, bool isregex=True) | |
sets a request handler according to the arguments given | |
setListenerLogOptions (softstring bind, softint code) | |
turns on or off header and body logging options for receive operations for the given listener More... | |
setListenerLogOptionsID (softint id, softint code) | |
turns on or off header and body logging options for receive operations for the given listener More... | |
setMaskCode (code maskfunc) | |
sets the closure or call reference that will be used to mask sensitive data in log messages More... | |
stop () | |
stops all listeners; only returns when all connections are closed on all listeners More... | |
stopListener (softstring bind) | |
stops a single listener based on its name or bind address; does not return until all connections on the listener have closed More... | |
stopListenerID (softint id) | |
stops a single listener based on its listener ID; does not return until all connections on the listener have closed More... | |
stopNoWait () | |
stops all listeners; does not wait for all connections on the listeners to close More... | |
waitStop () | |
waits for all listeners to be stopped; call after calling HttpServer::stopNoWait() | |
Static Public Member Methods | |
static string | getHttpServerVersionString () |
returns the HTTP server version string | |
static string | getURLFromBind (softstring bind, *string host) |
returns a complete URL from a bind address More... | |
static nothing | setReplyHeaders (Socket s, hash< auto > cx, reference< hash > rv) |
helper method to set HTTP response headers | |
Public Attributes | |
const | AIFlags = AI_PASSIVE | AI_ADDRCONFIG |
address info flags | |
const | CompressionThreshold = 1024 |
default threadhold for data compressions; transfers smaller than this size will not be compressed | |
const | ContentEncodings = ... |
content-encodings supported; this is a hash to simulate a set with O(ln(n)) access times | |
const | DefaultIdleThreads = 10 |
default number of idle threads to have waiting for new connections (accross all listeners) | |
const | HttpCodes = HttpServer::HttpCodes |
map of HTTP result codes and text messages | |
const | HttpMethods = ... |
HTTP methods supported by default. | |
const | LLO_RECV_BODY = (1 << 1) |
listener log option: log recv message body | |
const | LLO_RECV_HEADERS = (1 << 0) |
listener log option: log recv headers | |
const | LLO_SEND_BODY = (1 << 3) |
listener log option: log sent message body | |
const | LLO_SEND_HEADERS = (1 << 2) |
listener log option: log send headers | |
const | PollTimeout = 5000 |
default poll timeout in ms | |
const | ReadTimeout = HttpServer::ReadTimeout |
default read timeout in ms | |
const | Version = HttpServer::HttpServerVersion |
version of the HttpServer's implementation | |
Private Member Methods | |
int | getListenerIdFromBindUnlocked (string bind) |
returns the listener ID from the bind name or throws an exception if not valid More... | |
int | getListenerLogOptionsUnlocked (softstring id) |
returns header and body logging options for the given listener as a binary-or'ed value of listener log option codes | |
setListenerLogOptionsUnlocked (softstring id, int code) | |
turns on or off header and body logging options for the given listener according to the option code | |
The HttpServer class implements a multithreaded HTTP server.
deprecated HttpServer::HttpServer::addHandlerToListener | ( | softstring | bind, |
string | name, | ||
string | path, | ||
*softlist< softstring > | content_types, | ||
HttpServer::AbstractHttpRequestHandler | obj, | ||
*softlist< softstring > | special_headers, | ||
bool | isregex = True |
||
) |
adds a request handler to a listener given the listener's name or bind address
HttpServer::HttpServer::addHandlerToListenerID | ( | softint | id, |
string | name, | ||
hash< HttpHandlerConfigInfo > | info | ||
) |
adds a request handler to a listener given the listener's id
id | listener id |
name | handler name |
info | the handler configuration info to add to the listener |
INVALID-LISTENER-ERROR | invalid listener id passed |
HttpServer::HttpServer::addHandlerToListenerID | ( | softint | id, |
string | name, | ||
HttpServer::AbstractUrlHandler | obj | ||
) |
adds a request handler to a listener given the listener's id
id | listener id |
name | handler name |
obj | handler object |
INVALID-LISTENER-ERROR | invalid listener id passed |
HttpServer::HttpServer::addHandlerToListenerID | ( | softint | id, |
string | name, | ||
string | path, | ||
*softlist | content_type, | ||
HttpServer::AbstractHttpRequestHandler | obj, | ||
*softlist | special_headers, | ||
bool | isregex = True |
||
) |
adds a request handler to a listener given the listener's id
id | listener id |
name | handler name |
path | handler path |
content_type | handler's content type |
obj | handler object |
INVALID-LISTENER-ERROR | invalid listener id passed |
HttpServer::HttpServer::addHttpMethod | ( | string | m | ) |
add a new supported HTTP method
m | the HTTP method to add; the string is converted to upper case if necessary |
deprecated list<hash<auto> > HttpServer::HttpServer::addINETListeners | ( | *string | node, |
softstring | service, | ||
*string | cert_path, | ||
*string | key_path, | ||
*string | name, | ||
int | family = AF_UNSPEC , |
||
*string | pwd | ||
) |
adds one or more global listeners according to the bind address
node | the bind address for the listener; if not given then the listener will be bound on all interfaces |
service | a service name (which will be lookup up to derive a port number) or port number to bind on |
cert_path | an optional path to an X509 certificate for HTTPS listeners |
key_path | an optional path to a private key file for an X509 certificate for HTTPS listeners |
name | an optional name fo the listener |
family | one of the following network family constants: |
pwd | an optional password for the private key file (PEM format only) |
HTTPSERVER-ADDLISTENER-ERROR | duplicate bind address |
hash<auto> HttpServer::HttpServer::addListener | ( | hash< HttpListenerOptionInfo > | opts | ) |
adds a single global listener to the server
opts | the listener options |
HTTPSERVER-ADDLISTENER-ERROR | invalid options; duplicate bind address |
deprecated hash<auto> HttpServer::HttpServer::addListener | ( | softstring | bind, |
*string | cert_path, | ||
*string | key_path, | ||
*string | name, | ||
int | family = AF_UNSPEC , |
||
*string | pwd | ||
) |
adds a single global listener to the server
sock | the bind address for the listener |
cert_path | an optional path to an X509 certificate for HTTPS listeners |
key_path | an optional path to a private key file for an X509 certificate for HTTPS listeners |
name | an optional name for the listener; the actual listener name will have the listener id appended to it and will be returned in the "name" key in each hash element in the return value for each listener started |
family | one of the following network family constants: |
pwd | an optional password for the private key file (PEM format only) |
HTTPSERVER-ADDLISTENER-ERROR | duplicate bind address |
final list<hash<auto> > HttpServer::HttpServer::addListeners | ( | hash< HttpListenerOptionInfo > | info | ) |
adds one or more dedicated listeners to the server with the given bind address
bind | the bind address for the dedicated listener; this can be a port number or an address (or hostname) and a port number separated by a colon (ex: "192.168.20.4:8021" ); the bind address will overwrite any node and service addresses in the info hash; all possible addresses will be bound to new listeners |
info | a listener information hash |
HTTPSERVER-ADDLISTENER-ERROR | duplicate bind address |
bind
begins with a "/" character then it is assumed to be a UNIX socket path and the family key of the info hash is ignoreddeprecated softlist<hash<auto> > HttpServer::HttpServer::addListeners | ( | softstring | bind, |
*string | cert_path, | ||
*string | key_path, | ||
*string | name, | ||
int | family = AF_UNSPEC , |
||
*string | pwd | ||
) |
adds one or more global listeners according to the bind address
bind | the bind address for the listener |
cert_path | an optional path to an X509 certificate for HTTPS listeners |
key_path | an optional path to a private key file for an X509 certificate for HTTPS listeners |
name | an optional name for the listener; the actual listener name will have the listener id appended to it and will be returned in the "name" key in each hash element in the return value for each listener started |
family | one of the following network family constants: |
pwd | an optional password for the private key file (PEM format only) |
HTTPSERVER-ADDLISTENER-ERROR | duplicate bind address |
"/"
character then it is assumed to be a UNIX socket path and the family argument is ignoredfinal list<hash<auto> > HttpServer::HttpServer::addListeners | ( | string | bind, |
hash< auto > | lp, | ||
*code | logger, | ||
*code | errorlogger, | ||
*code | stopc, | ||
*string | name, | ||
int | family = AF_UNSPEC |
||
) |
adds a dedicated listener to the server
bind | the bind address for the dedicated listener; this can be a port number or an address (or hostname) and a port number separated by a colon (ex: "192.168.20.4:8021" ) |
lp | a hash with the following keys:
|
logger | an optional closure or call reference that will be called with logging information; if this is not set, then the logger set in the HttpServer::constructor() will be used instead |
errorlogger | an optional closure or call reference that will be called with error information; if this is not set, then the error logger set in the HttpServer::constructor() will be used instead |
stopc | an optional closure or call reference that will be called immediately after the listener is stopped |
name | an optional name for the listener; the actual listener name will have the listener id appended to it and will be returned in the "name" key in each hash element in the return value for each listener started |
family | one of the following network family constants: |
HTTPSERVER-ADDLISTENER-ERROR | duplicate bind address |
bind
begins with a "/" character then it is assumed to be a UNIX socket path and the family argument is ignoredfinal list<hash<auto> > HttpServer::HttpServer::addListeners | ( | string | bind, |
hash< HttpListenerOptionInfo > | info | ||
) |
adds one or more dedicated listeners to the server with the given bind address
bind | the bind address for the dedicated listener; this can be a port number or an address (or hostname) and a port number separated by a colon (ex: "192.168.20.4:8021" ); the bind address will overwrite any node and service addresses in the info hash; all possible addresses will be bound to new listeners |
info | a listener information hash |
HTTPSERVER-ADDLISTENER-ERROR | duplicate bind address |
bind
begins with a "/" character then it is assumed to be a UNIX socket path and the family key of the info hash is ignoreddeprecated final list<hash<auto> > HttpServer::HttpServer::addListenersWithHandler | ( | string | hname, |
HttpServer::AbstractHttpRequestHandler | handler, | ||
hash< auto > | lp, | ||
*code | logger, | ||
*code | errorlogger, | ||
*code | stopc, | ||
*string | name, | ||
int | family = AF_UNSPEC |
||
) |
adds a dedicated listener to the server with an explicit/dedicated handler for incoming connections
hname | the name of the handler |
handler | the HttpServer::AbstractHttpRequestHandler object that will handle incoming connections |
lp | a hash with the following keys:
|
logger | an optional closure or call reference that will be called with logging information; if this is not set, then the logger set in the HttpServer::constructor() will be used instead |
errorlogger | an optional closure or call reference that will be called with error information; if this is not set, then the error logger set in the HttpServer::constructor() will be used instead |
stopc | an optional closure or call reference that will be called immediately after the listener is stopped |
name | an optional name for the listener; the actual listener name will have the listener id appended to it and will be returned in the "name" key in each hash element in the return value for each listener started |
family | one of the following network family constants: |
SETHANDLER-ERROR | HTTP request handler already exists with the given name; non-string entry in special_header list |
HTTPSERVER-ADDLISTENER-ERROR | missing or invalid "bind" or "url" entry in the lp argument; duplicate bind address |
bind
begins with a "/" character then it is assumed to be a UNIX socket path and the family argument is ignoredHttpServer::HttpServer::constructor | ( | *code | logfunc, |
*code | errlogfunc, | ||
bool | dbg = False , |
||
string | name = HttpServer::HttpServerString , |
||
hash< auto > | hdr = { 'X-Powered-By':'Qore/'+Qore::VersionString} |
||
) |
creates the HttpServer
call addListener() to add and start listeners
n_logfunc | an optional closure or call reference that will be called with logging information; must take an initial string with optional arguments; ex: code log = sub (string fmt) { vprintf(fmt, argv); }
|
n_errlogfunc | an optional closure or call reference that will be called with error information; must take an initial string with optional arguments; ex: code errloglog = sub (string fmt) { vprintf(fmt, argv); }
|
n_dbg | if this parameter is set to True, then additional information will be logged when errors occur |
n_name | the name of the HTTP server as returned in the Server header (should be formatted according to RFC 2616 section 3.8 http://tools.ietf.org/html/rfc2616#section-3.8) |
n_hdr | a hash of headers to return in every response by default; to clear the default send an empty hash as the argument here |
returns the listener ID from the bind name or throws an exception if not valid
INVALID-LISTENER-ERROR | invalid listener bind address |
hash<auto> HttpServer::HttpServer::getListenerInfo | ( | softint | id | ) |
returns a hash of information about the listener given the listener ID
id | the listener ID |
name:
the listener namehostname:
the listening interface name if available (ex: "localhost"; note that this key is not present when retrieving information about UNIX sockets)hostname_desc:
a descriptive string giving the hostname and the address family if the hostname is available (ex: "ipv6[localhost]"; note that this key is not present when retrieving information about UNIX sockets)address:
a string giving the address (ex: "::ffff:0.0.0.0")address_desc:
a descriptive string giving the address and the address family (ex: "ipv6[::ffff:0.0.0.0]")port:
an integer port number if available (note that this key is not present when retrieving information about UNIX sockets)family:
the network address family (see Network Address Family Constants)familystr:
a string describing the network address family (ex: "ipv4")proto:
the protocol used; either "http"
or "https"
for secure listenersid:
the listener IDbind:
the bind specification usedINVALID-LISTENER-ERROR | invalid listener ID |
returns a hash of information about the listener given the listener name or bind ID
name | the listener name or bind ID |
name:
the listener namehostname:
the listening interface name if available (ex: "localhost"; note that this key is not present when retrieving information about UNIX sockets)hostname_desc:
a descriptive string giving the hostname and the address family if the hostname is available (ex: "ipv6[localhost]"; note that this key is not present when retrieving information about UNIX sockets)address:
a string giving the address (ex: "::ffff:0.0.0.0")address_desc:
a descriptive string giving the address and the address family (ex: "ipv6[::ffff:0.0.0.0]")port:
an integer port number if available (note that this key is not present when retrieving information about UNIX sockets)family:
the network address family (see Network Address Family Constants)familystr:
a string describing the network address family (ex: "ipv4")proto:
the protocol used; either "http"
or "https"
for secure listenersid:
the listener IDbind:
the bind specification usedINVALID-LISTENER-ERROR | invalid listener name or bind ID |
int HttpServer::HttpServer::getListenerLogOptions | ( | softstring | bind | ) |
returns a binary-or'ed product of listener log options corresponding to enabled log options for the given listener
INVALID-LISTENER-ERROR | invalid listener id passed |
int HttpServer::HttpServer::getListenerLogOptionsID | ( | softint | id | ) |
returns a binary-or'ed product of listener log options corresponding to enabled log options for the given listener
INVALID-LISTENER-ERROR | invalid listener id passed |
returns a hash of listener information
returns a complete URL from a bind address
bind | the bind address; if for any reason there is a path in the bind address, it will be ignored |
host | the hostname to use in case the bind string is only a port number; if none is passed or the value passed is equal to the return value of Qore::gethostname(), then "localhost" is used |
called from listener when the actual listener thread is running
id | the listener ID |
sinfo | a socket information hash as returned by Qore::Socket::getSocketInfo() |
bool HttpServer::HttpServer::removeHandlerFromListenerID | ( | softstring | id, |
HttpServer::AbstractHttpRequestHandler | handler | ||
) |
remove request handler from a listener given the listener's id
id | listener id |
handler | handler to remove; |
INVALID-LISTENER-ERROR | invalid listener id passed |
bool HttpServer::HttpServer::removeHandlerFromListenerID | ( | softstring | id, |
string | handler_name | ||
) |
remove request handler from a listener given the listener's id
id | listener id |
handler_name | name of handler to remove; |
INVALID-LISTENER-ERROR | invalid listener id passed |
HttpServer::HttpServer::setListenerLogOptions | ( | softstring | bind, |
softint | code | ||
) |
turns on or off header and body logging options for receive operations for the given listener
bind | the name of the listener or bind string |
code | a binary-or'ed product of listener log options; bits set enable logging; bits not set disable logging |
HttpServer::HttpServer::setListenerLogOptionsID | ( | softint | id, |
softint | code | ||
) |
turns on or off header and body logging options for receive operations for the given listener
id | the unique listener ID |
code | a binary-or'ed product of listener log options; bits set enable logging; bits not set disable logging |
INVALID-LISTENER-ERROR | invalid listener id passed |
HttpServer::HttpServer::setMaskCode | ( | code | maskfunc | ) |
sets the closure or call reference that will be used to mask sensitive data in log messages
maskfunc | a closure or call reference that takes a single string argument and returns the masked string |
HttpServer::HttpServer::stop | ( | ) |
stops all listeners; only returns when all connections are closed on all listeners
do not call stop() after calling stopNoWait(); it can cause a deadlock
HttpServer::HttpServer::stopListener | ( | softstring | bind | ) |
stops a single listener based on its name or bind address; does not return until all connections on the listener have closed
id | listener bind address |
INVALID-LISTENER-ERROR | invalid listener bind address |
HTTP-SERVER-ERROR | cannot stop last listener |
HttpServer::HttpServer::stopListenerID | ( | softint | id | ) |
stops a single listener based on its listener ID; does not return until all connections on the listener have closed
id | listener id |
INVALID-LISTENER-ERROR | invalid listener ID |
HTTP-SERVER-ERROR | cannot stop last listener |
HttpServer::HttpServer::stopNoWait | ( | ) |
stops all listeners; does not wait for all connections on the listeners to close
do not call stop() after calling stopNoWait(); it can cause a deadlock