VirtualBox

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

Last change on this file since 76408 was 74425, checked in by vboxsync, 6 years ago

IPRT/rest: Missed RT_NOEXCEPT in two place. Went wild adding RT_NOEXCEPT everywhere possible. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1/* $Id: RTCRestClientResponseBase.cpp 74425 2018-09-23 15:41:48Z 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() RT_NOEXCEPT
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() RT_NOEXCEPT
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) RT_NOEXCEPT
102{
103 int rc = RTHttpSetHeaderCallback(a_hHttp, receiveHttpHeaderCallback, this);
104 AssertRCReturn(rc, rc);
105
106 return VINF_SUCCESS;
107}
108
109
110void RTCRestClientResponseBase::receiveComplete(int a_rcStatus, RTHTTP a_hHttp) RT_NOEXCEPT
111{
112 RT_NOREF_PV(a_hHttp);
113 m_rcStatus = a_rcStatus;
114 if (a_rcStatus >= 0)
115 m_rcHttp = a_rcStatus;
116
117 int rc = RTHttpSetHeaderCallback(a_hHttp, NULL, NULL);
118 AssertRC(rc);
119}
120
121
122int RTCRestClientResponseBase::consumeHeader(uint32_t a_uMatchWord, const char *a_pchField, size_t a_cchField,
123 const char *a_pchValue, size_t a_cchValue) RT_NOEXCEPT
124{
125 if ( a_uMatchWord == RTHTTP_MAKE_HDR_MATCH_WORD(sizeof("Content-Type") - 1, 'c', 'o', 'n')
126 && RTStrNICmpAscii(a_pchField, RT_STR_TUPLE("Content-Type")) == 0)
127 {
128 int rc = RTStrValidateEncodingEx(a_pchValue, a_cchValue, RTSTR_VALIDATE_ENCODING_EXACT_LENGTH);
129 AssertRC(rc);
130 if (RT_SUCCESS(rc))
131 return m_strContentType.assignNoThrow(a_pchValue, a_cchValue);
132 }
133 RT_NOREF(a_cchField);
134 return VINF_SUCCESS;
135}
136
137
138/*static*/ DECLCALLBACK(int)
139RTCRestClientResponseBase::receiveHttpHeaderCallback(RTHTTP hHttp, uint32_t uMatchWord, const char *pchField, size_t cchField,
140 const char *pchValue, size_t cchValue, void *pvUser) RT_NOEXCEPT
141{
142 RTCRestClientResponseBase *pThis = (RTCRestClientResponseBase *)pvUser;
143 RT_NOREF(hHttp);
144 return pThis->consumeHeader(uMatchWord, pchField, cchField, pchValue, cchValue);
145}
146
147
148void RTCRestClientResponseBase::consumeBody(const char *a_pchData, size_t a_cbData) RT_NOEXCEPT
149{
150 RT_NOREF(a_pchData, a_cbData);
151}
152
153
154void RTCRestClientResponseBase::receiveFinal() RT_NOEXCEPT
155{
156}
157
158
159PRTERRINFO RTCRestClientResponseBase::getErrInfoInternal(void) RT_NOEXCEPT
160{
161 if (m_pErrInfo)
162 return m_pErrInfo;
163 size_t cbMsg = _4K;
164 m_pErrInfo = (PRTERRINFO)RTMemAllocZ(sizeof(*m_pErrInfo) + cbMsg);
165 if (m_pErrInfo)
166 return RTErrInfoInit(m_pErrInfo, (char *)(m_pErrInfo + 1), cbMsg);
167 return NULL;
168}
169
170
171void RTCRestClientResponseBase::deleteErrInfo(void) RT_NOEXCEPT
172{
173 if (m_pErrInfo)
174 {
175 RTMemFree(m_pErrInfo);
176 m_pErrInfo = NULL;
177 }
178}
179
180
181void RTCRestClientResponseBase::copyErrInfo(PCRTERRINFO pErrInfo) RT_NOEXCEPT
182{
183 deleteErrInfo();
184 m_pErrInfo = (PRTERRINFO)RTMemDup(pErrInfo, pErrInfo->cbMsg + sizeof(*pErrInfo));
185 if (m_pErrInfo)
186 {
187 m_pErrInfo->pszMsg = (char *)(m_pErrInfo + 1);
188 m_pErrInfo->apvReserved[0] = NULL;
189 m_pErrInfo->apvReserved[1] = NULL;
190 }
191}
192
193
194int RTCRestClientResponseBase::addError(int rc, const char *pszFormat, ...) RT_NOEXCEPT
195{
196 PRTERRINFO pErrInfo = getErrInfoInternal();
197 if (pErrInfo)
198 {
199 va_list va;
200 va_start(va, pszFormat);
201 if ( !RTErrInfoIsSet(pErrInfo)
202 || pErrInfo->cbMsg == 0
203 || pErrInfo->pszMsg[pErrInfo->cbMsg - 1] == '\n')
204 RTErrInfoAddV(pErrInfo, rc, pszFormat, va);
205 else
206 RTErrInfoAddF(pErrInfo, rc, "\n%N", pszFormat, &va);
207 va_end(va);
208 }
209 if (RT_SUCCESS(m_rcStatus) && RT_FAILURE_NP(rc))
210 m_rcStatus = rc;
211 return rc;
212}
213
214
215RTCRestClientResponseBase::PrimaryJsonCursorForBody::PrimaryJsonCursorForBody(RTJSONVAL hValue, const char *pszName,
216 RTCRestClientResponseBase *a_pThat) RT_NOEXCEPT
217 : RTCRestJsonPrimaryCursor(hValue, pszName, a_pThat->getErrInfoInternal())
218 , m_pThat(a_pThat)
219{
220}
221
222
223int RTCRestClientResponseBase::PrimaryJsonCursorForBody::addError(RTCRestJsonCursor const &a_rCursor, int a_rc,
224 const char *a_pszFormat, ...) RT_NOEXCEPT
225{
226 va_list va;
227 va_start(va, a_pszFormat);
228 char szPath[256];
229 m_pThat->addError(a_rc, "response body/%s: %N", getPath(a_rCursor, szPath, sizeof(szPath)), a_pszFormat, &va);
230 va_end(va);
231 return a_rc;
232}
233
234
235int RTCRestClientResponseBase::PrimaryJsonCursorForBody::unknownField(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT
236{
237 char szPath[256];
238 m_pThat->addError(VWRN_NOT_FOUND, "response body/%s: unknown field (type %s)",
239 getPath(a_rCursor, szPath, sizeof(szPath)), RTJsonValueTypeName(RTJsonValueGetType(a_rCursor.m_hValue)));
240 return VWRN_NOT_FOUND;
241}
242
243
244int RTCRestClientResponseBase::deserializeHeader(RTCRestObjectBase *a_pObj, const char *a_pchValue, size_t a_cchValue,
245 uint32_t a_fFlags, const char *a_pszErrorTag) RT_NOEXCEPT
246{
247 /*
248 * Start by checking the encoding and transfering the value to a RTCString object.
249 */
250 int rc = RTStrValidateEncodingEx(a_pchValue, a_cchValue, RTSTR_VALIDATE_ENCODING_EXACT_LENGTH);
251 if (RT_SUCCESS(rc))
252 {
253 RTCString strValue;
254 rc = strValue.assignNoThrow(a_pchValue, a_cchValue);
255 if (RT_SUCCESS(rc))
256 {
257 /*
258 * Try deserialize it.
259 */
260 RTERRINFOSTATIC ErrInfo;
261 rc = a_pObj->fromString(strValue, a_pszErrorTag, RTErrInfoInitStatic(&ErrInfo), a_fFlags);
262 if (RT_SUCCESS(rc))
263 { /* likely */ }
264 else if (RTErrInfoIsSet(&ErrInfo.Core))
265 addError(rc, "Error %Rrc parsing header field '%s': %s", rc, a_pszErrorTag, ErrInfo.Core.pszMsg);
266 else
267 addError(rc, "Error %Rrc parsing header field '%s'", rc, a_pszErrorTag);
268 }
269 }
270 else
271 {
272 addError(rc, "Error %Rrc validating value necoding of header field '%s': %.*Rhxs",
273 rc, a_pszErrorTag, a_cchValue, a_pchValue);
274 rc = VINF_SUCCESS; /* ignore */
275 }
276 return rc;
277}
278
279
280int RTCRestClientResponseBase::deserializeHeaderIntoMap(RTCRestStringMapBase *a_pMap, const char *a_pchField, size_t a_cchField,
281 const char *a_pchValue, size_t a_cchValue, uint32_t a_fFlags,
282 const char *a_pszErrorTag) RT_NOEXCEPT
283{
284 /*
285 * Start by checking the encoding of both the field and value,
286 * then transfering the value to a RTCString object.
287 */
288 int rc = RTStrValidateEncodingEx(a_pchField, a_cchField, RTSTR_VALIDATE_ENCODING_EXACT_LENGTH);
289 if (RT_SUCCESS(rc))
290 {
291 rc = RTStrValidateEncodingEx(a_pchValue, a_cchValue, RTSTR_VALIDATE_ENCODING_EXACT_LENGTH);
292 if (RT_SUCCESS(rc))
293 {
294 RTCString strValue;
295 rc = strValue.assignNoThrow(a_pchValue, a_cchValue);
296 if (RT_SUCCESS(rc))
297 {
298 /*
299 * Create a value object and put it into the map.
300 */
301 RTCRestObjectBase *pValue;
302 rc = a_pMap->putNewValue(&pValue, a_pchField, a_cchField);
303 if (RT_SUCCESS(rc))
304 {
305 /*
306 * Try deserialize the value.
307 */
308 RTERRINFOSTATIC ErrInfo;
309 rc = pValue->fromString(strValue, a_pszErrorTag, RTErrInfoInitStatic(&ErrInfo), a_fFlags);
310 if (RT_SUCCESS(rc))
311 { /* likely */ }
312 else if (RTErrInfoIsSet(&ErrInfo.Core))
313 addError(rc, "Error %Rrc parsing header field '%s' subfield '%.*s': %s",
314 rc, a_pszErrorTag, a_cchField, a_pchField, ErrInfo.Core.pszMsg);
315 else
316 addError(rc, "Error %Rrc parsing header field '%s' subfield '%.*s'",
317 rc, a_pszErrorTag, a_cchField, a_pchField);
318 }
319 }
320 }
321 else
322 {
323 addError(rc, "Error %Rrc validating value encoding of header field '%s': %.*Rhxs",
324 rc, a_pszErrorTag, a_cchValue, a_pchValue);
325 rc = VINF_SUCCESS; /* ignore */
326 }
327 }
328 else
329 {
330 addError(rc, "Error %Rrc validating sub-field encoding of header field '%s*': %.*Rhxs",
331 rc, a_pszErrorTag, a_cchField, a_pchField);
332 rc = VINF_SUCCESS; /* ignore */
333 }
334 return rc;
335}
336
337
338void RTCRestClientResponseBase::deserializeBody(const char *a_pchData, size_t a_cbData, const char *a_pszBodyName) RT_NOEXCEPT
339{
340 if (m_strContentType.startsWith("application/json"))
341 {
342 int rc = RTStrValidateEncodingEx(a_pchData, a_cbData, RTSTR_VALIDATE_ENCODING_EXACT_LENGTH);
343 if (RT_SUCCESS(rc))
344 {
345 RTERRINFOSTATIC ErrInfo;
346 RTJSONVAL hValue;
347 rc = RTJsonParseFromBuf(&hValue, (const uint8_t *)a_pchData, a_cbData, RTErrInfoInitStatic(&ErrInfo));
348 if (RT_SUCCESS(rc))
349 {
350 PrimaryJsonCursorForBody PrimaryCursor(hValue, a_pszBodyName, this); /* note: consumes hValue */
351 deserializeBodyFromJsonCursor(PrimaryCursor.m_Cursor);
352 }
353 else if (RTErrInfoIsSet(&ErrInfo.Core))
354 addError(rc, "Error %Rrc parsing server response as JSON (type %s): %s",
355 rc, a_pszBodyName, ErrInfo.Core.pszMsg);
356 else
357 addError(rc, "Error %Rrc parsing server response as JSON (type %s)", rc, a_pszBodyName);
358 }
359 else if (rc == VERR_INVALID_UTF8_ENCODING)
360 addError(VERR_REST_RESPONSE_INVALID_UTF8_ENCODING, "Invalid UTF-8 body encoding (object type %s; Content-Type: %s)",
361 a_pszBodyName, m_strContentType.c_str());
362 else if (rc == VERR_BUFFER_UNDERFLOW)
363 addError(VERR_REST_RESPONSE_EMBEDDED_ZERO_CHAR, "Embedded zero character in response (object type %s; Content-Type: %s)",
364 a_pszBodyName, m_strContentType.c_str());
365 else
366 addError(rc, "Unexpected body validation error (object type %s; Content-Type: %s): %Rrc",
367 a_pszBodyName, m_strContentType.c_str(), rc);
368 }
369 else
370 addError(VERR_REST_RESPONSE_CONTENT_TYPE_NOT_SUPPORTED, "Unsupported content type for '%s': %s",
371 a_pszBodyName, m_strContentType.c_str());
372}
373
374
375void RTCRestClientResponseBase::deserializeBodyFromJsonCursor(RTCRestJsonCursor const &a_rCursor) RT_NOEXCEPT
376{
377 a_rCursor.m_pPrimary->addError(a_rCursor, VERR_REST_INTERNAL_ERROR_8, "deserializeBodyFromJsonCursor must be overridden!");
378 AssertFailed();
379}
380
Note: See TracBrowser for help on using the repository browser.

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