1 | /** @file
|
---|
2 | * IPRT - C++ Representational State Transfer (REST) Base Classes.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2008-2018 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_cpp_restbase_h
|
---|
27 | #define ___iprt_cpp_restbase_h
|
---|
28 |
|
---|
29 | #include <iprt/types.h>
|
---|
30 | #include <iprt/assert.h>
|
---|
31 | #include <iprt/err.h>
|
---|
32 | #include <iprt/http.h>
|
---|
33 | #include <iprt/json.h>
|
---|
34 | #include <iprt/stdarg.h>
|
---|
35 | #include <iprt/cpp/ministring.h>
|
---|
36 | #include <iprt/cpp/utils.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /** @defgroup grp_rt_cpp_restbase C++ Representational State Transfer (REST) Base Classes.
|
---|
40 | * @ingroup grp_rt_cpp
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Very limited map class that avoids dragging in std::map.
|
---|
47 | */
|
---|
48 | template<class Type> class RTCRestStringMap
|
---|
49 | {
|
---|
50 | public:
|
---|
51 | RTCRestStringMap() {};
|
---|
52 | ~RTCRestStringMap() {};
|
---|
53 | /** @todo more later. */
|
---|
54 | };
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Abstract base class for serializing data objects.
|
---|
59 | */
|
---|
60 | class RTCRestOutputBase
|
---|
61 | {
|
---|
62 | public:
|
---|
63 | RTCRestOutputBase();
|
---|
64 | virtual ~RTCRestOutputBase();
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * RTStrPrintf like function (see @ref pg_rt_str_format).
|
---|
68 | *
|
---|
69 | * @returns Number of bytes outputted.
|
---|
70 | * @param uIndent The indentation level.
|
---|
71 | * @param pszFormat The format string.
|
---|
72 | * @param ... Argument specfied in @a pszFormat.
|
---|
73 | */
|
---|
74 | size_t printf(unsigned uIndent, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3)
|
---|
75 | {
|
---|
76 | va_list va;
|
---|
77 | va_start(va, pszFormat);
|
---|
78 | size_t cchWritten = this->vprintf(uIndent, pszFormat, va);
|
---|
79 | va_end(va);
|
---|
80 | return cchWritten;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * RTStrPrintfV like function (see @ref pg_rt_str_format).
|
---|
85 | *
|
---|
86 | * @returns Number of bytes outputted.
|
---|
87 | * @param uIndent The indentation level.
|
---|
88 | * @param pszFormat The format string.
|
---|
89 | * @param va Argument specfied in @a pszFormat.
|
---|
90 | */
|
---|
91 | virtual size_t vprintf(unsigned uIndent, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0) = 0;
|
---|
92 | };
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Serialize to a string object.
|
---|
97 | */
|
---|
98 | class RTCRestOutputToString : public RTCRestOutputBase
|
---|
99 | {
|
---|
100 | public:
|
---|
101 | /**
|
---|
102 | * Creates an instance that appends to @a a_pDst.
|
---|
103 | * @param a_pDst Pointer to the destination string object.
|
---|
104 | * NULL is not accepted and will assert.
|
---|
105 | */
|
---|
106 | RTCRestOutputToString(RTCString *a_pDst);
|
---|
107 | virtual ~RTCRestOutputToString();
|
---|
108 |
|
---|
109 | size_t vprintf(unsigned uIndent, const char *pszFormat, va_list va);
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Finalizes the output and releases the string object to the caller.
|
---|
113 | *
|
---|
114 | * @returns The released string object. NULL if we ran out of memory or if
|
---|
115 | * called already.
|
---|
116 | *
|
---|
117 | * @remark This sets m_pDst to NULL and the object cannot be use for any
|
---|
118 | * more output afterwards.
|
---|
119 | */
|
---|
120 | virtual RTCString *finalize(void);
|
---|
121 |
|
---|
122 |
|
---|
123 | protected:
|
---|
124 | /** Pointer to the destination string. NULL after finalize(). */
|
---|
125 | RTCString *m_pDst;
|
---|
126 | /** Set if we ran out of memory and should ignore subsequent calls. */
|
---|
127 | bool m_fOutOfMemory;
|
---|
128 |
|
---|
129 | /* Make non-copyable (RTCNonCopyable causes warnings): */
|
---|
130 | RTCRestOutputToString(RTCRestOutputToString const &);
|
---|
131 | RTCRestOutputToString *operator=(RTCRestOutputToString const &);
|
---|
132 | };
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Abstract base class for REST data objects.
|
---|
137 | */
|
---|
138 | class RTCRestObjectBase
|
---|
139 | {
|
---|
140 | public:
|
---|
141 | RTCRestObjectBase() {}
|
---|
142 | virtual ~RTCRestObjectBase() {}
|
---|
143 |
|
---|
144 | /** @todo Add some kind of state? */
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Serialize the object as JSON.
|
---|
148 | *
|
---|
149 | * @returns a_rDst
|
---|
150 | * @param a_rDst The destination for the serialization.
|
---|
151 | * @param uIndent The indentation level. Increment by 1 for child
|
---|
152 | * objects.
|
---|
153 | */
|
---|
154 | virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst, unsigned uIndent) = 0;
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Deserialize object from the given JSON iterator.
|
---|
158 | *
|
---|
159 | * @returns IPRT status code.
|
---|
160 | * @param hJsonIt The JSON iterator for this object.
|
---|
161 | * @param pErrInfo Where to return additional error information.
|
---|
162 | * Optional.
|
---|
163 | *
|
---|
164 | * @todo Take a RTJSONVAL?
|
---|
165 | */
|
---|
166 | virtual int deserializeFromJson(RTJSONIT hJsonIt, PRTERRINFO pErrInfo) = 0;
|
---|
167 | };
|
---|
168 |
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Base class for REST client requests.
|
---|
172 | */
|
---|
173 | class RTCRestClientRequestBase
|
---|
174 | {
|
---|
175 | public:
|
---|
176 | RTCRestClientRequestBase() {}
|
---|
177 | virtual ~RTCRestClientRequestBase() {};
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Prepares the HTTP handle for transmitting this request.
|
---|
181 | *
|
---|
182 | * @returns IPRT status code.
|
---|
183 | * @param a_hHttp The HTTP handle to prepare for transmitting.
|
---|
184 | */
|
---|
185 | virtual int xmitPrepare(RTHTTP a_hHttp);
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Always called after the request has been transmitted.
|
---|
189 | *
|
---|
190 | * @param a_rcStatus Negative numbers are IPRT errors, positive are HTTP status codes.
|
---|
191 | * @param a_hHttp The HTTP handle the request was performed on.
|
---|
192 | */
|
---|
193 | virtual void xmitComplete(int a_rcStatus, RTHTTP a_hHttp);
|
---|
194 | };
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Base class for REST client responses.
|
---|
199 | */
|
---|
200 | class RTCRestClientResponseBase
|
---|
201 | {
|
---|
202 | public:
|
---|
203 | RTCRestClientResponseBase()
|
---|
204 | : m_rcStatus(VERR_WRONG_ORDER)
|
---|
205 | {}
|
---|
206 | RTCRestClientResponseBase(RTCRestClientResponseBase const &a_rThat)
|
---|
207 | : m_rcStatus(a_rThat.m_rcStatus)
|
---|
208 | {}
|
---|
209 | virtual ~RTCRestClientResponseBase();
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Prepares the HTTP handle for receiving the response.
|
---|
213 | *
|
---|
214 | * This may install callbacks and such like.
|
---|
215 | *
|
---|
216 | * @returns IPRT status code.
|
---|
217 | * @param a_hHttp The HTTP handle to prepare for receiving.
|
---|
218 | */
|
---|
219 | virtual int receivePrepare(RTHTTP a_hHttp);
|
---|
220 |
|
---|
221 | virtual int consumeHeader(const char *a_pvData, size_t a_cbData); ///< ??
|
---|
222 | virtual int consumeBody(const char *a_pvData, size_t a_cbData); ///< ??
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Called when the HTTP request has been completely received.
|
---|
226 | *
|
---|
227 | * @returns IPRT status code?
|
---|
228 | * @param a_rcStatus Negative numbers are IPRT errors, positive are HTTP status codes.
|
---|
229 | * @param a_hHttp The HTTP handle the request was performed on.
|
---|
230 | */
|
---|
231 | virtual int receiveComplete(int a_rcStatus, RTHTTP a_hHttp);
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Getter for m_rcStatus.
|
---|
235 | * @returns Negative numbers are IPRT errors, positive are HTTP status codes.
|
---|
236 | */
|
---|
237 | int getStatus() { return m_rcStatus; }
|
---|
238 |
|
---|
239 | private:
|
---|
240 | /** Negative numbers are IPRT errors, positive are HTTP status codes. */
|
---|
241 | int m_rcStatus;
|
---|
242 |
|
---|
243 | };
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Base class for REST client responses.
|
---|
248 | */
|
---|
249 | class RTCRestClientApiBase
|
---|
250 | {
|
---|
251 | public:
|
---|
252 | RTCRestClientApiBase()
|
---|
253 | : m_hHttp(NIL_RTHTTP)
|
---|
254 | {}
|
---|
255 | virtual ~RTCRestClientApiBase();
|
---|
256 |
|
---|
257 | bool isConnected();
|
---|
258 |
|
---|
259 | protected:
|
---|
260 | /** Handle to the HTTP connection object. */
|
---|
261 | RTHTTP m_hHttp;
|
---|
262 |
|
---|
263 | /* Make non-copyable (RTCNonCopyable causes warnings): */
|
---|
264 | RTCRestClientApiBase(RTCRestOutputToString const &);
|
---|
265 | RTCRestClientApiBase *operator=(RTCRestOutputToString const &);
|
---|
266 | };
|
---|
267 |
|
---|
268 | /** @} */
|
---|
269 |
|
---|
270 | #endif
|
---|
271 |
|
---|