370 const PollTimeout = 250ms;
377 const AIFlags = AI_PASSIVE | AI_ADDRCONFIG;
380 const HttpMethods = {
396 const ContentEncodings = {
398 "deflate":
"deflate",
401 "x-deflate":
"deflate",
406 const DefaultIdleThreads = 10;
409 const CompressionThreshold = 1024;
421 const LLO_RECV_HEADERS = (1 << 0);
424 const LLO_RECV_BODY = (1 << 1);
427 const LLO_SEND_HEADERS = (1 << 2);
430 const LLO_SEND_BODY = (1 << 3);
444 Sequence seqSessions();
445 Sequence seqListeners();
447 bool stopped = False;
450 HttpHandlerList handlers();
453 hash<auto> defaultHandler;
456 hash<string, HttpListener> listeners;
459 hash<string, int> smap;
462 hash<string, int> nmap;
471 DynamicHttpHandlerList dhandlers();
474 ThreadPool threadPool(-1, DefaultIdleThreads);
482 *
string override_encoding;
485 string http_server_string;
488 hash<string, bool> http_methods = HttpMethods;
519 deprecated
constructor(*code logfunc, *code errlogfunc,
bool dbg = False,
522 http_server_string = name;
528 logger =
new Logger(http_server_string);
529 if (logfunc || errlogfunc);
531 logger.setLevel(LoggerLevel::INFO);
558 setDefaultTextEncoding(
string enc);
561 string getDefaultTextEncoding();
586 final list<hash<auto>>
addListeners(
string bind, hash<HttpListenerOptionInfo> info,
587 *reference<hash<string, string>> errmap) {
592 (info.node, info.service) = (bind =~ x/(.+):(\w+)/);
593 return addINETListenersIntern(info, \errmap);
610 final list<hash<auto>>
addListeners(hash<HttpListenerOptionInfo> info, *reference<hash<string, string>> errmap);
809 "content_types": content_types,
811 "headers": special_headers,
853 "content_types": content_type,
855 "headers": special_headers,
962 sendHttpError(
HttpListener listener, hash<auto> cx, Socket s,
int code, *data msg, *InputStream chunked_msg, *hash<auto> extra_hdrs, *
string encoding,
bool head);
1004 startConnection(code c);
1033 *
Qore::SSLPrivateKey key, *hash<
string, hash<HttpHandlerConfigInfo>> handler_info, *Logger logger,
1034 *code stopc, *
string name,
int family = AF_UNSPEC) {
1040 "handler_info": handler_info,
1048 final HttpListener addListenerIntern(hash<HttpListenerOptionInfo> info);
1053 final private list<hash<auto>> addINETListenersIntern(hash<HttpListenerOptionInfo> info,
1054 *reference<hash<string, string>> errmap) {
1055 list<auto> al = getaddrinfo(info.node, info.service, info.family, AIFlags);
1058 al = sort(al,
int (hash<auto> l, hash<auto> r) {
1059 return l.family === AF_INET6 ? -1 : 0;
1062 list<hash<auto>> rv = ();
1063 foreach hash<auto> h in (al);
1071 final hash<HttpResponseInfo> noHandlerError(hash<auto> cx, hash<auto> hdr,
auto body);
1077 final handleRequest(HttpListener listener, Socket s, reference<hash<auto>> cx, hash<auto> hdr,
bool head = False,
1078 HttpPersistentHandlerInfo phi, hash<auto> info, reference<bool> dedicated) {
1079 if (!http_methods{hdr.method});
1083 foreach string enc in (cx.
'header-info'.
'accept-encoding');
1087 cx.
"header-info".
"accept-encoding" = map {$1:
True}, cx.
"header-info".
"accept-encoding";
1090 on_exit remove cx.encoding;
1105 DynamicHandlerHelper dhh;
1113 if (listener.hasHandlers()) {
1114 hi = listener.findHandler(hdr, \score, True, \root_path);
1116 if (listener.defaultHandler)
1117 hi = listener.defaultHandler;
1119 sendHttpError(listener, cx, s, 404,
"Not found", NOTHING, NOTHING,
1120 cx.
"response-encoding", head);
1125 hi = handlers.findHandler(hdr, \score, False, \root_path);
1127 *HandlerInfo dhi = dhandlers.findHandler(hdr, \score, \dhh, \root_path);
1135 cx.handler_name = hi.name;
1137 cx.root_path = root_path;
1142 if (!handler && defaultHandler) {
1143 handler = defaultHandler.obj;
1144 cx.handler_name = defaultHandler.name;
1147 if (handler && hdr.expect) {
1148 if (hdr.expect ==
"100-continue") {
1150 sendReply(listener, s, handler, resp, \cx, hdr, head, \dedicated);
1152 string etxt =
sprintf(
"unrecognized 'Expect' header: %y", hdr.expect);
1153 string str =
sprintf(
"%s: received from %s via %s (header: %y)", etxt,
1154 cx.
"peer-info".address_desc, cx.
"socket-info".address_desc, hdr);
1155 listener.logError(str);
1157 sendHttpError(listener, cx, s, 400, etxt, NOTHING, NOTHING, cx.
"response-encoding",
1158 hdr.method ==
"HEAD");
1165 if (hdr.
"content-length") {
1167 if (hdr.
"content-encoding" || hdr.
"content-type" == MimeTypeOctetStream)
1171 #printf("HTTP DEBUG: %s\n", body);
1172 }
catch (hash<ExceptionInfo> ex) {
1173 string etxt =
sprintf(
"error reading body in %s (Content-Length: %d): %s: %s", hdr.method,
1174 hdr.
"content-length", ex.err, ex.desc);
1175 string str =
sprintf(
"%s: received from %s via %s (header: %y)", etxt,
1176 cx.
"peer-info".address_desc, cx.
"socket-info".address_desc, hdr);
1177 listener.logError(str);
1179 sendHttpError(listener, cx, s, 400, etxt, NOTHING, NOTHING, cx.
"response-encoding",
1180 hdr.method ==
"HEAD");
1185 # override assumed "iso-8859-1" character encoding if necessary
1186 if (body.typeCode() == NT_STRING && override_encoding && hdr.
"_qore_orig_content_type" !~ /charset=/) {
1190 listener.doLogRecvBody(body, cx, hdr, info);
1192 hash<HttpResponseInfo> rv;
1194 #printf("HTTP DEBUG: B: handler: %y strict-bool-eval: %y\n", cx.handler_name, get_parse_options() & PO_STRICT_BOOLEAN_EVAL);
1195 #printf("HTTP DEBUG: B: handler: %y: context: %y, hdr: %y, body: %y\n", cx.handler_name, cx, hdr, body);
1196 #printf("HTTP DEBUG: BEFORE handler=%s", dbg_node_info(handlers.(cx.handler_name)));
1198 #printf("HTTP DEBUG: A: cid: %d handler: %y auth: %y\n", cx.id, cx.handler_name, boolean(handler.auth));
1201 cx.handler_name =
"error";
1202 rv = noHandlerError(cx, hdr, body);
1204 # check for authentication info
1205 #printf("HTTP DEBUG: handler: %y (auth: %y) hdr: %y\n", cx.handler_name, exists handler.auth && handler.auth.requiresAuthentication(), hdr);
1210 # return an error if authentication failed
1214 if (!ah.body && !ah.hasKeyValue(
"chunked_body"))
1215 ah.body =
"Authentication is required to access this server";
1217 sendHttpError(listener, cx, s, ah.code, ah.body,
1218 ah.hasKey(
"chunked_body") ? ah.chunked_body : NOTHING, ah.hdr, cx.
"response-encoding",
1222 }
catch (hash<ExceptionInfo> ex) {
1224 string desc = !debug
1227 desc +=
sprintf(
": received from %s", cx.
"peer-info".address_desc);
1228 listener.logError(
"%s", desc);
1230 sendHttpError(listener, cx, s, 501,
"Server Authentication Error", NOTHING, NOTHING, NOTHING,
1234 #listener.log("authenticated with user %y", cx.user);
1240 # decode body if applicable
1241 if (body && hdr.
"content-encoding" && handler.
decompress)
1245 rv = cast<hash<HttpResponseInfo>>(handler.
handleRequest(listener, s, cx, hdr, body));
1247 # manage persistent handler info, if applicable
1250 phi.assign(dhh, handler);
1254 if (rv.reply_sent) {
1259 sendReply(listener, s, handler, rv, \cx, hdr, head, \dedicated);
1260 }
catch (hash<ExceptionInfo> ex) {
1261 string desc = !debug
1265 string str =
sprintf(
"handler: %s: %s", cx.handler_name, desc);
1267 if (ex.err ==
"ENCODING-CONVERSION-ERROR") {
1268 s.setEncoding(
"utf8");
1269 cx.
"response-encoding" =
"utf8";
1274 # if there is a pending chunked body that has not yet been read on exit, then read it and discard before sending any response
1275 on_exit
if (s.pendingHttpChunkedBody())
1278 sendHttpError(listener, cx, s, 500, str, NOTHING, NOTHING, cx.
"response-encoding", head);
1286 reference<hash> cx, hash<auto> hdr,
bool head, reference<bool> dedicated) {
1287 if (exists rv.close)
1290 rv.close = cx.close;
1293 on_exit
if (s.pendingHttpChunkedBody())
1307 rv.hdr = self.hdr + rv.hdr;
1309 if ((exists rv.body || (
bool chunked_body = rv.hasKeyValue(
"chunked_body")))
1313 || rv.code == 304)))) {
1321 if (rv.code >= 300 && rv.code < 400);
1322 else if (rv.code >= 400);
1325 # rv.hdr."Transfer-Encoding", cx.encoding, rv.body);
1327 # compress body if eligible for compression
1328 if (rv.body && !rv.hdr.
"Content-Encoding" && rv.body.size() > CompressionThreshold) {
1329 if (cx.encoding ==
"deflate") {
1330 rv.hdr.
"Content-Encoding" =
"deflate";
1332 }
else if (cx.encoding ==
"gzip") {
1333 rv.hdr.
"Content-Encoding" =
"gzip";
1334 rv.body =
gzip(rv.body);
1335 }
else if (cx.encoding ==
"bzip2") {
1336 rv.hdr.
"Content-Encoding" =
"bzip2";
1337 rv.body =
bzip2(rv.body);
1341 doResponse(listener, s, cx, rv);
1345 listener.log(
"cid %d: %s (from %s): %s", cx.id, cx.handler_name, cx.
"peer-info".address_desc, rv.log);
1347 listener.logError(
"cid %d: %s (from %s): %s", cx.id, cx.handler_name, cx.
"peer-info".address_desc,
1350 if (!rv.close && rv.user_state);
1353 if (rv.code == 101);
1359 doResponse(HttpListener listener, Socket s, hash<auto> cx, hash rv);
1365class HttpPersistentHandlerInfo {
1368 *DynamicHandlerHelper dhh;
1391 *hash<HttpListenerOptionInfo> opts;
1405 bool stopped = False;
1409 hash<string, AbstractHttpSocketHandlerInterface> shh;
1423 bool log_recv_headers = False;
1426 bool log_recv_body = False;
1429 bool log_send_headers = False;
1432 bool log_send_body = False;
1443 const PollInterval = 250ms;
1444 const ListenQueue = 100;
1445 const BodyLogLimit = 40;
1448 int handler_ref_cnt = 0;
1451 HttpHandlerList handlers();
1455 SSL_VERIFY_PEER:
"SSL_VERIFY_PEER",
1456 SSL_VERIFY_FAIL_IF_NO_PEER_CERT:
"SSL_VERIFY_FAIL_IF_NO_PEER_CERT",
1457 SSL_VERIFY_CLIENT_ONCE:
"SSL_VERIFY_CLIENT_ONCE",
1466 *HandlerInfo defaultHandler;
1506 setDefaultHandler(
string name);
1509 *HandlerInfo findHandler(hash<auto> hdr, reference<int> score,
bool finalv = False, *reference<string> root_path);
1521 hash getListenerSocketInfo();
1530 logRecvHeaders(softbool flag = True);
1533 logRecvBody(softbool flag = True);
1536 logSendHeaders(softbool flag = True);
1539 logSendBody(softbool flag = True);
1542 doLogRecvBody(*data body, hash<auto> cx, hash<auto> hdr, hash<auto> info);
1545 hash<auto> getLogOptions();
1560 hash<auto> getInfo();
1569 softlist<string> getSslVerifyModeList();
1573 auto removeUserThreadContext(*
string k);
1576 addUserThreadContext(hash<auto> uctx);
1585 logResponse(hash<auto> cx,
int code, *data body, *hash<auto> hdr);
1588 logResponse(hash<auto> cx,
int code, InputStream body, *hash<auto> hdr);
1591 logResponse(hash<auto> cx, hash<auto> rv);
1597 logError(
string fmt);
1607 connectionThread(Socket s);
1630 *hash<string, bool> content_type_map;
1631 *list<string> special_header_list;
1636 constructor(
string name, hash<HttpHandlerConfigInfo> info);
1639 bool matchContentType(
string ct);
1643 int matchRequest(hash<auto> hdr,
int score);
1651class HttpHandlerList {
1654 hash<string, HandlerInfo> handlers;
1669 setHandler(
string name, hash<HttpHandlerConfigInfo> info);
1676 removeHandler(
string handler_name);
1687 *HandlerInfo findHandler(hash<auto> hdr, reference<int> score,
bool finalv = False, *reference<string> root_path);
1698class DynamicHandlerInfo :
public HandlerInfo {
1703 constructor(
string name, hash<HttpHandlerConfigInfo> info) ;
1708class DynamicHandlerHelper {
1718 constructor(Counter n_c);
1726class DynamicHttpHandlerList :
public HttpHandlerList {
1733 hash<string, bool> disable_map;
1738 setHandler(
string name, hash<HttpHandlerConfigInfo> info);
1744 removeHandler(
string name, *
bool force_immediate);
1750 disableHandler(
string name);
1753 *DynamicHandlerInfo findHandler(hash<auto> hdr, reference<int> score, reference<DynamicHandlerHelper> dhh, *reference<string> root_path);
1757class HttpServerCallbackAppender :
public LoggerAppenderWithLayout {
1765 constructor(code logfunc) ;
1768 processEventImpl(
int type,
auto params);
*hash< HttpResponseInfo > authenticateRequest(HttpListenerInterface listener, hash< auto > hdr, reference< hash< auto > > cx)
hash< HttpResponseInfo > handleRequest(hash< auto > cx, hash< auto > hdr, *data body)
restoreThreadLocalData(*hash< auto > data)
bool decompress_to_string
static data decodeBody(string content_encoding, binary body, *string string_encoding)
*AbstractAuthenticator auth
*hash< auto > saveThreadLocalData()
hash< HttpResponseInfo > handleExpectHeader(hash< auto > cx, hash< auto > hdr)
this class implements the listeners for the HttpServer class
Definition: HttpServer.qm.dox.h:1384
removeHandler(HttpServer::AbstractHttpRequestHandler handler)
const SslVerifyMap
map for converting ssl verify flags to strings
Definition: HttpServer.qm.dox.h:1454
constructor(HttpServer server, string name, int id, Sequence ss, *hash< HttpListenerOptionInfo > opts)
creates the object with the given parameters
static bool stopIfNoHandlers(HttpServer::HttpListener listener)
stop listeners if there are no handlers
removeHandler(string handler_name)
addHandlers(hash< string, hash< HttpHandlerConfigInfo > > handler_info)
add handlers to the listener
bool get_remote_certs
get remote certificates
Definition: HttpServer.qm.dox.h:1435
int ssl_verify_flags
SSL verify flags.
Definition: HttpServer.qm.dox.h:1438
bool ssl_accept_all_certs
accept all certificates
Definition: HttpServer.qm.dox.h:1441
*Logger logger
listener-specific logger; if not present, then the server's logger will be used
Definition: HttpServer.qm.dox.h:1415
reloadCertificate()
Reloads the HTTPS certificate from the original location.
constructor()
Creates the server with default options and an empty logger.
stopListenerID(softint id)
stops a single listener based on its listener ID; does not return until all connections on the listen...
const ReadTimeout
default read timeout in ms
Definition: HttpServer.qm.dox.h:368
hash< string, hash< auto > > getListeners()
returns a hash of listener information
static nothing setReplyHeaders(Socket s, hash< auto > cx, reference< hash< auto > > rv)
helper method to set HTTP response headers
disableDynamicHandler(string name)
Disable dynamic handler.
final list< hash< auto > > addListeners(string bind, hash< HttpListenerOptionInfo > info, *reference< hash< string, string > > errmap)
adds one or more dedicated listeners to the server with the given bind address
Definition: HttpServer.qm.dox.h:586
setMaskCode(code maskfunc)
sets the closure or call reference that will be used to mask sensitive data in log messages
addHandlerToListener(softstring bind, string name, hash< HttpHandlerConfigInfo > info)
adds a request handler to a listener given the listener's name or bind address
static string getHttpServerVersionString()
returns the HTTP server version string
setDefaultHandler(string name, HttpServer::AbstractHttpRequestHandler obj)
sets the default request handler when no other handler can be matched
removeDynamicHandler(string name, *bool force_immediate)
remove dynamic handler
int getListenerCount()
returns the number of running HTTP listeners
destructor()
calls stop() and destroys the object
hash< auto > addListener(hash< HttpListenerOptionInfo > opts)
adds a single global listener to the server
setDynamicHandler(string name, HttpServer::AbstractUrlHandler obj)
sets a dynamic request handler according to the arguments given
string maskData(string msg)
masks log messages by removing sensitive data
listenerStarted(int id, hash< auto > sinfo)
called from listener when the actual listener thread is running
bool removeHandlerFromListenerID(softstring id, HttpServer::AbstractHttpRequestHandler handler)
remove request handler from a listener given the listener's id
waitStop()
waits for all listeners to be stopped; call after calling HttpServer::stopNoWait()
reloadListenerCertificate(int id)
Reloads an HTTPS certificate from the original location for the given listener from the listener ID.
hash< auto > getListenerInfoName(string name)
returns a hash of information about the listener given the listener name or bind ID
stopListener(softstring bind)
stops a single listener based on its name or bind address; does not return until all connections on t...
static string getURLFromBind(softstring bind, *string host)
returns a complete URL from a bind address
reloadListenerCertificateName(string name)
Reloads an HTTPS certificate from the original location for the given listener from the listener name...
copy()
throws an exception; these objects do not support copying
bool removeHandlerFromListenerID(softstring id, string handler_name)
remove request handler from a listener given the listener's id
logError(string fmt)
called to log error information to the registered error log code
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
addHandlerToListenerID(softint id, string name, hash< HttpHandlerConfigInfo > info)
adds a request handler to a listener given the listener's id
constructor(hash< HttpServerOptionInfo > opts)
Creates the server with the given options.
deprecated constructor(*code logfunc, *code errlogfunc, bool dbg=False, string name=HttpServer::HttpServerString, hash< auto > hdr={"X-Powered-By":"Qore/"+Qore::VersionString})
creates the HttpServer
Definition: HttpServer.qm.dox.h:519
addHttpMethod(string m)
add a new supported HTTP method
final list< hash< auto > > addListeners(hash< HttpListenerOptionInfo > info, *reference< hash< string, string > > errmap)
adds one or more dedicated listeners to the server with the given bind address
int getListenerTID(softint id)
gets the TID of a listener based on its listener ID
callListenerStopCallback(code stopc, string name, hash< auto > socket_info)
To call a listener's stop callback.
bool getDebug()
returns the current status of the debug flag
addHandlerToListenerID(softint id, string name, HttpServer::AbstractUrlHandler obj)
adds a request handler to a listener given the listener's id
setListenerLogOptions(softstring bind, softint code)
turns on or off header and body logging options for receive operations for the given listener
addHandlerToListener(softstring bind, string name, HttpServer::AbstractUrlHandler obj)
adds a request handler to a listener given the listener's name or bind address
setDebug(bool dbg=True)
turns on or off debugging; when debugging is enabled more verbose error messages are reported
const HttpCodes
map of HTTP result codes and text messages
Definition: HttpServer.qm.dox.h:393
hash< auto > addListener(int port)
adds a single global listener to the server
setHandler(string name, hash< HttpHandlerConfigInfo > info)
sets a request handler according to the arguments given
setListenerLogOptionsID(softint id, softint code)
turns on or off header and body logging options for receive operations for the given listener
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
log(string fmt)
called to log information to the registered log code
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
Definition: HttpServer.qm.dox.h:848
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
Definition: HttpServer.qm.dox.h:804
int getListenerLogOptionsUnlocked(softstring id)
returns header and body logging options for the given listener as a binary-or'ed value of listener lo...
int getListenerLogOptions(softstring bind)
returns a binary-or'ed product of listener log options corresponding to enabled log options for the g...
stopNoWait()
stops all listeners; does not wait for all connections on the listeners to close
stopListenerIDNoWait(softint id)
starts the shutdown process for a listener based on its listener ID; returns immediately
int getListenerLogOptionsID(softint id)
returns a binary-or'ed product of listener log options corresponding to enabled log options for the g...
setHandler(string name, HttpServer::AbstractUrlHandler obj)
sets a request handler according to the arguments given
setListenerLogOptionsUnlocked(softstring id, int code)
turns on or off header and body logging options for the given listener according to the option code
stop()
stops all listeners; only returns when all connections are closed on all listeners
hash< auto > getListenerInfo(softint id)
returns a hash of information about the listener given the listener ID
int getListenerIdFromBindUnlocked(string bind)
returns the listener ID from the bind name or throws an exception if not valid
setDynamicHandler(string name, hash< HttpHandlerConfigInfo > info)
sets a dynamic request handler according to the arguments given
binary bzip2(binary bin, softint level=BZ2_DEFAULT_COMPRESSION)
binary compress(string str, int level=Z_DEFAULT_COMPRESSION)
binary gzip(string str, int level=Z_DEFAULT_COMPRESSION)
string get_ex_pos(hash< auto > ex)
string sprintf(string fmt,...)
string force_encoding(string str, string encoding)
hash< auto > hash(object obj)
the main namespace for the HttpServer module
nothing http_set_reply_headers(Socket s, hash< auto > cx, reference< auto > rv, *string server_string)
string get_exception_string(hash< auto > ex)
returns a multi-line string from the exception hash argument suitable for logging or output on the co...
Private namespace.
Definition: HttpServer.qm.dox.h:1620
Hash for HttpServer options.
Definition: HttpServer.qm.dox.h:345
*Logger logger
HTTP server logger.
Definition: HttpServer.qm.dox.h:358
bool debug
Debug flag.
Definition: HttpServer.qm.dox.h:350
string name
HTTP server name.
Definition: HttpServer.qm.dox.h:347
hash< auto > hdr
Headers to add in responses.
Definition: HttpServer.qm.dox.h:353