summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/rest/RTCRestClientApiBase.cpp
blob: e865f97a3d65c6ca1089dcfabcc8c2136f883caf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* $Id: RTCRestClientApiBase.cpp $ */
/** @file
 * IPRT - C++ REST, RTCRestClientApiBase implementation.
 */

/*
 * Copyright (C) 2018-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
 * in the VirtualBox distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 *
 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_REST
#include <iprt/cpp/restclient.h>

#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/errcore.h>
#include <iprt/http.h>
#include <iprt/log.h>
#include <iprt/uri.h>


/**
 * Default constructor.
 */
RTCRestClientApiBase::RTCRestClientApiBase() RT_NOEXCEPT
    : m_hHttp(NIL_RTHTTP)
{
}


/**
 * The destructor.
 */
RTCRestClientApiBase::~RTCRestClientApiBase()
{
    if (m_hHttp != NIL_RTHTTP)
    {
        int rc = RTHttpDestroy(m_hHttp);
        AssertRC(rc);
        m_hHttp = NIL_RTHTTP;
    }
}


int RTCRestClientApiBase::setCAFile(const char *pcszCAFile) RT_NOEXCEPT
{
    return m_strCAFile.assignNoThrow(pcszCAFile);
}


int RTCRestClientApiBase::setCAFile(const RTCString &strCAFile) RT_NOEXCEPT
{
    return m_strCAFile.assignNoThrow(strCAFile);
}


const char *RTCRestClientApiBase::getServerUrl(void) const RT_NOEXCEPT
{
    if (m_strServerUrl.isEmpty())
        return getDefaultServerUrl();
    return m_strServerUrl.c_str();
}


int RTCRestClientApiBase::setServerUrl(const char *a_pszUrl) RT_NOEXCEPT
{
#ifdef RT_STRICT
    if (a_pszUrl)
    {
        RTURIPARSED Parsed;
        int rc = RTUriParse(a_pszUrl, &Parsed);
        AssertRC(rc);
    }
#endif

    return m_strServerUrl.assignNoThrow(a_pszUrl);
}


int RTCRestClientApiBase::setServerUrlPart(const char *a_pszServerUrl, size_t a_offDst, size_t a_cchDst,
                                           const char *a_pszSrc, size_t a_cchSrc) RT_NOEXCEPT
{
    if (   a_cchDst == a_cchSrc
        && memcmp(&a_pszServerUrl[0], a_pszSrc, a_cchSrc) == 0)
        return VINF_SUCCESS;

    if (m_strServerUrl.isEmpty())
    {
        int rc = m_strServerUrl.assignNoThrow(a_pszServerUrl);
        AssertRCReturn(rc, rc);
    }
    return m_strServerUrl.replaceNoThrow(a_offDst, a_cchDst, a_pszSrc, a_cchSrc);
}


int RTCRestClientApiBase::setServerScheme(const char *a_pszScheme) RT_NOEXCEPT
{
    /*
     * Validate.
     */
    AssertReturn(a_pszScheme, VERR_INVALID_POINTER);
    size_t const cchScheme = strlen(a_pszScheme);
    AssertReturn(cchScheme > 0, VERR_INVALID_PARAMETER);
    Assert(cchScheme < 16);
#ifdef RT_STRICT
    for (size_t i = 0; i < cchScheme; i++)
        Assert(RT_C_IS_ALNUM(a_pszScheme[i]));
#endif

    /*
     * Parse, compare & replace.
     */
    RTURIPARSED Parsed;
    const char *pszUrl = getServerUrl();
    int rc = RTUriParse(pszUrl, &Parsed);
    AssertRCReturn(rc, rc);
    return setServerUrlPart(pszUrl, 0, Parsed.cchScheme, a_pszScheme, cchScheme);
}


int RTCRestClientApiBase::setServerAuthority(const char *a_pszAuthority) RT_NOEXCEPT
{
    /*
     * Validate.
     */
    AssertReturn(a_pszAuthority, VERR_INVALID_POINTER);
    size_t const cchAuthority = strlen(a_pszAuthority);
    AssertReturn(cchAuthority > 0, VERR_INVALID_PARAMETER);
    Assert(memchr(a_pszAuthority, '/', cchAuthority) == NULL);
    Assert(memchr(a_pszAuthority, '\\', cchAuthority) == NULL);
    Assert(memchr(a_pszAuthority, '#', cchAuthority) == NULL);
    Assert(memchr(a_pszAuthority, '?', cchAuthority) == NULL);

    /*
     * Parse, compare & replace.
     */
    RTURIPARSED Parsed;
    const char *pszUrl = getServerUrl();
    int rc = RTUriParse(pszUrl, &Parsed);
    AssertRCReturn(rc, rc);
    return setServerUrlPart(pszUrl, Parsed.offAuthority, Parsed.cchAuthority, a_pszAuthority, cchAuthority);
}


