VirtualBox

Changeset 74142 in vbox for trunk


Ignore:
Timestamp:
Sep 7, 2018 1:29:43 PM (6 years ago)
Author:
vboxsync
Message:

IPRT/rest: Split up RTCRestBinaryString into a core RTCRestBinary type and two client side descenants for flexible handling of parameters and responses (still not tested). bugref:9167

Location:
trunk
Files:
1 added
2 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/restbase.h

    r74117 r74142  
    742742};
    743743
     744
     745/**
     746 * Class for handling binary blobs (strings).
     747 *
     748 * There are specializations of this class for body parameters and responses,
     749 * see RTCRestBinaryParameter and RTCRestBinaryResponse.
     750 */
     751class RT_DECL_CLASS RTCRestBinary : public RTCRestObjectBase
     752{
     753public:
     754    /** Default constructor. */
     755    RTCRestBinary();
     756    /** Destructor. */
     757    virtual ~RTCRestBinary();
     758
     759    /** Safe copy assignment method. */
     760    virtual int assignCopy(RTCRestBinary const &a_rThat);
     761    /** Safe buffer copy method. */
     762    virtual int assignCopy(void const *a_pvData, size_t a_cbData);
     763
     764    /** Use the specified data buffer directly. */
     765    virtual int assignReadOnly(void const *a_pvData, size_t a_cbData);
     766    /** Use the specified data buffer directly. */
     767    virtual int assignWriteable(void *a_pvBuf, size_t a_cbBuf);
     768    /** Frees the data held by the object and resets it default state. */
     769    virtual void freeData();
     770
     771    /** Returns a pointer to the data blob. */
     772    const uint8_t  *getPtr()  const { return m_pbData; }
     773    /** Gets the size of the data. */
     774    size_t          getSize() const { return m_cbData; }
     775
     776    /* Overridden methods: */
     777    virtual int setNull(void) RT_OVERRIDE;
     778    virtual int resetToDefault(void) RT_OVERRIDE;
     779    virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
     780    virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
     781    virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_OVERRIDE;
     782    virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
     783                           uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
     784    virtual kTypeClass typeClass(void) const RT_OVERRIDE;
     785    virtual const char *typeName(void) const RT_OVERRIDE;
     786
     787    /** Factory method. */
     788    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     789
     790protected:
     791    /** Pointer to data blob. */
     792    uint8_t    *m_pbData;
     793    /** Amount of valid data in the blob. */
     794    size_t      m_cbData;
     795    /** Number of bytes allocated for the m_pbData buffer. */
     796    size_t      m_cbAllocated;
     797    /** Set if the data is freeable, only ever clear if user data. */
     798    bool        m_fFreeable;
     799    /** Set if the data blob is readonly user provided data. */
     800    bool        m_fReadOnly;
     801
     802private:
     803    /* No copy constructor or copy assignment: */
     804    RTCRestBinary(RTCRestBinary const &a_rThat);
     805    RTCRestBinary &operator=(RTCRestBinary const &a_rThat);
     806};
     807
     808
    744809/** @} */
    745810
  • trunk/include/iprt/cpp/restclient.h

    r74052 r74142  
    3535 * @{
    3636 */
     37
     38/**
     39 * Specialization of RTCRestBinary for use with body parameters in a client.
     40 *
     41 * This enables registering data callbacks for provinding data to upload.
     42 */
     43class RT_DECL_CLASS RTCRestBinaryParameter : public RTCRestBinary
     44{
     45public:
     46    /** Default constructor. */
     47    RTCRestBinaryParameter();
     48
     49    /** Safe copy assignment method. */
     50    virtual int assignCopy(RTCRestBinaryParameter const &a_rThat);
     51    /** Safe copy assignment method.
     52      * @note Resets callbacks and ASSUMES that @a a_cbData is the content length. */
     53    virtual int assignCopy(RTCRestBinary const &a_rThat) RT_OVERRIDE;
     54    /** Safe copy assignment method.
     55     * @note Resets callbacks and ASSUMES that @a a_cbData is the content length. */
     56    virtual int assignCopy(void const *a_pvData, size_t a_cbData) RT_OVERRIDE;
     57
     58    /**
     59     * Use the specified data buffer directly.
     60     * @note Resets callbacks and ASSUMES that @a a_cbData is the content length. */
     61    virtual int assignReadOnly(void const *a_pvData, size_t a_cbData) RT_OVERRIDE;
     62    /**
     63     * Use the specified data buffer directly.
     64     * @note This will assert and work like assignReadOnly. */
     65    virtual int assignWriteable(void *a_pvBuf, size_t a_cbBuf) RT_OVERRIDE;
     66
     67    /* Overridden methods: */
     68    virtual int resetToDefault() RT_OVERRIDE;
     69    virtual const char *typeName(void) const RT_OVERRIDE;
     70
     71    /** Factory method. */
     72    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     73
     74    /**
     75     * Retrieves the callback data.
     76     */
     77    void *getCallbackData() const  { return m_pvCallbackData; }
     78
     79    /**
     80     * Sets the content-type for an upload.
     81     *
     82     * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
     83     * @param   a_pszContentType    The content type to set.
     84     *                              If NULL, no content type is set.
     85     */
     86    int setContentType(const char *a_pszContentType);
     87
     88    /**
     89     * Gets the content type that was set.
     90     */
     91    RTCString const &getContentType() const { return m_strContentType; }
     92
     93    /**
     94     * Callback for producing bytes to upload.
     95     *
     96     * @returns IPRT status code.
     97     * @param   a_pThis         The related string object.
     98     * @param   a_pvDst         Where to put the bytes.
     99     * @param   a_cbDst         Max number of bytes to produce.
     100     * @param   a_offContent    The byte offset corresponding to the start of @a a_pvDst.
     101     * @param   a_pcbActual     Where to return the number of bytes actually produced.
     102     * @remarks Use getCallbackData to get the user data.
     103     */
     104    typedef DECLCALLBACK(int) FNPRODUCER(RTCRestBinaryParameter *a_pThis, void *a_pvDst, size_t a_cbDst,
     105                                         uint64_t a_offContent, size_t *a_pcbActual);
     106    /** Pointer to a byte producer callback. */
     107    typedef FNPRODUCER *PFNPRODUCER;
     108
     109    /**
     110     * Sets the producer callback.
     111     *
     112     * @param   a_pfnProducer       The callback function for producing data.
     113     * @param   a_pvCallbackData    Data the can be retrieved from the callback
     114     *                              using getCallbackData().
     115     * @param   a_cbContentLength   The amount of data that will be uploaded and
     116     *                              to be set as the value of the content-length
     117     *                              header field.  Pass UINT64_MAX if not known.
     118     *
     119     * @note    This will drop any buffer previously registered using setUploadData().
     120     */
     121    void setProducerCallback(PFNPRODUCER a_pfnProducer, void *a_pvCallbackData = NULL, uint64_t a_cbContentLength = UINT64_MAX);
     122
     123    /**
     124     * Preprares transmission via the @a a_hHttp client instance.
     125     *
     126     * @returns IPRT status code.
     127     * @param   a_hHttp             The HTTP client instance.
     128     * @internal
     129     */
     130    virtual int xmitPrepare(RTHTTP a_hHttp) const;
     131
     132    /**
     133     * For completing and/or undoing setup from xmitPrepare.
     134     *
     135     * @param   a_hHttp             The HTTP client instance.
     136     * @internal
     137     */
     138    virtual void xmitComplete(RTHTTP a_hHttp) const;
     139
     140protected:
     141    /** Number of bytes corresponding to content-length.
     142     * UINT64_MAX if not known.  Used both for unploads and downloads. */
     143    uint64_t    m_cbContentLength;
     144    /** The content type if set (upload only). */
     145    RTCString   m_strContentType;
     146    /** Pointer to user-registered producer callback function (upload only). */
     147    PFNPRODUCER m_pfnProducer;
     148    /** User argument for both callbacks (both). */
     149    void       *m_pvCallbackData;
     150
     151    /** Callback for use with RTHttpSetUploadCallback. */
     152    static FNRTHTTPUPLOADCALLBACK   xmitHttpCallback;
     153
     154private:
     155    /* No copy constructor or copy assignment: */
     156    RTCRestBinaryParameter(RTCRestBinaryParameter const &a_rThat);
     157    RTCRestBinaryParameter &operator=(RTCRestBinaryParameter const &a_rThat);
     158};
     159
     160
     161/**
     162 * Specialization of RTCRestBinary for use with responses in a client.
     163 *
     164 * This enables registering data callbacks for consuming downloaded data.
     165 */
     166class RT_DECL_CLASS RTCRestBinaryResponse : public RTCRestBinary
     167{
     168public:
     169    /** Default constructor. */
     170    RTCRestBinaryResponse();
     171
     172    /** Safe copy assignment method. */
     173    virtual int assignCopy(RTCRestBinaryResponse const &a_rThat);
     174    /** Safe copy assignment method. */
     175    virtual int assignCopy(RTCRestBinary const &a_rThat) RT_OVERRIDE;
     176    /** Safe copy assignment method.
     177     * @note This will assert and fail as it makes no sense for a download.  */
     178    virtual int assignCopy(void const *a_pvData, size_t a_cbData) RT_OVERRIDE;
     179
     180    /**
     181     * Use the specified data buffer directly.
     182     * @note This will assert and fail as it makes no sense for a download.
     183     */
     184    virtual int assignReadOnly(void const *a_pvData, size_t a_cbData) RT_OVERRIDE;
     185    /**
     186     * Use the specified data buffer directly.
     187     * @note This will drop any previously registered producer callback and user data.
     188     */
     189    virtual int assignWriteable(void *a_pvBuf, size_t a_cbBuf) RT_OVERRIDE;
     190
     191    /* Overridden methods: */
     192    virtual int resetToDefault() RT_OVERRIDE;
     193    virtual const char *typeName(void) const RT_OVERRIDE;
     194
     195    /** Factory method. */
     196    static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
     197
     198    /**
     199     * Retrieves the callback data.
     200     */
     201    void *getCallbackData() const  { return m_pvCallbackData; }
     202
     203    /**
     204     * Sets the max size to download to memory.
     205     *
     206     * This also indicates the intention to download to a memory buffer, so it
     207     * will drop any previously registered consumer callback and its user data.
     208     *
     209     * @param   a_cbMaxDownload Maximum number of bytes to download to memory.
     210     *                          If 0, a default is selected (currently 32MiB for
     211     *                          32-bit hosts and 128MiB for 64-bit).
     212     */
     213    void setMaxDownloadSize(size_t a_cbMaxDownload);
     214
     215    /**
     216     * Gets the content-length value (UINT64_MAX if not available).
     217     */
     218    uint64_t getContentLength() const { return m_cbContentLength; }
     219
     220    /**
     221     * Callback for consuming downloaded bytes.
     222     *
     223     * @returns IPRT status code.
     224     * @param   a_pThis         The related string object.
     225     * @param   a_pvSrc         Buffer containing the bytes.
     226     * @param   a_cbSrc         The number of bytes in the buffer.
     227     * @param   a_uHttpStatus   The HTTP status code.
     228     * @param   a_offContent    The byte offset corresponding to the start of @a a_pvSrc.
     229     * @param   a_cbContent     The content length field value, UINT64_MAX if not available.
     230     * @remarks Use getCallbackData to get the user data.
     231     */
     232    typedef DECLCALLBACK(int) FNCONSUMER(RTCRestBinaryResponse *a_pThis, const void *a_pvSrc, size_t a_cbSrc,
     233                                         uint32_t a_uHttpStatus, uint64_t a_offContent, uint64_t a_cbContent);
     234    /** Pointer to a byte consumer callback. */
     235    typedef FNCONSUMER *PFNCONSUMER;
     236
     237    /**
     238     * Sets the consumer callback.
     239     *
     240     * @param   a_pfnConsumer       The callback function for consuming downloaded data.
     241     *                              NULL if data should be stored in m_pbData (the default).
     242     * @param   a_pvCallbackData    Data the can be retrieved from the callback
     243     *                              using getCallbackData().
     244     */
     245    void setConsumerCallback(PFNCONSUMER a_pfnConsumer, void *a_pvCallbackData = NULL);
     246
     247    /**
     248     * Preprares for receiving via the @a a_hHttp client instance.
     249     *
     250     * @returns IPRT status code.
     251     * @param   a_hHttp             The HTTP client instance.
     252     * @param   a_fCallbackFlags    The HTTP callback flags (status code spec).
     253     * @internal
     254     */
     255    virtual int receivePrepare(RTHTTP a_hHttp, uint32_t a_fCallbackFlags);
     256
     257    /**
     258     * For completing and/or undoing setup from receivePrepare.
     259     *
     260     * @param   a_hHttp             The HTTP client instance.
     261     * @internal
     262     */
     263    virtual void receiveComplete(RTHTTP a_hHttp);
     264
     265protected:
     266    /** Number of bytes corresponding to content-length.
     267     * UINT64_MAX if not known.  Used both for unploads and downloads. */
     268    uint64_t    m_cbContentLength;
     269    /** Number of bytes downloaded thus far. */
     270    uint64_t    m_cbDownloaded;
     271    /** Pointer to user-registered consumer callback function (download only). */
     272    PFNCONSUMER m_pfnConsumer;
     273    /** User argument for both callbacks (both). */
     274    void       *m_pvCallbackData;
     275    /** Maximum data to download to memory (download only). */
     276    size_t      m_cbMaxDownload;
     277
     278    /** Callback for use with RTHttpSetDownloadCallback. */
     279    static FNRTHTTPDOWNLOADCALLBACK receiveHttpCallback;
     280
     281private:
     282    /* No copy constructor or copy assignment: */
     283    RTCRestBinaryResponse(RTCRestBinaryResponse const &a_rThat);
     284    RTCRestBinaryResponse &operator=(RTCRestBinaryResponse const &a_rThat);
     285};
     286
    37287
    38288/**
  • trunk/src/VBox/Runtime/Makefile.kmk

    r74117 r74142  
    16531653        generic/http-curl.cpp \
    16541654        common/rest/rest-primary-object-types.cpp \
     1655        common/rest/rest-binary.cpp \
    16551656        common/rest/RTCRestAnyObject.cpp \
    16561657        common/rest/RTCRestArrayBase.cpp \
    1657         common/rest/RTCRestBinaryString.cpp \
    16581658        common/rest/RTCRestClientApiBase.cpp \
    16591659        common/rest/RTCRestClientApiBaseOci.cpp \
Note: See TracChangeset for help on using the changeset viewer.

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