VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rest/RTCRestClientResponseBase.cpp@ 74025

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

IPRT/rest: split up restbase.h into several files to make it more managable. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
1/* $Id: RTCRestClientResponseBase.cpp 74025 2018-09-02 14:14:43Z vboxsync $ */
2/** @file
3 * IPRT - C++ REST, RTCRestClientResponseBase implementation.
4 */
5
6/*
7 * Copyright (C) 2018 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_REST
32#include <iprt/cpp/restclient.h>
33
34#include <iprt/ctype.h>
35#include <iprt/err.h>
36#include <iprt/cpp/reststringmap.h>
37
38
39/**
40 * Default constructor.
41 */
42RTCRestClientResponseBase::RTCRestClientResponseBase()
43 : m_rcStatus(VERR_WRONG_ORDER)
44 , m_rcHttp(VERR_NOT_AVAILABLE)
45 , m_pErrInfo(NULL)
46{
47}
48
49
50/**
51 * Destructor.
52 */
53RTCRestClientResponseBase::~RTCRestClientResponseBase()
54{
55 deleteErrInfo();
56}
57
58
59/**
60 * Copy constructor.
61 */
62RTCRestClientResponseBase::RTCRestClientResponseBase(RTCRestClientResponseBase const &a_rThat)
63 : m_rcStatus(a_rThat.m_rcStatus)
64 , m_rcHttp(a_rThat.m_rcHttp)
65 , m_pErrInfo(NULL)
66 , m_strContentType(a_rThat.m_strContentType)
67{
68 if (a_rThat.m_pErrInfo)
69 copyErrInfo(a_rThat.m_pErrInfo);
70}
71
72
73/**
74 * Copy assignment operator.
75 */
76RTCRestClientResponseBase &RTCRestClientResponseBase::operator=(RTCRestClientResponseBase const &a_rThat)
77{
78 m_rcStatus = a_rThat.m_rcStatus;
79 m_rcHttp = a_rThat.m_rcHttp;
80 m_strContentType = a_rThat.m_strContentType;
81 if (a_rThat.m_pErrInfo)
82 copyErrInfo(a_rThat.m_pErrInfo);
83 else if (m_pErrInfo)
84 deleteErrInfo();
85
86 return *this;
87}
88
89
90void RTCRestClientResponseBase::reset()
91{
92 /* Return to default constructor state. */
93 m_rcStatus = VERR_WRONG_ORDER;
94 m_rcHttp = VERR_NOT_AVAILABLE;
95 if (m_pErrInfo)
96 deleteErrInfo();
97 m_strContentType.setNull();
98}
99
100
101int RTCRestClientResponseBase::receivePrepare(RTHTTP a_hHttp, void ***a_pppvHdr, void ***a_pppvBody)
102{
103 RT_NOREF(a_hHttp, a_pppvHdr, a_pppvBody);
104 return VINF_SUCCESS;
105}
106
107
108void RTCRestClientResponseBase::receiveComplete(int a_rcStatus, RTHTTP a_hHttp)
109{
110 RT_NOREF_PV(a_hHttp);
111 m_rcStatus = a_rcStatus;
112 if (a_rcStatus >= 0)
113 m_rcHttp = a_rcStatus;
114}
115
116
117void RTCRestClientResponseBase::consumeHeaders(const char *a_pchData, size_t a_cbData)
118{
119 /*
120 * Get the the content type.
121 */
122 int rc = extractHeaderFromBlob(RT_STR_TUPLE("Content-Type"), a_pchData, a_cbData, &m_strContentType);
123 if (rc == VERR_NOT_FOUND)
124 rc = VINF_SUCCESS;
125 AssertRCReturnVoidStmt(rc, m_rcStatus = rc);
126}
127
128
129void RTCRestClientResponseBase::consumeBody(const char *a_pchData, size_t a_cbData)
130{
131 RT_NOREF(a_pchData, a_cbData);
132}
133
134
135void RTCRestClientResponseBase::receiveFinal()
136{
137}
138
139
140PRTERRINFO RTCRestClientResponseBase::getErrInfoInternal(void)
141{
142 if (m_pErrInfo)
143 return m_pErrInfo;
144 size_t cbMsg = _4K;
145 m_pErrInfo = (PRTERRINFO)RTMemAllocZ(sizeof(*m_pErrInfo) + cbMsg);
146 if (m_pErrInfo)
147 return RTErrInfoInit(m_pErrInfo, (char *)(m_pErrInfo + 1), cbMsg);
148 return NULL;
149}
150
151
152void RTCRestClientResponseBase::deleteErrInfo(void)
153{
154 if (m_pErrInfo)
155 {
156 RTMemFree(m_pErrInfo);
157 m_pErrInfo = NULL;
158 }
159}
160
161
162void RTCRestClientResponseBase::copyErrInfo(PCRTERRINFO pErrInfo)
163{
164 deleteErrInfo();
165 m_pErrInfo = (PRTERRINFO)RTMemDup(pErrInfo, pErrInfo->cbMsg + sizeof(*pErrInfo));
166 if (m_pErrInfo)
167 {
168 m_pErrInfo->pszMsg = (char *)(m_pErrInfo + 1);
169 m_pErrInfo->apvReserved[0] = NULL;
170 m_pErrInfo->apvReserved[1] = NULL;
171 }
172}
173
174
175int RTCRestClientResponseBase::addError(int rc, const char *pszFormat, ...)
176{
177 PRTERRINFO pErrInfo = getErrInfoInternal();
178 if (pErrInfo)
179 {
180 va_list va;
181 va_start(va, pszFormat);
182 if ( !RTErrInfoIsSet(pErrInfo)
183 || pErrInfo->cbMsg == 0
184 || pErrInfo->pszMsg[pErrInfo->cbMsg - 1] == '\n')
185 RTErrInfoAddV(pErrInfo, rc, pszFormat, va);
186 else
187 RTErrInfoAddF(pErrInfo, rc, "\n%N", pszFormat, &va);
188 va_end(va);
189 }
190 if (RT_SUCCESS(m_rcStatus) && RT_FAILURE_NP(rc))
191 m_rcStatus = rc;
192 return rc;
193}
194
195
196void RTCRestClientResponseBase::extracHeaderFieldsFromBlob(HEADERFIELDDESC const *a_paFieldDescs,
197 RTCRestObjectBase ***a_pappFieldValues,
198 size_t a_cFields, const char *a_pchData, size_t a_cbData)
199
200{
201 RTCString strValue; /* (Keep it out here to encourage buffer allocation reuse and default construction call.) */
202
203 /*
204 * Work our way through the header blob.
205 */
206 while (a_cbData >= 2)
207 {
208 /*
209 * Determine length of the header name:value combo.
210 * Note! Multi-line field values are not currently supported.
211 */
212 const char *pchEol = (const char *)memchr(a_pchData, '\n', a_cbData);
213 while (pchEol && (pchEol == a_pchData || pchEol[-1] != '\r'))
214 pchEol = (const char *)memchr(pchEol, '\n', a_cbData - (pchEol - a_pchData));
215
216 size_t const cchField = pchEol ? pchEol - a_pchData + 1 : a_cbData;
217 size_t const cchFieldNoCrLf = pchEol ? pchEol - a_pchData - 1 : a_cbData;
218
219 const char *pchColon = (const char *)memchr(a_pchData, ':', cchFieldNoCrLf);
220 Assert(pchColon);
221 if (pchColon)
222 {
223 size_t const cchName = pchColon - a_pchData;
224 size_t const offValue = cchName + (RT_C_IS_BLANK(pchColon[1]) ? 2 : 1);
225 size_t const cchValue = cchFieldNoCrLf - offValue;
226
227 /*
228 * Match headers.
229 */
230 bool fHaveValue = false;
231 for (size_t i = 0; i < a_cFields; i++)
232 {
233 size_t const cchThisName = a_paFieldDescs[i].cchName;
234 if ( (!(a_paFieldDescs[i].fFlags & kHdrField_MapCollection) ? cchThisName == cchName : cchThisName < cchName)
235 && RTStrNICmpAscii(a_pchData, a_paFieldDescs[i].pszName, cchThisName) == 0)
236 {
237 /* Get and clean the value. */
238 if (!fHaveValue)
239 {
240 int rc = strValue.assignNoThrow(&a_pchData[offValue], cchValue);
241 if (RT_SUCCESS(rc))
242 {
243 RTStrPurgeEncoding(strValue.mutableRaw()); /** @todo this is probably a little wrong... */
244 fHaveValue = true;
245 }
246 else
247 {
248 addError(rc, "Error allocating %u bytes for header field %s", a_paFieldDescs[i].pszName);
249 break;
250 }
251 }
252
253 /*
254 * Create field to deserialize.
255 */
256 RTCRestStringMapBase *pMap = NULL;
257 RTCRestObjectBase *pObj = NULL;
258 if (!(a_paFieldDescs[i].fFlags & kHdrField_MapCollection))
259 {
260 /* Only once. */
261 if (!*a_pappFieldValues[i])
262 {
263 pObj = a_paFieldDescs[i].pfnCreateInstance();
264 if (pObj)
265 *a_pappFieldValues[i] = pObj;
266 else
267 {
268 addError(VERR_NO_MEMORY, "out of memory");
269 break;
270 }
271 }
272 else
273 {
274 addError(VERR_REST_RESPONSE_REPEAT_HEADER_FIELD, "Already saw header field '%s'", a_paFieldDescs[i].pszName);
275 continue;
276 }
277 }
278 else
279 {
280 /* Make sure we've got a map to work with. */
281 if (!*a_pappFieldValues[i])
282 *a_pappFieldValues[i] = pObj = a_paFieldDescs[i].pfnCreateInstance();
283 else
284 pObj = *a_pappFieldValues[i];
285 AssertBreak(pObj->typeClass() == RTCRestObjectBase::kTypeClass_StringMap);
286 pMap = (RTCRestStringMapBase *)pObj;
287
288 /* Insert the header field name (sans prefix) into the map. We then use the
289 new value object for the deserialization of the header field value below. */
290 int rc = pMap->putNewValue(&pObj, &a_pchData[cchThisName], cchName - cchThisName);
291 if (RT_SUCCESS(rc))
292 { /* likely */ }
293 else if (rc == VERR_ALREADY_EXISTS)
294 {
295 addError(VERR_REST_RESPONSE_REPEAT_HEADER_FIELD, "Already saw header field '%s'", a_paFieldDescs[i].pszName);
296 continue;
297 }
298 else
299 {
300 addError(rc, "out of memory");
301 break;
302 }
303 }
304
305 /*
306 * Deserialize it.
307 */
308 RTERRINFOSTATIC ErrInfo;
309 int rc = pObj->fromString(strValue, a_paFieldDescs[i].pszName, RTErrInfoInitStatic(&ErrInfo),
310 a_paFieldDescs[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask);
311 if (RT_SUCCESS(rc))
312 { /* likely */ }
313 else if (RTErrInfoIsSet(&ErrInfo.Core))
314 addError(rc, "Error %Rrc parsing header field '%s': %s",
315 rc, a_paFieldDescs[i].pszName, ErrInfo.Core.pszMsg);
316 else
317 addError(rc, "Error %Rrc parsing header field '%s'", rc, a_paFieldDescs[i].pszName);
318 }
319 }
320 }
321
322 /*
323 * Advance to the next field.
324 */
325 a_cbData -= cchField;
326 a_pchData += cchField;
327 }
328}
329
330int RTCRestClientResponseBase::extractHeaderFromBlob(const char *a_pszField, size_t a_cchField,
331 const char *a_pchData, size_t a_cbData,
332 RTCString *a_pStrDst)
333{
334 char const chUpper0 = RT_C_TO_UPPER(a_pszField[0]);
335 char const chLower0 = RT_C_TO_LOWER(a_pszField[0]);
336 Assert(!RT_C_IS_SPACE(chUpper0));
337
338 while (a_cbData > a_cchField)
339 {
340 /* Determine length of the header name:value combo.
341 Note! Multi-line field values are not currently supported. */
342 const char *pchEol = (const char *)memchr(a_pchData, '\n', a_cbData);
343 while (pchEol && (pchEol == a_pchData || pchEol[-1] != '\r'))
344 pchEol = (const char *)memchr(pchEol, '\n', a_cbData - (pchEol - a_pchData));
345
346 size_t cchField = pchEol ? pchEol - a_pchData + 1 : a_cbData;
347
348 /* Try match */
349 if ( a_pchData[a_cchField] == ':'
350 && ( a_pchData[0] == chUpper0
351 || a_pchData[0] == chLower0)
352 && RTStrNICmpAscii(a_pchData, a_pszField, a_cchField) == 0)
353 {
354 /* Drop CRLF. */
355 if (pchEol)
356 cchField -= 2;
357
358 /* Drop the field name and optional whitespace. */
359 cchField -= a_cchField + 1;
360 a_pchData += a_cchField + 1;
361 if (cchField > 0 && RT_C_IS_BLANK(*a_pchData))
362 {
363 a_pchData++;
364 cchField--;
365 }
366
367 /* Return the value. */
368 int rc = a_pStrDst->assignNoThrow(a_pchData, cchField);
369 if (RT_SUCCESS(rc))
370 RTStrPurgeEncoding(a_pStrDst->mutableRaw()); /** @todo this is probably a little wrong... */
371 return rc;
372 }
373
374 /* Advance to the next field. */
375 a_pchData += cchField;
376 a_cbData -= cchField;
377 }
378
379 return VERR_NOT_FOUND;
380}
381
382
383
384RTCRestClientResponseBase::PrimaryJsonCursorForBody::PrimaryJsonCursorForBody(RTJSONVAL hValue, const char *pszName,
385 RTCRestClientResponseBase *a_pThat)
386 : RTCRestJsonPrimaryCursor(hValue, pszName, a_pThat->getErrInfoInternal())
387 , m_pThat(a_pThat)
388{
389}
390
391
392int RTCRestClientResponseBase::PrimaryJsonCursorForBody::addError(RTCRestJsonCursor const &a_rCursor, int a_rc,
393 const char *a_pszFormat, ...)
394{
395 va_list va;
396 va_start(va, a_pszFormat);
397 char szPath[256];
398 m_pThat->addError(a_rc, "response body/%s: %N", getPath(a_rCursor, szPath, sizeof(szPath)), a_pszFormat, &va);
399 va_end(va);
400 return a_rc;
401}
402
403
404int RTCRestClientResponseBase::PrimaryJsonCursorForBody::unknownField(RTCRestJsonCursor const &a_rCursor)
405{
406 char szPath[256];
407 m_pThat->addError(VWRN_NOT_FOUND, "response body/%s: unknown field (type %s)",
408 getPath(a_rCursor, szPath, sizeof(szPath)), RTJsonValueTypeName(RTJsonValueGetType(a_rCursor.m_hValue)));
409 return VWRN_NOT_FOUND;
410}
411
412
413void RTCRestClientResponseBase::deserializeBody(RTCRestObjectBase *a_pDst, const char *a_pchData, size_t a_cbData)
414{
415 if (m_strContentType.startsWith("application/json"))
416 {
417 int rc = RTStrValidateEncodingEx(a_pchData, a_cbData, RTSTR_VALIDATE_ENCODING_EXACT_LENGTH);
418 if (RT_SUCCESS(rc))
419 {
420 RTERRINFOSTATIC ErrInfo;
421 RTJSONVAL hValue;
422 rc = RTJsonParseFromBuf(&hValue, (const uint8_t *)a_pchData, a_cbData, RTErrInfoInitStatic(&ErrInfo));
423 if (RT_SUCCESS(rc))
424 {
425 PrimaryJsonCursorForBody PrimaryCursor(hValue, a_pDst->typeName(), this); /* note: consumes hValue */
426 a_pDst->deserializeFromJson(PrimaryCursor.m_Cursor);
427 }
428 else if (RTErrInfoIsSet(&ErrInfo.Core))
429 addError(rc, "Error %Rrc parsing server response as JSON (type %s): %s",
430 rc, a_pDst->typeName(), ErrInfo.Core.pszMsg);
431 else
432 addError(rc, "Error %Rrc parsing server response as JSON (type %s)", rc, a_pDst->typeName());
433 }
434 else if (rc == VERR_INVALID_UTF8_ENCODING)
435 addError(VERR_REST_RESPONSE_INVALID_UTF8_ENCODING, "Invalid UTF-8 body encoding (object type %s; Content-Type: %s)",
436 a_pDst->typeName(), m_strContentType.c_str());
437 else if (rc == VERR_BUFFER_UNDERFLOW)
438 addError(VERR_REST_RESPONSE_EMBEDDED_ZERO_CHAR, "Embedded zero character in response (object type %s; Content-Type: %s)",
439 a_pDst->typeName(), m_strContentType.c_str());
440 else
441 addError(rc, "Unexpected body validation error (object type %s; Content-Type: %s): %Rrc",
442 a_pDst->typeName(), m_strContentType.c_str(), rc);
443 }
444 else
445 addError(VERR_REST_RESPONSE_CONTENT_TYPE_NOT_SUPPORTED, "Unsupported content type for '%s': %s",
446 a_pDst->typeName(), m_strContentType.c_str());
447}
448
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