VirtualBox

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

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

IPRT/rest: Started implemented more flexible handling of binary uploads and downloads. bugref:9167

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