1 | /* $Id: clipboard-transfers-http.cpp 100676 2023-07-21 11:26:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Shared Clipboard: HTTP server implementation for Shared Clipboard transfers on UNIX-y guests / hosts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2020-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <signal.h>
|
---|
33 |
|
---|
34 | #include <iprt/http.h>
|
---|
35 | #include <iprt/http-server.h>
|
---|
36 |
|
---|
37 | #include <iprt/net.h> /* To make use of IPv4Addr in RTGETOPTUNION. */
|
---|
38 |
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/ctype.h>
|
---|
42 | #include <iprt/errcore.h>
|
---|
43 | #include <iprt/file.h>
|
---|
44 | #include <iprt/getopt.h>
|
---|
45 | #include <iprt/initterm.h>
|
---|
46 | #include <iprt/list.h>
|
---|
47 | #include <iprt/mem.h>
|
---|
48 | #include <iprt/message.h>
|
---|
49 | #include <iprt/path.h>
|
---|
50 | #include <iprt/rand.h>
|
---|
51 | #include <iprt/semaphore.h>
|
---|
52 | #include <iprt/stream.h>
|
---|
53 | #include <iprt/string.h>
|
---|
54 | #include <iprt/thread.h>
|
---|
55 | #include <iprt/uri.h>
|
---|
56 | #include <iprt/uuid.h>
|
---|
57 | #include <iprt/vfs.h>
|
---|
58 |
|
---|
59 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
60 | #include <iprt/log.h>
|
---|
61 |
|
---|
62 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
63 | #include <VBox/GuestHost/SharedClipboard-x11.h>
|
---|
64 | #include <VBox/GuestHost/SharedClipboard-transfers.h>
|
---|
65 |
|
---|
66 |
|
---|
67 | /*********************************************************************************************************************************
|
---|
68 | * Definitations *
|
---|
69 | *********************************************************************************************************************************/
|
---|
70 |
|
---|
71 | #ifdef DEBUG_andy_0
|
---|
72 | /** When enabled, this lets the HTTP server run at a predictable URL and port for debugging:
|
---|
73 | * URL: http://localhost:49200/transfer<ID> */
|
---|
74 | # define VBOX_SHCL_DEBUG_HTTPSERVER
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | typedef struct _SHCLHTTPSERVERTRANSFER
|
---|
78 | {
|
---|
79 | /** The node list. */
|
---|
80 | RTLISTNODE Node;
|
---|
81 | /** Pointer to associated transfer. */
|
---|
82 | PSHCLTRANSFER pTransfer;
|
---|
83 | /** Critical section for serializing access. */
|
---|
84 | RTCRITSECT CritSect;
|
---|
85 | /** The handle we're going to use for this HTTP transfer. */
|
---|
86 | SHCLOBJHANDLE hObj;
|
---|
87 | /** The virtual path of the HTTP server's root directory for this transfer.
|
---|
88 | * Always has to start with a "/". */
|
---|
89 | char szPathVirtual[RTPATH_MAX];
|
---|
90 | } SHCLHTTPSERVERTRANSFER;
|
---|
91 | typedef SHCLHTTPSERVERTRANSFER *PSHCLHTTPSERVERTRANSFER;
|
---|
92 |
|
---|
93 |
|
---|
94 | /*********************************************************************************************************************************
|
---|
95 | * Prototypes *
|
---|
96 | *********************************************************************************************************************************/
|
---|
97 | static int shClTransferHttpServerDestroyInternal(PSHCLHTTPSERVER pThis);
|
---|
98 | static const char *shClTransferHttpServerGetHost(PSHCLHTTPSERVER pSrv);
|
---|
99 | static int shClTransferHttpServerDestroyTransfer(PSHCLHTTPSERVER pSrv, PSHCLHTTPSERVERTRANSFER pSrvTx);
|
---|
100 | static SHCLHTTPSERVERSTATUS shclTransferHttpServerSetStatusLocked(PSHCLHTTPSERVER pSrv, SHCLHTTPSERVERSTATUS fStatus);
|
---|
101 |
|
---|
102 |
|
---|
103 | /*********************************************************************************************************************************
|
---|
104 | * Static assets *
|
---|
105 | *********************************************************************************************************************************/
|
---|
106 |
|
---|
107 | static char s_shClHttpServerPage404[] = " \
|
---|
108 | <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \
|
---|
109 | \"http://www.w3.org/TR/html4/strict.dtd\"> \
|
---|
110 | <html> \
|
---|
111 | <head> \
|
---|
112 | <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"> \
|
---|
113 | <title>VirtualBox Shared Clipboard</title> \
|
---|
114 | </head> \
|
---|
115 | <body> \
|
---|
116 | <h1>VirtualBox Shared Clipboard</h1> \
|
---|
117 | <p>Error: 404</p> \
|
---|
118 | <p>Message: Entry not found.</p> \
|
---|
119 | </body> \
|
---|
120 | </html>";
|
---|
121 |
|
---|
122 |
|
---|
123 | /*********************************************************************************************************************************
|
---|
124 | * Internal Shared Clipboard HTTP transfer functions *
|
---|
125 | *********************************************************************************************************************************/
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Locks the critical section of a Shared Clipboard HTTP server instance.
|
---|
129 | *
|
---|
130 | * @param pSrv Shared Clipboard HTTP server instance to lock.
|
---|
131 | */
|
---|
132 | DECLINLINE(void) shClTransferHttpServerLock(PSHCLHTTPSERVER pSrv)
|
---|
133 | {
|
---|
134 | int rc2 = RTCritSectEnter(&pSrv->CritSect);
|
---|
135 | AssertRC(rc2);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Unlocks the critical section of a Shared Clipboard HTTP server instance.
|
---|
140 | *
|
---|
141 | * @param pSrv Shared Clipboard HTTP server instance to unlock.
|
---|
142 | */
|
---|
143 | DECLINLINE(void) shClTransferHttpServerUnlock(PSHCLHTTPSERVER pSrv)
|
---|
144 | {
|
---|
145 | int rc2 = RTCritSectLeave(&pSrv->CritSect);
|
---|
146 | AssertRC(rc2);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Locks an HTTP transfer.
|
---|
151 | *
|
---|
152 | * @param pSrvTx HTTP transfer to lock.
|
---|
153 | */
|
---|
154 | DECLINLINE(void) shClHttpTransferLock(PSHCLHTTPSERVERTRANSFER pSrvTx)
|
---|
155 | {
|
---|
156 | int rc2 = RTCritSectEnter(&pSrvTx->CritSect);
|
---|
157 | AssertRC(rc2);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Unlocks an HTTP transfer.
|
---|
162 | *
|
---|
163 | * @param pSrvTx HTTP transfer to unlock.
|
---|
164 | */
|
---|
165 | DECLINLINE(void) shClHttpTransferUnlock(PSHCLHTTPSERVERTRANSFER pSrvTx)
|
---|
166 | {
|
---|
167 | int rc2 = RTCritSectLeave(&pSrvTx->CritSect);
|
---|
168 | AssertRC(rc2);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Return the HTTP server transfer for a specific transfer ID.
|
---|
173 | *
|
---|
174 | * @returns Pointer to HTTP server transfer if found, NULL if not found.
|
---|
175 | * @param pSrv HTTP server instance.
|
---|
176 | * @param idTransfer Transfer ID to return HTTP server transfer for.
|
---|
177 | */
|
---|
178 | static PSHCLHTTPSERVERTRANSFER shClTransferHttpServerGetTransferById(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer)
|
---|
179 | {
|
---|
180 | PSHCLHTTPSERVERTRANSFER pSrvTx;
|
---|
181 | RTListForEach(&pSrv->lstTransfers, pSrvTx, SHCLHTTPSERVERTRANSFER, Node) /** @todo Slow O(n) lookup, but does it for now. */
|
---|
182 | {
|
---|
183 | if (pSrvTx->pTransfer->State.uID == idTransfer)
|
---|
184 | return pSrvTx;
|
---|
185 | }
|
---|
186 |
|
---|
187 | return NULL;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Returns a HTTP server transfer from a given URL.
|
---|
192 | *
|
---|
193 | * @returns Pointer to HTTP server transfer if found, NULL if not found.
|
---|
194 | * @param pThis HTTP server instance data.
|
---|
195 | * @param pszUrl URL to validate.
|
---|
196 | */
|
---|
197 | DECLINLINE(PSHCLHTTPSERVERTRANSFER) shClTransferHttpGetTransferFromUrl(PSHCLHTTPSERVER pThis, const char *pszUrl)
|
---|
198 | {
|
---|
199 | AssertPtrReturn(pszUrl, NULL);
|
---|
200 |
|
---|
201 | PSHCLHTTPSERVERTRANSFER pSrvTx = NULL;
|
---|
202 |
|
---|
203 | PSHCLHTTPSERVERTRANSFER pSrvTxCur;
|
---|
204 | RTListForEach(&pThis->lstTransfers, pSrvTxCur, SHCLHTTPSERVERTRANSFER, Node)
|
---|
205 | {
|
---|
206 | AssertPtr(pSrvTxCur->pTransfer);
|
---|
207 |
|
---|
208 | LogFlowFunc(("pSrvTxCur=%s\n", pSrvTxCur->szPathVirtual));
|
---|
209 |
|
---|
210 | /* Be picky here, do a case sensitive comparison. */
|
---|
211 | if (RTStrStartsWith(pszUrl, pSrvTxCur->szPathVirtual))
|
---|
212 | {
|
---|
213 | pSrvTx = pSrvTxCur;
|
---|
214 | break;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (!pSrvTx)
|
---|
219 | LogRel2(("Shared Clipboard: HTTP URL '%s' not valid\n", pszUrl));
|
---|
220 |
|
---|
221 | LogFlowFunc(("pszUrl=%s, pSrvTx=%p\n", pszUrl, pSrvTx));
|
---|
222 | return pSrvTx;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Returns a HTTP server transfer from an internal HTTP handle.
|
---|
227 | *
|
---|
228 | * @returns Pointer to HTTP server transfer if found, NULL if not found.
|
---|
229 | * @param pThis HTTP server instance data.
|
---|
230 | * @param pvHandle Handle to return transfer for.
|
---|
231 | */
|
---|
232 | DECLINLINE(PSHCLHTTPSERVERTRANSFER) shClTransferHttpGetTransferFromHandle(PSHCLHTTPSERVER pThis, void *pvHandle)
|
---|
233 | {
|
---|
234 | AssertPtrReturn(pvHandle, NULL);
|
---|
235 |
|
---|
236 | const SHCLTRANSFERID uHandle = *(uint16_t *)pvHandle;
|
---|
237 |
|
---|
238 | /** @ŧodo Use a handle lookup table (map) later. */
|
---|
239 | PSHCLHTTPSERVERTRANSFER pSrvTxCur;
|
---|
240 | RTListForEach(&pThis->lstTransfers, pSrvTxCur, SHCLHTTPSERVERTRANSFER, Node)
|
---|
241 | {
|
---|
242 | AssertPtr(pSrvTxCur->pTransfer);
|
---|
243 |
|
---|
244 | if (pSrvTxCur->pTransfer->State.uID == uHandle) /** @ŧodo We're using the transfer ID as handle for now. */
|
---|
245 | return pSrvTxCur;
|
---|
246 | }
|
---|
247 |
|
---|
248 | return NULL;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | /*********************************************************************************************************************************
|
---|
253 | * HTTP server callback implementations *
|
---|
254 | *********************************************************************************************************************************/
|
---|
255 |
|
---|
256 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnRequestBegin */
|
---|
257 | static DECLCALLBACK(int) shClTransferHttpBegin(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq)
|
---|
258 | {
|
---|
259 | PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser; RT_NOREF(pThis);
|
---|
260 | Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
|
---|
261 |
|
---|
262 | LogRel2(("Shared Clipboard: HTTP request begin\n"));
|
---|
263 |
|
---|
264 | PSHCLHTTPSERVERTRANSFER pSrvTx = shClTransferHttpGetTransferFromUrl(pThis, pReq->pszUrl);
|
---|
265 | if (pSrvTx)
|
---|
266 | {
|
---|
267 | pReq->pvUser = pSrvTx;
|
---|
268 | }
|
---|
269 |
|
---|
270 | return VINF_SUCCESS;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnRequestEnd */
|
---|
274 | static DECLCALLBACK(int) shClTransferHttpEnd(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq)
|
---|
275 | {
|
---|
276 | PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser; RT_NOREF(pThis);
|
---|
277 | Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
|
---|
278 |
|
---|
279 | LogRel2(("Shared Clipboard: HTTP request end\n"));
|
---|
280 |
|
---|
281 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
282 | if (pSrvTx)
|
---|
283 | {
|
---|
284 | pReq->pvUser = NULL;
|
---|
285 | }
|
---|
286 |
|
---|
287 | return VINF_SUCCESS;
|
---|
288 |
|
---|
289 | }
|
---|
290 |
|
---|
291 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnOpen */
|
---|
292 | static DECLCALLBACK(int) shClTransferHttpOpen(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void **ppvHandle)
|
---|
293 | {
|
---|
294 | PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser; RT_NOREF(pThis);
|
---|
295 | Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
|
---|
296 |
|
---|
297 | int rc;
|
---|
298 |
|
---|
299 | AssertPtr(pReq->pvUser);
|
---|
300 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
301 | if (pSrvTx)
|
---|
302 | {
|
---|
303 | LogRel2(("Shared Clipboard: HTTP transfer (handle %RU64) started ...\n", pSrvTx->hObj));
|
---|
304 |
|
---|
305 | Assert(pSrvTx->hObj != NIL_SHCLOBJHANDLE);
|
---|
306 | *ppvHandle = &pSrvTx->hObj;
|
---|
307 | }
|
---|
308 | else
|
---|
309 | rc = VERR_NOT_FOUND;
|
---|
310 |
|
---|
311 | if (RT_FAILURE(rc))
|
---|
312 | LogRel(("Shared Clipboard: Error starting HTTP transfer for '%s', rc=%Rrc\n", pReq->pszUrl, rc));
|
---|
313 |
|
---|
314 | LogFlowFuncLeaveRC(rc);
|
---|
315 | return rc;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnRead */
|
---|
319 | static DECLCALLBACK(int) shClTransferHttpRead(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq,
|
---|
320 | void *pvHandle, void *pvBuf, size_t cbBuf, size_t *pcbRead)
|
---|
321 | {
|
---|
322 | RT_NOREF(pData);
|
---|
323 |
|
---|
324 | if (pvHandle == NULL) /* Serve a 404 page if we got an invalid handle. */
|
---|
325 | {
|
---|
326 | Assert(cbBuf >= sizeof(s_shClHttpServerPage404)); /* Keep it simple for now. */
|
---|
327 | memcpy(pvBuf, &s_shClHttpServerPage404, RT_MIN(cbBuf, sizeof(s_shClHttpServerPage404)));
|
---|
328 | *pcbRead = sizeof(s_shClHttpServerPage404);
|
---|
329 | return VINF_SUCCESS;
|
---|
330 | }
|
---|
331 |
|
---|
332 | int rc;
|
---|
333 |
|
---|
334 | LogRel3(("Shared Clipboard: Reading %RU32 bytes from HTTP ...\n", cbBuf));
|
---|
335 |
|
---|
336 | AssertPtr(pReq->pvUser);
|
---|
337 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
338 | if (pSrvTx)
|
---|
339 | {
|
---|
340 | PSHCLOBJHANDLE phObj = (PSHCLOBJHANDLE)pvHandle;
|
---|
341 | if (phObj)
|
---|
342 | {
|
---|
343 | uint32_t cbRead;
|
---|
344 | rc = ShClTransferObjRead(pSrvTx->pTransfer, *phObj, pvBuf, cbBuf, 0 /* fFlags */, &cbRead);
|
---|
345 | if (RT_SUCCESS(rc))
|
---|
346 | *pcbRead = (uint32_t)cbRead;
|
---|
347 |
|
---|
348 | if (RT_FAILURE(rc))
|
---|
349 | LogRel(("Shared Clipboard: Error reading HTTP transfer (handle %RU64), rc=%Rrc\n", *phObj, rc));
|
---|
350 | }
|
---|
351 | else
|
---|
352 | rc = VERR_NOT_FOUND;
|
---|
353 | }
|
---|
354 | else
|
---|
355 | rc = VERR_NOT_FOUND;
|
---|
356 |
|
---|
357 | LogFlowFuncLeaveRC(rc);
|
---|
358 | return rc;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnClose */
|
---|
362 | static DECLCALLBACK(int) shClTransferHttpClose(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void *pvHandle)
|
---|
363 | {
|
---|
364 | RT_NOREF(pData);
|
---|
365 |
|
---|
366 | int rc;
|
---|
367 |
|
---|
368 | AssertPtr(pReq->pvUser);
|
---|
369 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
370 | if (pSrvTx)
|
---|
371 | {
|
---|
372 | PSHCLOBJHANDLE phObj = (PSHCLOBJHANDLE)pvHandle;
|
---|
373 | if (phObj)
|
---|
374 | {
|
---|
375 | Assert(*phObj != NIL_SHCLOBJHANDLE);
|
---|
376 | rc = ShClTransferObjClose(pSrvTx->pTransfer, *phObj);
|
---|
377 | if (RT_SUCCESS(rc))
|
---|
378 | {
|
---|
379 | pSrvTx->hObj = NIL_SHCLOBJHANDLE;
|
---|
380 | LogRel2(("Shared Clipboard: HTTP transfer %RU16 done\n", pSrvTx->pTransfer->State.uID));
|
---|
381 | }
|
---|
382 |
|
---|
383 | if (RT_FAILURE(rc))
|
---|
384 | LogRel(("Shared Clipboard: Error closing HTTP transfer (handle %RU64), rc=%Rrc\n", *phObj, rc));
|
---|
385 | }
|
---|
386 | else
|
---|
387 | rc = VERR_NOT_FOUND;
|
---|
388 | }
|
---|
389 | else
|
---|
390 | rc = VERR_NOT_FOUND;
|
---|
391 |
|
---|
392 | LogFlowFuncLeaveRC(rc);
|
---|
393 | return rc;
|
---|
394 | }
|
---|
395 |
|
---|
396 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnQueryInfo */
|
---|
397 | static DECLCALLBACK(int) shClTransferHttpQueryInfo(PRTHTTPCALLBACKDATA pData,
|
---|
398 | PRTHTTPSERVERREQ pReq, PRTFSOBJINFO pObjInfo, char **ppszMIMEHint)
|
---|
399 | {
|
---|
400 | RT_NOREF(pData);
|
---|
401 | RT_NOREF(ppszMIMEHint);
|
---|
402 |
|
---|
403 | AssertReturn(RTStrIsValidEncoding(pReq->pszUrl), VERR_INVALID_PARAMETER);
|
---|
404 |
|
---|
405 | size_t const cchUrl = strlen(pReq->pszUrl);
|
---|
406 | AssertReturn(cchUrl, VERR_INVALID_PARAMETER);
|
---|
407 |
|
---|
408 | int rc;
|
---|
409 |
|
---|
410 | /* For now we only know the transfer -- now we need to figure out the entry we want to serve. */
|
---|
411 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
412 | if (pSrvTx)
|
---|
413 | {
|
---|
414 | size_t const cchBase = strlen(pSrvTx->szPathVirtual) + 1 /* Skip slash separating the base from the rest */;
|
---|
415 | AssertReturn(cchUrl >= cchBase, VERR_INVALID_PARAMETER);
|
---|
416 |
|
---|
417 | SHCLOBJOPENCREATEPARMS openParms;
|
---|
418 | rc = ShClTransferObjOpenParmsInit(&openParms);
|
---|
419 | if (RT_SUCCESS(rc))
|
---|
420 | {
|
---|
421 | openParms.fCreate = SHCL_OBJ_CF_ACCESS_READ
|
---|
422 | | SHCL_OBJ_CF_ACCESS_DENYWRITE;
|
---|
423 |
|
---|
424 | PSHCLTRANSFER pTx = pSrvTx->pTransfer;
|
---|
425 | AssertPtr(pTx);
|
---|
426 |
|
---|
427 | rc = VERR_NOT_FOUND; /* Must find the matching root entry first. */
|
---|
428 |
|
---|
429 | uint64_t const cRoots = ShClTransferRootsCount(pTx);
|
---|
430 | for (uint32_t i = 0; i < cRoots; i++)
|
---|
431 | {
|
---|
432 | PCSHCLLISTENTRY pEntry = ShClTransferRootsEntryGet(pTx, i);
|
---|
433 | AssertPtrBreakStmt(pEntry, rc = VERR_NOT_FOUND);
|
---|
434 |
|
---|
435 | const char *pszName = pReq->pszUrl + cchBase;
|
---|
436 |
|
---|
437 | Log3Func(("pReqUrl=%s -> pszName=%s vs. pEntry=%s\n", pReq->pszUrl, pszName, pEntry->pszName));
|
---|
438 |
|
---|
439 | if (RTStrCmp(pEntry->pszName, pszName)) /* Case-sensitive! */
|
---|
440 | continue;
|
---|
441 |
|
---|
442 | LogRel2(("Shared Clipboard: Querying HTTP transfer information for '%s' ...\n", pEntry->pszName));
|
---|
443 |
|
---|
444 | rc = RTStrCopy(openParms.pszPath, openParms.cbPath, pEntry->pszName);
|
---|
445 | if (RT_SUCCESS(rc))
|
---|
446 | {
|
---|
447 | rc = ShClTransferObjOpen(pTx, &openParms, &pSrvTx->hObj);
|
---|
448 | if (RT_SUCCESS(rc))
|
---|
449 | {
|
---|
450 | rc = VERR_NOT_SUPPORTED; /* Play safe by default. */
|
---|
451 |
|
---|
452 | if ( pEntry->fInfo & VBOX_SHCL_INFO_F_FSOBJINFO
|
---|
453 | && pEntry->cbInfo == sizeof(SHCLFSOBJINFO))
|
---|
454 | {
|
---|
455 | PCSHCLFSOBJINFO pSrcObjInfo = (PSHCLFSOBJINFO)pEntry->pvInfo;
|
---|
456 |
|
---|
457 | LogFlowFunc(("pszName=%s, cbInfo=%RU32, fMode=%#x (type %#x)\n",
|
---|
458 | pEntry->pszName, pEntry->cbInfo, pSrcObjInfo->Attr.fMode, (pSrcObjInfo->Attr.fMode & RTFS_TYPE_MASK)));
|
---|
459 |
|
---|
460 | if (RTFS_IS_FILE(pSrcObjInfo->Attr.fMode))
|
---|
461 | {
|
---|
462 | memcpy(pObjInfo, pSrcObjInfo, sizeof(SHCLFSOBJINFO));
|
---|
463 | rc = VINF_SUCCESS;
|
---|
464 | }
|
---|
465 | }
|
---|
466 | else
|
---|
467 | LogRel2(("Shared Clipboard: Supplied entry information for '%s' not supported (fInfo=%#x, cbInfo=%RU32\n",
|
---|
468 | pEntry->pszName, pEntry->fInfo, pEntry->cbInfo));
|
---|
469 | }
|
---|
470 | }
|
---|
471 |
|
---|
472 | break;
|
---|
473 | }
|
---|
474 |
|
---|
475 | ShClTransferObjOpenParmsDestroy(&openParms);
|
---|
476 | }
|
---|
477 | }
|
---|
478 | else
|
---|
479 | rc = VERR_NOT_FOUND;
|
---|
480 |
|
---|
481 | if (RT_FAILURE(rc))
|
---|
482 | LogRel(("Shared Clipboard: Querying info for HTTP transfer failed with %Rrc\n", rc));
|
---|
483 |
|
---|
484 | LogFlowFuncLeaveRC(rc);
|
---|
485 | return rc;
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | /*********************************************************************************************************************************
|
---|
490 | * Internal Shared Clipboard HTTP server functions *
|
---|
491 | *********************************************************************************************************************************/
|
---|
492 |
|
---|
493 | /**
|
---|
494 | * Destroys a Shared Clipboard HTTP server instance, internal version.
|
---|
495 | *
|
---|
496 | * @returns VBox status code.
|
---|
497 | * @param pSrv Shared Clipboard HTTP server instance to destroy.
|
---|
498 | *
|
---|
499 | * @note Caller needs to take the critical section.
|
---|
500 | */
|
---|
501 | static int shClTransferHttpServerDestroyInternal(PSHCLHTTPSERVER pSrv)
|
---|
502 | {
|
---|
503 | Assert(RTCritSectIsOwner(&pSrv->CritSect));
|
---|
504 |
|
---|
505 | LogFlowFuncEnter();
|
---|
506 |
|
---|
507 | pSrv->fInitialized = false;
|
---|
508 | pSrv->fRunning = false;
|
---|
509 |
|
---|
510 | int rc = VINF_SUCCESS;
|
---|
511 |
|
---|
512 | PSHCLHTTPSERVERTRANSFER pSrvTx, pSrvTxNext;
|
---|
513 | RTListForEachSafe(&pSrv->lstTransfers, pSrvTx, pSrvTxNext, SHCLHTTPSERVERTRANSFER, Node)
|
---|
514 | {
|
---|
515 | int rc2 = shClTransferHttpServerDestroyTransfer(pSrv, pSrvTx);
|
---|
516 | if (RT_SUCCESS(rc))
|
---|
517 | rc = rc2;
|
---|
518 | }
|
---|
519 |
|
---|
520 | RTHttpServerResponseDestroy(&pSrv->Resp);
|
---|
521 |
|
---|
522 | pSrv->hHTTPServer = NIL_RTHTTPSERVER;
|
---|
523 |
|
---|
524 | shClTransferHttpServerUnlock(pSrv); /* Unlock critical section taken by the caller before deleting it. */
|
---|
525 |
|
---|
526 | if (RTCritSectIsInitialized(&pSrv->CritSect))
|
---|
527 | {
|
---|
528 | int rc2 = RTCritSectDelete(&pSrv->CritSect);
|
---|
529 | if (RT_SUCCESS(rc))
|
---|
530 | rc = rc2;
|
---|
531 | }
|
---|
532 |
|
---|
533 | LogFlowFuncLeaveRC(rc);
|
---|
534 | return rc;
|
---|
535 | }
|
---|
536 |
|
---|
537 | /**
|
---|
538 | * Initializes a new Shared Clipboard HTTP server instance.
|
---|
539 | *
|
---|
540 | * @return VBox status code.
|
---|
541 | * @param pSrv HTTP server instance to initialize.
|
---|
542 | */
|
---|
543 | static int shClTransferHttpServerInitInternal(PSHCLHTTPSERVER pSrv)
|
---|
544 | {
|
---|
545 | int rc = RTCritSectInit(&pSrv->CritSect);
|
---|
546 | AssertRCReturn(rc, rc);
|
---|
547 |
|
---|
548 | rc = RTSemEventCreate(&pSrv->StatusEvent);
|
---|
549 | AssertRCReturn(rc, rc);
|
---|
550 |
|
---|
551 | pSrv->hHTTPServer = NIL_RTHTTPSERVER;
|
---|
552 | pSrv->uPort = 0;
|
---|
553 | RTListInit(&pSrv->lstTransfers);
|
---|
554 | pSrv->cTransfers = 0;
|
---|
555 |
|
---|
556 | rc = RTHttpServerResponseInit(&pSrv->Resp);
|
---|
557 | AssertRCReturn(rc, rc);
|
---|
558 |
|
---|
559 | ASMAtomicXchgBool(&pSrv->fInitialized, true);
|
---|
560 | ASMAtomicXchgBool(&pSrv->fRunning, false);
|
---|
561 |
|
---|
562 | return rc;
|
---|
563 | }
|
---|
564 |
|
---|
565 |
|
---|
566 | /*********************************************************************************************************************************
|
---|
567 | * Public Shared Clipboard HTTP server functions *
|
---|
568 | *********************************************************************************************************************************/
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Initializes a new Shared Clipboard HTTP server instance.
|
---|
572 | *
|
---|
573 | * @return VBox status code.
|
---|
574 | * @param pSrv HTTP server instance to initialize.
|
---|
575 | */
|
---|
576 | int ShClTransferHttpServerInit(PSHCLHTTPSERVER pSrv)
|
---|
577 | {
|
---|
578 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
579 |
|
---|
580 | return shClTransferHttpServerInitInternal(pSrv);
|
---|
581 | }
|
---|
582 |
|
---|
583 | /**
|
---|
584 | * Returns whether a given TCP port is known to be buggy or not.
|
---|
585 | *
|
---|
586 | * @returns \c true if the given port is known to be buggy, or \c false if not.
|
---|
587 | * @param uPort TCP port to check.
|
---|
588 | */
|
---|
589 | static bool shClTransferHttpServerPortIsBuggy(uint16_t uPort)
|
---|
590 | {
|
---|
591 | uint16_t const aBuggyPorts[] = {
|
---|
592 | #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
|
---|
593 | /* GNOME Nautilus ("Files") v43 is unable download HTTP files from this port. */
|
---|
594 | 8080
|
---|
595 | #else /* Prevents zero-sized arrays. */
|
---|
596 | 0
|
---|
597 | #endif
|
---|
598 | };
|
---|
599 |
|
---|
600 | for (size_t i = 0; i < RT_ELEMENTS(aBuggyPorts); i++)
|
---|
601 | if (uPort == aBuggyPorts[i])
|
---|
602 | return true;
|
---|
603 | return false;
|
---|
604 | }
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Starts the Shared Clipboard HTTP server instance, extended version.
|
---|
608 | *
|
---|
609 | * @returns VBox status code.
|
---|
610 | * @return VERR_ADDRESS_CONFLICT if the port is already taken or the port is known to be buggy.
|
---|
611 | * @param pSrv HTTP server instance to create.
|
---|
612 | * @param uPort TCP port number to use.
|
---|
613 | */
|
---|
614 | int ShClTransferHttpServerStartEx(PSHCLHTTPSERVER pSrv, uint16_t uPort)
|
---|
615 | {
|
---|
616 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
617 | AssertReturn(uPort, VERR_INVALID_PARAMETER);
|
---|
618 |
|
---|
619 | AssertReturn(!shClTransferHttpServerPortIsBuggy(uPort), VERR_ADDRESS_CONFLICT);
|
---|
620 |
|
---|
621 | shClTransferHttpServerLock(pSrv);
|
---|
622 |
|
---|
623 | RTHTTPSERVERCALLBACKS Callbacks;
|
---|
624 | RT_ZERO(Callbacks);
|
---|
625 |
|
---|
626 | Callbacks.pfnRequestBegin = shClTransferHttpBegin;
|
---|
627 | Callbacks.pfnRequestEnd = shClTransferHttpEnd;
|
---|
628 | Callbacks.pfnOpen = shClTransferHttpOpen;
|
---|
629 | Callbacks.pfnRead = shClTransferHttpRead;
|
---|
630 | Callbacks.pfnClose = shClTransferHttpClose;
|
---|
631 | Callbacks.pfnQueryInfo = shClTransferHttpQueryInfo;
|
---|
632 |
|
---|
633 | /* Note: The server always and *only* runs against the localhost interface. */
|
---|
634 | int rc = RTHttpServerCreate(&pSrv->hHTTPServer, "localhost", uPort, &Callbacks,
|
---|
635 | pSrv, sizeof(SHCLHTTPSERVER));
|
---|
636 | if (RT_SUCCESS(rc))
|
---|
637 | {
|
---|
638 | pSrv->uPort = uPort;
|
---|
639 | ASMAtomicXchgBool(&pSrv->fRunning, true);
|
---|
640 |
|
---|
641 | LogRel2(("Shared Clipboard: HTTP server started at port %RU16\n", pSrv->uPort));
|
---|
642 |
|
---|
643 | rc = shclTransferHttpServerSetStatusLocked(pSrv, SHCLHTTPSERVERSTATUS_STARTED);
|
---|
644 | }
|
---|
645 |
|
---|
646 | shClTransferHttpServerUnlock(pSrv);
|
---|
647 |
|
---|
648 | if (RT_FAILURE(rc))
|
---|
649 | LogRel(("Shared Clipboard: HTTP server failed to start, rc=%Rrc\n", rc));
|
---|
650 |
|
---|
651 | return rc;
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Translates a Shared Clipboard HTTP server status to a string.
|
---|
656 | *
|
---|
657 | * @returns Status as a string.
|
---|
658 | * @param uMsg Status to translate.
|
---|
659 | */
|
---|
660 | static const char *shClTransferHttpServerStatusToStr(SHCLHTTPSERVERSTATUS enmStatus)
|
---|
661 | {
|
---|
662 | switch (enmStatus)
|
---|
663 | {
|
---|
664 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_NONE);
|
---|
665 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_STARTED);
|
---|
666 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_STOPPED);
|
---|
667 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_TRANSFER_REGISTERED);
|
---|
668 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_TRANSFER_UNREGISTERED);
|
---|
669 | }
|
---|
670 |
|
---|
671 | AssertFailedReturn("Unknown");
|
---|
672 | }
|
---|
673 |
|
---|
674 | /**
|
---|
675 | * Starts the Shared Clipboard HTTP server instance using a random port (>= 49152).
|
---|
676 | *
|
---|
677 | * This does automatic probing of TCP ports if a port already is being used.
|
---|
678 | *
|
---|
679 | * @returns VBox status code.
|
---|
680 | * @param pSrv HTTP server instance to create.
|
---|
681 | * @param cMaxAttempts Maximum number of attempts to create a HTTP server.
|
---|
682 | * @param puPort Where to return the TCP port number being used on success. Optional.
|
---|
683 | *
|
---|
684 | * @note Complies with RFC 6335 (IANA).
|
---|
685 | */
|
---|
686 | int ShClTransferHttpServerStart(PSHCLHTTPSERVER pSrv, unsigned cMaxAttempts, uint16_t *puPort)
|
---|
687 | {
|
---|
688 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
689 | AssertReturn(cMaxAttempts, VERR_INVALID_PARAMETER);
|
---|
690 | /* puPort is optional. */
|
---|
691 |
|
---|
692 | int rc;
|
---|
693 | #ifdef VBOX_SHCL_DEBUG_HTTPSERVER
|
---|
694 | uint16_t uDebugPort = 49200;
|
---|
695 | rc = ShClTransferHttpServerStartEx(pSrv, (uint32_t)uDebugPort);
|
---|
696 | if (RT_SUCCESS(rc))
|
---|
697 | {
|
---|
698 | if (puPort)
|
---|
699 | *puPort = uDebugPort;
|
---|
700 | }
|
---|
701 | return rc;
|
---|
702 | #endif
|
---|
703 |
|
---|
704 | RTRAND hRand;
|
---|
705 | rc = RTRandAdvCreateSystemFaster(&hRand); /* Should be good enough for this task. */
|
---|
706 | if (RT_SUCCESS(rc))
|
---|
707 | {
|
---|
708 | uint16_t uPort;
|
---|
709 | unsigned i = 0;
|
---|
710 | for (i; i < cMaxAttempts; i++)
|
---|
711 | {
|
---|
712 | /* Try some random ports >= 49152 (i.e. "dynamic ports", see RFC 6335)
|
---|
713 | * -- required, as VBoxClient runs as a user process on the guest. */
|
---|
714 | uPort = RTRandAdvU32Ex(hRand, 49152, UINT16_MAX);
|
---|
715 |
|
---|
716 | /* If the port selected turns is known to be buggy for whatever reason, skip it and try another one. */
|
---|
717 | if (shClTransferHttpServerPortIsBuggy(uPort))
|
---|
718 | continue;
|
---|
719 |
|
---|
720 | rc = ShClTransferHttpServerStartEx(pSrv, (uint32_t)uPort);
|
---|
721 | if (RT_SUCCESS(rc))
|
---|
722 | {
|
---|
723 | if (puPort)
|
---|
724 | *puPort = uPort;
|
---|
725 | break;
|
---|
726 | }
|
---|
727 | }
|
---|
728 |
|
---|
729 | if ( RT_FAILURE(rc)
|
---|
730 | && i == cMaxAttempts)
|
---|
731 | LogRel(("Shared Clipboard: Maximum attempts to start HTTP server reached (%u), giving up\n", cMaxAttempts));
|
---|
732 |
|
---|
733 | RTRandAdvDestroy(hRand);
|
---|
734 | }
|
---|
735 |
|
---|
736 | return rc;
|
---|
737 | }
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * Stops a Shared Clipboard HTTP server instance.
|
---|
741 | *
|
---|
742 | * @returns VBox status code.
|
---|
743 | * @param pSrv HTTP server instance to stop.
|
---|
744 | */
|
---|
745 | int ShClTransferHttpServerStop(PSHCLHTTPSERVER pSrv)
|
---|
746 | {
|
---|
747 | LogFlowFuncEnter();
|
---|
748 |
|
---|
749 | shClTransferHttpServerLock(pSrv);
|
---|
750 |
|
---|
751 | int rc = VINF_SUCCESS;
|
---|
752 |
|
---|
753 | if (pSrv->fRunning)
|
---|
754 | {
|
---|
755 | Assert(pSrv->hHTTPServer != NIL_RTHTTPSERVER);
|
---|
756 |
|
---|
757 | rc = RTHttpServerDestroy(pSrv->hHTTPServer);
|
---|
758 | if (RT_SUCCESS(rc))
|
---|
759 | {
|
---|
760 | pSrv->hHTTPServer = NIL_RTHTTPSERVER;
|
---|
761 | pSrv->fRunning = false;
|
---|
762 |
|
---|
763 | /* Let any eventual waiters know. */
|
---|
764 | shclTransferHttpServerSetStatusLocked(pSrv, SHCLHTTPSERVERSTATUS_STOPPED);
|
---|
765 |
|
---|
766 | LogRel2(("Shared Clipboard: HTTP server stopped\n"));
|
---|
767 | }
|
---|
768 | }
|
---|
769 |
|
---|
770 | if (RT_FAILURE(rc))
|
---|
771 | LogRel(("Shared Clipboard: HTTP server failed to stop, rc=%Rrc\n", rc));
|
---|
772 |
|
---|
773 | shClTransferHttpServerUnlock(pSrv);
|
---|
774 |
|
---|
775 | LogFlowFuncLeaveRC(rc);
|
---|
776 | return rc;
|
---|
777 | }
|
---|
778 |
|
---|
779 | /**
|
---|
780 | * Destroys a Shared Clipboard HTTP server instance.
|
---|
781 | *
|
---|
782 | * @returns VBox status code.
|
---|
783 | * @param pSrv HTTP server instance to destroy.
|
---|
784 | */
|
---|
785 | int ShClTransferHttpServerDestroy(PSHCLHTTPSERVER pSrv)
|
---|
786 | {
|
---|
787 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
788 |
|
---|
789 | int rc = ShClTransferHttpServerStop(pSrv);
|
---|
790 | if (RT_FAILURE(rc))
|
---|
791 | return rc;
|
---|
792 |
|
---|
793 | if (!ASMAtomicReadBool(&pSrv->fInitialized))
|
---|
794 | return VINF_SUCCESS;
|
---|
795 |
|
---|
796 | shClTransferHttpServerLock(pSrv);
|
---|
797 |
|
---|
798 | rc = shClTransferHttpServerDestroyInternal(pSrv);
|
---|
799 |
|
---|
800 | /* Unlock not needed anymore, as the critical section got destroyed. */
|
---|
801 |
|
---|
802 | return rc;
|
---|
803 | }
|
---|
804 |
|
---|
805 | /**
|
---|
806 | * Returns the host name (scheme) of a HTTP server instance.
|
---|
807 | *
|
---|
808 | * @returns Host name (scheme).
|
---|
809 | * @param pSrv HTTP server instance to return host name (scheme) for.
|
---|
810 | *
|
---|
811 | * @note This is hardcoded to "localhost" for now.
|
---|
812 | */
|
---|
813 | static const char *shClTransferHttpServerGetHost(PSHCLHTTPSERVER pSrv)
|
---|
814 | {
|
---|
815 | RT_NOREF(pSrv);
|
---|
816 | return "http://localhost"; /* Hardcoded for now. */
|
---|
817 | }
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * Destroys a server transfer, internal version.
|
---|
821 | *
|
---|
822 | * @returns VBox status code.
|
---|
823 | * @param pSrv HTTP server instance to unregister transfer from.
|
---|
824 | * @param pTransfer Server transfer to destroy
|
---|
825 | * The pointer will be invalid on success.
|
---|
826 | *
|
---|
827 | * @note Caller needs to take the server critical section.
|
---|
828 | */
|
---|
829 | static int shClTransferHttpServerDestroyTransfer(PSHCLHTTPSERVER pSrv, PSHCLHTTPSERVERTRANSFER pSrvTx)
|
---|
830 | {
|
---|
831 | Assert(RTCritSectIsOwner(&pSrv->CritSect));
|
---|
832 |
|
---|
833 | RTListNodeRemove(&pSrvTx->Node);
|
---|
834 |
|
---|
835 | Assert(pSrv->cTransfers);
|
---|
836 | pSrv->cTransfers--;
|
---|
837 |
|
---|
838 | LogFunc(("pTransfer=%p, idTransfer=%RU16, szPath=%s -> %RU32 transfers\n",
|
---|
839 | pSrvTx->pTransfer, pSrvTx->pTransfer->State.uID, pSrvTx->szPathVirtual, pSrv->cTransfers));
|
---|
840 |
|
---|
841 | LogRel2(("Shared Clipboard: Destroyed HTTP transfer %RU16, now %RU32 HTTP transfers total\n",
|
---|
842 | pSrvTx->pTransfer->State.uID, pSrv->cTransfers));
|
---|
843 |
|
---|
844 | if (RTCritSectIsInitialized(&pSrvTx->CritSect))
|
---|
845 | {
|
---|
846 | int rc = RTCritSectDelete(&pSrvTx->CritSect);
|
---|
847 | AssertRCReturn(rc, rc);
|
---|
848 | }
|
---|
849 |
|
---|
850 | RTMemFree(pSrvTx);
|
---|
851 | pSrvTx = NULL;
|
---|
852 |
|
---|
853 | return VINF_SUCCESS;
|
---|
854 | }
|
---|
855 |
|
---|
856 |
|
---|
857 | /*********************************************************************************************************************************
|
---|
858 | * Public Shared Clipboard HTTP server functions *
|
---|
859 | *********************************************************************************************************************************/
|
---|
860 |
|
---|
861 | /**
|
---|
862 | * Registers a Shared Clipboard transfer to a HTTP server instance.
|
---|
863 | *
|
---|
864 | * @returns VBox status code.
|
---|
865 | * @param pSrv HTTP server instance to register transfer for.
|
---|
866 | * @param pTransfer Transfer to register. Needs to be on the heap.
|
---|
867 | */
|
---|
868 | int ShClTransferHttpServerRegisterTransfer(PSHCLHTTPSERVER pSrv, PSHCLTRANSFER pTransfer)
|
---|
869 | {
|
---|
870 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
871 | AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
|
---|
872 |
|
---|
873 | AssertMsgReturn(pTransfer->State.uID, ("Transfer needs to be registered with a transfer context first\n"),
|
---|
874 | VERR_INVALID_PARAMETER);
|
---|
875 |
|
---|
876 | uint64_t const cRoots = ShClTransferRootsCount(pTransfer);
|
---|
877 | AssertMsgReturn(cRoots > 0, ("Transfer has no root entries\n"), VERR_INVALID_PARAMETER);
|
---|
878 | /** @todo Check for directories? */
|
---|
879 |
|
---|
880 | shClTransferHttpServerLock(pSrv);
|
---|
881 |
|
---|
882 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)RTMemAllocZ(sizeof(SHCLHTTPSERVERTRANSFER));
|
---|
883 | AssertPtrReturn(pSrvTx, VERR_NO_MEMORY);
|
---|
884 |
|
---|
885 | RTUUID Uuid;
|
---|
886 | int rc = RTUuidCreate(&Uuid);
|
---|
887 | if (RT_SUCCESS(rc))
|
---|
888 | {
|
---|
889 | char szUuid[64];
|
---|
890 | rc = RTUuidToStr(&Uuid, szUuid, sizeof(szUuid));
|
---|
891 | if (RT_SUCCESS(rc))
|
---|
892 | {
|
---|
893 | rc = RTCritSectInit(&pSrvTx->CritSect);
|
---|
894 | AssertRCReturn(rc, rc);
|
---|
895 |
|
---|
896 | /* Create the virtual HTTP path for the transfer.
|
---|
897 | * Every transfer has a dedicated HTTP path (but live in the same URL namespace). */
|
---|
898 | char *pszPath;
|
---|
899 | #ifdef VBOX_SHCL_DEBUG_HTTPSERVER
|
---|
900 | # ifdef DEBUG_andy /** Too lazy to specify a different transfer ID for debugging. */
|
---|
901 | ssize_t cch = RTStrAPrintf(&pszPath, "//transfer");
|
---|
902 | # else
|
---|
903 | ssize_t cch = RTStrAPrintf(&pszPath, "//transfer%RU16", pTransfer->State.uID);
|
---|
904 | # endif
|
---|
905 | #else /* Release mode */
|
---|
906 | ssize_t cch = RTStrAPrintf(&pszPath, "//%s/%s", SHCL_HTTPT_URL_NAMESPACE, szUuid);
|
---|
907 | #endif
|
---|
908 | AssertReturn(cch, VERR_NO_MEMORY);
|
---|
909 |
|
---|
910 | const char szScheme[] = "http"; /** @todo For now we only support HTTP. */
|
---|
911 | const size_t cchScheme = strlen(szScheme) + 3 /* "://" */;
|
---|
912 |
|
---|
913 | char *pszURI = RTUriCreate(szScheme, NULL /* pszAuthority */, pszPath, NULL /* pszQuery */, NULL /* pszFragment */);
|
---|
914 | if (pszURI)
|
---|
915 | {
|
---|
916 | if (strlen(pszURI) >= cchScheme)
|
---|
917 | {
|
---|
918 | /* For the virtual path we only keep everything after the full scheme (e.g. "http://").
|
---|
919 | * The virtual path always has to start with a "/". */
|
---|
920 | if (RTStrPrintf2(pSrvTx->szPathVirtual, sizeof(pSrvTx->szPathVirtual), "/%s", pszURI + cchScheme) <= 0)
|
---|
921 | rc = VERR_BUFFER_OVERFLOW;
|
---|
922 | }
|
---|
923 | else
|
---|
924 | rc = VERR_INVALID_PARAMETER;
|
---|
925 |
|
---|
926 | RTStrFree(pszURI);
|
---|
927 | pszURI = NULL;
|
---|
928 | }
|
---|
929 | else
|
---|
930 | rc = VERR_NO_MEMORY;
|
---|
931 |
|
---|
932 | RTStrFree(pszPath);
|
---|
933 | pszPath = NULL;
|
---|
934 |
|
---|
935 | AssertRCReturn(rc, rc);
|
---|
936 |
|
---|
937 | pSrvTx->pTransfer = pTransfer;
|
---|
938 | pSrvTx->hObj = NIL_SHCLOBJHANDLE;
|
---|
939 |
|
---|
940 | RTListAppend(&pSrv->lstTransfers, &pSrvTx->Node);
|
---|
941 | pSrv->cTransfers++;
|
---|
942 |
|
---|
943 | shclTransferHttpServerSetStatusLocked(pSrv, SHCLHTTPSERVERSTATUS_TRANSFER_REGISTERED);
|
---|
944 |
|
---|
945 | LogFunc(("pTransfer=%p, idTransfer=%RU16, szPath=%s -> %RU32 transfers\n",
|
---|
946 | pSrvTx->pTransfer, pSrvTx->pTransfer->State.uID, pSrvTx->szPathVirtual, pSrv->cTransfers));
|
---|
947 |
|
---|
948 | LogRel2(("Shared Clipboard: Registered HTTP transfer %RU16, now %RU32 HTTP transfers total\n",
|
---|
949 | pTransfer->State.uID, pSrv->cTransfers));
|
---|
950 | }
|
---|
951 |
|
---|
952 | }
|
---|
953 |
|
---|
954 | if (RT_FAILURE(rc))
|
---|
955 | RTMemFree(pSrvTx);
|
---|
956 |
|
---|
957 | shClTransferHttpServerUnlock(pSrv);
|
---|
958 |
|
---|
959 | LogFlowFuncLeaveRC(rc);
|
---|
960 | return rc;
|
---|
961 | }
|
---|
962 |
|
---|
963 | /**
|
---|
964 | * Unregisters a formerly registered Shared Clipboard transfer.
|
---|
965 | *
|
---|
966 | * @returns VBox status code.
|
---|
967 | * @param pSrv HTTP server instance to unregister transfer from.
|
---|
968 | * @param pTransfer Transfer to unregister.
|
---|
969 | */
|
---|
970 | int ShClTransferHttpServerUnregisterTransfer(PSHCLHTTPSERVER pSrv, PSHCLTRANSFER pTransfer)
|
---|
971 | {
|
---|
972 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
973 | AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
|
---|
974 |
|
---|
975 | shClTransferHttpServerLock(pSrv);
|
---|
976 |
|
---|
977 | int rc = VINF_SUCCESS;
|
---|
978 |
|
---|
979 | PSHCLHTTPSERVERTRANSFER pSrvTx, pSrvTxNext;
|
---|
980 | RTListForEachSafe(&pSrv->lstTransfers, pSrvTx, pSrvTxNext, SHCLHTTPSERVERTRANSFER, Node)
|
---|
981 | {
|
---|
982 | AssertPtr(pSrvTx->pTransfer);
|
---|
983 | if (pSrvTx->pTransfer->State.uID == pTransfer->State.uID)
|
---|
984 | {
|
---|
985 | rc = shClTransferHttpServerDestroyTransfer(pSrv, pSrvTx);
|
---|
986 | if (RT_SUCCESS(rc))
|
---|
987 | shclTransferHttpServerSetStatusLocked(pSrv, SHCLHTTPSERVERSTATUS_TRANSFER_UNREGISTERED);
|
---|
988 | break;
|
---|
989 | }
|
---|
990 | }
|
---|
991 |
|
---|
992 | shClTransferHttpServerUnlock(pSrv);
|
---|
993 |
|
---|
994 | LogFlowFuncLeaveRC(rc);
|
---|
995 | return rc;
|
---|
996 | }
|
---|
997 |
|
---|
998 | /**
|
---|
999 | * Sets a new status.
|
---|
1000 | *
|
---|
1001 | * @returns New status set.
|
---|
1002 | * @param pSrv HTTP server instance to set status for.
|
---|
1003 | * @param fStatus New status to set.
|
---|
1004 | *
|
---|
1005 | * @note Caller needs to take critical section.
|
---|
1006 | */
|
---|
1007 | static SHCLHTTPSERVERSTATUS shclTransferHttpServerSetStatusLocked(PSHCLHTTPSERVER pSrv, SHCLHTTPSERVERSTATUS enmStatus)
|
---|
1008 | {
|
---|
1009 | Assert(RTCritSectIsOwner(&pSrv->CritSect));
|
---|
1010 |
|
---|
1011 | /* Bogus checks. */
|
---|
1012 | Assert(!(enmStatus & SHCLHTTPSERVERSTATUS_NONE) || enmStatus == SHCLHTTPSERVERSTATUS_NONE);
|
---|
1013 |
|
---|
1014 | pSrv->enmStatus = enmStatus;
|
---|
1015 | LogFlowFunc(("fStatus=%#x\n", pSrv->enmStatus));
|
---|
1016 |
|
---|
1017 | int rc2 = RTSemEventSignal(pSrv->StatusEvent);
|
---|
1018 | AssertRC(rc2);
|
---|
1019 |
|
---|
1020 | return pSrv->enmStatus;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | /**
|
---|
1024 | * Returns the first transfer in the list.
|
---|
1025 | *
|
---|
1026 | * @returns Pointer to first transfer if found, or NULL if not found.
|
---|
1027 | * @param pSrv HTTP server instance.
|
---|
1028 | */
|
---|
1029 | PSHCLTRANSFER ShClTransferHttpServerGetTransferFirst(PSHCLHTTPSERVER pSrv)
|
---|
1030 | {
|
---|
1031 | shClTransferHttpServerLock(pSrv);
|
---|
1032 |
|
---|
1033 | PSHCLHTTPSERVERTRANSFER pHttpTransfer = RTListGetFirst(&pSrv->lstTransfers, SHCLHTTPSERVERTRANSFER, Node);
|
---|
1034 |
|
---|
1035 | shClTransferHttpServerUnlock(pSrv);
|
---|
1036 |
|
---|
1037 | return pHttpTransfer ? pHttpTransfer->pTransfer : NULL;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | /**
|
---|
1041 | * Returns the last transfer in the list.
|
---|
1042 | *
|
---|
1043 | * @returns Pointer to last transfer if found, or NULL if not found.
|
---|
1044 | * @param pSrv HTTP server instance.
|
---|
1045 | */
|
---|
1046 | PSHCLTRANSFER ShClTransferHttpServerGetTransferLast(PSHCLHTTPSERVER pSrv)
|
---|
1047 | {
|
---|
1048 | shClTransferHttpServerLock(pSrv);
|
---|
1049 |
|
---|
1050 | PSHCLHTTPSERVERTRANSFER pHttpTransfer = RTListGetLast(&pSrv->lstTransfers, SHCLHTTPSERVERTRANSFER, Node);
|
---|
1051 |
|
---|
1052 | shClTransferHttpServerUnlock(pSrv);
|
---|
1053 |
|
---|
1054 | return pHttpTransfer ? pHttpTransfer->pTransfer : NULL;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | /**
|
---|
1058 | * Returns a transfer for a specific ID.
|
---|
1059 | *
|
---|
1060 | * @returns Pointer to the transfer if found, or NULL if not found.
|
---|
1061 | * @param pSrv HTTP server instance.
|
---|
1062 | * @param idTransfer Transfer ID of transfer to return..
|
---|
1063 | */
|
---|
1064 | bool ShClTransferHttpServerGetTransfer(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer)
|
---|
1065 | {
|
---|
1066 | AssertPtrReturn(pSrv, false);
|
---|
1067 |
|
---|
1068 | shClTransferHttpServerLock(pSrv);
|
---|
1069 |
|
---|
1070 | PSHCLHTTPSERVERTRANSFER pTransfer = shClTransferHttpServerGetTransferById(pSrv, idTransfer);
|
---|
1071 |
|
---|
1072 | shClTransferHttpServerUnlock(pSrv);
|
---|
1073 |
|
---|
1074 | return pTransfer;
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /**
|
---|
1078 | * Returns the used TCP port number of a HTTP server instance.
|
---|
1079 | *
|
---|
1080 | * @returns TCP port number. 0 if not specified yet.
|
---|
1081 | * @param pSrv HTTP server instance to return port for.
|
---|
1082 | */
|
---|
1083 | uint16_t ShClTransferHttpServerGetPort(PSHCLHTTPSERVER pSrv)
|
---|
1084 | {
|
---|
1085 | AssertPtrReturn(pSrv, 0);
|
---|
1086 |
|
---|
1087 | shClTransferHttpServerLock(pSrv);
|
---|
1088 |
|
---|
1089 | const uint16_t uPort = pSrv->uPort;
|
---|
1090 |
|
---|
1091 | shClTransferHttpServerUnlock(pSrv);
|
---|
1092 |
|
---|
1093 | return uPort;
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | /**
|
---|
1097 | * Returns the number of registered HTTP server transfers of a HTTP server instance.
|
---|
1098 | *
|
---|
1099 | * @returns Number of registered transfers.
|
---|
1100 | * @param pSrv HTTP server instance to return registered transfers for.
|
---|
1101 | */
|
---|
1102 | uint32_t ShClTransferHttpServerGetTransferCount(PSHCLHTTPSERVER pSrv)
|
---|
1103 | {
|
---|
1104 | AssertPtrReturn(pSrv, 0);
|
---|
1105 |
|
---|
1106 | shClTransferHttpServerLock(pSrv);
|
---|
1107 |
|
---|
1108 | const uint32_t cTransfers = pSrv->cTransfers;
|
---|
1109 | LogFlowFunc(("cTransfers=%RU32\n", cTransfers));
|
---|
1110 |
|
---|
1111 | shClTransferHttpServerUnlock(pSrv);
|
---|
1112 |
|
---|
1113 | return cTransfers;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | /**
|
---|
1117 | * Returns an allocated string with a HTTP server instance's address.
|
---|
1118 | *
|
---|
1119 | * @returns Allocated string with a HTTP server instance's address, or NULL on OOM.
|
---|
1120 | * Needs to be free'd by the caller using RTStrFree().
|
---|
1121 | * @param pSrv HTTP server instance to return address for.
|
---|
1122 | */
|
---|
1123 | char *ShClTransferHttpServerGetAddressA(PSHCLHTTPSERVER pSrv)
|
---|
1124 | {
|
---|
1125 | AssertPtrReturn(pSrv, NULL);
|
---|
1126 |
|
---|
1127 | shClTransferHttpServerLock(pSrv);
|
---|
1128 |
|
---|
1129 | char *pszAddress = RTStrAPrintf2("%s:%RU16", shClTransferHttpServerGetHost(pSrv), pSrv->uPort);
|
---|
1130 | AssertPtr(pszAddress);
|
---|
1131 |
|
---|
1132 | shClTransferHttpServerUnlock(pSrv);
|
---|
1133 |
|
---|
1134 | return pszAddress;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | /**
|
---|
1138 | * Returns an allocated string with the URL of a given Shared Clipboard transfer ID.
|
---|
1139 | *
|
---|
1140 | * @returns Allocated string with the URL of a given Shared Clipboard transfer ID, or NULL if not found.
|
---|
1141 | * Needs to be free'd by the caller using RTStrFree().
|
---|
1142 | * @param pSrv HTTP server instance to return URL for.
|
---|
1143 | * @param idTransfer Transfer ID to return the URL for.
|
---|
1144 | * @param idxEntry Index of transfer entry to return URL for.
|
---|
1145 | */
|
---|
1146 | char *ShClTransferHttpServerGetUrlA(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer, uint64_t idxEntry)
|
---|
1147 | {
|
---|
1148 | AssertPtrReturn(pSrv, NULL);
|
---|
1149 | AssertReturn(idTransfer != NIL_SHCLTRANSFERID, NULL);
|
---|
1150 |
|
---|
1151 | shClTransferHttpServerLock(pSrv);
|
---|
1152 |
|
---|
1153 | PSHCLHTTPSERVERTRANSFER pSrvTx = shClTransferHttpServerGetTransferById(pSrv, idTransfer);
|
---|
1154 | if (!pSrvTx)
|
---|
1155 | {
|
---|
1156 | AssertFailed();
|
---|
1157 | shClTransferHttpServerUnlock(pSrv);
|
---|
1158 | return NULL;
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | PSHCLTRANSFER pTx = pSrvTx->pTransfer;
|
---|
1162 | AssertPtr(pTx);
|
---|
1163 |
|
---|
1164 | char *pszUrl = NULL;
|
---|
1165 |
|
---|
1166 | /* For now this only supports root entries. */
|
---|
1167 | PCSHCLLISTENTRY pEntry = ShClTransferRootsEntryGet(pTx, idxEntry);
|
---|
1168 | if (pEntry)
|
---|
1169 | {
|
---|
1170 | AssertReturn(RTStrNLen(pSrvTx->szPathVirtual, RTPATH_MAX), NULL);
|
---|
1171 | pszUrl = RTStrAPrintf2("%s:%RU16%s/%s", shClTransferHttpServerGetHost(pSrv), pSrv->uPort, pSrvTx->szPathVirtual, pEntry->pszName);
|
---|
1172 | AssertPtr(pszUrl);
|
---|
1173 |
|
---|
1174 | shClTransferHttpServerUnlock(pSrv);
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | return pszUrl;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | /**
|
---|
1181 | * Returns whether a given HTTP server instance is initialized or not.
|
---|
1182 | *
|
---|
1183 | * @returns \c true if running, or \c false if not.
|
---|
1184 | * @param pSrv HTTP server instance to check initialized state for.
|
---|
1185 | */
|
---|
1186 | bool ShClTransferHttpServerIsInitialized(PSHCLHTTPSERVER pSrv)
|
---|
1187 | {
|
---|
1188 | AssertPtrReturn(pSrv, false);
|
---|
1189 |
|
---|
1190 | return ASMAtomicReadBool(&pSrv->fInitialized);
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | /**
|
---|
1194 | * Returns whether a given HTTP server instance is running or not.
|
---|
1195 | *
|
---|
1196 | * @returns \c true if running, or \c false if not.
|
---|
1197 | * @param pSrv HTTP server instance to check running state for.
|
---|
1198 | */
|
---|
1199 | bool ShClTransferHttpServerIsRunning(PSHCLHTTPSERVER pSrv)
|
---|
1200 | {
|
---|
1201 | AssertPtrReturn(pSrv, false);
|
---|
1202 |
|
---|
1203 | return ASMAtomicReadBool(&pSrv->fRunning);
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | /**
|
---|
1207 | * Waits for a status change.
|
---|
1208 | *
|
---|
1209 | * @returns VBox status code.
|
---|
1210 | * @retval VERR_STATE_CHANGED if the HTTP server was uninitialized.
|
---|
1211 | * @param pSrv HTTP server instance to wait for.
|
---|
1212 | * @param fStatus Status to wait for.
|
---|
1213 | * Multiple statuses are possible, @sa SHCLHTTPSERVERSTATUS.
|
---|
1214 | * @param msTimeout Timeout (in ms) to wait.
|
---|
1215 | */
|
---|
1216 | int ShClTransferHttpServerWaitForStatusChange(PSHCLHTTPSERVER pSrv, SHCLHTTPSERVERSTATUS fStatus, RTMSINTERVAL msTimeout)
|
---|
1217 | {
|
---|
1218 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
1219 | AssertMsgReturn(ASMAtomicReadBool(&pSrv->fInitialized), ("Server not initialized yet\n"), VERR_WRONG_ORDER);
|
---|
1220 |
|
---|
1221 | shClTransferHttpServerLock(pSrv);
|
---|
1222 |
|
---|
1223 | uint64_t const tsStartMs = RTTimeMilliTS();
|
---|
1224 |
|
---|
1225 | int rc = VERR_TIMEOUT;
|
---|
1226 |
|
---|
1227 | LogFlowFunc(("fStatus=%#x, msTimeout=%RU32 -- current is %#x\n", fStatus, msTimeout, pSrv->enmStatus));
|
---|
1228 |
|
---|
1229 | while (RTTimeMilliTS() - tsStartMs <= msTimeout)
|
---|
1230 | {
|
---|
1231 | if (!pSrv->fInitialized)
|
---|
1232 | {
|
---|
1233 | rc = VERR_STATE_CHANGED;
|
---|
1234 | break;
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | shClTransferHttpServerUnlock(pSrv); /* Leave lock before waiting. */
|
---|
1238 |
|
---|
1239 | rc = RTSemEventWait(pSrv->StatusEvent, msTimeout);
|
---|
1240 |
|
---|
1241 | shClTransferHttpServerLock(pSrv);
|
---|
1242 |
|
---|
1243 | if (RT_FAILURE(rc))
|
---|
1244 | break;
|
---|
1245 |
|
---|
1246 | LogFlowFunc(("Current status now is: %#x\n", pSrv->enmStatus));
|
---|
1247 | LogRel2(("Shared Clipboard: HTTP server entered status '%s'\n", shClTransferHttpServerStatusToStr(pSrv->enmStatus)));
|
---|
1248 |
|
---|
1249 | if (pSrv->enmStatus & fStatus)
|
---|
1250 | {
|
---|
1251 | rc = VINF_SUCCESS;
|
---|
1252 | break;
|
---|
1253 | }
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | shClTransferHttpServerUnlock(pSrv);
|
---|
1257 |
|
---|
1258 | LogFlowFuncLeaveRC(rc);
|
---|
1259 | return rc;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 |
|
---|
1263 | /*********************************************************************************************************************************
|
---|
1264 | * Public Shared Clipboard HTTP context functions *
|
---|
1265 | *********************************************************************************************************************************/
|
---|
1266 |
|
---|
1267 | /**
|
---|
1268 | * Starts the HTTP server, if not started already.
|
---|
1269 | *
|
---|
1270 | * @returns VBox status code.
|
---|
1271 | * @param pCtx HTTP context to start HTTP server for.
|
---|
1272 | */
|
---|
1273 | int ShClTransferHttpServerMaybeStart(PSHCLHTTPCONTEXT pCtx)
|
---|
1274 | {
|
---|
1275 | int rc = VINF_SUCCESS;
|
---|
1276 |
|
---|
1277 | LogFlowFuncEnter();
|
---|
1278 |
|
---|
1279 | /* Start the built-in HTTP server to serve file(s). */
|
---|
1280 | if (!ShClTransferHttpServerIsRunning(&pCtx->HttpServer)) /* Only one HTTP server per transfer context. */
|
---|
1281 | rc = ShClTransferHttpServerStart(&pCtx->HttpServer, 32 /* cMaxAttempts */, NULL /* puPort */);
|
---|
1282 |
|
---|
1283 | LogFlowFuncLeaveRC(rc);
|
---|
1284 | return rc;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | /**
|
---|
1288 | * Stops the HTTP server, if no running transfers are left.
|
---|
1289 | *
|
---|
1290 | * @returns VBox status code.
|
---|
1291 | * @param pCtx HTTP context to stop HTTP server for.
|
---|
1292 | */
|
---|
1293 | int ShClTransferHttpServerMaybeStop(PSHCLHTTPCONTEXT pCtx)
|
---|
1294 | {
|
---|
1295 | int rc = VINF_SUCCESS;
|
---|
1296 |
|
---|
1297 | LogFlowFuncEnter();
|
---|
1298 |
|
---|
1299 | if (ShClTransferHttpServerIsRunning(&pCtx->HttpServer))
|
---|
1300 | {
|
---|
1301 | /* No more registered transfers left? Tear down the HTTP server instance then. */
|
---|
1302 | if (ShClTransferHttpServerGetTransferCount(&pCtx->HttpServer) == 0)
|
---|
1303 | rc = ShClTransferHttpServerStop(&pCtx->HttpServer);
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | LogFlowFuncLeaveRC(rc);
|
---|
1307 | return rc;
|
---|
1308 | }
|
---|
1309 |
|
---|