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
525namespace RestHandler {
527
535
536public:
537
538
539protected:
541 hash<auto> cx;
542
544 *hash<auto> ex;
545
547 *hash<auto> ah;
548
550 hash<auto> rhdr;
551
554
555public:
556
558
561 constructor(hash<auto> cx, *hash<auto> ah);
562
563
565
575 hash<HttpServer::HttpHandlerResponseInfo> getResponseHeaderMessage();
576
577
579
598 nothing recv(hash<auto> v);
599
600
602
607 auto send();
608
609
612
613
616
617
620
621
624
625
627 streamError(hash<auto> ex);
628
629
631
640 abstract hash<auto> getResponseHeaderMessageImpl();
641
643
653 abstract nothing recvImpl(hash<auto> v);
654
656
659 abstract auto sendImpl();
660 };
661
664
665public:
666protected:
668 bool deserialized;
669
670public:
671
674 ;
675
676
679
680
681 // callback method for the response header; this calls @ref RestHandler::AbstractRestStreamRequestHandler::getResponseHeaderMessage() and returns the result
691protected:
692 hash<HttpServer::HttpHandlerResponseInfo> getResponseHeaderMessageImpl();
693public:
694
695
697
707protected:
708 nothing recvImpl(hash<auto> v);
709public:
710
711
713
716protected:
717 auto sendImpl();
718public:
719
720 };
721
724
725public:
727 hash<string, AbstractRestClass> class_hash;
728
730 const RestBasicMethodSet = map {$1: True}, RestHandler::Methods.iterator();
731
732 constructor();
733
734
736 abstract string name();
737
740
741
743
761 *AbstractRestClass subClass(string name, hash<auto> cx, *hash<auto> args);
762
763
765
781 *AbstractRestClass subClassImpl(string name, hash<auto> cx, *hash<auto> args);
782
783
785 hash<HttpServer::HttpHandlerResponseInfo> handleRequest(HttpListenerInterface listener, RestHandler rh, Socket s, *list<string> cl, string mn, hash<auto> cx, *hash<auto> args);
786
787
789protected:
790 hash<HttpServer::HttpHandlerResponseInfo> dispatchStream(HttpListenerInterface listener, RestHandler rh, Socket s, string mn, *hash<auto> ah, hash<auto> cx);
791public:
792
793
795
806protected:
807 hash<HttpServer::HttpHandlerResponseInfo> dispatch(RestHandler rh, string mn, *hash<auto> ah, hash<auto> cx);
808public:
809
810
812
825protected:
826 hash<HttpServer::HttpHandlerResponseInfo> unknownSubClassError(string cls_name, hash<auto> cx, *hash<auto> ah);
827public:
828
829
831
843protected:
844 *hash<string, bool> doGetPossibleSubClasses(hash<auto> cx, *hash<auto> ah);
845public:
846
847 };
848
850
861
862public:
864 const Err501 = ...;
865
866
868 const Methods = ...;
869
870
871protected:
874
875public:
876
878
885 ;
886
887
889
896 constructor(Logger::LoggerInterface logger, *HttpServer::AbstractAuthenticator auth, RestSchemaValidator::AbstractRestSchemaValidator validator = new NullRestSchemaValidator(logger))
897 ;
898
899
901 string name();
902
903
905 setLogger(*Logger::LoggerInterface logger);
906
907
909 *AbstractRestSchemaValidator getRestSchemaValidator();
910
911
913
937 auto handleExternalRequest(string method, string path, *hash<auto> body, hash<auto> cx = {});
938
939
941
960 hash<HttpResponseInfo> handleRequest(HttpListenerInterface listener, Socket s, hash<auto> cx, hash<auto> hdr, *data b);
961
962
964
966protected:
967 *hash<auto> errorResponseHeaders(hash<auto> cx);
968public:
969
970
972protected:
973 hash<HttpHandlerResponseInfo> dispatchRequest(HttpListenerInterface listener, Socket s, *list<string> class_list, string method_name, string path, hash<auto> cx, *hash<auto> args);
974public:
975
976
978
981protected:
982 hash<HttpHandlerResponseInfo> returnRestException(hash<ExceptionInfo> ex);
983public:
984
985
987protected:
988 checkExceptionSerializable(reference<hash<ExceptionInfo>> ex);
989public:
990
991
993 hash<auto> get(hash<auto> cx, *hash<auto> ah);
994
995
997 removeRootPath(reference<string> path);
998
999
1001 requestDeserializationError(hash<auto> hdr, hash<auto> cx, string body);
1002
1003
1005 responseSerializationError(hash<auto> cx, *hash<auto> aih, hash<auto> rv);
1006
1007
1009
1011 logInfo(string fmt, ...);
1012
1013
1015
1017 logError(string fmt, ...);
1018
1019
1021
1023 logDebug(string fmt, ...);
1024
1025
1027 static hash<HttpHandlerResponseInfo> make200(string fmt, ...);
1028
1030 static hash<HttpHandlerResponseInfo> make200(hash<auto> hdr, string fmt, ...);
1031
1033 static hash<HttpHandlerResponseInfo> make400(string fmt, ...);
1034
1036 static hash<HttpHandlerResponseInfo> make400(hash<auto> hdr, string fmt, ...);
1037
1039 static hash<HttpHandlerResponseInfo> makeResponse(int code, auto body, *hash<auto> hdr);
1040
1042 static hash<HttpHandlerResponseInfo> make500(string fmt, ...);
1043
1045 static hash<HttpHandlerResponseInfo> make500(hash<auto> hdr, string fmt, ...);
1046
1048 static hash<HttpHandlerResponseInfo> make501(string fmt, ...);
1049
1051 static hash<HttpHandlerResponseInfo> make501(hash<auto> hdr, string fmt, ...);
1052 };
1053};
1054
1055// private, unexported namespace
1056namespace Priv {
1057 // dumHttpListenerInterface class
1058class DummyListenerInterface : public HttpListenerInterface {
1059
1060public:
1061 addUserThreadContext(hash<auto> uctx);
1062
1063
1064 auto removeUserThreadContext(*string k);
1065
1066
1067 log(string fmt, ...);
1068
1069
1070 logError(string fmt, ...);
1071
1072 };
1073};
AbstractHttpRequestHandler handler
HttpListenerInterface listener
the base abstract class for REST handler classes
Definition RestHandler.qm.dox.h:723
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:727
*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:730
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:534
hash< auto > rhdr
headers to add in the response
Definition RestHandler.qm.dox.h:550
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:541
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:547
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:553
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:544
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:868
RestSchemaValidator::AbstractRestSchemaValidator validator
REST schema validator.
Definition RestHandler.qm.dox.h:873
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:663
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:525