Qore RestHandler Module Reference 1.6.3
Loading...
Searching...
No Matches
RestHandler.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* RestHandler.qm Copyright (C) 2013 - 2025 Qore Technologies, s.r.o.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23*/
24
25// minimum qore version
26
27// use new-style
28
29// require type definitions everywhere
30
31// enable all warnings
32
33
34
521namespace RestHandler {
523
531
532public:
533
534
535protected:
537 hash<auto> cx;
538
540 *hash<auto> ex;
541
543 *hash<auto> ah;
544
546 hash<auto> rhdr;
547
550
551public:
552
554
557 constructor(hash<auto> cx, *hash<auto> ah);
558
559
561
571 hash<HttpServer::HttpHandlerResponseInfo> getResponseHeaderMessage();
572
573
575
594 nothing recv(hash<auto> v);
595
596
598
603 auto send();
604
605
608
609
612
613
616
617
620
621
623 streamError(hash<auto> ex);
624
625
627
636 abstract hash<auto> getResponseHeaderMessageImpl();
637
639
649 abstract nothing recvImpl(hash<auto> v);
650
652
655 abstract auto sendImpl();
656 };
657
660
661public:
662protected:
664 bool deserialized;
665
666public:
667
670 ;
671
672
675
676
677 // callback method for the response header; this calls @ref RestHandler::AbstractRestStreamRequestHandler::getResponseHeaderMessage() and returns the result
687protected:
688 hash<HttpServer::HttpHandlerResponseInfo> getResponseHeaderMessageImpl();
689public:
690
691
693
703protected:
704 nothing recvImpl(hash<auto> v);
705public:
706
707
709
712protected:
713 auto sendImpl();
714public:
715
716 };
717
720
721public:
723 hash<string, AbstractRestClass> class_hash;
724
726 const RestBasicMethodSet = map {$1: True}, RestHandler::Methods.iterator();
727
728 constructor();
729
730
732 abstract string name();
733
736
737
739
757 *AbstractRestClass subClass(string name, hash<auto> cx, *hash<auto> args);
758
759
761
777 *AbstractRestClass subClassImpl(string name, hash<auto> cx, *hash<auto> args);
778
779
781 hash<HttpServer::HttpHandlerResponseInfo> handleRequest(HttpListenerInterface listener, RestHandler rh, Socket s, *list<string> cl, string mn, hash<auto> cx, *hash<auto> args);
782
783
785protected:
786 hash<HttpServer::HttpHandlerResponseInfo> dispatchStream(HttpListenerInterface listener, RestHandler rh, Socket s, string mn, *hash<auto> ah, hash<auto> cx);
787public:
788
789
791
802protected:
803 hash<HttpServer::HttpHandlerResponseInfo> dispatch(RestHandler rh, string mn, *hash<auto> ah, hash<auto> cx);
804public:
805
806
808
821protected:
822 hash<HttpServer::HttpHandlerResponseInfo> unknownSubClassError(string cls_name, hash<auto> cx, *hash<auto> ah);
823public:
824
825
827
839protected:
840 *hash<string, bool> doGetPossibleSubClasses(hash<auto> cx, *hash<auto> ah);
841public:
842
843 };
844
846
857
858public:
860 const Err501 = ...;
861
862
864 const Methods = ...;
865
866
867protected:
870
871public:
872
874
881 ;
882
883
885
892 constructor(Logger::LoggerInterface logger, *HttpServer::AbstractAuthenticator auth, RestSchemaValidator::AbstractRestSchemaValidator validator = new NullRestSchemaValidator(logger))
893 ;
894
895
897 string name();
898
899
901 setLogger(*Logger::LoggerInterface logger);
902
903
905 *AbstractRestSchemaValidator getRestSchemaValidator();
906
907
909
933 auto handleExternalRequest(string method, string path, *hash<auto> body, hash<auto> cx = {});
934
935
937
956 hash<HttpResponseInfo> handleRequest(HttpListenerInterface listener, Socket s, hash<auto> cx, hash<auto> hdr, *data b);
957
958
960
962protected:
963 *hash<auto> errorResponseHeaders(hash<auto> cx);
964public:
965
966
968protected:
969 hash<HttpHandlerResponseInfo> dispatchRequest(HttpListenerInterface listener, Socket s, *list<string> class_list, string method_name, string path, hash<auto> cx, *hash<auto> args);
970public:
971
972
974
977protected:
978 hash<HttpHandlerResponseInfo> returnRestException(hash<ExceptionInfo> ex);
979public:
980
981
983protected:
984 checkExceptionSerializable(reference<hash<ExceptionInfo>> ex);
985public:
986
987
989 hash<auto> get(hash<auto> cx, *hash<auto> ah);
990
991
993 removeRootPath(reference<string> path);
994
995
997 requestDeserializationError(hash<auto> hdr, hash<auto> cx, string body);
998
999
1001 responseSerializationError(hash<auto> cx, *hash<auto> aih, hash<auto> rv);
1002
1003
1005
1007 logInfo(string fmt, ...);
1008
1009
1011
1013 logError(string fmt, ...);
1014
1015
1017
1019 logDebug(string fmt, ...);
1020
1021
1023 static hash<HttpHandlerResponseInfo> make200(string fmt, ...);
1024
1026 static hash<HttpHandlerResponseInfo> make200(hash<auto> hdr, string fmt, ...);
1027
1029 static hash<HttpHandlerResponseInfo> make400(string fmt, ...);
1030
1032 static hash<HttpHandlerResponseInfo> make400(hash<auto> hdr, string fmt, ...);
1033
1035 static hash<HttpHandlerResponseInfo> makeResponse(int code, auto body, *hash<auto> hdr);
1036
1038 static hash<HttpHandlerResponseInfo> make500(string fmt, ...);
1039
1041 static hash<HttpHandlerResponseInfo> make500(hash<auto> hdr, string fmt, ...);
1042
1044 static hash<HttpHandlerResponseInfo> make501(string fmt, ...);
1045
1047 static hash<HttpHandlerResponseInfo> make501(hash<auto> hdr, string fmt, ...);
1048 };
1049};
1050
1051// private, unexported namespace
1052namespace Priv {
1053 // dumHttpListenerInterface class
1054class DummyListenerInterface : public HttpListenerInterface {
1055
1056public:
1057 addUserThreadContext(hash<auto> uctx);
1058
1059
1060 auto removeUserThreadContext(*string k);
1061
1062
1063 log(string fmt, ...);
1064
1065
1066 logError(string fmt, ...);
1067
1068 };
1069};
AbstractHttpRequestHandler handler
HttpListenerInterface listener
the base abstract class for REST handler classes
Definition RestHandler.qm.dox.h:719
hash< HttpServer::HttpHandlerResponseInfo > unknownSubClassError(string cls_name, hash< auto > cx, *hash< auto > ah)
returns a 404 Not Found response when a request tries to access an unknown subclass
abstract string name()
this provides the name of the REST class
hash< HttpServer::HttpHandlerResponseInfo > dispatchStream(HttpListenerInterface listener, RestHandler rh, Socket s, string mn, *hash< auto > ah, hash< auto > cx)
this method is called to dispatch streamed requests on the given object
hash< HttpServer::HttpHandlerResponseInfo > handleRequest(HttpListenerInterface listener, RestHandler rh, Socket s, *list< string > cl, string mn, hash< auto > cx, *hash< auto > args)
this method is called by the RestHandler class to match the right object with incoming requests
addClass(AbstractRestClass cls)
adds a REST class to the handler
*AbstractRestClass subClassImpl(string name, hash< auto > cx, *hash< auto > args)
this method will be called to find a sub-class
*hash< string, bool > doGetPossibleSubClasses(hash< auto > cx, *hash< auto > ah)
returns a set of possible subclasses for a particular request
hash< string, AbstractRestClass > class_hash
class hash: name -> AbstractRestClass
Definition RestHandler.qm.dox.h:723
*AbstractRestClass subClass(string name, hash< auto > cx, *hash< auto > args)
this method will be called to find a sub-class
const RestBasicMethodSet
set of REST class method names based on basic HTTP methods
Definition RestHandler.qm.dox.h:726
hash< HttpServer::HttpHandlerResponseInfo > dispatch(RestHandler rh, string mn, *hash< auto > ah, hash< auto > cx)
this method is called to dispatch requests on the given object
the base abstract class for REST stream request handlers
Definition RestHandler.qm.dox.h:530
hash< auto > rhdr
headers to add in the response
Definition RestHandler.qm.dox.h:546
auto send()
this method provides the callback method for sending chunked data by calling sendImpl()
abstract auto sendImpl()
abstract callback method for sending chunked data
hash< auto > cx
call context hash
Definition RestHandler.qm.dox.h:537
nothing recv(hash< auto > v)
this method provides the callback method for receiving chunked data by calling recvImpl()
*int getTimeout()
returns the timeout in milliseconds or NOTHING if no timeout is set
*hash< auto > ah
call argument hash
Definition RestHandler.qm.dox.h:543
hash< HttpServer::HttpHandlerResponseInfo > getResponseHeaderMessage()
this method returns the response message description hash by calling getResponseHeaderMessageImpl()
bool isPersistent()
returns True if the connection is persistent; this method in the base class returns False by default
abstract hash< auto > getResponseHeaderMessageImpl()
this method should return the response message description hash
*int timeout_ms
socket I/O timeout in milliseconds
Definition RestHandler.qm.dox.h:549
constructor(hash< auto > cx, *hash< auto > ah)
creates the object with the given arguments
*hash< auto > ex
if an exception is raised in a callback then the exception hash is saved here
Definition RestHandler.qm.dox.h:540
streamError(hash< auto > ex)
registers stream errors in the send operation with the stream handler if no error is already present
*code getPersistentClosedNotification()
returns a callable value in case a persistent connection is in progress; NOTHING if not; this method ...
setTimeout(timeout timeout_ms)
sets the internal socket I/O timeout value in ms
abstract nothing recvImpl(hash< auto > v)
abstract callback method for receiving chunked data
auto handleExternalRequest(string method, string path, *hash< auto > body, hash< auto > cx={})
processes REST API calls outside the HTTP server
logError(string fmt,...)
This method is called with error log messages.
responseSerializationError(hash< auto > cx, *hash< auto > aih, hash< auto > rv)
default implementation is empty
static hash< HttpHandlerResponseInfo > make500(hash< auto > hdr, string fmt,...)
creates a hash for an HTTP 500 error response with the response message body as a string
setLogger(*Logger::LoggerInterface logger)
Sets a new logger.
const Methods
supported HTTP methods
Definition RestHandler.qm.dox.h:864
RestSchemaValidator::AbstractRestSchemaValidator validator
REST schema validator.
Definition RestHandler.qm.dox.h:869
removeRootPath(reference< string > path)
default implementation is empty
hash< HttpResponseInfo > handleRequest(HttpListenerInterface listener, Socket s, hash< auto > cx, hash< auto > hdr, *data b)
called by the HTTP server to handle incoming HTTP requests
logInfo(string fmt,...)
This method is called with informational log messages.
hash< HttpHandlerResponseInfo > dispatchRequest(HttpListenerInterface listener, Socket s, *list< string > class_list, string method_name, string path, hash< auto > cx, *hash< auto > args)
Dispatches the request and returns the response.
*AbstractRestSchemaValidator getRestSchemaValidator()
Returns any REST schema validator set for the handler.
static hash< HttpHandlerResponseInfo > make501(string fmt,...)
creates a hash for an HTTP 501 error response with the response message body as a string
checkExceptionSerializable(reference< hash< ExceptionInfo > > ex)
Recursively ensure that exception arguments are serializable.
static hash< HttpHandlerResponseInfo > make500(string fmt,...)
creates a hash for an HTTP 500 error response with the response message body as a string
static hash< HttpHandlerResponseInfo > make400(string fmt,...)
creates a hash for an HTTP 400 error response with the response message body as a string
static hash< HttpHandlerResponseInfo > makeResponse(int code, auto body, *hash< auto > hdr)
creates a hash for an HTTP response with the response code and a literal response message body
logDebug(string fmt,...)
This method is called with debug log messages.
hash< HttpHandlerResponseInfo > returnRestException(hash< ExceptionInfo > ex)
method that determines how exceptions handling REST requests are returned
constructor(Logger::LoggerInterface logger, *HttpServer::AbstractAuthenticator auth, RestSchemaValidator::AbstractRestSchemaValidator validator=new NullRestSchemaValidator(logger))
create the object optionally with the given Logger and authenticator
constructor(*HttpServer::AbstractAuthenticator auth, RestSchemaValidator::AbstractRestSchemaValidator validator=new NullRestSchemaValidator())
create the object optionally with the given HttpServer::AbstractAuthenticator
static hash< HttpHandlerResponseInfo > make501(hash< auto > hdr, string fmt,...)
creates a hash for an HTTP 501 error response with the response message body as a string
string name()
returns the name of the root REST class
*hash< auto > errorResponseHeaders(hash< auto > cx)
Retrieves headers for an error response.
static hash< HttpHandlerResponseInfo > make400(hash< auto > hdr, string fmt,...)
creates a hash for an HTTP 400 error response with the response message body as a string
static hash< HttpHandlerResponseInfo > make200(string fmt,...)
creates a hash for an HTTP 200 OK error response with the response message body as a string
hash< auto > get(hash< auto > cx, *hash< auto > ah)
default get handler for the base handler class
requestDeserializationError(hash< auto > hdr, hash< auto > cx, string body)
default implementation is empty
static hash< HttpHandlerResponseInfo > make200(hash< auto > hdr, string fmt,...)
creates a hash for an HTTP 200 OK error response with the response message body as a string
the base class for handling HTTP chunked requests and responses within the RestHandler infrastructure
Definition RestHandler.qm.dox.h:659
nothing recvImpl(hash< auto > v)
callback method for receiving chunked data; this calls RestHandler::AbstractRestStreamRequestHandler:...
hash< HttpServer::HttpHandlerResponseInfo > getResponseHeaderMessageImpl()
destructor()
destroys the object and updates the request handler about the status of the persistent connection
constructor(RestHandler::AbstractRestStreamRequestHandler stream, HttpServer::HttpListenerInterface listener, HttpServer::AbstractHttpRequestHandler handler, Socket s, hash< auto > cx, hash< auto > hdr, auto body)
creates the object with the given attributes
auto sendImpl()
callback method for sending chunked data; this calls RestHandler::AbstractRestStreamRequestHandler::s...
the RestHandler namespace contains all the objects in the RestHandler module
Definition RestHandler.qm.dox.h:521