VirtualBox

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

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

IPRT/rest: RTCRestClientApiBase::reinitHttpInstance - RTHTTP no longer
gets confused about proxy settings after RTHttpReset(), so drop the
workaround that re-created it for each roundtrip to the cloud.
Speeds things up quite a bit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1/* $Id: RTCRestClientApiBase.cpp 79862 2019-07-18 16:05:52Z 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 return RTHttpReset(m_hHttp, 0 /*fFlags*/);
179
180 int rc = RTHttpCreate(&m_hHttp);
181 if (RT_FAILURE(rc))
182 m_hHttp = NIL_RTHTTP;
183 return rc;
184}
185
186
187int RTCRestClientApiBase::xmitReady(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
188 RTCString const &a_rStrXmitBody, uint32_t a_fFlags) RT_NOEXCEPT
189{
190 RT_NOREF(a_hHttp, a_rStrFullUrl, a_enmHttpMethod, a_rStrXmitBody, a_fFlags);
191 return VINF_SUCCESS;
192}
193
194
195int RTCRestClientApiBase::doCall(RTCRestClientRequestBase const &a_rRequest, RTHTTPMETHOD a_enmHttpMethod,
196 RTCRestClientResponseBase *a_pResponse, const char *a_pszMethod, uint32_t a_fFlags) RT_NOEXCEPT
197{
198 LogFlow(("doCall: %s %s\n", a_pszMethod, RTHttpMethodName(a_enmHttpMethod)));
199
200
201 /*
202 * Reset the response object (allowing reuse of such) and check the request
203 * object for assignment errors.
204 */
205 int rc;
206 RTHTTP hHttp = NIL_RTHTTP;
207
208 a_pResponse->reset();
209 if (!a_rRequest.hasAssignmentErrors())
210 {
211 /*
212 * Initialize the HTTP instance.
213 */
214 rc = reinitHttpInstance();
215 if (RT_SUCCESS(rc))
216 {
217 hHttp = m_hHttp;
218 Assert(hHttp != NIL_RTHTTP);
219
220 /*
221 * Prepare the response side.
222 */
223 rc = a_pResponse->receivePrepare(hHttp);
224 if (RT_SUCCESS(rc))
225 {
226 /*
227 * Prepare the request for the transmission.
228 */
229 RTCString strExtraPath;
230 RTCString strQuery;
231 RTCString strXmitBody;
232 rc = a_rRequest.xmitPrepare(&strExtraPath, &strQuery, hHttp, &strXmitBody);
233 if (RT_SUCCESS(rc))
234 {
235 /*
236 * Construct the full URL.
237 */
238 RTCString strFullUrl;
239 rc = strFullUrl.assignNoThrow(getServerUrl());
240 if (strExtraPath.isNotEmpty())
241 {
242 if (!strExtraPath.startsWith("/") && !strFullUrl.endsWith("/") && RT_SUCCESS(rc))
243 rc = strFullUrl.appendNoThrow('/');
244 if (RT_SUCCESS(rc))
245 rc = strFullUrl.appendNoThrow(strExtraPath);
246 strExtraPath.setNull();
247 }
248 if (strQuery.isNotEmpty())
249 {
250 Assert(strQuery.startsWith("?"));
251 rc = strFullUrl.appendNoThrow(strQuery);
252 strQuery.setNull();
253 }
254 if (RT_SUCCESS(rc))
255 {
256 rc = xmitReady(hHttp, strFullUrl, a_enmHttpMethod, strXmitBody, a_fFlags);
257 if (RT_SUCCESS(rc))
258 {
259 /*
260 * Perform HTTP request.
261 */
262 uint32_t uHttpStatus = 0;
263 size_t cbBody = 0;
264 void *pvBody = NULL;
265 rc = RTHttpPerform(hHttp, strFullUrl.c_str(), a_enmHttpMethod,
266 strXmitBody.c_str(), strXmitBody.length(),
267 &uHttpStatus, NULL /*ppvHdrs*/, NULL /*pcbHdrs*/, &pvBody, &cbBody);
268 if (RT_SUCCESS(rc))
269 {
270 a_rRequest.xmitComplete(uHttpStatus, hHttp);
271
272 /*
273 * Do response processing.
274 */
275 a_pResponse->receiveComplete(uHttpStatus, hHttp);
276 a_pResponse->consumeBody((const char *)pvBody, cbBody);
277 if (pvBody)
278 RTHttpFreeResponse(pvBody);
279 a_pResponse->receiveFinal();
280
281 return a_pResponse->getStatus();
282 }
283 }
284 }
285 }
286 a_rRequest.xmitComplete(rc, hHttp);
287 }
288 }
289 }
290 else
291 rc = VERR_NO_MEMORY;
292
293 a_pResponse->receiveComplete(rc, hHttp);
294 RT_NOREF_PV(a_pszMethod);
295
296 return a_pResponse->getStatus();
297}
298
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