VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/rest/RTCRestClientApiBase.cpp@ 78222

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

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: RTCRestClientApiBase.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - C++ REST, RTCRestClientApiBase 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/ctype.h>
36#include <iprt/errcore.h>
37#include <iprt/http.h>
38#include <iprt/log.h>
39#include <iprt/uri.h>
40
41
42/**
43 * Default constructor.
44 */
45RTCRestClientApiBase::RTCRestClientApiBase() RT_NOEXCEPT
46 : m_hHttp(NIL_RTHTTP)
47{
48}
49
50
51/**
52 * The destructor.
53 */
54RTCRestClientApiBase::~RTCRestClientApiBase()
55{
56 if (m_hHttp != NIL_RTHTTP)
57 {
58 int rc = RTHttpDestroy(m_hHttp);
59 AssertRC(rc);
60 m_hHttp = NIL_RTHTTP;
61 }
62}
63
64
65const char *RTCRestClientApiBase::getServerUrl(void) const RT_NOEXCEPT
66{
67 if (m_strServerUrl.isEmpty())
68 return getDefaultServerUrl();
69 return m_strServerUrl.c_str();
70}
71
72
73int RTCRestClientApiBase::setServerUrl(const char *a_pszUrl) RT_NOEXCEPT
74{
75#ifdef RT_STRICT
76 if (a_pszUrl)
77 {
78 RTURIPARSED Parsed;
79 int rc = RTUriParse(a_pszUrl, &Parsed);
80 AssertRC(rc);
81 }
82#endif
83
84 return m_strServerUrl.assignNoThrow(a_pszUrl);
85}
86
87
88int RTCRestClientApiBase::setServerUrlPart(const char *a_pszServerUrl, size_t a_offDst, size_t a_cchDst,
89 const char *a_pszSrc, size_t a_cchSrc) RT_NOEXCEPT
90{
91 if ( a_cchDst == a_cchSrc
92 && memcmp(&a_pszServerUrl[0], a_pszSrc, a_cchSrc) == 0)
93 return VINF_SUCCESS;
94
95 if (m_strServerUrl.isEmpty())
96 {
97 int rc = m_strServerUrl.assignNoThrow(a_pszServerUrl);
98 AssertRCReturn(rc, rc);
99 }
100 return m_strServerUrl.replaceNoThrow(a_offDst, a_cchDst, a_pszSrc, a_cchSrc);
101}
102
103
104int RTCRestClientApiBase::setServerScheme(const char *a_pszScheme) RT_NOEXCEPT
105{
106 /*
107 * Validate.
108 */
109 AssertReturn(a_pszScheme, VERR_INVALID_POINTER);
110 size_t const cchScheme = strlen(a_pszScheme);
111 AssertReturn(cchScheme > 0, VERR_INVALID_PARAMETER);
112 Assert(cchScheme < 16);
113#ifdef RT_STRICT
114 for (size_t i = 0; i < cchScheme; i++)
115 Assert(RT_C_IS_ALNUM(a_pszScheme[i]));
116#endif
117
118 /*
119 * Parse, compare & replace.
120 */
121 RTURIPARSED Parsed;
122 const char *pszUrl = getServerUrl();
123 int rc = RTUriParse(pszUrl, &Parsed);
124 AssertRCReturn(rc, rc);
125 return setServerUrlPart(pszUrl, 0, Parsed.cchScheme, a_pszScheme, cchScheme);
126}
127
128
129int RTCRestClientApiBase::setServerAuthority(const char *a_pszAuthority) RT_NOEXCEPT
130{
131 /*
132 * Validate.
133 */
134 AssertReturn(a_pszAuthority, VERR_INVALID_POINTER);
135 size_t const cchAuthority = strlen(a_pszAuthority);
136 AssertReturn(cchAuthority > 0, VERR_INVALID_PARAMETER);
137 Assert(memchr(a_pszAuthority, '/', cchAuthority) == NULL);
138 Assert(memchr(a_pszAuthority, '\\', cchAuthority) == NULL);
139 Assert(memchr(a_pszAuthority, '#', cchAuthority) == NULL);
140 Assert(memchr(a_pszAuthority, '?', cchAuthority) == NULL);
141
142 /*
143 * Parse, compare & replace.
144 */
145 RTURIPARSED Parsed;
146 const char *pszUrl = getServerUrl();
147 int rc = RTUriParse(pszUrl, &Parsed);
148 AssertRCReturn(rc, rc);
149 return setServerUrlPart(pszUrl, Parsed.offAuthority, Parsed.cchAuthority, a_pszAuthority, cchAuthority);
150}
151
152
153int RTCRestClientApiBase::setServerBasePath(const char *a_pszBasePath) RT_NOEXCEPT
154{
155 /*
156 * Validate.
157 */
158 AssertReturn(a_pszBasePath, VERR_INVALID_POINTER);
159 size_t const cchBasePath = strlen(a_pszBasePath);
160 AssertReturn(cchBasePath > 0, VERR_INVALID_PARAMETER);
161 Assert(memchr(a_pszBasePath, '?', cchBasePath) == NULL);
162 Assert(memchr(a_pszBasePath, '#', cchBasePath) == NULL);
163
164 /*
165 * Parse, compare & replace.
166 */
167 RTURIPARSED Parsed;
168 const char *pszUrl = getServerUrl();
169 int rc = RTUriParse(pszUrl, &Parsed);
170 AssertRCReturn(rc, rc);
171 return setServerUrlPart(pszUrl, Parsed.offPath, Parsed.cchPath, a_pszBasePath, cchBasePath);
172}
173
174
175int RTCRestClientApiBase::reinitHttpInstance() RT_NOEXCEPT
176{
177 if (m_hHttp != NIL_RTHTTP)
178 {
179#if 0 /** @todo XXX: disable for now as it causes the RTHTTP handle state and curl state to get out of sync. */
180 return RTHttpReset(m_hHttp, 0 /*fFlags*/);
181#else
182 RTHttpDestroy(m_hHttp);
183 m_hHttp = NIL_RTHTTP;
184#endif
185 }
186
187 int rc = RTHttpCreate(&m_hHttp);
188 if (RT_FAILURE(rc))
189 m_hHttp = NIL_RTHTTP;
190 return rc;
191}
192
193
194int RTCRestClientApiBase::xmitReady(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
195 RTCString const &a_rStrXmitBody, uint32_t a_fFlags) RT_NOEXCEPT
196{
197 RT_NOREF(a_hHttp, a_rStrFullUrl, a_enmHttpMethod, a_rStrXmitBody, a_fFlags);
198 return VINF_SUCCESS;
199}
200
201
202int RTCRestClientApiBase::doCall(RTCRestClientRequestBase const &a_rRequest, RTHTTPMETHOD a_enmHttpMethod,
203 RTCRestClientResponseBase *a_pResponse, const char *a_pszMethod, uint32_t a_fFlags) RT_NOEXCEPT
204{
205 LogFlow(("doCall: %s %s\n", a_pszMethod, RTHttpMethodName(a_enmHttpMethod)));
206
207
208 /*
209 * Reset the response object (allowing reuse of such) and check the request
210 * object for assignment errors.
211 */
212 int rc;
213 RTHTTP hHttp = NIL_RTHTTP;
214
215 a_pResponse->reset();
216 if (!a_rRequest.hasAssignmentErrors())
217 {
218 /*
219 * Initialize the HTTP instance.
220 */
221 rc = reinitHttpInstance();
222 if (RT_SUCCESS(rc))
223 {
224 hHttp = m_hHttp;
225 Assert(hHttp != NIL_RTHTTP);
226
227 /*
228 * Prepare the response side.
229 */
230 rc = a_pResponse->receivePrepare(hHttp);
231 if (RT_SUCCESS(rc))
232 {
233 /*
234 * Prepare the request for the transmission.
235 */
236 RTCString strExtraPath;
237 RTCString strQuery;
238 RTCString strXmitBody;
239 rc = a_rRequest.xmitPrepare(&strExtraPath, &strQuery, hHttp, &strXmitBody);
240 if (RT_SUCCESS(rc))
241 {
242 /*
243 * Construct the full URL.
244 */
245 RTCString strFullUrl;
246 rc = strFullUrl.assignNoThrow(getServerUrl());
247 if (strExtraPath.isNotEmpty())
248 {
249 if (!strExtraPath.startsWith("/") && !strFullUrl.endsWith("/") && RT_SUCCESS(rc))
250 rc = strFullUrl.appendNoThrow('/');
251 if (RT_SUCCESS(rc))
252 rc = strFullUrl.appendNoThrow(strExtraPath);
253 strExtraPath.setNull();
254 }
255 if (strQuery.isNotEmpty())
256 {
257 Assert(strQuery.startsWith("?"));
258 rc = strFullUrl.appendNoThrow(strQuery);
259 strQuery.setNull();
260 }
261 if (RT_SUCCESS(rc))
262 {
263 rc = xmitReady(hHttp, strFullUrl, a_enmHttpMethod, strXmitBody, a_fFlags);
264 if (RT_SUCCESS(rc))
265 {
266 /*
267 * Perform HTTP request.
268 */
269 uint32_t uHttpStatus = 0;
270 size_t cbBody = 0;
271 void *pvBody = NULL;
272 rc = RTHttpPerform(hHttp, strFullUrl.c_str(), a_enmHttpMethod,
273 strXmitBody.c_str(), strXmitBody.length(),
274 &uHttpStatus, NULL /*ppvHdrs*/, NULL /*pcbHdrs*/, &pvBody, &cbBody);
275 if (RT_SUCCESS(rc))
276 {
277 a_rRequest.xmitComplete(uHttpStatus, hHttp);
278
279 /*
280 * Do response processing.
281 */
282 a_pResponse->receiveComplete(uHttpStatus, hHttp);
283 if (pvBody)
284 {
285 a_pResponse->consumeBody((const char *)pvBody, cbBody);
286 RTHttpFreeResponse(pvBody);
287 }
288 a_pResponse->receiveFinal();
289
290 return a_pResponse->getStatus();
291 }
292 }
293 }
294 }
295 a_rRequest.xmitComplete(rc, hHttp);
296 }
297 }
298 }
299 else
300 rc = VERR_NO_MEMORY;
301
302 a_pResponse->receiveComplete(rc, hHttp);
303 RT_NOREF_PV(a_pszMethod);
304
305 return a_pResponse->getStatus();
306}
307
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