1 | /* $Id: RTCRestClientApiBase.cpp 73918 2018-08-27 15:08:55Z 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 | #include <iprt/cpp/restbase.h>
|
---|
32 |
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/http.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * The destructor.
|
---|
39 | */
|
---|
40 | RTCRestClientApiBase::~RTCRestClientApiBase()
|
---|
41 | {
|
---|
42 | if (m_hHttp != NIL_RTHTTP)
|
---|
43 | {
|
---|
44 | int rc = RTHttpDestroy(m_hHttp);
|
---|
45 | AssertRC(rc);
|
---|
46 | m_hHttp = NIL_RTHTTP;
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | int RTCRestClientApiBase::reinitHttpInstance()
|
---|
52 | {
|
---|
53 | if (m_hHttp != NIL_RTHTTP)
|
---|
54 | return RTHttpReset(m_hHttp);
|
---|
55 |
|
---|
56 | int rc = RTHttpCreate(&m_hHttp);
|
---|
57 | if (RT_FAILURE(rc))
|
---|
58 | m_hHttp = NIL_RTHTTP;
|
---|
59 | return rc;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | void RTCRestClientApiBase::doCall(RTCRestClientRequestBase const &a_rRequest, RTHTTPMETHOD a_enmHttpMethod,
|
---|
64 | RTCRestClientResponseBase *a_pResponse)
|
---|
65 | {
|
---|
66 | /*
|
---|
67 | * Initialize the HTTP instance.
|
---|
68 | */
|
---|
69 | RTHTTP hHttp = NIL_RTHTTP;
|
---|
70 | int rc = reinitHttpInstance();
|
---|
71 | if (RT_SUCCESS(rc))
|
---|
72 | {
|
---|
73 | hHttp = m_hHttp;
|
---|
74 | Assert(hHttp != NIL_RTHTTP);
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * Prepare the response side. This may install output callbacks and
|
---|
78 | * indicate this by clearing the ppvBody/ppvHdr variables.
|
---|
79 | */
|
---|
80 | size_t cbHdrs = 0;
|
---|
81 | void *pvHdrs = NULL;
|
---|
82 | void **ppvHdrs = &pvHdrs;
|
---|
83 |
|
---|
84 | size_t cbBody = 0;
|
---|
85 | void *pvBody = NULL;
|
---|
86 | void **ppvBody = &pvBody;
|
---|
87 |
|
---|
88 | rc = a_pResponse->receivePrepare(hHttp, &ppvBody, &ppvHdrs);
|
---|
89 | if (RT_SUCCESS(rc))
|
---|
90 | {
|
---|
91 | /*
|
---|
92 | * Prepare the request for the transmission.
|
---|
93 | */
|
---|
94 | RTCString strExtraPath;
|
---|
95 | RTCString strQuery;
|
---|
96 | RTCString strXmitBody;
|
---|
97 | rc = a_rRequest.xmitPrepare(&strExtraPath, &strQuery, hHttp, &strXmitBody);
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | {
|
---|
100 | /*
|
---|
101 | * Construct the full URL.
|
---|
102 | */
|
---|
103 | RTCString strFullUrl;
|
---|
104 | rc = strFullUrl.assignNoThrow(m_strBasePath);
|
---|
105 | if (strExtraPath.isNotEmpty())
|
---|
106 | {
|
---|
107 | if (!strExtraPath.startsWith("/") && !strFullUrl.endsWith("/") && RT_SUCCESS(rc))
|
---|
108 | rc = strFullUrl.appendNoThrow('/');
|
---|
109 | if (RT_SUCCESS(rc))
|
---|
110 | rc = strFullUrl.appendNoThrow(strExtraPath);
|
---|
111 | strExtraPath.setNull();
|
---|
112 | }
|
---|
113 | if (strQuery.isNotEmpty())
|
---|
114 | {
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | rc = strFullUrl.appendNoThrow('?');
|
---|
117 | if (RT_SUCCESS(rc))
|
---|
118 | rc = strFullUrl.appendNoThrow(strQuery);
|
---|
119 | strQuery.setNull();
|
---|
120 | }
|
---|
121 | if (RT_SUCCESS(rc))
|
---|
122 | {
|
---|
123 | /*
|
---|
124 | * Perform HTTP request.
|
---|
125 | */
|
---|
126 | uint32_t uHttpStatus = 0;
|
---|
127 | rc = RTHttpPerform(hHttp, strFullUrl.c_str(), a_enmHttpMethod,
|
---|
128 | &uHttpStatus, ppvHdrs, &cbHdrs, ppvBody, &cbBody);
|
---|
129 | if (RT_SUCCESS(rc))
|
---|
130 | {
|
---|
131 | a_rRequest.xmitComplete(uHttpStatus, hHttp);
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Do response processing.
|
---|
135 | */
|
---|
136 | a_pResponse->receiveComplete(uHttpStatus, hHttp);
|
---|
137 | if (pvHdrs)
|
---|
138 | {
|
---|
139 | a_pResponse->consumeHeaders((const char *)pvHdrs, cbHdrs);
|
---|
140 | RTHttpFreeResponse(pvHdrs);
|
---|
141 | }
|
---|
142 | if (pvBody)
|
---|
143 | {
|
---|
144 | a_pResponse->consumeBody((const char *)pvBody, cbBody);
|
---|
145 | RTHttpFreeResponse(pvBody);
|
---|
146 | }
|
---|
147 | a_pResponse->receiveFinal();
|
---|
148 |
|
---|
149 | return;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | a_rRequest.xmitComplete(rc, hHttp);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | a_pResponse->receiveComplete(rc, hHttp);
|
---|
157 | }
|
---|
158 |
|
---|