241 class HttpServer::HandlerInfo {
246 AbstractHttpRequestHandler obj;
255 constructor(
string name, AbstractHttpRequestHandler obj,
string path,
bool isregex =
True, *softlist content, *softlist shdr);
258 bool matchContentType(
string ct);
262 int matchRequest(
hash hdr,
int score);
267 class HttpServer::HttpHandlerList {
277 static checkSpecialHeaders(reference sh);
282 setHandler(
string name,
string path,
bool isregex =
True, *softlist content, AbstractHttpRequestHandler obj, *softlist special_headers);
286 *HandlerInfo findHandler(
hash hdr, reference score,
bool final =
False);
297 class HttpServer::DynamicHandlerInfo :
public HttpServer::HandlerInfo {
305 constructor(
string name, AbstractHttpRequestHandler obj,
string path,
bool isregex =
True, *softlist content, *softlist shdr);
310 class HttpServer::DynamicHandlerHelper {
322 constructor(Counter c);
330 class HttpServer::DynamicHttpHandlerList :
public HttpServer::HttpHandlerList {
339 setHandler(
string name,
string path,
bool isregex, *softlist content_type, AbstractHttpRequestHandler obj, *softlist special_headers);
343 removeHandler(
string name);
346 *DynamicHandlerInfo findHandler(
hash hdr, reference score, reference dhh);
388 private hash getAuthHeader();
391 private hash do401(
string msg =
"Authentication is required to access this server");
549 hash handleRequest();
584 private logChunk(
bool send,
int size);
831 stop(softstring lid);
910 const LP_LOGPARAMS = 1 << 16;
911 const LP_LEVELMASK = LP_LOGPARAMS - 1;
932 "101":
"Switching Protocols",
941 "203":
"Non-Authoritative Information",
943 "205":
"Reset Content",
944 "206":
"Partial Content",
947 "207":
"Multi-Status",
950 "208":
"Already Reported",
956 "300":
"Multiple Choices",
957 "301":
"Moved Permanently",
960 "304":
"Not Modified",
963 "307":
"Temporary Redirect",
966 "400":
"Bad Request",
967 "401":
"Unauthorized",
968 "402":
"Payment Required",
971 "405":
"Method Not Allowed",
972 "406":
"Not Acceptable",
973 "407":
"Proxy Authentication Required",
974 "408":
"Request Timeout",
977 "411":
"Length Required",
978 "412":
"Precondition Failed",
979 "413":
"Request Entity Too Large",
980 "414":
"Request-URI Too Long",
981 "415":
"Unsupported Media Type",
982 "416":
"Requested Range Not Satisfiable",
983 "417":
"Expectation Failed",
986 "418":
"I'm a teapot",
989 "420":
"Enhance Yextern Calm",
992 "422":
"Unprocessable Entity",
998 "424":
"Failed Dependency",
1001 "425":
"Unordered Collection",
1004 "426":
"Upgrade Required",
1007 "428":
"Precondition Required",
1010 "429":
"Too Many Requests",
1013 "431":
"Request Header Fields Too Large",
1016 "500":
"Internal Server Error",
1017 "501":
"Not Implemented",
1018 "502":
"Bad Gateway",
1019 "503":
"Service Unavailable",
1020 "504":
"Gateway Timeout",
1021 "505":
"HTTP Version Not Supported",
1022 "509":
"Bandwidth Limit Exceeded",
1025 "510":
"Not Extended",
1028 "511":
"Network Authentication Required",
1034 "deflate":
"deflate",
1037 "x-deflate":
"deflate",
1060 Sequence seqSessions();
1061 Sequence seqListeners();
1063 bool stopped =
False;
1066 HttpHandlerList handlers();
1069 hash defaultHandler;
1087 DynamicHttpHandlerList dhandlers();
1096 *
string override_encoding;
1098 static string HttpServerString;
1122 setDefaultTextEncoding(
string enc);
1125 string getDefaultTextEncoding();
1195 softlist
addListeners(softstring sock, *
string cert_path, *
string key_path, *
string name,
int family =
AF_UNSPEC);
1377 startConnection(code c);
1389 static final private hash getSSLObjects(
string cert_path, *
string key_path);
1392 final private list addINETListenersIntern(*
string node, softstring service, *
hash sd, *
hash lp, *code logger, *code errorlogger, *code stopc, *
string name,
int family =
AF_UNSPEC);
1396 final private hash noHandlerError(
hash cx,
hash hdr, any body);
1401 final handleRequest(
HttpListener listener, Socket s, reference cx,
hash hdr,
hash hh, *data body,
bool head =
False, HttpPersistentHandlerInfo phi);
1411 class HttpServer::HttpPersistentHandlerInfo {
1415 *DynamicHandlerHelper dhh;
1437 *SSLCertificate cert;
1446 bool stopped =
False;
1464 const PollInterval = 1s;
1465 const ListenQueue = 100;
1474 HttpHandlerList handlers();
1477 *HandlerInfo defaultHandler;
1482 constructor(
HttpServer server,
int id, Sequence ss, *
string node, *softstring service, *
Qore::SSLCertificate cert, *
Qore::SSLPrivateKey key, *
hash hi, *code logger, *code errorlogger, *code stopc,
string name,
int family =
AF_UNSPEC);
1485 addHandlers(
hash hi);
1488 setDefaultHandler(
string name);
1512 any removeUserThreadContext(
string k);
1515 addUserThreadContext(
hash uctx);
1524 logResponse(
hash cx,
int code, *data body, *
hash hdr);
1536 private mainThread();
1540 private connectionThread(Socket s);
constructor(HttpListener listener, AbstractHttpRequestHandler handler, Socket s, hash cx, hash hdr, any body)
creates the object with the given attributes
logError()
called to log error information to the registered error log code
logErrorArgs(softlist args=())
calls the error log function/closure with the given args
logArgs(softlist args=())
calls the log function/closure with the given args
any send()
this method provides the callback method for sending chunked data by calling sendImpl() ...
abstract nothing recvImpl(hash v)
abstract callback method for receiving chunked data
string sprintf(string fmt,...)
hash getListeners()
returns a hash of listener information
hash hdr
a hash of request headers
Definition: HttpServer.qm.dox.h:539
hash addListener(softstring sock, *string cert_path, *string key_path, *string name, int family=AF_UNSPEC)
adds a global listener to the server
private nothing recvImpl(hash v)
abstract callback method for receiving chunked data
string get_exception_string(hash ex)
returns a multi-line string from the exception hash argument suitable for logging or output on the co...
abstract class for streaming HTTP chunked requests/responses
Definition: HttpServer.qm.dox.h:522
abstract hash handleRequest(hash cx, hash hdr, *data b)
called by the HTTP server to handle incoming HTTP requests
setDebug(bool dbg=True)
turns on or off debugging; when debugging is enabled more verbose error messages are reported ...
string getRealm()
returns the authentication realm as a string
abstract class that all HTTP request handler objects must inherit from
Definition: HttpServer.qm.dox.h:631
*AbstractAuthenticator auth
the optional AbstractAuthenticator for requests to this handler
Definition: HttpServer.qm.dox.h:636
static string getURLFromBind(softstring bind, *string host)
returns a complete URL from a bind address
constructor(hash cx)
creates the object with the given arguments
const ReadTimeout
default read timeout in ms
Definition: HttpServer.qm.dox.h:905
removeDynamicHandler(string name)
remove dynamic handler
hash lh
hash of listener references; this is to stop all connections associated with a particular listener ...
Definition: HttpServer.qm.dox.h:792
*hash ex
if an exception is raised in a callback then the exception hash is saved here
Definition: HttpServer.qm.dox.h:427
addHandlerToListenerID(softint id, string name, string path, *softlist content_type, AbstractHttpRequestHandler obj, *softlist special_headers, bool isregex=True)
adds a request handler to a listener given the listener's id
destructor()
calls stop() and destroys the object
addHandlerToListener(softstring bind, string name, string path, *softlist content_type, AbstractHttpRequestHandler obj, *softlist special_headers, bool isregex=True)
adds a request handler to a listener given the listener's name or bind address
start(softstring lid, hash cx, hash hdr, Socket s)
called from the HTTP server after the handleRequest() method indicates that a dedicated connection sh...
string getRelativePath(string path)
returns the relative path anchored from the url_root if possible; URI query arguments are stripped of...
bool persistent
persistent connection flag
Definition: HttpServer.qm.dox.h:648
setDynamicHandler(string name, string path, *softlist content_type, AbstractHttpRequestHandler obj, *softlist special_headers, bool isregex=True)
sets a dynamic request handler according to the arguments given
*data getMessageBody(Socket s, hash hdr, *data body, bool decode=True)
optionally retrieves and post-processes any message body
const DefaultIdleThreads
default number of idle threads to have waiting for new connections (accross all listeners) ...
Definition: HttpServer.qm.dox.h:1042
stopListener(softstring bind)
stops a single listener based on its name or bind address; does not return until all connections on t...
stopListenerID(softint id)
stops a single listener based on its listener ID; does not return until all connections on the listen...
private hash sendResponse()
called to either create the response hash or send a chunked response directly
const HttpMethods
supported HTTP methods
Definition: HttpServer.qm.dox.h:917
AbstractHttpRequestHandler handler
the request handler for the request
Definition: HttpServer.qm.dox.h:533
nothing recv(hash v)
this method provides the callback method for receiving chunked data by calling recvImpl() ...
bool decompress
if POSTed data should be decompressed automatically if there is content-encoding
Definition: HttpServer.qm.dox.h:639
abstract private startImpl(softstring lid, hash cx, hash hdr, Socket s)
called from the HTTP server after the handleRequest() method indicates that a dedicated connection sh...
hash cx
call context hash
Definition: HttpServer.qm.dox.h:425
private hash getResponseHeaderMessageImpl()
this method should return the response message description hash
stop()
stops all listeners; only returns when all connections are closed on all listeners ...
bool stream
if the handler supports streaming requests/responses with chunked data
Definition: HttpServer.qm.dox.h:645
constructor(*AbstractAuthenticator auth, softbool stream=False)
create the object optionally with the given AbstractAuthenticator
constructor(string url_root, *AbstractAuthenticator auth)
creates the object based on the URL root and optional authenticator
Mutex m()
listener reference hash mutex
abstract class for embedded objects serving streaming requests
Definition: HttpServer.qm.dox.h:416
private stopImpl()
called from the HTTP server when the socket should be closed due to an external request; the start() ...
static hash make501(string fmt)
creates a hash for an HTTP 501 error response with the response message body as a string ...
constructor(*code logfunc, *code errlogfunc, bool dbg=False, string name=sprintf("Qore-HTTP-Server/%s", HttpServer::Version), hash hdr=("X-Powered-By":"Qore/"+Qore::VersionString))
creates the HttpServer
bool requiresAuthentication()
called to check if the connection requires authentication
abstract base class for external authentication
Definition: HttpServer.qm.dox.h:353
static hash makeResponse(int code, string fmt)
creates a hash for an HTTP response with the response code and the response message body as a formatt...
static hash make400(string fmt)
creates a hash for an HTTP 400 error response with the response message body as a string ...
hash getListenerInfo(softint id)
returns a hash of information about the listener given the listener ID
restoreThreadLocalData(*hash data)
called after handleRequest() with any data returned from saveThreadData()
private nothing recv(hash v)
this is the primary callback for receiving chunked data; data will be logged, and then recvImpl() is ...
stop()
called from the HTTP server when the socket should be closed due to an external request; the start() ...
const PollTimeout
default poll timeout in ms
Definition: HttpServer.qm.dox.h:907
hash cx
the call context variable
Definition: HttpServer.qm.dox.h:537
Socket s
the Socket object for the response
Definition: HttpServer.qm.dox.h:535
softlist addListeners(softstring sock, *string cert_path, *string key_path, *string name, int family=AF_UNSPEC)
adds one or more global listeners according to the bind address
private any sendImpl()
abstract callback method for sending chunked data
constructor(*AbstractAuthenticator auth)
create the object optionally with the given AbstractAuthenticator
nothing exit(softint rc=0)
hash parse_uri_query(string path)
parses a URI path for a arguments and a method; where the method is the part of the path before the f...
static binary encodeBody(string ce, data body)
encodes a message body with content-encoding
final list addListenersWithHandler(string hname, AbstractHttpRequestHandler handler, hash 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 ...
static string getHttpServerVersionString()
returns the HTTP server version string
static *string getLogMessage(hash cx, hash api, reference params, *reference args)
helper method for handling log messages
sendHttpError(HttpListener listener, hash cx, Socket s, int code, string msg, *hash extra_hdrs, *string encoding)
sends an HTTP error message on the socket
stopNoWait()
stops all listeners; does not wait for all connections on the listeners to close
bool getDebug()
returns the current status of the debug flag
private AbstractStreamRequest getStreamRequestImpl(HttpListener listener, Socket s, hash cx, hash hdr, *data body)
returns the AbstractStreamRequest object for handling chunked requests
abstract any sendImpl()
abstract callback method for sending chunked data
const CompressionThreshold
default threadhold for data compressions; transfers smaller than this size will not be compressed ...
Definition: HttpServer.qm.dox.h:1045
abstract hash getResponseHeaderMessageImpl()
this method should return the response message description hash
private any send()
this is the primary callback for sending chunked responses; first sendImpl() is called to get the raw...
abstract class that all HTTP dedicated socket handler objects must inherit from
Definition: HttpServer.qm.dox.h:784
abstract class for HTTP request handlers anchored at a specific URL
Definition: HttpServer.qm.dox.h:760
waitStop()
waits for all listeners to be stopped; call after calling HttpServer::stopNoWait() ...
static data decodeBody(string ce, binary body, *string enc)
decodes a message body with content-encoding
hash handleRequest(hash cx, hash hdr, *data body)
will be called when a request is received that should be directed to the handler
static nothing setReplyHeaders(Socket s, hash cx, reference rv)
helper method to set HTTP response headers
const AIFlags
address info flags
Definition: HttpServer.qm.dox.h:914
private hash getResponseHeaderMessage()
this method returns the response message description hash by calling getResponseHeaderMessageImpl() ...
bool decompress_to_string
if automatically decompressed POSTed data should be converted to a string (if False, then it will be decompressed to a binary)
Definition: HttpServer.qm.dox.h:642
copy()
throws an exception; these objects do not support copying
const Version
version of the HttpServer's implementation
Definition: HttpServer.qm.dox.h:903
setDefaultHandler(string name, AbstractHttpRequestHandler obj)
sets the default request handler when no other handler can be matched
The HttpServer class implements a multithreaded HTTP server primarily designed for serving RPC-style ...
Definition: HttpServer.qm.dox.h:898
authenticate(string user, string pass="")
called to authenticate a user for a connection
HttpListener listener
the listener servicing the request for logging purposes
Definition: HttpServer.qm.dox.h:531
hash getResponseHeaderMessage()
this method returns the response message description hash by calling getResponseHeaderMessageImpl() ...
setHandler(string name, string path, *softlist content_type, AbstractHttpRequestHandler obj, *softlist special_headers, bool isregex=True)
sets a request handler according to the arguments given
int getListenerCount()
returns the number of running HTTP listeners
this class implements the listeners for the HttpServer class
Definition: HttpServer.qm.dox.h:1431
log()
called to log information to the registered log code
*hash saveThreadLocalData()
called before handleRequest() any data returned here will be given to restoreThreadLocalData() after ...
list addINETListeners(*string node, softstring service, *string cert_path, *string key_path, *string name, int family=AF_UNSPEC)
adds one or more global listeners according to the bind address
authenticateByIP(string ip, reference user)
called when the connection requires authentication, but no authentication credentials were supplied...
string url_root
root part of URL for matching requests
Definition: HttpServer.qm.dox.h:765
hash getListenerInfoName(string name)
returns a hash of information about the listener given the listener name or bind ID ...
const HttpCodes
map of HTTP result codes and text messages
Definition: HttpServer.qm.dox.h:929
nothing persistentClosed()
called externally when a persistent connection is closed
static hash redirect(hash cx, hash hdr, string path)
generates a redirect hash for the given path
*hash authenticateRequest(HttpListener listener, hash hdr, reference cx)
primary method called to authenticate each request
hash lsh
hash of listener stop flags
Definition: HttpServer.qm.dox.h:795
any body
any message body given in a non-chunked request; could already be deserialized
Definition: HttpServer.qm.dox.h:541
private nothing checkPersistent(hash cx, hash hdr)
this method will throw an exception if a persistent connection cannot be granted
const ContentEncodings
content-encodings supported; this is a hash to simulate a set with O(ln(n)) access times ...
Definition: HttpServer.qm.dox.h:1032
int getListenerTID(softint id)
gets the TID of a listener based on its listener ID