VirtualBox

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

Last change on this file since 80346 was 79888, checked in by vboxsync, 5 years ago

IPRT/rest: doQueryParameters - adjust previous, use %RMpa that encodes
everything (%RMpf encodes spaces as + which is wrong here (or the +
must be percent encoded)).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1/* $Id: RTCRestClientRequestBase.cpp 79888 2019-07-19 13:22:20Z vboxsync $ */
2/** @file
3 * IPRT - C++ REST, RTCRestClientRequestBase implementation.
4 */
5
6/*
7 * Copyright (C) 2018-2019 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 for (size_t i = 0; i < a_cPathParams; i++)
97 {
98 AssertReturn( (a_paPathParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
99 != RTCRestObjectBase::kCollectionFormat_multi,
100 VERR_INTERNAL_ERROR_3);
101 AssertMsgReturn(a_paPathParamStates[i].pObj != NULL,
102 ("Path parameter '%s' is not set!\n", a_paPathParams[i].pszName),
103 VERR_REST_PATH_PARAMETER_NOT_SET);
104 AssertMsgReturn(m_fIsSet & RT_BIT_64(a_paPathParams[i].iBitNo),
105 ("Path parameter '%s' is not set!\n", a_paPathParams[i].pszName),
106 VERR_REST_PATH_PARAMETER_NOT_SET);
107
108 RTCString strPathParam;
109 rc = a_paPathParamStates[i].pObj->toString(&strPathParam, a_paPathParams[i].fFlags);
110 AssertRCReturn(rc, rc);
111
112 RTCString strTmpVal;
113 rc = strTmpVal.printfNoThrow("%RMpa", strPathParam.c_str()); /* urlencode */
114 AssertRCReturn(rc, rc);
115
116 /* Replace. */
117 ssize_t cchAdjust = strTmpVal.length() - a_paPathParams[i].cchName;
118 rc = a_pStrPath->replaceNoThrow(a_paPathParamStates[i].offName, a_paPathParams[i].cchName, strTmpVal);
119 AssertRCReturn(rc, rc);
120
121 /* Adjust subsequent fields. */
122 if (cchAdjust != 0)
123 for (size_t j = i + 1; j < a_cPathParams; j++)
124 if (a_paPathParamStates[j].offName > a_paPathParamStates[i].offName)
125 a_paPathParamStates[j].offName += cchAdjust;
126 }
127
128 return VINF_SUCCESS;
129}
130
131
132int RTCRestClientRequestBase::doQueryParameters(RTCString *a_pStrQuery, QUERYPARAMDESC const *a_paQueryParams,
133 RTCRestObjectBase const **a_papQueryParamObjs, size_t a_cQueryParams) const RT_NOEXCEPT
134{
135 RTCString strTmpVal;
136 char chSep = a_pStrQuery->isEmpty() ? '?' : '&';
137 for (size_t i = 0; i < a_cQueryParams; i++)
138 {
139 if ( a_paQueryParams[i].fRequired
140 || (m_fIsSet & RT_BIT_64(a_paQueryParams[i].iBitNo)) )
141 {
142 AssertMsgReturn(a_papQueryParamObjs[i] != NULL,
143 ("Required query parameter '%s' is not set!\n", a_paQueryParams[i].pszName),
144 VERR_REST_REQUIRED_QUERY_PARAMETER_NOT_SET);
145 AssertMsgReturn(m_fIsSet & RT_BIT_64(a_paQueryParams[i].iBitNo),
146 ("Required query parameter '%s' is not set!\n", a_paQueryParams[i].pszName),
147 VERR_REST_REQUIRED_QUERY_PARAMETER_NOT_SET);
148
149 if ( (a_paQueryParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
150 != RTCRestObjectBase::kCollectionFormat_multi)
151 {
152 int rc = a_papQueryParamObjs[i]->toString(&strTmpVal, a_paQueryParams[i].fFlags);
153 AssertRCReturn(rc, rc);
154
155 rc = a_pStrQuery->appendPrintfNoThrow("%c%RMpa=%RMpa", chSep, a_paQueryParams[i].pszName, strTmpVal.c_str());
156 AssertRCReturn(rc, rc);
157
158 chSep = '&';
159 }
160 else
161 {
162 /*
163 * Enumerate array and add 'name=element' for each element in it.
164 */
165 AssertReturn(a_papQueryParamObjs[i]->typeClass() == RTCRestObjectBase::kTypeClass_Array,
166 VERR_REST_INTERNAL_ERROR_2);
167 RTCRestArrayBase const *pArray = (RTCRestArrayBase const *)a_papQueryParamObjs[i];
168 for (size_t j = 0; j < pArray->size(); j++)
169 {
170 RTCRestObjectBase const *pObj = pArray->atBase(j);
171 int rc = pObj->toString(&strTmpVal, a_paQueryParams[i].fFlags & ~RTCRestObjectBase::kCollectionFormat_Mask);
172 AssertRCReturn(rc, rc);
173
174 rc = a_pStrQuery->appendPrintfNoThrow("%c%RMpa=%RMpa", chSep, a_paQueryParams[i].pszName, strTmpVal.c_str());
175 AssertRCReturn(rc, rc);
176
177 chSep = '&';
178 }
179 }
180 }
181 }
182 return VINF_SUCCESS;
183}
184
185
186int RTCRestClientRequestBase::doHeaderParameters(RTHTTP a_hHttp, HEADERPARAMDESC const *a_paHeaderParams,
187 RTCRestObjectBase const **a_papHeaderParamObjs, size_t a_cHeaderParams) const RT_NOEXCEPT
188{
189 RTCString strTmpVal;
190 for (size_t i = 0; i < a_cHeaderParams; i++)
191 {
192 AssertReturn( (a_paHeaderParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
193 != RTCRestObjectBase::kCollectionFormat_multi,
194 VERR_INTERNAL_ERROR_3);
195
196 if ( a_paHeaderParams[i].fRequired
197 || (m_fIsSet & RT_BIT_64(a_paHeaderParams[i].iBitNo)) )
198 {
199 AssertMsgReturn(m_fIsSet & RT_BIT_64(a_paHeaderParams[i].iBitNo),
200 ("Required header parameter '%s' is not set!\n", a_paHeaderParams[i].pszName),
201 VERR_REST_REQUIRED_HEADER_PARAMETER_NOT_SET);
202 AssertMsgReturn(a_papHeaderParamObjs[i] != NULL,
203 ("Required header parameter '%s' is not set!\n", a_paHeaderParams[i].pszName),
204 VERR_REST_REQUIRED_HEADER_PARAMETER_NOT_SET);
205
206 if (!a_paHeaderParams[i].fMapCollection)
207 {
208 int rc = a_papHeaderParamObjs[i]->toString(&strTmpVal, a_paHeaderParams[i].fFlags);
209 AssertRCReturn(rc, rc);
210
211 rc = RTHttpAddHeader(a_hHttp, a_paHeaderParams[i].pszName, strTmpVal.c_str(), strTmpVal.length(),
212 RTHTTPADDHDR_F_BACK);
213 AssertRCReturn(rc, rc);
214 }
215 else if (!a_papHeaderParamObjs[i]->isNull())
216 {
217 /*
218 * Enumerate the map and produce a series of head fields on the form:
219 * (a_paHeaderParams[i].pszName + key): value.toString()
220 */
221 AssertReturn(a_papHeaderParamObjs[i]->typeClass() == RTCRestObjectBase::kTypeClass_StringMap,
222 VERR_REST_INTERNAL_ERROR_1);
223 RTCRestStringMapBase const *pMap = (RTCRestStringMapBase const *)a_papHeaderParamObjs[i];
224 const size_t cchName = strlen(a_paHeaderParams[i].pszName);
225 Assert(a_paHeaderParams[i].pszName[cchName - 1] != '*');
226 RTCString strTmpName;
227 for (RTCRestStringMapBase::ConstIterator it = pMap->begin(); it != pMap->end(); ++it)
228 {
229 int rc = strTmpName.assignNoThrow(a_paHeaderParams[i].pszName, cchName);
230 AssertRCReturn(rc, rc);
231 rc = strTmpName.appendNoThrow(it.getKey());
232 AssertRCReturn(rc, rc);
233
234 rc = it.getValue()->toString(&strTmpVal, a_paHeaderParams[i].fFlags);
235 AssertRCReturn(rc, rc);
236
237 rc = RTHttpAddHeader(a_hHttp, strTmpName.c_str(), strTmpVal.c_str(), strTmpVal.length(),
238 RTHTTPADDHDR_F_BACK);
239 AssertRCReturn(rc, rc);
240 }
241 }
242 else
243 Assert(!a_paHeaderParams[i].fRequired);
244 }
245 }
246 return VINF_SUCCESS;
247}
248
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