VirtualBox

source: vbox/trunk/include/iprt/cpp/restbinarystring.h@ 74126

Last change on this file since 74126 was 74126, checked in by vboxsync, 7 years ago

IPRT/rest: More work on binary downloads and uploads. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 KB
Line 
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 */
43class RT_DECL_CLASS RTCRestBinaryString : public RTCRestObjectBase
44{
45public:
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 /** Frees the data held by the object.
55 * Will set m_pbData to NULL and m_cbData to UINT64_MAX. */
56 void freeData();
57
58 /* Overridden methods: */
59 virtual int resetToDefault() RT_OVERRIDE;
60 virtual RTCRestOutputBase &serializeAsJson(RTCRestOutputBase &a_rDst) const RT_OVERRIDE;
61 virtual int deserializeFromJson(RTCRestJsonCursor const &a_rCursor) RT_OVERRIDE;
62 virtual int toString(RTCString *a_pDst, uint32_t a_fFlags = kCollectionFormat_Unspecified) const RT_OVERRIDE;
63 virtual int fromString(RTCString const &a_rValue, const char *a_pszName, PRTERRINFO a_pErrInfo = NULL,
64 uint32_t a_fFlags = kCollectionFormat_Unspecified) RT_OVERRIDE;
65 virtual kTypeClass typeClass(void) const RT_OVERRIDE;
66 virtual const char *typeName(void) const RT_OVERRIDE;
67
68 /** Factory method. */
69 static DECLCALLBACK(RTCRestObjectBase *) createInstance(void);
70
71 /**
72 * Retrieves the callback data.
73 */
74 void *getCallbackData() const { return m_pvCallbackData; }
75
76
77 /** @name Upload methods
78 * @{ */
79
80 /**
81 * Sets the content-type for an upload.
82 *
83 * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY.
84 * @param a_pszContentType The content type to set.
85 * If NULL, no content type is set.
86 */
87 int setContentType(const char *a_pszContentType);
88
89 /**
90 * Gets the content type that was set.
91 */
92 RTCString const &getContentType() const { return m_strContentType; }
93
94 /**
95 * Sets the data to upload.
96 *
97 * @returns IPRT status code.
98 * @param a_pvData The data buffer. NULL can be used to actively
99 * deregister previous data.
100 * @param a_cbData The amount of data to upload from that buffer.
101 * @param a_fCopy Whether to make a copy (@a true) or use the
102 * buffer directly (@a false). In the latter case
103 * the caller must make sure the data remains available
104 * for the entire lifetime of this object (or until
105 * setUploadData is called with NULL parameters).
106 *
107 * @note This will drop any previously registered producer callback and user data..
108 */
109 int setUploadData(void const *a_pvData, size_t a_cbData, bool a_fCopy = true);
110
111 /**
112 * Callback for producing bytes to upload.
113 *
114 * @returns IPRT status code.
115 * @param a_pThis The related string object.
116 * @param a_pvDst Where to put the bytes.
117 * @param a_cbDst Max number of bytes to produce.
118 * @param a_offContent The byte offset corresponding to the start of @a a_pvDst.
119 * @param a_pcbActual Where to return the number of bytes actually produced.
120 * @remarks Use getCallbackData to get the user data.
121 */
122 typedef DECLCALLBACK(int) FNPRODUCER(RTCRestBinaryString *a_pThis, void *a_pvDst, size_t a_cbDst,
123 uint64_t a_offContent, size_t *a_pcbActual);
124 /** Pointer to a byte producer callback. */
125 typedef FNPRODUCER *PFNPRODUCER;
126
127 /**
128 * Sets the producer callback.
129 *
130 * @param a_pfnProducer The callback function for producing data.
131 * @param a_pvCallbackData Data the can be retrieved from the callback
132 * using getCallbackData().
133 * @param a_cbContentLength The amount of data that will be uploaded and
134 * to be set as the value of the content-length
135 * header field. Pass UINT64_MAX if not known.
136 *
137 * @note This will drop any buffer previously registered using setUploadData().
138 */
139 void setProducerCallback(PFNPRODUCER a_pfnProducer, void *a_pvCallbackData = NULL, uint64_t a_cbContentLength = UINT64_MAX);
140
141 /**
142 * Preprares transmission via the @a a_hHttp client instance.
143 *
144 * @returns IPRT status code.
145 * @param a_hHttp The HTTP client instance.
146 * @internal
147 */
148 virtual int xmitPrepare(RTHTTP a_hHttp) const;
149
150 /**
151 * For completing and/or undoing setup from xmitPrepare.
152 *
153 * @param a_hHttp The HTTP client instance.
154 * @internal
155 */
156 virtual void xmitComplete(RTHTTP a_hHttp) const;
157 /** @} */
158
159
160 /** @name Download methods
161 * @{ */
162
163 /**
164 * Sets the max size to download to memory.
165 *
166 * This also indicates the intention to download to a memory buffer, so it
167 * will drop any previously registered consumer callback and its user data.
168 *
169 * @param a_cbMax Maximum number of bytes to download to memory.
170 * If 0, a default is selected (currently 32MiB for
171 * 32-bit hosts and 128MiB for 64-bit).
172 */
173 void setMaxDownloadSize(size_t a_cbMaxDownload);
174
175 /**
176 * Gets the content-length value (UINT64_MAX if not available).
177 */
178 uint64_t getContentLength() const { return m_cbContentLength; }
179
180 /**
181 * Gets the number of bytes that has actually been downloaded.
182 */
183 uint64_t getDownloadSize() const { return m_cbDownloaded; }
184
185 /**
186 * Returns the pointer to the download buffer.
187 * @note returns NULL if setConsumerCallback was used or no data was downloaded.
188 */
189 uint8_t const *getDownloadData() const { return m_pbData; }
190
191 /**
192 * Callback for consuming downloaded bytes.
193 *
194 * @returns IPRT status code.
195 * @param a_pThis The related string object.
196 * @param a_pvSrc Buffer containing the bytes.
197 * @param a_cbSrc The number of bytes in the buffer.
198 * @param a_uHttpStatus The HTTP status code.
199 * @param a_offContent The byte offset corresponding to the start of @a a_pvSrc.
200 * @param a_cbContent The content length field value, UINT64_MAX if not available.
201 * @remarks Use getCallbackData to get the user data.
202 */
203 typedef DECLCALLBACK(int) FNCONSUMER(RTCRestBinaryString *a_pThis, const void *a_pvSrc, size_t a_cbSrc,
204 uint32_t a_uHttpStatus, uint64_t a_offContent, uint64_t a_cbContent);
205 /** Pointer to a byte consumer callback. */
206 typedef FNCONSUMER *PFNCONSUMER;
207
208 /**
209 * Sets the consumer callback.
210 *
211 * @param a_pfnConsumer The callback function for consuming downloaded data.
212 * NULL if data should be stored in m_pbData (the default).
213 * @param a_pvCallbackData Data the can be retrieved from the callback
214 * using getCallbackData().
215 */
216 void setConsumerCallback(PFNCONSUMER a_pfnConsumer, void *a_pvCallbackData = NULL);
217
218 /**
219 * Preprares for receiving via the @a a_hHttp client instance.
220 *
221 * @returns IPRT status code.
222 * @param a_hHttp The HTTP client instance.
223 * @param a_fCallbackFlags The HTTP callback flags (status code spec).
224 * @internal
225 */
226 virtual int receivePrepare(RTHTTP a_hHttp, uint32_t a_fCallbackFlags);
227
228 /**
229 * For completing and/or undoing setup from receivePrepare.
230 *
231 * @param a_hHttp The HTTP client instance.
232 * @internal
233 */
234 virtual void receiveComplete(RTHTTP a_hHttp);
235 /** @} */
236
237
238protected:
239 /** Pointer to the bytes, if provided directly. (both) */
240 uint8_t *m_pbData;
241 /** Number of bytes allocated for the m_pbData buffer (both). */
242 size_t m_cbAllocated;
243 /** Set if m_pbData must be freed (both). */
244 bool m_fFreeData;
245 /** Number of bytes corresponding to content-length.
246 * UINT64_MAX if not known. Used both for unploads and downloads. */
247 uint64_t m_cbContentLength;
248 /** User argument for both callbacks (both). */
249 void *m_pvCallbackData;
250
251 /** Pointer to user-registered producer callback function (upload only). */
252 PFNPRODUCER m_pfnProducer;
253 /** The content type if set (upload only). */
254 RTCString m_strContentType;
255
256 /** Pointer to user-registered consumer callback function (download only). */
257 PFNCONSUMER m_pfnConsumer;
258 /** Number of bytes downloaded thus far. */
259 uint64_t m_cbDownloaded;
260 /** Maximum data to download to memory (download only). */
261 size_t m_cbMaxDownload;
262
263 /** Callback for use with RTHttpSetUploadCallback. */
264 static FNRTHTTPUPLOADCALLBACK xmitHttpCallback;
265 /** Callback for use with RTHttpSetDownloadCallback. */
266 static FNRTHTTPDOWNLOADCALLBACK receiveHttpCallback;
267
268private:
269 /* No copy constructor or copy assignment: */
270 RTCRestBinaryString(RTCRestBinaryString const &a_rThat);
271 RTCRestBinaryString &operator=(RTCRestBinaryString const &a_rThat);
272};
273
274#endif
275
Note: See TracBrowser for help on using the repository browser.

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