VirtualBox

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

Last change on this file since 74232 was 74192, checked in by vboxsync, 7 years ago

IPRT/rest: RTCRestClientApiBase::reinitHttpInstance - as a temporary
workaround always create new RTHTTP handle. RTHttpReset() causes the
handle state and the curl state to get out of sync.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* $Id: RTCRestClientApiBase.cpp 74192 2018-09-11 11:22:36Z 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 * The destructor.
41 */
42RTCRestClientApiBase::~RTCRestClientApiBase()
43{
44 if (m_hHttp != NIL_RTHTTP)
45 {
46 int rc = RTHttpDestroy(m_hHttp);
47 AssertRC(rc);
48 m_hHttp = NIL_RTHTTP;
49 }
50}
51
52
53int RTCRestClientApiBase::reinitHttpInstance()
54{
55 if (m_hHttp != NIL_RTHTTP)
56 {
57#if 0
58 /*
59 * XXX: disable for now as it causes the RTHTTP handle state
60 * and curl state to get out of sync.
61 */
62 return RTHttpReset(m_hHttp);
63#else
64 RTHttpDestroy(m_hHttp);
65 m_hHttp = NIL_RTHTTP;
66#endif
67 }
68
69 int rc = RTHttpCreate(&m_hHttp);
70 if (RT_FAILURE(rc))
71 m_hHttp = NIL_RTHTTP;
72 return rc;
73}
74
75
76int RTCRestClientApiBase::xmitReady(RTHTTP a_hHttp, RTCString const &a_rStrFullUrl, RTHTTPMETHOD a_enmHttpMethod,
77 RTCString const &a_rStrXmitBody, uint32_t a_fFlags)
78{
79 RT_NOREF(a_hHttp, a_rStrFullUrl, a_enmHttpMethod, a_rStrXmitBody, a_fFlags);
80 return VINF_SUCCESS;
81}
82
83
84int RTCRestClientApiBase::doCall(RTCRestClientRequestBase const &a_rRequest, RTHTTPMETHOD a_enmHttpMethod,
85 RTCRestClientResponseBase *a_pResponse, const char *a_pszMethod, uint32_t a_fFlags)
86{
87 LogFlow(("doCall: %s %s\n", a_pszMethod, RTHttpMethodName(a_enmHttpMethod)));
88
89
90 /*
91 * Reset the response object (allowing reuse of such) and check the request
92 * object for assignment errors.
93 */
94 int rc;
95 RTHTTP hHttp = NIL_RTHTTP;
96
97 a_pResponse->reset();
98 if (!a_rRequest.hasAssignmentErrors())
99 {
100 /*
101 * Initialize the HTTP instance.
102 */
103 rc = reinitHttpInstance();
104 if (RT_SUCCESS(rc))
105 {
106 hHttp = m_hHttp;
107 Assert(hHttp != NIL_RTHTTP);
108
109 /*
110 * Prepare the response side. This may install output callbacks and
111 * indicate this by clearing the ppvBody/ppvHdr variables.
112 */
113 size_t cbHdrs = 0;
114 void *pvHdrs = NULL;
115 void **ppvHdrs = &pvHdrs;
116
117 size_t cbBody = 0;
118 void *pvBody = NULL;
119 void **ppvBody = &pvBody;
120
121 rc = a_pResponse->receivePrepare(hHttp, &ppvBody, &ppvHdrs);
122 if (RT_SUCCESS(rc))
123 {
124 /*
125 * Prepare the request for the transmission.
126 */
127 RTCString strExtraPath;
128 RTCString strQuery;
129 RTCString strXmitBody;
130 rc = a_rRequest.xmitPrepare(&strExtraPath, &strQuery, hHttp, &strXmitBody);
131 if (RT_SUCCESS(rc))
132 {
133 /*
134 * Construct the full URL.
135 */
136 RTCString strFullUrl;
137 rc = strFullUrl.assignNoThrow(m_strBasePath);
138 if (strExtraPath.isNotEmpty())
139 {
140 if (!strExtraPath.startsWith("/") && !strFullUrl.endsWith("/") && RT_SUCCESS(rc))
141 rc = strFullUrl.appendNoThrow('/');
142 if (RT_SUCCESS(rc))
143 rc = strFullUrl.appendNoThrow(strExtraPath);
144 strExtraPath.setNull();
145 }
146 if (strQuery.isNotEmpty())
147 {
148 Assert(strQuery.startsWith("?"));
149 rc = strFullUrl.appendNoThrow(strQuery);
150 strQuery.setNull();
151 }
152 if (RT_SUCCESS(rc))
153 {
154 rc = xmitReady(hHttp, strFullUrl, a_enmHttpMethod, strXmitBody, a_fFlags);
155 if (RT_SUCCESS(rc))
156 {
157 /*
158 * Perform HTTP request.
159 */
160 uint32_t uHttpStatus = 0;
161 rc = RTHttpPerform(hHttp, strFullUrl.c_str(), a_enmHttpMethod,
162 strXmitBody.c_str(), strXmitBody.length(),
163 &uHttpStatus, ppvHdrs, &cbHdrs, ppvBody, &cbBody);
164 if (RT_SUCCESS(rc))
165 {
166 a_rRequest.xmitComplete(uHttpStatus, hHttp);
167
168 /*
169 * Do response processing.
170 */
171 a_pResponse->receiveComplete(uHttpStatus, hHttp);
172 if (pvHdrs)
173 {
174 a_pResponse->consumeHeaders((const char *)pvHdrs, cbHdrs);
175 RTHttpFreeResponse(pvHdrs);
176 }
177 if (pvBody)
178 {
179 a_pResponse->consumeBody((const char *)pvBody, cbBody);
180 RTHttpFreeResponse(pvBody);
181 }
182 a_pResponse->receiveFinal();
183
184 return a_pResponse->getStatus();
185 }
186 }
187 }
188 }
189 a_rRequest.xmitComplete(rc, hHttp);
190 }
191 }
192 }
193 else
194 rc = VERR_NO_MEMORY;
195
196 a_pResponse->receiveComplete(rc, hHttp);
197 RT_NOREF_PV(a_pszMethod);
198
199 return a_pResponse->getStatus();
200}
201
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette