1 | /* $Id: RTCRestClientRequestBase.cpp 73977 2018-08-30 12:13:02Z 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/restbase.h>
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Default constructor.
|
---|
40 | */
|
---|
41 | RTCRestClientRequestBase::RTCRestClientRequestBase()
|
---|
42 | : m_fIsSet(0)
|
---|
43 | , m_fErrorSet(0)
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Copy constructor.
|
---|
50 | */
|
---|
51 | RTCRestClientRequestBase::RTCRestClientRequestBase(RTCRestClientRequestBase const &a_rThat)
|
---|
52 | : m_fIsSet(a_rThat.m_fIsSet)
|
---|
53 | , m_fErrorSet(a_rThat.m_fErrorSet)
|
---|
54 | {
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Destructor
|
---|
60 | */
|
---|
61 | RTCRestClientRequestBase::~RTCRestClientRequestBase()
|
---|
62 | {
|
---|
63 | /* nothing to do */
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Copy assignment operator.
|
---|
69 | */
|
---|
70 | RTCRestClientRequestBase &RTCRestClientRequestBase::operator=(RTCRestClientRequestBase const &a_rThat)
|
---|
71 | {
|
---|
72 | m_fIsSet = a_rThat.m_fIsSet;
|
---|
73 | m_fErrorSet = a_rThat.m_fErrorSet;
|
---|
74 | return *this;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | int RTCRestClientRequestBase::doPathParameters(RTCString *a_pStrPath, const char *a_pszPathTemplate, size_t a_cchPathTemplate,
|
---|
79 | PATHREPLACEENTRY *a_paPathParams, size_t a_cPathParams) const
|
---|
80 | {
|
---|
81 | int rc = a_pStrPath->assignNoThrow(a_pszPathTemplate, a_cchPathTemplate);
|
---|
82 | AssertRCReturn(rc, rc);
|
---|
83 |
|
---|
84 | /* Locate the sub-string to replace with values first: */
|
---|
85 | for (size_t i = 0; i < a_cPathParams; i++)
|
---|
86 | {
|
---|
87 | char const *psz = strstr(a_pszPathTemplate, a_paPathParams[i].pszName);
|
---|
88 | AssertReturn(psz, VERR_INTERNAL_ERROR_5);
|
---|
89 | a_paPathParams[i].offName = psz - a_pszPathTemplate;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /* Replace with actual values: */
|
---|
93 | RTCString strTmpVal;
|
---|
94 | for (size_t i = 0; i < a_cPathParams; i++)
|
---|
95 | {
|
---|
96 | rc = a_paPathParams[i].pObj->toString(&strTmpVal, a_paPathParams[i].fFlags);
|
---|
97 | AssertRCReturn(rc, rc);
|
---|
98 |
|
---|
99 | /* Replace. */
|
---|
100 | ssize_t cchAdjust = strTmpVal.length() - a_paPathParams[i].cchName;
|
---|
101 | rc = a_pStrPath->replaceNoThrow(a_paPathParams[i].offName, a_paPathParams[i].cchName, strTmpVal);
|
---|
102 | AssertRCReturn(rc, rc);
|
---|
103 |
|
---|
104 | /* Adjust subsequent fields. */
|
---|
105 | if (cchAdjust != 0)
|
---|
106 | for (size_t j = i + 1; j < a_cPathParams; j++)
|
---|
107 | if (a_paPathParams[j].offName > a_paPathParams[i].offName)
|
---|
108 | a_paPathParams[j].offName += cchAdjust;
|
---|
109 | }
|
---|
110 |
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 |
|
---|