VirtualBox

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

最後變更 在這個檔案從74402是 74396,由 vboxsync 提交於 7 年 前

IPRT/rest: Prepped for polymorphic body responses. bugref:9167

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

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