1 | /* $Id: RTCRestClientApiBase.cpp 74348 2018-09-18 17:06:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - C++ REST, RTCRestClientApiBase 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/err.h>
|
---|
35 | #include <iprt/http.h>
|
---|
36 | #include <iprt/log.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Default constructor.
|
---|
41 | */
|
---|
42 | RTCRestClientApiBase::RTCRestClientApiBase()
|
---|
43 | : m_hHttp(NIL_RTHTTP)
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * The destructor.
|
---|
50 | */
|
---|
51 | RTCRestClientApiBase::~RTCRestClientApiBase()
|
---|
52 | {
|
---|
53 | if (m_hHttp != NIL_RTHTTP)
|
---|
54 | {
|
---|
55 | int rc = RTHttpDestroy(m_hHttp);
|
---|
56 | AssertRC(rc);
|
---|
57 | m_hHttp = NIL_RTHTTP;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | const char *RTCRestClientApiBase::getHost() const
|
---|
63 | {
|
---|
64 | return m_strHost.isEmpty() ? getDefaultHost() : m_strHost.c_str();
|
---|
65 | }
|
---|
66 |
|
---|
67 | int RTCRestClientApiBase::setHost(const char *a_pszHost)
|
---|
68 | {
|
---|
69 | return m_strHost.assignNoThrow(a_pszHost);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | int RTCRestClientApiBase::setHost(RTCString const &a_strPath)
|
---|
74 | {
|
---|
75 | return setHost(a_strPath.c_str());
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | const char *RTCRestClientApiBase::getBasePath(void) const
|
---|
80 | {
|
---|
81 | return m_strBasePath.isEmpty() ? getDefaultBasePath() : m_strBasePath.c_str();
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | int RTCRestClientApiBase::setBasePath(const char *a_pszPath)
|
---|
86 | {
|
---|
87 | return m_strBasePath.assignNoThrow(a_pszPath);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | int RTCRestClientApiBase::setBasePath(RTCString const &a_strPath)
|
---|
92 | {
|
---|
93 | return setBasePath(a_strPath.c_str());
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | int RTCRestClientApiBase::reinitHttpInstance()
|
---|
98 | {
|
---|
99 | if (m_hHttp != NIL_RTHTTP)
|
---|
100 | {
|
---|
101 | #if 0
|
---|
102 | /*
|
---|
103 | * XXX: disable for now as it causes the RTHTTP handle state
|
---|
104 | * and curl state to get out of sync.
|
---|
105 | */
|
---|
106 | return RTHttpReset(m_hHttp);
|
---|
107 | #else
|
---|
108 | RTHttpDestroy(m_hHttp);
|
---|
109 | m_hHttp = NIL_RTHTTP;
|
---|
110 | #endif
|
---|
111 | }
|
---|
112 |
|
---|
113 | int rc = RTHttpCreate(&m_hHttp);
|
---|
114 | if (RT_FAILURE(rc))
|
---|
115 | m_hHttp = NIL_RTHTTP;
|
---|
116 | return rc;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | int RTCRestClientApiBase::xmitReady(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
|
---|
121 | RTCString const &a_rStrXmitBody, uint32_t a_fFlags)
|
---|
122 | {
|
---|
123 | RT_NOREF(a_hHttp, a_rStrFullUrl, a_enmHttpMethod, a_rStrXmitBody, a_fFlags);
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | int RTCRestClientApiBase::doCall(RTCRestClientRequestBase const &a_rRequest, RTHTTPMETHOD a_enmHttpMethod,
|
---|
129 | RTCRestClientResponseBase *a_pResponse, const char *a_pszMethod, uint32_t a_fFlags)
|
---|
130 | {
|
---|
131 | LogFlow(("doCall: %s %s\n", a_pszMethod, RTHttpMethodName(a_enmHttpMethod)));
|
---|
132 |
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Reset the response object (allowing reuse of such) and check the request
|
---|
136 | * object for assignment errors.
|
---|
137 | */
|
---|
138 | int rc;
|
---|
139 | RTHTTP hHttp = NIL_RTHTTP;
|
---|
140 |
|
---|
141 | a_pResponse->reset();
|
---|
142 | if (!a_rRequest.hasAssignmentErrors())
|
---|
143 | {
|
---|
144 | /*
|
---|
145 | * Initialize the HTTP instance.
|
---|
146 | */
|
---|
147 | rc = reinitHttpInstance();
|
---|
148 | if (RT_SUCCESS(rc))
|
---|
149 | {
|
---|
150 | hHttp = m_hHttp;
|
---|
151 | Assert(hHttp != NIL_RTHTTP);
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Prepare the response side.
|
---|
155 | */
|
---|
156 | rc = a_pResponse->receivePrepare(hHttp);
|
---|
157 | if (RT_SUCCESS(rc))
|
---|
158 | {
|
---|
159 | /*
|
---|
160 | * Prepare the request for the transmission.
|
---|
161 | */
|
---|
162 | RTCString strExtraPath;
|
---|
163 | RTCString strQuery;
|
---|
164 | RTCString strXmitBody;
|
---|
165 | rc = a_rRequest.xmitPrepare(&strExtraPath, &strQuery, hHttp, &strXmitBody);
|
---|
166 | if (RT_SUCCESS(rc))
|
---|
167 | {
|
---|
168 | /*
|
---|
169 | * Construct the full URL.
|
---|
170 | */
|
---|
171 | RTCString strFullUrl;
|
---|
172 | rc = strFullUrl.assignNoThrow(getHost());
|
---|
173 | if (RT_SUCCESS(rc))
|
---|
174 | rc = strFullUrl.appendNoThrow(getBasePath());
|
---|
175 | if (strExtraPath.isNotEmpty())
|
---|
176 | {
|
---|
177 | if (!strExtraPath.startsWith("/") && !strFullUrl.endsWith("/") && RT_SUCCESS(rc))
|
---|
178 | rc = strFullUrl.appendNoThrow('/');
|
---|
179 | if (RT_SUCCESS(rc))
|
---|
180 | rc = strFullUrl.appendNoThrow(strExtraPath);
|
---|
181 | strExtraPath.setNull();
|
---|
182 | }
|
---|
183 | if (strQuery.isNotEmpty())
|
---|
184 | {
|
---|
185 | Assert(strQuery.startsWith("?"));
|
---|
186 | rc = strFullUrl.appendNoThrow(strQuery);
|
---|
187 | strQuery.setNull();
|
---|
188 | }
|
---|
189 | if (RT_SUCCESS(rc))
|
---|
190 | {
|
---|
191 | rc = xmitReady(hHttp, strFullUrl, a_enmHttpMethod, strXmitBody, a_fFlags);
|
---|
192 | if (RT_SUCCESS(rc))
|
---|
193 | {
|
---|
194 | /*
|
---|
195 | * Perform HTTP request.
|
---|
196 | */
|
---|
197 | uint32_t uHttpStatus = 0;
|
---|
198 | size_t cbBody = 0;
|
---|
199 | void *pvBody = NULL;
|
---|
200 | rc = RTHttpPerform(hHttp, strFullUrl.c_str(), a_enmHttpMethod,
|
---|
201 | strXmitBody.c_str(), strXmitBody.length(),
|
---|
202 | &uHttpStatus, NULL /*ppvHdrs*/, NULL /*pcbHdrs*/, &pvBody, &cbBody);
|
---|
203 | if (RT_SUCCESS(rc))
|
---|
204 | {
|
---|
205 | a_rRequest.xmitComplete(uHttpStatus, hHttp);
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Do response processing.
|
---|
209 | */
|
---|
210 | a_pResponse->receiveComplete(uHttpStatus, hHttp);
|
---|
211 | if (pvBody)
|
---|
212 | {
|
---|
213 | a_pResponse->consumeBody((const char *)pvBody, cbBody);
|
---|
214 | RTHttpFreeResponse(pvBody);
|
---|
215 | }
|
---|
216 | a_pResponse->receiveFinal();
|
---|
217 |
|
---|
218 | return a_pResponse->getStatus();
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | a_rRequest.xmitComplete(rc, hHttp);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 | else
|
---|
228 | rc = VERR_NO_MEMORY;
|
---|
229 |
|
---|
230 | a_pResponse->receiveComplete(rc, hHttp);
|
---|
231 | RT_NOREF_PV(a_pszMethod);
|
---|
232 |
|
---|
233 | return a_pResponse->getStatus();
|
---|
234 | }
|
---|
235 |
|
---|