1 | /** @file
|
---|
2 | * IPRT - C++ Representational State Transfer (REST) Binary String Class.
|
---|
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_restbinarystring_h
|
---|
27 | #define ___iprt_cpp_restbinarystring_h
|
---|
28 |
|
---|
29 | #include <iprt/cpp/restbase.h>
|
---|
30 | #include <iprt/http.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /** @defgroup grp_rt_cpp_restbinstr C++ Representational State Transfer (REST) Binary String Class.
|
---|
34 | * @ingroup grp_rt_cpp
|
---|
35 | * @{
|
---|
36 | */
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Class for handling strings on the binary format.
|
---|
40 | *
|
---|
41 | * This can only be used for body parameters.
|
---|
42 | */
|
---|
43 | class RT_DECL_CLASS RTCRestBinaryString : public RTCRestObjectBase
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | /** Default constructor. */
|
---|
47 | RTCRestBinaryString();
|
---|
48 | /** Destructor. */
|
---|
49 | virtual ~RTCRestBinaryString();
|
---|
50 |
|
---|
51 | /** Safe copy assignment method. */
|
---|
52 | int assignCopy(RTCRestBinaryString const &a_rThat);
|
---|
53 |
|
---|
54 | /* Overridden methods: */
|
---|
55 | virtual int resetToDefault() RT_OVERRIDE;
|
---|
56 | virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
|
---|
57 | virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
|
---|
58 | virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_OVERRIDE;
|
---|
59 | virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
|
---|
60 | uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
|
---|
61 | virtual kTypeClass typeClass(void) const RT_OVERRIDE;
|
---|
62 | virtual const char *typeName(void) const RT_OVERRIDE;
|
---|
63 |
|
---|
64 | /** Factory method. */
|
---|
65 | static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Gets the data size.
|
---|
69 | *
|
---|
70 | * This can be used from a consumer callback to get the Content-Length field
|
---|
71 | * value if available. Returns UINT64_MAX if not available.
|
---|
72 | */
|
---|
73 | uint64_t getDataSize() const { return m_cbData; }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Sets the data to upload.
|
---|
77 | *
|
---|
78 | * @returns IPRT status code.
|
---|
79 | * @param a_pvData The data buffer. NULL can be used to actively
|
---|
80 | * deregister previous data.
|
---|
81 | * @param a_cbData The amount of data to upload from that buffer.
|
---|
82 | * @param a_fCopy Whether to make a copy (@a true) or use the
|
---|
83 | * buffer directly (@a false). In the latter case
|
---|
84 | * the caller must make sure the data remains available
|
---|
85 | * for the entire lifetime of this object (or until
|
---|
86 | * setUploadData is called with NULL parameters).
|
---|
87 | * @param a_pszContentType Specifies the content type to set. Pass NULL (default)
|
---|
88 | * to not explictly set the content type.
|
---|
89 | */
|
---|
90 | int setUploadData(void const *a_pvData, size_t a_cbData, bool a_fCopy, const char *a_pszContentType = NULL);
|
---|
91 |
|
---|
92 | /** @name Data callbacks.
|
---|
93 | * @{ */
|
---|
94 | /**
|
---|
95 | * Callback for producing bytes to upload.
|
---|
96 | *
|
---|
97 | * @returns IPRT status code.
|
---|
98 | * @param a_pThis The related string object.
|
---|
99 | * @param a_pvDst Where to put the bytes.
|
---|
100 | * @param a_cbDst Max number of bytes to produce.
|
---|
101 | * @param a_offContent The byte offset corresponding to the start of @a a_pvDst.
|
---|
102 | * @param a_pcbActual Where to return the number of bytes actually produced.
|
---|
103 | * @remarks Use getCallbackData to get the user data.
|
---|
104 | */
|
---|
105 | typedef DECLCALLBACK(int) FNPRODUCER(RTCRestBinaryString *a_pThis, void *a_pvDst, size_t a_cbDst,
|
---|
106 | uint64_t a_offContent, size_t *a_pcbActual);
|
---|
107 | /** Pointer to a byte producer callback. */
|
---|
108 | typedef FNPRODUCER *PFNPRODUCER;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Callback for consuming downloaded bytes.
|
---|
112 | *
|
---|
113 | * @returns IPRT status code.
|
---|
114 | * @param a_pThis The related string object.
|
---|
115 | * @param a_pvSrc Buffer containing the bytes.
|
---|
116 | * @param a_cbSrc The number of bytes in the buffer.
|
---|
117 | * @remarks Use getCallbackData to get the user data.
|
---|
118 | */
|
---|
119 | typedef DECLCALLBACK(int) FNCONSUMER(RTCRestBinaryString *a_pThis, const void *a_pvSrc, size_t a_cbSrc);
|
---|
120 | /** Pointer to a byte consumer callback. */
|
---|
121 | typedef FNCONSUMER *PFNCONSUMER;
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Retrieves the callback data.
|
---|
125 | */
|
---|
126 | void *getCallbackData() const { return m_pvCallbackData; }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Sets the consumer callback.
|
---|
130 | *
|
---|
131 | * @returns IPRT status code.
|
---|
132 | * @param a_pfnConsumer The callback function for consuming downloaded data.
|
---|
133 | * NULL if data should be stored in m_pbData/m_cbData (the default).
|
---|
134 | * @param a_pvCallbackData Data the can be retrieved from the callback
|
---|
135 | * using getCallbackData().
|
---|
136 | */
|
---|
137 | int setConsumerCallback(PFNCONSUMER a_pfnConsumer, void *a_pvCallbackData = NULL);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Sets the producer callback.
|
---|
141 | *
|
---|
142 | * @returns IPRT status code.
|
---|
143 | * @param a_pfnProducer The callback function for producing data.
|
---|
144 | * @param a_pvCallbackData Data the can be retrieved from the callback
|
---|
145 | * using getCallbackData().
|
---|
146 | * @param a_cbData The amount of data that will be uploaded,
|
---|
147 | * UINT64_MAX if not unknown.
|
---|
148 | *
|
---|
149 | * @note This will drop any buffer previously registered using
|
---|
150 | * setUploadData(), unless a_pfnProducer is NULL.
|
---|
151 | */
|
---|
152 | int setProducerCallback(PFNPRODUCER a_pfnProducer, void *a_pvCallbackData = NULL, uint64_t a_cbData = UINT64_MAX);
|
---|
153 | /** @} */
|
---|
154 |
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Preprares transmission via @a a_hHttp.
|
---|
158 | *
|
---|
159 | * @returns IPRT status code.
|
---|
160 | * @param a_hHttp The HTTP client instance.
|
---|
161 | */
|
---|
162 | virtual int xmitPrepare(RTHTTP a_hHttp) const;
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * For completing and/or undoing setup from xmitPrepare.
|
---|
166 | *
|
---|
167 | * @param a_hHttp The HTTP client instance.
|
---|
168 | */
|
---|
169 | virtual void xmitComplete(RTHTTP a_hHttp) const;
|
---|
170 |
|
---|
171 | //virtual int receivePrepare(RTHTTP a_hHttp);
|
---|
172 | //virtual int receiveComplete(int a_rcStatus, RTHTTP a_hHttp);
|
---|
173 |
|
---|
174 | protected:
|
---|
175 | /** Pointer to the bytes, if provided directly. */
|
---|
176 | uint8_t *m_pbData;
|
---|
177 | /** Number of bytes. UINT64_MAX if not known. */
|
---|
178 | uint64_t m_cbData;
|
---|
179 | /** User argument for callbacks. */
|
---|
180 | void *m_pvCallbackData;
|
---|
181 | /** Pointer to user-registered consumer callback function. */
|
---|
182 | PFNCONSUMER m_pfnConsumer;
|
---|
183 | /** Pointer to user-registered producer callback function. */
|
---|
184 | PFNPRODUCER m_pfnProducer;
|
---|
185 | /** Set if m_pbData must be freed. */
|
---|
186 | bool m_fFreeData;
|
---|
187 | /** The content type (upload only). */
|
---|
188 | RTCString m_strContentType;
|
---|
189 |
|
---|
190 |
|
---|
191 | static FNRTHTTPUPLOADCALLBACK xmitHttpCallback;
|
---|
192 |
|
---|
193 | private:
|
---|
194 | /* No copy constructor or copy assignment: */
|
---|
195 | RTCRestBinaryString(RTCRestBinaryString const &a_rThat);
|
---|
196 | RTCRestBinaryString &operator=(RTCRestBinaryString const &a_rThat);
|
---|
197 | };
|
---|
198 |
|
---|
199 | #endif
|
---|
200 |
|
---|