VirtualBox

source: vbox/trunk/include/iprt/cpp/restclient.h@ 74883

Last change on this file since 74883 was 74428, checked in by vboxsync, 6 years ago

IPRT/rest: doxygen fix. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.8 KB
Line 
1/** @file
2 * IPRT - C++ Representational State Transfer (REST) Client 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_restclient_h
27#define ___iprt_cpp_restclient_h
28
29#include <iprt/http.h>
30#include <iprt/cpp/restbase.h>
31#include <iprt/cpp/reststringmap.h>
32
33
34/** @defgroup grp_rt_cpp_restclient C++ Representational State Transfer (REST) Client Classes.
35 * @ingroup grp_rt_cpp
36 * @{
37 */
38
39/**
40 * Specialization of RTCRestBinary for use with body parameters in a client.
41 *
42 * This enables registering data callbacks for provinding data to upload.
43 */
44class RT_DECL_CLASS RTCRestBinaryParameter : public RTCRestBinary
45{
46public:
47 /** Default constructor. */
48 RTCRestBinaryParameter() RT_NOEXCEPT;
49
50 /** Safe copy assignment method. */
51 virtual int assignCopy(RTCRestBinaryParameter const &a_rThat) RT_NOEXCEPT;
52 /** Safe copy assignment method.
53 * @note Resets callbacks and ASSUMES that @a a_cbData is the content length. */
54 virtual int assignCopy(RTCRestBinary const &a_rThat) RT_NOEXCEPT RT_OVERRIDE;
55 /** Safe copy assignment method.
56 * @note Resets callbacks and ASSUMES that @a a_cbData is the content length. */
57 virtual int assignCopy(void const *a_pvData, size_t a_cbData) RT_NOEXCEPT RT_OVERRIDE;
58
59 /**
60 * Use the specified data buffer directly.
61 * @note Resets callbacks and ASSUMES that @a a_cbData is the content length. */
62 virtual int assignReadOnly(void const *a_pvData, size_t a_cbData) RT_NOEXCEPT RT_OVERRIDE;
63 /**
64 * Use the specified data buffer directly.
65 * @note This will assert and work like assignReadOnly. */
66 virtual int assignWriteable(void *a_pvBuf, size_t a_cbBuf) RT_NOEXCEPT RT_OVERRIDE;
67
68 /** Make a clone of this object. */
69 inline RTCRestBinaryParameter *clone() const RT_NOEXCEPT { return (RTCRestBinaryParameter *)baseClone(); }
70
71 /* Overridden methods: */
72 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
73 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
74 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
75
76 /** Factory method. */
77 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
78
79 /**
80 * Retrieves the callback data.
81 */
82 inline void *getCallbackData() const RT_NOEXCEPT { return m_pvCallbackData; }
83
84 /**
85 * Sets the content-type for an upload.
86 *
87 * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
88 * @param a_pszContentType The content type to set.
89 * If NULL, no content type is set.
90 */
91 int setContentType(const char *a_pszContentType) RT_NOEXCEPT;
92
93 /**
94 * Gets the content type that was set.
95 */
96 inline RTCString const &getContentType() const RT_NOEXCEPT { return m_strContentType; }
97
98 /**
99 * Gets the content-length value (UINT64_MAX if not available).
100 */
101 inline uint64_t getContentLength() const RT_NOEXCEPT { return m_cbContentLength; }
102
103 /**
104 * Callback for producing bytes to upload.
105 *
106 * @returns IPRT status code.
107 * @param a_pThis The related string object.
108 * @param a_pvDst Where to put the bytes.
109 * @param a_cbDst Max number of bytes to produce.
110 * @param a_offContent The byte offset corresponding to the start of @a a_pvDst.
111 * @param a_pcbActual Where to return the number of bytes actually produced.
112 *
113 * @remarks Use getCallbackData to get the user data.
114 *
115 * @note The @a a_offContent parameter does not imply random access or anthing
116 * like that, it is just a convenience provided by the caller. The value
117 * is the sum of the previously returned @a *pcbActual values.
118 */
119 typedef DECLCALLBACK(int) FNPRODUCER(RTCRestBinaryParameter *a_pThis, void *a_pvDst, size_t a_cbDst,
120 uint64_t a_offContent, size_t *a_pcbActual) /*RT_NOEXCEPT*/;
121 /** Pointer to a byte producer callback. */
122 typedef FNPRODUCER *PFNPRODUCER;
123
124 /**
125 * Sets the producer callback.
126 *
127 * @param a_pfnProducer The callback function for producing data.
128 * @param a_pvCallbackData Data the can be retrieved from the callback
129 * using getCallbackData().
130 * @param a_cbContentLength The amount of data that will be uploaded and
131 * to be set as the value of the content-length
132 * header field. Pass UINT64_MAX if not known.
133 *
134 * @note This will drop any buffer previously registered using setUploadData().
135 */
136 void setProducerCallback(PFNPRODUCER a_pfnProducer, void *a_pvCallbackData = NULL, uint64_t a_cbContentLength = UINT64_MAX) RT_NOEXCEPT;
137
138 /**
139 * Preprares transmission via the @a a_hHttp client instance.
140 *
141 * @returns IPRT status code.
142 * @param a_hHttp The HTTP client instance.
143 * @internal
144 */
145 virtual int xmitPrepare(RTHTTP a_hHttp) const RT_NOEXCEPT;
146
147 /**
148 * For completing and/or undoing setup from xmitPrepare.
149 *
150 * @param a_hHttp The HTTP client instance.
151 * @internal
152 */
153 virtual void xmitComplete(RTHTTP a_hHttp) const RT_NOEXCEPT;
154
155protected:
156 /** Number of bytes corresponding to content-length.
157 * UINT64_MAX if not known. Used both for unploads and downloads. */
158 uint64_t m_cbContentLength;
159 /** The content type if set (upload only). */
160 RTCString m_strContentType;
161 /** Pointer to user-registered producer callback function (upload only). */
162 PFNPRODUCER m_pfnProducer;
163 /** User argument for both callbacks (both). */
164 void *m_pvCallbackData;
165
166 /** @copydoc FNRTHTTPUPLOADCALLBACK */
167 static DECLCALLBACK(int) xmitHttpCallback(RTHTTP hHttp, void *pvBuf, size_t cbBuf, uint64_t offContent,
168 size_t *pcbActual, void *pvUser) RT_NOEXCEPT;
169
170private:
171 /* No copy constructor or copy assignment: */
172 RTCRestBinaryParameter(RTCRestBinaryParameter const &a_rThat);
173 RTCRestBinaryParameter &operator=(RTCRestBinaryParameter const &a_rThat);
174};
175
176
177/**
178 * Specialization of RTCRestBinary for use with responses in a client.
179 *
180 * This enables registering data callbacks for consuming downloaded data.
181 */
182class RT_DECL_CLASS RTCRestBinaryResponse : public RTCRestBinary
183{
184public:
185 /** Default constructor. */
186 RTCRestBinaryResponse() RT_NOEXCEPT;
187
188 /** Safe copy assignment method. */
189 virtual int assignCopy(RTCRestBinaryResponse const &a_rThat) RT_NOEXCEPT;
190 /** Safe copy assignment method. */
191 virtual int assignCopy(RTCRestBinary const &a_rThat) RT_NOEXCEPT RT_OVERRIDE;
192 /** Safe copy assignment method.
193 * @note This will assert and fail as it makes no sense for a download. */
194 virtual int assignCopy(void const *a_pvData, size_t a_cbData) RT_NOEXCEPT RT_OVERRIDE;
195
196 /**
197 * Use the specified data buffer directly.
198 * @note This will assert and fail as it makes no sense for a download.
199 */
200 virtual int assignReadOnly(void const *a_pvData, size_t a_cbData) RT_NOEXCEPT RT_OVERRIDE;
201 /**
202 * Use the specified data buffer directly.
203 * @note This will drop any previously registered producer callback and user data.
204 */
205 virtual int assignWriteable(void *a_pvBuf, size_t a_cbBuf) RT_NOEXCEPT RT_OVERRIDE;
206
207 /** Make a clone of this object. */
208 inline RTCRestBinaryResponse *clone() const RT_NOEXCEPT { return (RTCRestBinaryResponse *)baseClone(); }
209
210 /* Overridden methods: */
211 virtual RTCRestObjectBase *baseClone() const RT_NOEXCEPT RT_OVERRIDE;
212 virtual int resetToDefault() RT_NOEXCEPT RT_OVERRIDE;
213 virtual const char *typeName(void) const RT_NOEXCEPT RT_OVERRIDE;
214
215 /** Factory method. */
216 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void) RT_NOEXCEPT;
217
218 /**
219 * Retrieves the callback data.
220 */
221 inline void *getCallbackData() const RT_NOEXCEPT { return m_pvCallbackData; }
222
223 /**
224 * Sets the max size to download to memory.
225 *
226 * This also indicates the intention to download to a memory buffer, so it
227 * will drop any previously registered consumer callback and its user data.
228 *
229 * @param a_cbMaxDownload Maximum number of bytes to download to memory.
230 * If 0, a default is selected (currently 32MiB for
231 * 32-bit hosts and 128MiB for 64-bit).
232 */
233 void setMaxDownloadSize(size_t a_cbMaxDownload) RT_NOEXCEPT;
234
235 /**
236 * Gets the content-length value (UINT64_MAX if not available).
237 */
238 inline uint64_t getContentLength() const RT_NOEXCEPT { return m_cbContentLength; }
239
240 /**
241 * Callback for consuming downloaded bytes.
242 *
243 * @returns IPRT status code.
244 * @param a_pThis The related string object.
245 * @param a_pvSrc Buffer containing the bytes.
246 * @param a_cbSrc The number of bytes in the buffer.
247 * @param a_uHttpStatus The HTTP status code.
248 * @param a_offContent The byte offset corresponding to the start of @a a_pvSrc.
249 * @param a_cbContent The content length field value, UINT64_MAX if not available.
250 *
251 * @remarks Use getCallbackData to get the user data.
252 *
253 * @note The @a a_offContent parameter does not imply random access or anthing
254 * like that, it is just a convenience provided by the caller. The value
255 * is the sum of the previous @a a_cbSrc values.
256 */
257 typedef DECLCALLBACK(int) FNCONSUMER(RTCRestBinaryResponse *a_pThis, const void *a_pvSrc, size_t a_cbSrc,
258 uint32_t a_uHttpStatus, uint64_t a_offContent, uint64_t a_cbContent) /*RT_NOEXCEPT*/;
259 /** Pointer to a byte consumer callback. */
260 typedef FNCONSUMER *PFNCONSUMER;
261
262 /**
263 * Sets the consumer callback.
264 *
265 * @param a_pfnConsumer The callback function for consuming downloaded data.
266 * NULL if data should be stored in m_pbData (the default).
267 * @param a_pvCallbackData Data the can be retrieved from the callback
268 * using getCallbackData().
269 */
270 void setConsumerCallback(PFNCONSUMER a_pfnConsumer, void *a_pvCallbackData = NULL) RT_NOEXCEPT;
271
272 /**
273 * Preprares for receiving via the @a a_hHttp client instance.
274 *
275 * @returns IPRT status code.
276 * @param a_hHttp The HTTP client instance.
277 * @param a_fCallbackFlags The HTTP callback flags (status code spec).
278 * @internal
279 */
280 virtual int receivePrepare(RTHTTP a_hHttp, uint32_t a_fCallbackFlags) RT_NOEXCEPT;
281
282 /**
283 * For completing and/or undoing setup from receivePrepare.
284 *
285 * @param a_hHttp The HTTP client instance.
286 * @internal
287 */
288 virtual void receiveComplete(RTHTTP a_hHttp) RT_NOEXCEPT;
289
290protected:
291 /** Number of bytes corresponding to content-length.
292 * UINT64_MAX if not known. Used both for unploads and downloads. */
293 uint64_t m_cbContentLength;
294 /** Number of bytes downloaded thus far. */
295 uint64_t m_cbDownloaded;
296 /** Pointer to user-registered consumer callback function (download only). */
297 PFNCONSUMER m_pfnConsumer;
298 /** User argument for both callbacks (both). */
299 void *m_pvCallbackData;
300 /** Maximum data to download to memory (download only). */
301 size_t m_cbMaxDownload;
302
303 /** @copydoc FNRTHTTPDOWNLOADCALLBACK */
304 static DECLCALLBACK(int) receiveHttpCallback(RTHTTP hHttp, void const *pvBuf, size_t cbBuf, uint32_t uHttpStatus,
305 uint64_t offContent, uint64_t cbContent, void *pvUser) RT_NOEXCEPT;
306
307private:
308 /* No copy constructor or copy assignment: */
309 RTCRestBinaryResponse(RTCRestBinaryResponse const &a_rThat);
310 RTCRestBinaryResponse &operator=(RTCRestBinaryResponse const &a_rThat);
311};
312
313
314/**
315 * Base class for REST client requests.
316 *
317 * This encapsulates parameters and helps transform them into a HTTP request.
318 *
319 * Parameters can be transfered in a number of places:
320 * - Path part of the URL.
321 * - Query part of the URL.
322 * - HTTP header fields.
323 * - FORM body.
324 * - JSON body.
325 * - XML body.
326 * - ...
327 *
328 * They can be require or optional. The latter may have default values. In
329 * swagger 3 they can also be nullable, which means the null-indicator cannot
330 * be used for tracking optional parameters.
331 */
332class RT_DECL_CLASS RTCRestClientRequestBase
333{
334public:
335 RTCRestClientRequestBase() RT_NOEXCEPT;
336 virtual ~RTCRestClientRequestBase();
337 RTCRestClientRequestBase(RTCRestClientRequestBase const &a_rThat) RT_NOEXCEPT;
338 RTCRestClientRequestBase &operator=(RTCRestClientRequestBase const &a_rThat) RT_NOEXCEPT;
339
340 /**
341 * Reset all members to default values.
342 * @returns IPRT status code.
343 */
344 virtual int resetToDefault() RT_NOEXCEPT = 0;
345
346 /**
347 * Prepares the HTTP handle for transmitting this request.
348 *
349 * @returns IPRT status code.
350 * @param a_pStrPath Where to set path parameters. Will be appended to the base path.
351 * @param a_pStrQuery Where to set query parameters.
352 * @param a_hHttp Where to set header parameters and such.
353 * @param a_pStrBody Where to set body parameters.
354 */
355 virtual int xmitPrepare(RTCString *a_pStrPath, RTCString *a_pStrQuery, RTHTTP a_hHttp, RTCString *a_pStrBody) const RT_NOEXCEPT = 0;
356
357 /**
358 * Always called after the request has been transmitted.
359 *
360 * @param a_rcStatus Negative numbers are IPRT errors, positive are HTTP status codes.
361 * @param a_hHttp The HTTP handle the request was performed on.
362 */
363 virtual void xmitComplete(int a_rcStatus, RTHTTP a_hHttp) const RT_NOEXCEPT = 0;
364
365 /**
366 * Checks if there are were any assignment errors.
367 */
368 inline bool hasAssignmentErrors() const RT_NOEXCEPT { return m_fErrorSet != 0; }
369
370protected:
371 /** Set of fields that have been explicitly assigned a value. */
372 uint64_t m_fIsSet;
373 /** Set of fields where value assigning failed. */
374 uint64_t m_fErrorSet;
375
376 /** Path parameter descriptor. */
377 typedef struct
378 {
379 const char *pszName; /**< The name string to replace (including {}). */
380 size_t cchName; /**< Length of pszName. */
381 uint32_t fFlags; /**< The toString flags. */
382 uint8_t iBitNo; /**< The parameter bit number. */
383 } PATHPARAMDESC;
384
385 /** Path parameter state. */
386 typedef struct
387 {
388 RTCRestObjectBase const *pObj; /**< Pointer to the parameter object. */
389 size_t offName; /**< Maintained by worker. */
390 } PATHPARAMSTATE;
391
392 /**
393 * Do path parameters.
394 *
395 * @returns IPRT status code
396 * @param a_pStrPath The destination path.
397 * @param a_pszPathTemplate The path template string.
398 * @param a_cchPathTemplate The length of the path template string.
399 * @param a_paPathParams The path parameter descriptors (static).
400 * @param a_paPathParamStates The path parameter objects and states.
401 * @param a_cPathParams Number of path parameters.
402 */
403 int doPathParameters(RTCString *a_pStrPath, const char *a_pszPathTemplate, size_t a_cchPathTemplate,
404 PATHPARAMDESC const *a_paPathParams, PATHPARAMSTATE *a_paPathParamStates, size_t a_cPathParams) const RT_NOEXCEPT;
405
406 /** Query parameter descriptor. */
407 typedef struct
408 {
409 const char *pszName; /**< The parameter name. */
410 uint32_t fFlags; /**< The toString flags. */
411 bool fRequired; /**< Required or not. */
412 uint8_t iBitNo; /**< The parameter bit number. */
413 } QUERYPARAMDESC;
414
415 /**
416 * Do query parameters.
417 *
418 * @returns IPRT status code
419 * @param a_pStrQuery The destination string.
420 * @param a_paQueryParams The query parameter descriptors.
421 * @param a_papQueryParamObjs The query parameter objects, parallel to @a a_paQueryParams.
422 * @param a_cQueryParams Number of query parameters.
423 */
424 int doQueryParameters(RTCString *a_pStrQuery, QUERYPARAMDESC const *a_paQueryParams,
425 RTCRestObjectBase const **a_papQueryParamObjs, size_t a_cQueryParams) const RT_NOEXCEPT;
426
427 /** Header parameter descriptor. */
428 typedef struct
429 {
430 const char *pszName; /**< The parameter name. */
431 uint32_t fFlags; /**< The toString flags. */
432 bool fRequired; /**< Required or not. */
433 uint8_t iBitNo; /**< The parameter bit number. */
434 bool fMapCollection; /**< Collect headers starting with pszName into a map. */
435 } HEADERPARAMDESC;
436
437 /**
438 * Do header parameters.
439 *
440 * @returns IPRT status code
441 * @param a_hHttp Where to set header parameters.
442 * @param a_paHeaderParams The header parameter descriptors.
443 * @param a_papHeaderParamObjs The header parameter objects, parallel to @a a_paHeaderParams.
444 * @param a_cHeaderParams Number of header parameters.
445 */
446 int doHeaderParameters(RTHTTP a_hHttp, HEADERPARAMDESC const *a_paHeaderParams,
447 RTCRestObjectBase const **a_papHeaderParamObjs, size_t a_cHeaderParams) const RT_NOEXCEPT;
448};
449
450
451/**
452 * Base class for REST client responses.
453 */
454class RT_DECL_CLASS RTCRestClientResponseBase
455{
456public:
457 /** Default constructor. */
458 RTCRestClientResponseBase() RT_NOEXCEPT;
459 /** Destructor. */
460 virtual ~RTCRestClientResponseBase();
461 /** Copy constructor. */
462 RTCRestClientResponseBase(RTCRestClientResponseBase const &a_rThat);
463 /** Copy assignment operator. */
464 RTCRestClientResponseBase &operator=(RTCRestClientResponseBase const &a_rThat);
465
466 /**
467 * Resets the object state.
468 */
469 virtual void reset(void) RT_NOEXCEPT;
470
471 /**
472 * Prepares the HTTP handle for receiving the response.
473 *
474 * This may install callbacks and such like.
475 *
476 * When overridden, the parent class must always be called.
477 *
478 * @returns IPRT status code.
479 * @param a_hHttp The HTTP handle to prepare for receiving.
480 */
481 virtual int receivePrepare(RTHTTP a_hHttp) RT_NOEXCEPT;
482
483 /**
484 * Called when the HTTP request has been completely received.
485 *
486 * @param a_rcStatus Negative numbers are IPRT errors, positive are HTTP status codes.
487 * @param a_hHttp The HTTP handle the request was performed on.
488 * This can be NIL_RTHTTP should something fail early, in
489 * which case it is possible receivePrepare() wasn't called.
490 *
491 * @note Called before consumeBody() but after consumeHeader().
492 */
493 virtual void receiveComplete(int a_rcStatus, RTHTTP a_hHttp) RT_NOEXCEPT;
494
495 /**
496 * Callback that consumes HTTP body data from the server.
497 *
498 * @param a_pchData Body data.
499 * @param a_cbData Amount of body data.
500 *
501 * @note Called after consumeHeader().
502 */
503 virtual void consumeBody(const char *a_pchData, size_t a_cbData) RT_NOEXCEPT;
504
505 /**
506 * Called after status, headers and body all have been presented.
507 *
508 * @returns IPRT status code.
509 */
510 virtual void receiveFinal() RT_NOEXCEPT;
511
512 /**
513 * Getter for m_rcStatus.
514 * @returns Negative numbers are IPRT errors, positive are HTTP status codes.
515 */
516 inline int getStatus() const RT_NOEXCEPT { return m_rcStatus; }
517
518 /**
519 * Getter for m_rcHttp.
520 * @returns HTTP status code or VERR_NOT_AVAILABLE.
521 */
522 inline int getHttpStatus() const RT_NOEXCEPT { return m_rcHttp; }
523
524 /**
525 * Getter for m_pErrInfo.
526 */
527 inline PCRTERRINFO getErrInfo(void) const RT_NOEXCEPT { return m_pErrInfo; }
528
529 /**
530 * Getter for m_strContentType.
531 */
532 inline RTCString const &getContentType(void) const RT_NOEXCEPT { return m_strContentType; }
533
534
535protected:
536 /** Negative numbers are IPRT errors, positive are HTTP status codes. */
537 int m_rcStatus;
538 /** The HTTP status code, VERR_NOT_AVAILABLE if not set. */
539 int m_rcHttp;
540 /** Error information. */
541 PRTERRINFO m_pErrInfo;
542 /** The value of the Content-Type header field. */
543 RTCString m_strContentType;
544
545 PRTERRINFO getErrInfoInternal(void) RT_NOEXCEPT;
546 void deleteErrInfo(void) RT_NOEXCEPT;
547 void copyErrInfo(PCRTERRINFO pErrInfo) RT_NOEXCEPT;
548
549 /**
550 * Reports an error (or warning if a_rc non-negative).
551 *
552 * @returns a_rc
553 * @param a_rc The status code to report and return. The first
554 * error status is assigned to m_rcStatus, subsequent
555 * ones as well as informational statuses are not
556 * recorded by m_rcStatus.
557 * @param a_pszFormat The message format string.
558 * @param ... Message arguments.
559 */
560 int addError(int a_rc, const char *a_pszFormat, ...) RT_NOEXCEPT;
561
562 /**
563 * Deserializes a header field value.
564 *
565 * @returns IPRT status code.
566 * @param a_pObj The object to deserialize into.
567 * @param a_pchValue Pointer to the value (not zero terminated).
568 * Not necessarily valid UTF-8!
569 * @param a_cchValue The value length.
570 * @param a_fFlags Flags to pass to fromString().
571 * @param a_pszErrorTag The error tag (field name).
572 */
573 int deserializeHeader(RTCRestObjectBase *a_pObj, const char *a_pchValue, size_t a_cchValue,
574 uint32_t a_fFlags, const char *a_pszErrorTag) RT_NOEXCEPT;
575
576 /**
577 * Deserializes a header field value.
578 *
579 * @returns IPRT status code.
580 * @param a_pMap The string map object to deserialize into.
581 * @param a_pchField Pointer to the map field name. (Caller dropped the prefix.)
582 * Not necessarily valid UTF-8!
583 * @param a_cchField Length of field name.
584 * @param a_pchValue Pointer to the value (not zero terminated).
585 * Not necessarily valid UTF-8!
586 * @param a_cchValue The value length.
587 * @param a_fFlags Flags to pass to fromString().
588 * @param a_pszErrorTag The error tag (field name).
589 */
590 int deserializeHeaderIntoMap(RTCRestStringMapBase *a_pMap, const char *a_pchField, size_t a_cchField,
591 const char *a_pchValue, size_t a_cchValue, uint32_t a_fFlags, const char *a_pszErrorTag) RT_NOEXCEPT;
592
593 /**
594 * Helper that does the deserializing of the response body
595 * via deserializeBodyFromJsonCursor().
596 *
597 * @param a_pchData The body blob.
598 * @param a_cbData The size of the body blob.
599 * @param a_pszBodyName The name of the body parameter.
600 */
601 void deserializeBody(const char *a_pchData, size_t a_cbData, const char *a_pszBodyName) RT_NOEXCEPT;
602
603 /**
604 * Called by deserializeBody to do the actual body deserialization.
605 *
606 * @param a_rCursor The JSON cursor.
607 */
608 virtual void deserializeBodyFromJsonCursor(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT;
609
610 /**
611 * Primary json cursor for parsing bodies.
612 */
613 class PrimaryJsonCursorForBody : public RTCRestJsonPrimaryCursor
614 {
615 public:
616 RTCRestClientResponseBase *m_pThat; /**< Pointer to response object. */
617 PrimaryJsonCursorForBody(RTJSONVAL hValue, const char *pszName, RTCRestClientResponseBase *a_pThat) RT_NOEXCEPT;
618 virtual int addError(RTCRestJsonCursor const &a_rCursor, int a_rc, const char *a_pszFormat, ...) RT_NOEXCEPT RT_OVERRIDE;
619 virtual int unknownField(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT RT_OVERRIDE;
620 };
621
622
623 /**
624 * Consumes a header.
625 *
626 * Child classes can override this to pick up their header fields, but must
627 * always call the parent class.
628 *
629 * @returns IPRT status code.
630 * @param a_uMatchWord Match word constructed by RTHTTP_MAKE_HDR_MATCH_WORD
631 * @param a_pchField The field name (not zero terminated).
632 * Not necessarily valid UTF-8!
633 * @param a_cchField The length of the field.
634 * @param a_pchValue The field value (not zero terminated).
635 * @param a_cchValue The length of the value.
636 */
637 virtual int consumeHeader(uint32_t a_uMatchWord, const char *a_pchField, size_t a_cchField,
638 const char *a_pchValue, size_t a_cchValue) RT_NOEXCEPT;
639
640private:
641 /** Callback for use with RTHttpSetHeaderCallback. */
642 static DECLCALLBACK(int) receiveHttpHeaderCallback(RTHTTP hHttp, uint32_t uMatchWord, const char *pchField, size_t cchField,
643 const char *pchValue, size_t cchValue, void *pvUser) RT_NOEXCEPT;
644};
645
646
647/**
648 * Base class for REST client responses.
649 */
650class RT_DECL_CLASS RTCRestClientApiBase
651{
652public:
653 RTCRestClientApiBase() RT_NOEXCEPT;
654 virtual ~RTCRestClientApiBase();
655
656 /** @name Host and Base path (URL) handling.
657 * @{ */
658 /**
659 * Gets the server URL.
660 */
661 const char *getServerUrl(void) const RT_NOEXCEPT;
662
663 /**
664 * Sets the whole server URL.
665 * @returns IPRT status code.
666 * @param a_pszUrl The new server URL. NULL/empty to reset to default.
667 */
668 int setServerUrl(const char *a_pszUrl) RT_NOEXCEPT;
669
670 /**
671 * Sets the scheme part of the the server URL.
672 * @returns IPRT status code.
673 * @param a_pszScheme The new scheme. Does not accept NULL or empty string.
674 */
675 int setServerScheme(const char *a_pszScheme) RT_NOEXCEPT;
676
677 /**
678 * Sets the authority (hostname + port) part of the the server URL.
679 * @returns IPRT status code.
680 * @param a_pszAuthority The new authority. Does not accept NULL or empty string.
681 */
682 int setServerAuthority(const char *a_pszAuthority) RT_NOEXCEPT;
683
684 /**
685 * Sets the base path part of the the server URL.
686 * @returns IPRT status code.
687 * @param a_pszBasePath The new base path. Does not accept NULL or empty string.
688 */
689 int setServerBasePath(const char *a_pszBasePath) RT_NOEXCEPT;
690
691 /**
692 * Gets the default server URL as specified in the specs.
693 * @returns Server URL.
694 */
695 virtual const char *getDefaultServerUrl() const RT_NOEXCEPT = 0;
696
697 /**
698 * Gets the default server base path as specified in the specs.
699 * @returns Host string (start of URL).
700 */
701 virtual const char *getDefaultServerBasePath() const RT_NOEXCEPT = 0;
702 /** @} */
703
704 /** Flags to doCall. */
705 enum
706 {
707 kDoCall_OciReqSignExcludeBody = 1 /**< Exclude the body when doing OCI request signing. */
708 };
709
710protected:
711 /** Handle to the HTTP connection object. */
712 RTHTTP m_hHttp;
713 /** The server URL to use. If empty use the default. */
714 RTCString m_strServerUrl;
715
716 /* Make non-copyable (RTCNonCopyable causes warnings): */
717 RTCRestClientApiBase(RTCRestClientApiBase const &);
718 RTCRestClientApiBase *operator=(RTCRestClientApiBase const &);
719
720 /**
721 * Re-initializes the HTTP instance.
722 *
723 * @returns IPRT status code.
724 */
725 virtual int reinitHttpInstance() RT_NOEXCEPT;
726
727 /**
728 * Hook that's called when doCall has fully assembled the request.
729 *
730 * Can be used for request signing and similar final steps.
731 *
732 * @returns IPRT status code.
733 * @param a_hHttp The HTTP client instance.
734 * @param a_rStrFullUrl The full URL.
735 * @param a_enmHttpMethod The HTTP request method.
736 * @param a_rStrXmitBody The body text.
737 * @param a_fFlags kDoCall_XXX.
738 */
739 virtual int xmitReady(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
740 RTCString const &a_rStrXmitBody, uint32_t a_fFlags) RT_NOEXCEPT;
741
742 /**
743 * Implements stuff for making an API call.
744 *
745 * @returns a_pResponse->getStatus()
746 * @param a_rRequest Reference to the request object.
747 * @param a_enmHttpMethod The HTTP request method.
748 * @param a_pResponse Pointer to the response object.
749 * @param a_pszMethod The method name, for logging purposes.
750 * @param a_fFlags kDoCall_XXX.
751 */
752 virtual int doCall(RTCRestClientRequestBase const &a_rRequest, RTHTTPMETHOD a_enmHttpMethod,
753 RTCRestClientResponseBase *a_pResponse, const char *a_pszMethod, uint32_t a_fFlags) RT_NOEXCEPT;
754
755 /**
756 * Implements OCI style request signing.
757 *
758 * @returns IPRT status code.
759 * @param a_hHttp The HTTP client instance.
760 * @param a_rStrFullUrl The full URL.
761 * @param a_enmHttpMethod The HTTP request method.
762 * @param a_rStrXmitBody The body text.
763 * @param a_fFlags kDoCall_XXX.
764 * @param a_hKey The key to use for signing.
765 * @param a_rStrKeyId The key ID.
766 *
767 * @remarks The signing scheme is covered by a series of drafts RFC, the latest being:
768 * https://tools.ietf.org/html/draft-cavage-http-signatures-10
769 */
770 int ociSignRequest(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
771 RTCString const &a_rStrXmitBody, uint32_t a_fFlags, RTCRKEY a_hKey, RTCString const &a_rStrKeyId) RT_NOEXCEPT;
772
773 /**
774 * Worker for the server URL modifiers.
775 *
776 * @returns IPRT status code.
777 * @param a_pszServerUrl The current server URL (for comparing).
778 * @param a_offDst The offset of the component in the current server URL.
779 * @param a_cchDst The current component length.
780 * @param a_pszSrc The new URL component value.
781 * @param a_cchSrc The length of the new component.
782 */
783 int setServerUrlPart(const char *a_pszServerUrl, size_t a_offDst, size_t a_cchDst, const char *a_pszSrc, size_t a_cchSrc) RT_NOEXCEPT;
784};
785
786/** @} */
787
788#endif
789
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette