VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rest/RTCRestClientRequestBase.cpp@ 74425

Last change on this file since 74425 was 74425, checked in by vboxsync, 7 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: 10.1 KB
Line 
1/* $Id: RTCRestClientRequestBase.cpp 74425 2018-09-23 15:41:48Z vboxsync $ */
2/** @file
3 * IPRT - C++ REST, RTCRestClientRequestBase 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/assert.h>
35#include <iprt/err.h>
36#include <iprt/cpp/restarray.h>
37#include <iprt/cpp/reststringmap.h>
38
39
40/**
41 * Default constructor.
42 */
43RTCRestClientRequestBase::RTCRestClientRequestBase() RT_NOEXCEPT
44 : m_fIsSet(0)
45 , m_fErrorSet(0)
46{
47}
48
49
50/**
51 * Copy constructor.
52 */
53RTCRestClientRequestBase::RTCRestClientRequestBase(RTCRestClientRequestBase const &a_rThat) RT_NOEXCEPT
54 : m_fIsSet(a_rThat.m_fIsSet)
55 , m_fErrorSet(a_rThat.m_fErrorSet)
56{
57}
58
59
60/**
61 * Destructor
62 */
63RTCRestClientRequestBase::~RTCRestClientRequestBase()
64{
65 /* nothing to do */
66}
67
68
69/**
70 * Copy assignment operator.
71 */
72RTCRestClientRequestBase &RTCRestClientRequestBase::operator=(RTCRestClientRequestBase const &a_rThat) RT_NOEXCEPT
73{
74 m_fIsSet = a_rThat.m_fIsSet;
75 m_fErrorSet = a_rThat.m_fErrorSet;
76 return *this;
77}
78
79
80int RTCRestClientRequestBase::doPathParameters(RTCString *a_pStrPath, const char *a_pszPathTemplate, size_t a_cchPathTemplate,
81 PATHPARAMDESC const *a_paPathParams, PATHPARAMSTATE *a_paPathParamStates,
82 size_t a_cPathParams) const RT_NOEXCEPT
83{
84 int rc = a_pStrPath->assignNoThrow(a_pszPathTemplate, a_cchPathTemplate);
85 AssertRCReturn(rc, rc);
86
87 /* Locate the sub-string to replace with values first: */
88 for (size_t i = 0; i < a_cPathParams; i++)
89 {
90 char const *psz = strstr(a_pszPathTemplate, a_paPathParams[i].pszName);
91 AssertReturn(psz, VERR_INTERNAL_ERROR_5);
92 a_paPathParamStates[i].offName = psz - a_pszPathTemplate;
93 }
94
95 /* Replace with actual values: */
96 RTCString strTmpVal;
97 for (size_t i = 0; i < a_cPathParams; i++)
98 {
99 AssertReturn( (a_paPathParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
100 != RTCRestObjectBase::kCollectionFormat_multi,
101 VERR_INTERNAL_ERROR_3);
102 AssertMsgReturn(a_paPathParamStates[i].pObj != NULL,
103 ("Path parameter '%s' is not set!\n", a_paPathParams[i].pszName),
104 VERR_REST_PATH_PARAMETER_NOT_SET);
105 AssertMsgReturn(m_fIsSet & RT_BIT_64(a_paPathParams[i].iBitNo),
106 ("Path parameter '%s' is not set!\n", a_paPathParams[i].pszName),
107 VERR_REST_PATH_PARAMETER_NOT_SET);
108
109 rc = a_paPathParamStates[i].pObj->toString(&strTmpVal, a_paPathParams[i].fFlags);
110 AssertRCReturn(rc, rc);
111
112 /* Replace. */
113 ssize_t cchAdjust = strTmpVal.length() - a_paPathParams[i].cchName;
114 rc = a_pStrPath->replaceNoThrow(a_paPathParamStates[i].offName, a_paPathParams[i].cchName, strTmpVal);
115 AssertRCReturn(rc, rc);
116
117 /* Adjust subsequent fields. */
118 if (cchAdjust != 0)
119 for (size_t j = i + 1; j < a_cPathParams; j++)
120 if (a_paPathParamStates[j].offName > a_paPathParamStates[i].offName)
121 a_paPathParamStates[j].offName += cchAdjust;
122 }
123
124 return VINF_SUCCESS;
125}
126
127
128int RTCRestClientRequestBase::doQueryParameters(RTCString *a_pStrQuery, QUERYPARAMDESC const *a_paQueryParams,
129 RTCRestObjectBase const **a_papQueryParamObjs, size_t a_cQueryParams) const RT_NOEXCEPT
130{
131 RTCString strTmpVal;
132 char chSep = a_pStrQuery->isEmpty() ? '?' : '&';
133 for (size_t i = 0; i < a_cQueryParams; i++)
134 {
135 if ( a_paQueryParams[i].fRequired
136 || (m_fIsSet & RT_BIT_64(a_paQueryParams[i].iBitNo)) )
137 {
138 AssertMsgReturn(a_papQueryParamObjs[i] != NULL,
139 ("Required query parameter '%s' is not set!\n", a_paQueryParams[i].pszName),
140 VERR_REST_REQUIRED_QUERY_PARAMETER_NOT_SET);
141 AssertMsgReturn(m_fIsSet & RT_BIT_64(a_paQueryParams[i].iBitNo),
142 ("Required query parameter '%s' is not set!\n", a_paQueryParams[i].pszName),
143 VERR_REST_REQUIRED_QUERY_PARAMETER_NOT_SET);
144
145 if ( (a_paQueryParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
146 != RTCRestObjectBase::kCollectionFormat_multi)
147 {
148 int rc = a_papQueryParamObjs[i]->toString(&strTmpVal, a_paQueryParams[i].fFlags);
149 AssertRCReturn(rc, rc);
150
151 rc = a_pStrQuery->appendPrintfNoThrow("%c%RMpq=%RMpq", chSep, a_paQueryParams[i].pszName, strTmpVal.c_str());
152 AssertRCReturn(rc, rc);
153
154 chSep = '&';
155 }
156 else
157 {
158 /*
159 * Enumerate array and add 'name=element' for each element in it.
160 */
161 AssertReturn(a_papQueryParamObjs[i]->typeClass() == RTCRestObjectBase::kTypeClass_Array,
162 VERR_REST_INTERNAL_ERROR_2);
163 RTCRestArrayBase const *pArray = (RTCRestArrayBase const *)a_papQueryParamObjs[i];
164 for (size_t j = 0; j < pArray->size(); j++)
165 {
166 RTCRestObjectBase const *pObj = pArray->atBase(j);
167 int rc = pObj->toString(&strTmpVal, a_paQueryParams[i].fFlags & ~RTCRestObjectBase::kCollectionFormat_Mask);
168 AssertRCReturn(rc, rc);
169
170 rc = a_pStrQuery->appendPrintfNoThrow("%c%RMpq=%RMpq", chSep, a_paQueryParams[i].pszName, strTmpVal.c_str());
171 AssertRCReturn(rc, rc);
172
173 chSep = '&';
174 }
175 }
176 }
177 }
178 return VINF_SUCCESS;
179}
180
181
182int RTCRestClientRequestBase::doHeaderParameters(RTHTTP a_hHttp, HEADERPARAMDESC const *a_paHeaderParams,
183 RTCRestObjectBase const **a_papHeaderParamObjs, size_t a_cHeaderParams) const RT_NOEXCEPT
184{
185 RTCString strTmpVal;
186 for (size_t i = 0; i < a_cHeaderParams; i++)
187 {
188 AssertReturn( (a_paHeaderParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
189 != RTCRestObjectBase::kCollectionFormat_multi,
190 VERR_INTERNAL_ERROR_3);
191
192 if ( a_paHeaderParams[i].fRequired
193 || (m_fIsSet & RT_BIT_64(a_paHeaderParams[i].iBitNo)) )
194 {
195 AssertMsgReturn(m_fIsSet & RT_BIT_64(a_paHeaderParams[i].iBitNo),
196 ("Required header parameter '%s' is not set!\n", a_paHeaderParams[i].pszName),
197 VERR_REST_REQUIRED_HEADER_PARAMETER_NOT_SET);
198 AssertMsgReturn(a_papHeaderParamObjs[i] != NULL,
199 ("Required header parameter '%s' is not set!\n", a_paHeaderParams[i].pszName),
200 VERR_REST_REQUIRED_HEADER_PARAMETER_NOT_SET);
201
202 if (!a_paHeaderParams[i].fMapCollection)
203 {
204 int rc = a_papHeaderParamObjs[i]->toString(&strTmpVal, a_paHeaderParams[i].fFlags);
205 AssertRCReturn(rc, rc);
206
207 rc = RTHttpAddHeader(a_hHttp, a_paHeaderParams[i].pszName, strTmpVal.c_str(), strTmpVal.length(),
208 RTHTTPADDHDR_F_BACK);
209 AssertRCReturn(rc, rc);
210 }
211 else if (!a_papHeaderParamObjs[i]->isNull())
212 {
213 /*
214 * Enumerate the map and produce a series of head fields on the form:
215 * (a_paHeaderParams[i].pszName + key): value.toString()
216 */
217 AssertReturn(a_papHeaderParamObjs[i]->typeClass() == RTCRestObjectBase::kTypeClass_StringMap,
218 VERR_REST_INTERNAL_ERROR_1);
219 RTCRestStringMapBase const *pMap = (RTCRestStringMapBase const *)a_papHeaderParamObjs[i];
220 const size_t cchName = strlen(a_paHeaderParams[i].pszName);
221 Assert(a_paHeaderParams[i].pszName[cchName - 1] != '*');
222 RTCString strTmpName;
223 for (RTCRestStringMapBase::ConstIterator it = pMap->begin(); it != pMap->end(); ++it)
224 {
225 int rc = strTmpName.assignNoThrow(a_paHeaderParams[i].pszName, cchName);
226 AssertRCReturn(rc, rc);
227 rc = strTmpName.appendNoThrow(it.getKey());
228 AssertRCReturn(rc, rc);
229
230 rc = it.getValue()->toString(&strTmpVal, a_paHeaderParams[i].fFlags);
231 AssertRCReturn(rc, rc);
232
233 rc = RTHttpAddHeader(a_hHttp, strTmpName.c_str(), strTmpVal.c_str(), strTmpVal.length(),
234 RTHTTPADDHDR_F_BACK);
235 AssertRCReturn(rc, rc);
236 }
237 }
238 else
239 Assert(!a_paHeaderParams[i].fRequired);
240 }
241 }
242 return VINF_SUCCESS;
243}
244
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