int RTCRestClientApiBase::setServerBasePath(const char *a_pszBasePath) RT_NOEXCEPT
{
    /*
     * Validate.
     */
    AssertReturn(a_pszBasePath, VERR_INVALID_POINTER);
    size_t const cchBasePath = strlen(a_pszBasePath);
    AssertReturn(cchBasePath > 0, VERR_INVALID_PARAMETER);
    Assert(memchr(a_pszBasePath, '?', cchBasePath) == NULL);
    Assert(memchr(a_pszBasePath, '#', cchBasePath) == NULL);

    /*
     * Parse, compare & replace.
     */
    RTURIPARSED Parsed;
    const char *pszUrl = getServerUrl();
    int rc = RTUriParse(pszUrl, &Parsed);
    AssertRCReturn(rc, rc);
    return setServerUrlPart(pszUrl, Parsed.offPath, Parsed.cchPath, a_pszBasePath, cchBasePath);
}


int RTCRestClientApiBase::reinitHttpInstance() RT_NOEXCEPT
{
    if (m_hHttp != NIL_RTHTTP)
        return RTHttpReset(m_hHttp, 0 /*fFlags*/);

    int rc = RTHttpCreate(&m_hHttp);
    if (RT_SUCCESS(rc) && m_strCAFile.isNotEmpty())
        rc = RTHttpSetCAFile(m_hHttp, m_strCAFile.c_str());

    if (RT_FAILURE(rc) && m_hHttp != NIL_RTHTTP)
    {
        RTHttpDestroy(m_hHttp);
        m_hHttp = NIL_RTHTTP;
    }
    return rc;
}


int RTCRestClientApiBase::xmitReady(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
                                    RTCString const &a_rStrXmitBody, uint32_t a_fFlags) RT_NOEXCEPT
{
    RT_NOREF(a_hHttp, a_rStrFullUrl, a_enmHttpMethod, a_rStrXmitBody, a_fFlags);
    return VINF_SUCCESS;
}


int RTCRestClientApiBase::doCall(RTCRestClientRequestBase const &a_rRequest, RTHTTPMETHOD a_enmHttpMethod,
                                 RTCRestClientResponseBase *a_pResponse, const char *a_pszMethod, uint32_t a_fFlags) RT_NOEXCEPT
{
    LogFlow(("doCall: %s %s\n", a_pszMethod, RTHttpMethodToStr(a_enmHttpMethod)));


    /*
     * Reset the response object (allowing reuse of such) and check the request
     * object for assignment errors.
     */
    int    rc;
    RTHTTP hHttp = NIL_RTHTTP;

    a_pResponse->reset();
    if (!a_rRequest.hasAssignmentErrors())
    {
        /*
         * Initialize the HTTP instance.
         */
        rc = reinitHttpInstance();
        if (RT_SUCCESS(rc))
        {
            hHttp = m_hHttp;
            Assert(hHttp != NIL_RTHTTP);

            /*
             * Prepare the response side.
             */
            rc = a_pResponse->receivePrepare(hHttp);
            if (RT_SUCCESS(rc))
            {
                /*
                 * Prepare the request for the transmission.
                 */
                RTCString strExtraPath;
                RTCString strQuery;
                RTCString strXmitBody;
                rc = a_rRequest.xmitPrepare(&strExtraPath, &strQuery, hHttp, &strXmitBody);
                if (RT_SUCCESS(rc))
                {
                    /*
                     * Construct the full URL.
                     */
                    RTCString strFullUrl;
                    rc = strFullUrl.assignNoThrow(getServerUrl());
                    if (strExtraPath.isNotEmpty())
                    {
                        if (!strExtraPath.startsWith("/") && !strFullUrl.endsWith("/") && RT_SUCCESS(rc))
                            rc = strFullUrl.appendNoThrow('/');
                        if (RT_SUCCESS(rc))
                            rc = strFullUrl.appendNoThrow(strExtraPath);
                        strExtraPath.setNull();
                    }
                    if (strQuery.isNotEmpty())
                    {
                        Assert(strQuery.startsWith("?"));
                        rc = strFullUrl.appendNoThrow(strQuery);
                        strQuery.setNull();
                    }
                    if (RT_SUCCESS(rc))
                    {
                        rc = xmitReady(hHttp, strFullUrl, a_enmHttpMethod, strXmitBody, a_fFlags);
                        if (RT_SUCCESS(rc))
                        {
                            /*
                             * Perform HTTP request.
                             */
                            uint32_t uHttpStatus = 0;
                            size_t   cbBody      = 0;
                            void    *pvBody      = NULL;
                            rc = RTHttpPerform(hHttp, strFullUrl.c_str(), a_enmHttpMethod,
                                               strXmitBody.c_str(), strXmitBody.length(),
                                               &uHttpStatus, NULL /*ppvHdrs*/, NULL /*pcbHdrs*/, &pvBody, &cbBody);
                            if (RT_SUCCESS(rc))
                            {
                                a_rRequest.xmitComplete(uHttpStatus, hHttp);

                                /*
                                 * Do response processing.
                                 */
                                a_pResponse->receiveComplete(uHttpStatus, hHttp);
                                a_pResponse->consumeBody((const char *)pvBody, cbBody);
                                if (pvBody)
                                    RTHttpFreeResponse(pvBody);
                                a_pResponse->receiveFinal();

                                return a_pResponse->getStatus();
                            }
                        }
                    }
                }
                a_rRequest.xmitComplete(rc, hHttp);
            }
        }
    }
    else
        rc = VERR_NO_MEMORY;

    a_pResponse->receiveComplete(rc, hHttp);
    RT_NOREF_PV(a_pszMethod);

    return a_pResponse->getStatus();
}