1 | /* $Id: clipboard-transfers-http.cpp 103132 2024-01-31 09:01:15Z 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 "/". Unescaped. */
|
---|
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 | * Creates an URL from a given path, extended version.
|
---|
173 | *
|
---|
174 | * @returns VBox status code.
|
---|
175 | * @retval VERR_INVALID_PARAMETER if the path is not valid.
|
---|
176 | * @param pszPath Path to create URL for.
|
---|
177 | * @param ppszURL Where to return the allocated URL on success.
|
---|
178 | * @param pchScheme Where to return the size of the full HTTP scheme including "://". Optional and can be NULL.
|
---|
179 | * Right now this always is sizeof("http://").
|
---|
180 | *
|
---|
181 | * @note The path is not checked on file system level.
|
---|
182 | */
|
---|
183 | static int shClTransferHttpURLCreateFromPathEx(const char *pszPath, char **ppszURL, size_t *pchScheme)
|
---|
184 | {
|
---|
185 | AssertRCReturn(ShClTransferValidatePath(pszPath, false /* fMustExist */), VERR_INVALID_PARAMETER);
|
---|
186 |
|
---|
187 | int rc = VINF_SUCCESS;
|
---|
188 |
|
---|
189 | const char szScheme[] = "http://"; /** @todo For now we only support HTTP. */
|
---|
190 | const size_t cchScheme = strlen(szScheme);
|
---|
191 |
|
---|
192 | char *pszURL = RTStrAPrintf2("%s%s", szScheme, pszPath);
|
---|
193 | if (pszURL)
|
---|
194 | {
|
---|
195 | AssertReturn(strlen(pszURL) > cchScheme, VERR_INVALID_PARAMETER);
|
---|
196 |
|
---|
197 | *ppszURL = pszURL;
|
---|
198 | if (pchScheme)
|
---|
199 | *pchScheme = cchScheme;
|
---|
200 | }
|
---|
201 | else
|
---|
202 | rc = VERR_NO_MEMORY;
|
---|
203 |
|
---|
204 | return rc;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Creates an URL from a given path.
|
---|
209 | *
|
---|
210 | * @returns VBox status code.
|
---|
211 | * @retval VERR_INVALID_PARAMETER if the path is not valid.
|
---|
212 | * @param pszPath Path to create URL for.
|
---|
213 | * @param ppszURL Where to return the allocated URL on success.
|
---|
214 | *
|
---|
215 | * @note The path is not checked on file system level.
|
---|
216 | */
|
---|
217 | static int shClTransferHttpURLCreateFromPath(const char *pszPath, char **ppszURL)
|
---|
218 | {
|
---|
219 | return shClTransferHttpURLCreateFromPathEx(pszPath, ppszURL, NULL /* pchScheme */);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Return the HTTP server transfer for a specific transfer ID.
|
---|
224 | *
|
---|
225 | * @returns Pointer to HTTP server transfer if found, NULL if not found.
|
---|
226 | * @param pSrv HTTP server instance.
|
---|
227 | * @param idTransfer Transfer ID to return HTTP server transfer for.
|
---|
228 | */
|
---|
229 | static PSHCLHTTPSERVERTRANSFER shClTransferHttpServerGetTransferById(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer)
|
---|
230 | {
|
---|
231 | PSHCLHTTPSERVERTRANSFER pSrvTx;
|
---|
232 | RTListForEach(&pSrv->lstTransfers, pSrvTx, SHCLHTTPSERVERTRANSFER, Node) /** @todo Slow O(n) lookup, but does it for now. */
|
---|
233 | {
|
---|
234 | if (pSrvTx->pTransfer->State.uID == idTransfer)
|
---|
235 | return pSrvTx;
|
---|
236 | }
|
---|
237 |
|
---|
238 | return NULL;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Returns a HTTP server transfer from a given URL.
|
---|
243 | *
|
---|
244 | * @returns Pointer to HTTP server transfer if found, NULL if not found.
|
---|
245 | * @param pThis HTTP server instance data.
|
---|
246 | * @param pszUrl URL to validate.
|
---|
247 | */
|
---|
248 | DECLINLINE(PSHCLHTTPSERVERTRANSFER) shClTransferHttpGetTransferFromUrl(PSHCLHTTPSERVER pThis, const char *pszUrl)
|
---|
249 | {
|
---|
250 | AssertPtrReturn(pszUrl, NULL);
|
---|
251 |
|
---|
252 | PSHCLHTTPSERVERTRANSFER pSrvTx = NULL;
|
---|
253 |
|
---|
254 | PSHCLHTTPSERVERTRANSFER pSrvTxCur;
|
---|
255 | RTListForEach(&pThis->lstTransfers, pSrvTxCur, SHCLHTTPSERVERTRANSFER, Node)
|
---|
256 | {
|
---|
257 | AssertPtr(pSrvTxCur->pTransfer);
|
---|
258 |
|
---|
259 | LogFlowFunc(("pSrvTxCur=%s\n", pSrvTxCur->szPathVirtual));
|
---|
260 |
|
---|
261 | /* Be picky here, do a case sensitive comparison. */
|
---|
262 | if (RTStrStartsWith(pszUrl, pSrvTxCur->szPathVirtual))
|
---|
263 | {
|
---|
264 | pSrvTx = pSrvTxCur;
|
---|
265 | break;
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (!pSrvTx)
|
---|
270 | LogRel2(("Shared Clipboard: HTTP URL '%s' not valid\n", pszUrl));
|
---|
271 |
|
---|
272 | LogFlowFunc(("pszUrl=%s, pSrvTx=%p\n", pszUrl, pSrvTx));
|
---|
273 | return pSrvTx;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Returns a HTTP server transfer from an internal HTTP handle.
|
---|
278 | *
|
---|
279 | * @returns Pointer to HTTP server transfer if found, NULL if not found.
|
---|
280 | * @param pThis HTTP server instance data.
|
---|
281 | * @param pvHandle Handle to return transfer for.
|
---|
282 | */
|
---|
283 | DECLINLINE(PSHCLHTTPSERVERTRANSFER) shClTransferHttpGetTransferFromHandle(PSHCLHTTPSERVER pThis, void *pvHandle)
|
---|
284 | {
|
---|
285 | AssertPtrReturn(pvHandle, NULL);
|
---|
286 |
|
---|
287 | const SHCLTRANSFERID uHandle = *(uint16_t *)pvHandle;
|
---|
288 |
|
---|
289 | /** @ŧodo Use a handle lookup table (map) later. */
|
---|
290 | PSHCLHTTPSERVERTRANSFER pSrvTxCur;
|
---|
291 | RTListForEach(&pThis->lstTransfers, pSrvTxCur, SHCLHTTPSERVERTRANSFER, Node)
|
---|
292 | {
|
---|
293 | AssertPtr(pSrvTxCur->pTransfer);
|
---|
294 |
|
---|
295 | if (pSrvTxCur->pTransfer->State.uID == uHandle) /** @ŧodo We're using the transfer ID as handle for now. */
|
---|
296 | return pSrvTxCur;
|
---|
297 | }
|
---|
298 |
|
---|
299 | return NULL;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | /*********************************************************************************************************************************
|
---|
304 | * HTTP server callback implementations *
|
---|
305 | *********************************************************************************************************************************/
|
---|
306 |
|
---|
307 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnRequestBegin */
|
---|
308 | static DECLCALLBACK(int) shClTransferHttpBegin(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq)
|
---|
309 | {
|
---|
310 | PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser; RT_NOREF(pThis);
|
---|
311 | Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
|
---|
312 |
|
---|
313 | LogRel2(("Shared Clipboard: HTTP request begin\n"));
|
---|
314 |
|
---|
315 | PSHCLHTTPSERVERTRANSFER pSrvTx = shClTransferHttpGetTransferFromUrl(pThis, pReq->pszUrl);
|
---|
316 | if (pSrvTx)
|
---|
317 | {
|
---|
318 | pReq->pvUser = pSrvTx;
|
---|
319 | }
|
---|
320 |
|
---|
321 | return VINF_SUCCESS;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnRequestEnd */
|
---|
325 | static DECLCALLBACK(int) shClTransferHttpEnd(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq)
|
---|
326 | {
|
---|
327 | PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser; RT_NOREF(pThis);
|
---|
328 | Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
|
---|
329 |
|
---|
330 | LogRel2(("Shared Clipboard: HTTP request end\n"));
|
---|
331 |
|
---|
332 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
333 | if (pSrvTx)
|
---|
334 | {
|
---|
335 | pReq->pvUser = NULL;
|
---|
336 | }
|
---|
337 |
|
---|
338 | return VINF_SUCCESS;
|
---|
339 |
|
---|
340 | }
|
---|
341 |
|
---|
342 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnOpen */
|
---|
343 | static DECLCALLBACK(int) shClTransferHttpOpen(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void **ppvHandle)
|
---|
344 | {
|
---|
345 | PSHCLHTTPSERVER pThis = (PSHCLHTTPSERVER)pData->pvUser; RT_NOREF(pThis);
|
---|
346 | Assert(pData->cbUser == sizeof(SHCLHTTPSERVER));
|
---|
347 |
|
---|
348 | int rc;
|
---|
349 |
|
---|
350 | AssertPtr(pReq->pvUser);
|
---|
351 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
352 | if (pSrvTx)
|
---|
353 | {
|
---|
354 | LogRel2(("Shared Clipboard: HTTP transfer (handle %RU64) started ...\n", pSrvTx->hObj));
|
---|
355 |
|
---|
356 | Assert(pSrvTx->hObj != NIL_SHCLOBJHANDLE);
|
---|
357 | *ppvHandle = &pSrvTx->hObj;
|
---|
358 | rc = VINF_SUCCESS;
|
---|
359 | }
|
---|
360 | else
|
---|
361 | rc = VERR_NOT_FOUND;
|
---|
362 |
|
---|
363 | if (RT_FAILURE(rc))
|
---|
364 | LogRel(("Shared Clipboard: Error starting HTTP transfer for '%s', rc=%Rrc\n", pReq->pszUrl, rc));
|
---|
365 |
|
---|
366 | LogFlowFuncLeaveRC(rc);
|
---|
367 | return rc;
|
---|
368 | }
|
---|
369 |
|
---|
370 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnRead */
|
---|
371 | static DECLCALLBACK(int) shClTransferHttpRead(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq,
|
---|
372 | void *pvHandle, void *pvBuf, size_t cbBuf, size_t *pcbRead)
|
---|
373 | {
|
---|
374 | RT_NOREF(pData);
|
---|
375 |
|
---|
376 | if (pvHandle == NULL) /* Serve a 404 page if we got an invalid handle. */
|
---|
377 | {
|
---|
378 | Assert(cbBuf >= sizeof(s_shClHttpServerPage404)); /* Keep it simple for now. */
|
---|
379 | memcpy(pvBuf, &s_shClHttpServerPage404, RT_MIN(cbBuf, sizeof(s_shClHttpServerPage404)));
|
---|
380 | *pcbRead = sizeof(s_shClHttpServerPage404);
|
---|
381 | return VINF_SUCCESS;
|
---|
382 | }
|
---|
383 |
|
---|
384 | int rc;
|
---|
385 |
|
---|
386 | LogRel3(("Shared Clipboard: Reading %RU32 bytes from HTTP ...\n", cbBuf));
|
---|
387 |
|
---|
388 | AssertPtr(pReq->pvUser);
|
---|
389 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
390 | if (pSrvTx)
|
---|
391 | {
|
---|
392 | PSHCLOBJHANDLE phObj = (PSHCLOBJHANDLE)pvHandle;
|
---|
393 | if (phObj)
|
---|
394 | {
|
---|
395 | uint32_t cbRead;
|
---|
396 | rc = ShClTransferObjRead(pSrvTx->pTransfer, *phObj, pvBuf, cbBuf, 0 /* fFlags */, &cbRead);
|
---|
397 | if (RT_SUCCESS(rc))
|
---|
398 | *pcbRead = (uint32_t)cbRead;
|
---|
399 |
|
---|
400 | if (RT_FAILURE(rc))
|
---|
401 | LogRel(("Shared Clipboard: Error reading HTTP transfer (handle %RU64), rc=%Rrc\n", *phObj, rc));
|
---|
402 | }
|
---|
403 | else
|
---|
404 | rc = VERR_NOT_FOUND;
|
---|
405 | }
|
---|
406 | else
|
---|
407 | rc = VERR_NOT_FOUND;
|
---|
408 |
|
---|
409 | LogFlowFuncLeaveRC(rc);
|
---|
410 | return rc;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnClose */
|
---|
414 | static DECLCALLBACK(int) shClTransferHttpClose(PRTHTTPCALLBACKDATA pData, PRTHTTPSERVERREQ pReq, void *pvHandle)
|
---|
415 | {
|
---|
416 | RT_NOREF(pData);
|
---|
417 |
|
---|
418 | int rc;
|
---|
419 |
|
---|
420 | AssertPtr(pReq->pvUser);
|
---|
421 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
422 | if (pSrvTx)
|
---|
423 | {
|
---|
424 | PSHCLOBJHANDLE phObj = (PSHCLOBJHANDLE)pvHandle;
|
---|
425 | if (phObj)
|
---|
426 | {
|
---|
427 | Assert(*phObj != NIL_SHCLOBJHANDLE);
|
---|
428 | rc = ShClTransferObjClose(pSrvTx->pTransfer, *phObj);
|
---|
429 | if (RT_SUCCESS(rc))
|
---|
430 | {
|
---|
431 | pSrvTx->hObj = NIL_SHCLOBJHANDLE;
|
---|
432 | LogRel2(("Shared Clipboard: HTTP transfer %RU16 done\n", pSrvTx->pTransfer->State.uID));
|
---|
433 | }
|
---|
434 |
|
---|
435 | if (RT_FAILURE(rc))
|
---|
436 | LogRel(("Shared Clipboard: Error closing HTTP transfer (handle %RU64), rc=%Rrc\n", *phObj, rc));
|
---|
437 | }
|
---|
438 | else
|
---|
439 | rc = VERR_NOT_FOUND;
|
---|
440 | }
|
---|
441 | else
|
---|
442 | rc = VERR_NOT_FOUND;
|
---|
443 |
|
---|
444 | LogFlowFuncLeaveRC(rc);
|
---|
445 | return rc;
|
---|
446 | }
|
---|
447 |
|
---|
448 | /** @copydoc RTHTTPSERVERCALLBACKS::pfnQueryInfo */
|
---|
449 | static DECLCALLBACK(int) shClTransferHttpQueryInfo(PRTHTTPCALLBACKDATA pData,
|
---|
450 | PRTHTTPSERVERREQ pReq, PRTFSOBJINFO pObjInfo, char **ppszMIMEHint)
|
---|
451 | {
|
---|
452 | RT_NOREF(pData);
|
---|
453 | RT_NOREF(ppszMIMEHint);
|
---|
454 |
|
---|
455 | AssertReturn(RTStrIsValidEncoding(pReq->pszUrl), VERR_INVALID_PARAMETER);
|
---|
456 |
|
---|
457 | LogRel2(("Shared Clipboard: HTTP query for '%s' ...\n", pReq->pszUrl));
|
---|
458 |
|
---|
459 | char *pszUrl;
|
---|
460 | int rc = shClTransferHttpURLCreateFromPath(pReq->pszUrl, &pszUrl);
|
---|
461 | AssertRCReturn(rc, rc);
|
---|
462 |
|
---|
463 | RTURIPARSED Parsed;
|
---|
464 | rc = RTUriParse(pszUrl, &Parsed);
|
---|
465 | if (RT_SUCCESS(rc))
|
---|
466 | {
|
---|
467 | char *pszPath = RTUriParsedPath(pszUrl, &Parsed);
|
---|
468 | AssertPtrReturn(pszPath, VERR_NO_MEMORY); /* Should be okay, as we succeeded RTUriParse() above. */
|
---|
469 | size_t const cchPath = strlen(pszPath);
|
---|
470 |
|
---|
471 | /* For now we only know the transfer -- now we need to figure out the entry we want to serve. */
|
---|
472 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)pReq->pvUser;
|
---|
473 | if (pSrvTx)
|
---|
474 | {
|
---|
475 | size_t const cchBase = strlen(pSrvTx->szPathVirtual) + 1 /* Skip slash separating the base from the rest */;
|
---|
476 | AssertReturn(cchPath >= cchBase, VERR_INVALID_PARAMETER);
|
---|
477 |
|
---|
478 | SHCLOBJOPENCREATEPARMS openParms;
|
---|
479 | rc = ShClTransferObjOpenParmsInit(&openParms);
|
---|
480 | if (RT_SUCCESS(rc))
|
---|
481 | {
|
---|
482 | openParms.fCreate = SHCL_OBJ_CF_ACCESS_READ
|
---|
483 | | SHCL_OBJ_CF_ACCESS_DENYWRITE;
|
---|
484 |
|
---|
485 | PSHCLTRANSFER pTx = pSrvTx->pTransfer;
|
---|
486 | AssertPtr(pTx);
|
---|
487 |
|
---|
488 | rc = VERR_NOT_FOUND; /* Must find the matching root entry first. */
|
---|
489 |
|
---|
490 | uint64_t const cRoots = ShClTransferRootsCount(pTx);
|
---|
491 | for (uint32_t i = 0; i < cRoots; i++)
|
---|
492 | {
|
---|
493 | PCSHCLLISTENTRY pEntry = ShClTransferRootsEntryGet(pTx, i);
|
---|
494 | AssertPtrBreakStmt(pEntry, rc = VERR_NOT_FOUND);
|
---|
495 |
|
---|
496 | Log3Func(("pszPath=%s vs. pEntry=%s\n", pszPath, pEntry->pszName));
|
---|
497 |
|
---|
498 | if (RTStrCmp(pEntry->pszName, pszPath + cchBase)) /* Case-sensitive! */
|
---|
499 | continue;
|
---|
500 |
|
---|
501 | rc = RTStrCopy(openParms.pszPath, openParms.cbPath, pEntry->pszName);
|
---|
502 | if (RT_SUCCESS(rc))
|
---|
503 | {
|
---|
504 | rc = ShClTransferObjOpen(pTx, &openParms, &pSrvTx->hObj);
|
---|
505 | if (RT_SUCCESS(rc))
|
---|
506 | {
|
---|
507 | rc = VERR_NOT_SUPPORTED; /* Play safe by default. */
|
---|
508 |
|
---|
509 | if ( pEntry->fInfo & VBOX_SHCL_INFO_F_FSOBJINFO
|
---|
510 | && pEntry->cbInfo == sizeof(SHCLFSOBJINFO))
|
---|
511 | {
|
---|
512 | PCSHCLFSOBJINFO pSrcObjInfo = (PSHCLFSOBJINFO)pEntry->pvInfo;
|
---|
513 |
|
---|
514 | LogFlowFunc(("pszName=%s, cbInfo=%RU32, fMode=%#x (type %#x)\n",
|
---|
515 | pEntry->pszName, pEntry->cbInfo, pSrcObjInfo->Attr.fMode, (pSrcObjInfo->Attr.fMode & RTFS_TYPE_MASK)));
|
---|
516 |
|
---|
517 | LogRel2(("Shared Clipboard: HTTP object info: fMode=%#x, cbObject=%zu\n", pSrcObjInfo->Attr.fMode, pSrcObjInfo->cbObject));
|
---|
518 |
|
---|
519 | if (RTFS_IS_FILE(pSrcObjInfo->Attr.fMode))
|
---|
520 | {
|
---|
521 | memcpy(pObjInfo, pSrcObjInfo, sizeof(SHCLFSOBJINFO));
|
---|
522 | rc = VINF_SUCCESS;
|
---|
523 | }
|
---|
524 | }
|
---|
525 | else
|
---|
526 | LogRel2(("Shared Clipboard: Supplied entry information for '%s' not supported (fInfo=%#x, cbInfo=%RU32\n",
|
---|
527 | pEntry->pszName, pEntry->fInfo, pEntry->cbInfo));
|
---|
528 | }
|
---|
529 | }
|
---|
530 |
|
---|
531 | break;
|
---|
532 | }
|
---|
533 |
|
---|
534 | ShClTransferObjOpenParmsDestroy(&openParms);
|
---|
535 | }
|
---|
536 |
|
---|
537 | RTStrFree(pszPath);
|
---|
538 | pszPath = NULL;
|
---|
539 | }
|
---|
540 | else
|
---|
541 | rc = VERR_NOT_FOUND;
|
---|
542 | }
|
---|
543 |
|
---|
544 | RTStrFree(pszUrl);
|
---|
545 |
|
---|
546 | if (RT_FAILURE(rc))
|
---|
547 | LogRel(("Shared Clipboard: Querying info for HTTP transfer failed with %Rrc\n", rc));
|
---|
548 |
|
---|
549 | LogFlowFuncLeaveRC(rc);
|
---|
550 | return rc;
|
---|
551 | }
|
---|
552 |
|
---|
553 |
|
---|
554 | /*********************************************************************************************************************************
|
---|
555 | * Internal Shared Clipboard HTTP server functions *
|
---|
556 | *********************************************************************************************************************************/
|
---|
557 |
|
---|
558 | /**
|
---|
559 | * Destroys a Shared Clipboard HTTP server instance, internal version.
|
---|
560 | *
|
---|
561 | * @returns VBox status code.
|
---|
562 | * @param pSrv Shared Clipboard HTTP server instance to destroy.
|
---|
563 | *
|
---|
564 | * @note Caller needs to take the critical section.
|
---|
565 | */
|
---|
566 | static int shClTransferHttpServerDestroyInternal(PSHCLHTTPSERVER pSrv)
|
---|
567 | {
|
---|
568 | Assert(RTCritSectIsOwner(&pSrv->CritSect));
|
---|
569 |
|
---|
570 | LogFlowFuncEnter();
|
---|
571 |
|
---|
572 | pSrv->fInitialized = false;
|
---|
573 | pSrv->fRunning = false;
|
---|
574 |
|
---|
575 | int rc = VINF_SUCCESS;
|
---|
576 |
|
---|
577 | PSHCLHTTPSERVERTRANSFER pSrvTx, pSrvTxNext;
|
---|
578 | RTListForEachSafe(&pSrv->lstTransfers, pSrvTx, pSrvTxNext, SHCLHTTPSERVERTRANSFER, Node)
|
---|
579 | {
|
---|
580 | int rc2 = shClTransferHttpServerDestroyTransfer(pSrv, pSrvTx);
|
---|
581 | if (RT_SUCCESS(rc))
|
---|
582 | rc = rc2;
|
---|
583 | }
|
---|
584 |
|
---|
585 | RTHttpServerResponseDestroy(&pSrv->Resp);
|
---|
586 |
|
---|
587 | pSrv->hHTTPServer = NIL_RTHTTPSERVER;
|
---|
588 |
|
---|
589 | shClTransferHttpServerUnlock(pSrv); /* Unlock critical section taken by the caller before deleting it. */
|
---|
590 |
|
---|
591 | if (RTCritSectIsInitialized(&pSrv->CritSect))
|
---|
592 | {
|
---|
593 | int rc2 = RTCritSectDelete(&pSrv->CritSect);
|
---|
594 | if (RT_SUCCESS(rc))
|
---|
595 | rc = rc2;
|
---|
596 | }
|
---|
597 |
|
---|
598 | RTSemEventDestroy(pSrv->StatusEvent);
|
---|
599 | pSrv->StatusEvent = NIL_RTSEMEVENT;
|
---|
600 |
|
---|
601 | LogFlowFuncLeaveRC(rc);
|
---|
602 | return rc;
|
---|
603 | }
|
---|
604 |
|
---|
605 | /**
|
---|
606 | * Initializes a new Shared Clipboard HTTP server instance.
|
---|
607 | *
|
---|
608 | * @return VBox status code.
|
---|
609 | * @param pSrv HTTP server instance to initialize.
|
---|
610 | */
|
---|
611 | static int shClTransferHttpServerInitInternal(PSHCLHTTPSERVER pSrv)
|
---|
612 | {
|
---|
613 | int rc = RTCritSectInit(&pSrv->CritSect);
|
---|
614 | AssertRCReturn(rc, rc);
|
---|
615 |
|
---|
616 | rc = RTSemEventCreate(&pSrv->StatusEvent);
|
---|
617 | AssertRCReturn(rc, rc);
|
---|
618 |
|
---|
619 | pSrv->hHTTPServer = NIL_RTHTTPSERVER;
|
---|
620 | pSrv->uPort = 0;
|
---|
621 | RTListInit(&pSrv->lstTransfers);
|
---|
622 | pSrv->cTransfers = 0;
|
---|
623 |
|
---|
624 | rc = RTHttpServerResponseInit(&pSrv->Resp);
|
---|
625 | AssertRCReturn(rc, rc);
|
---|
626 |
|
---|
627 | ASMAtomicXchgBool(&pSrv->fInitialized, true);
|
---|
628 | ASMAtomicXchgBool(&pSrv->fRunning, false);
|
---|
629 |
|
---|
630 | return rc;
|
---|
631 | }
|
---|
632 |
|
---|
633 |
|
---|
634 | /*********************************************************************************************************************************
|
---|
635 | * Public Shared Clipboard HTTP server functions *
|
---|
636 | *********************************************************************************************************************************/
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * Initializes a new Shared Clipboard HTTP server instance.
|
---|
640 | *
|
---|
641 | * @return VBox status code.
|
---|
642 | * @param pSrv HTTP server instance to initialize.
|
---|
643 | */
|
---|
644 | int ShClTransferHttpServerInit(PSHCLHTTPSERVER pSrv)
|
---|
645 | {
|
---|
646 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
647 |
|
---|
648 | return shClTransferHttpServerInitInternal(pSrv);
|
---|
649 | }
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Returns whether a given TCP port is known to be buggy or not.
|
---|
653 | *
|
---|
654 | * @returns \c true if the given port is known to be buggy, or \c false if not.
|
---|
655 | * @param uPort TCP port to check.
|
---|
656 | */
|
---|
657 | static bool shClTransferHttpServerPortIsBuggy(uint16_t uPort)
|
---|
658 | {
|
---|
659 | uint16_t const aBuggyPorts[] = {
|
---|
660 | #if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
|
---|
661 | /* GNOME Nautilus ("Files") v43 is unable download HTTP files from this port. */
|
---|
662 | 8080
|
---|
663 | #else /* Prevents zero-sized arrays. */
|
---|
664 | 0
|
---|
665 | #endif
|
---|
666 | };
|
---|
667 |
|
---|
668 | for (size_t i = 0; i < RT_ELEMENTS(aBuggyPorts); i++)
|
---|
669 | if (uPort == aBuggyPorts[i])
|
---|
670 | return true;
|
---|
671 | return false;
|
---|
672 | }
|
---|
673 |
|
---|
674 | /**
|
---|
675 | * Starts the Shared Clipboard HTTP server instance, extended version.
|
---|
676 | *
|
---|
677 | * @returns VBox status code.
|
---|
678 | * @return VERR_ADDRESS_CONFLICT if the port is already taken or the port is known to be buggy.
|
---|
679 | * @param pSrv HTTP server instance to create.
|
---|
680 | * @param uPort TCP port number to use.
|
---|
681 | */
|
---|
682 | int ShClTransferHttpServerStartEx(PSHCLHTTPSERVER pSrv, uint16_t uPort)
|
---|
683 | {
|
---|
684 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
685 | AssertReturn(uPort, VERR_INVALID_PARAMETER);
|
---|
686 |
|
---|
687 | AssertReturn(!shClTransferHttpServerPortIsBuggy(uPort), VERR_ADDRESS_CONFLICT);
|
---|
688 |
|
---|
689 | shClTransferHttpServerLock(pSrv);
|
---|
690 |
|
---|
691 | RTHTTPSERVERCALLBACKS Callbacks;
|
---|
692 | RT_ZERO(Callbacks);
|
---|
693 |
|
---|
694 | Callbacks.pfnRequestBegin = shClTransferHttpBegin;
|
---|
695 | Callbacks.pfnRequestEnd = shClTransferHttpEnd;
|
---|
696 | Callbacks.pfnOpen = shClTransferHttpOpen;
|
---|
697 | Callbacks.pfnRead = shClTransferHttpRead;
|
---|
698 | Callbacks.pfnClose = shClTransferHttpClose;
|
---|
699 | Callbacks.pfnQueryInfo = shClTransferHttpQueryInfo;
|
---|
700 |
|
---|
701 | /* Note: The server always and *only* runs against the localhost interface. */
|
---|
702 | int rc = RTHttpServerCreate(&pSrv->hHTTPServer, "localhost", uPort, &Callbacks,
|
---|
703 | pSrv, sizeof(SHCLHTTPSERVER));
|
---|
704 | if (RT_SUCCESS(rc))
|
---|
705 | {
|
---|
706 | pSrv->uPort = uPort;
|
---|
707 | ASMAtomicXchgBool(&pSrv->fRunning, true);
|
---|
708 |
|
---|
709 | LogRel2(("Shared Clipboard: HTTP server started at port %RU16\n", pSrv->uPort));
|
---|
710 |
|
---|
711 | rc = shclTransferHttpServerSetStatusLocked(pSrv, SHCLHTTPSERVERSTATUS_STARTED);
|
---|
712 | }
|
---|
713 |
|
---|
714 | shClTransferHttpServerUnlock(pSrv);
|
---|
715 |
|
---|
716 | if (RT_FAILURE(rc))
|
---|
717 | LogRel(("Shared Clipboard: HTTP server failed to start, rc=%Rrc\n", rc));
|
---|
718 |
|
---|
719 | return rc;
|
---|
720 | }
|
---|
721 |
|
---|
722 | /**
|
---|
723 | * Translates a Shared Clipboard HTTP server status to a string.
|
---|
724 | *
|
---|
725 | * @returns Status as a string.
|
---|
726 | * @param uMsg Status to translate.
|
---|
727 | */
|
---|
728 | static const char *shClTransferHttpServerStatusToStr(SHCLHTTPSERVERSTATUS enmStatus)
|
---|
729 | {
|
---|
730 | switch (enmStatus)
|
---|
731 | {
|
---|
732 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_NONE);
|
---|
733 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_STARTED);
|
---|
734 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_STOPPED);
|
---|
735 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_TRANSFER_REGISTERED);
|
---|
736 | RT_CASE_RET_STR(SHCLHTTPSERVERSTATUS_TRANSFER_UNREGISTERED);
|
---|
737 | }
|
---|
738 |
|
---|
739 | AssertFailedReturn("Unknown");
|
---|
740 | }
|
---|
741 |
|
---|
742 | /**
|
---|
743 | * Starts the Shared Clipboard HTTP server instance using a random port (>= 49152).
|
---|
744 | *
|
---|
745 | * This does automatic probing of TCP ports if a port already is being used.
|
---|
746 | *
|
---|
747 | * @returns VBox status code.
|
---|
748 | * @param pSrv HTTP server instance to create.
|
---|
749 | * @param cMaxAttempts Maximum number of attempts to create a HTTP server.
|
---|
750 | * @param puPort Where to return the TCP port number being used on success. Optional.
|
---|
751 | *
|
---|
752 | * @note Complies with RFC 6335 (IANA).
|
---|
753 | */
|
---|
754 | int ShClTransferHttpServerStart(PSHCLHTTPSERVER pSrv, unsigned cMaxAttempts, uint16_t *puPort)
|
---|
755 | {
|
---|
756 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
757 | AssertReturn(cMaxAttempts, VERR_INVALID_PARAMETER);
|
---|
758 | /* puPort is optional. */
|
---|
759 |
|
---|
760 | int rc;
|
---|
761 | #ifdef VBOX_SHCL_DEBUG_HTTPSERVER
|
---|
762 | uint16_t uDebugPort = 49200;
|
---|
763 | rc = ShClTransferHttpServerStartEx(pSrv, (uint32_t)uDebugPort);
|
---|
764 | if (RT_SUCCESS(rc))
|
---|
765 | {
|
---|
766 | if (puPort)
|
---|
767 | *puPort = uDebugPort;
|
---|
768 | }
|
---|
769 | return rc;
|
---|
770 | #endif
|
---|
771 |
|
---|
772 | RTRAND hRand;
|
---|
773 | rc = RTRandAdvCreateSystemFaster(&hRand); /* Should be good enough for this task. */
|
---|
774 | if (RT_SUCCESS(rc))
|
---|
775 | {
|
---|
776 | uint16_t uPort;
|
---|
777 | unsigned i = 0;
|
---|
778 | for (i; i < cMaxAttempts; i++)
|
---|
779 | {
|
---|
780 | /* Try some random ports >= 49152 (i.e. "dynamic ports", see RFC 6335)
|
---|
781 | * -- required, as VBoxClient runs as a user process on the guest. */
|
---|
782 | uPort = RTRandAdvU32Ex(hRand, 49152, UINT16_MAX);
|
---|
783 |
|
---|
784 | /* If the port selected turns is known to be buggy for whatever reason, skip it and try another one. */
|
---|
785 | if (shClTransferHttpServerPortIsBuggy(uPort))
|
---|
786 | continue;
|
---|
787 |
|
---|
788 | rc = ShClTransferHttpServerStartEx(pSrv, (uint32_t)uPort);
|
---|
789 | if (RT_SUCCESS(rc))
|
---|
790 | {
|
---|
791 | if (puPort)
|
---|
792 | *puPort = uPort;
|
---|
793 | break;
|
---|
794 | }
|
---|
795 | }
|
---|
796 |
|
---|
797 | if ( RT_FAILURE(rc)
|
---|
798 | && i == cMaxAttempts)
|
---|
799 | LogRel(("Shared Clipboard: Maximum attempts to start HTTP server reached (%u), giving up\n", cMaxAttempts));
|
---|
800 |
|
---|
801 | RTRandAdvDestroy(hRand);
|
---|
802 | }
|
---|
803 |
|
---|
804 | return rc;
|
---|
805 | }
|
---|
806 |
|
---|
807 | /**
|
---|
808 | * Stops a Shared Clipboard HTTP server instance.
|
---|
809 | *
|
---|
810 | * @returns VBox status code.
|
---|
811 | * @param pSrv HTTP server instance to stop.
|
---|
812 | */
|
---|
813 | int ShClTransferHttpServerStop(PSHCLHTTPSERVER pSrv)
|
---|
814 | {
|
---|
815 | LogFlowFuncEnter();
|
---|
816 |
|
---|
817 | shClTransferHttpServerLock(pSrv);
|
---|
818 |
|
---|
819 | int rc = VINF_SUCCESS;
|
---|
820 |
|
---|
821 | if (pSrv->fRunning)
|
---|
822 | {
|
---|
823 | Assert(pSrv->hHTTPServer != NIL_RTHTTPSERVER);
|
---|
824 |
|
---|
825 | rc = RTHttpServerDestroy(pSrv->hHTTPServer);
|
---|
826 | if (RT_SUCCESS(rc))
|
---|
827 | {
|
---|
828 | pSrv->hHTTPServer = NIL_RTHTTPSERVER;
|
---|
829 | pSrv->fRunning = false;
|
---|
830 |
|
---|
831 | /* Let any eventual waiters know. */
|
---|
832 | shclTransferHttpServerSetStatusLocked(pSrv, SHCLHTTPSERVERSTATUS_STOPPED);
|
---|
833 |
|
---|
834 | LogRel2(("Shared Clipboard: HTTP server stopped\n"));
|
---|
835 | }
|
---|
836 | }
|
---|
837 |
|
---|
838 | if (RT_FAILURE(rc))
|
---|
839 | LogRel(("Shared Clipboard: HTTP server failed to stop, rc=%Rrc\n", rc));
|
---|
840 |
|
---|
841 | shClTransferHttpServerUnlock(pSrv);
|
---|
842 |
|
---|
843 | LogFlowFuncLeaveRC(rc);
|
---|
844 | return rc;
|
---|
845 | }
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Destroys a Shared Clipboard HTTP server instance.
|
---|
849 | *
|
---|
850 | * @returns VBox status code.
|
---|
851 | * @param pSrv HTTP server instance to destroy.
|
---|
852 | */
|
---|
853 | int ShClTransferHttpServerDestroy(PSHCLHTTPSERVER pSrv)
|
---|
854 | {
|
---|
855 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
856 |
|
---|
857 | int rc = ShClTransferHttpServerStop(pSrv);
|
---|
858 | if (RT_FAILURE(rc))
|
---|
859 | return rc;
|
---|
860 |
|
---|
861 | if (!ASMAtomicReadBool(&pSrv->fInitialized))
|
---|
862 | return VINF_SUCCESS;
|
---|
863 |
|
---|
864 | shClTransferHttpServerLock(pSrv);
|
---|
865 |
|
---|
866 | rc = shClTransferHttpServerDestroyInternal(pSrv);
|
---|
867 |
|
---|
868 | /* Unlock not needed anymore, as the critical section got destroyed. */
|
---|
869 |
|
---|
870 | return rc;
|
---|
871 | }
|
---|
872 |
|
---|
873 | /**
|
---|
874 | * Returns the host name (scheme) of a HTTP server instance.
|
---|
875 | *
|
---|
876 | * @returns Host name (scheme).
|
---|
877 | * @param pSrv HTTP server instance to return host name (scheme) for.
|
---|
878 | *
|
---|
879 | * @note This is hardcoded to "localhost" for now.
|
---|
880 | */
|
---|
881 | static const char *shClTransferHttpServerGetHost(PSHCLHTTPSERVER pSrv)
|
---|
882 | {
|
---|
883 | RT_NOREF(pSrv);
|
---|
884 | return "http://localhost"; /* Hardcoded for now. */
|
---|
885 | }
|
---|
886 |
|
---|
887 | /**
|
---|
888 | * Destroys a server transfer, internal version.
|
---|
889 | *
|
---|
890 | * @returns VBox status code.
|
---|
891 | * @param pSrv HTTP server instance to unregister transfer from.
|
---|
892 | * @param pTransfer Server transfer to destroy
|
---|
893 | * The pointer will be invalid on success.
|
---|
894 | *
|
---|
895 | * @note Caller needs to take the server critical section.
|
---|
896 | */
|
---|
897 | static int shClTransferHttpServerDestroyTransfer(PSHCLHTTPSERVER pSrv, PSHCLHTTPSERVERTRANSFER pSrvTx)
|
---|
898 | {
|
---|
899 | Assert(RTCritSectIsOwner(&pSrv->CritSect));
|
---|
900 |
|
---|
901 | RTListNodeRemove(&pSrvTx->Node);
|
---|
902 |
|
---|
903 | Assert(pSrv->cTransfers);
|
---|
904 | pSrv->cTransfers--;
|
---|
905 |
|
---|
906 | LogFunc(("pTransfer=%p, idTransfer=%RU16, szPath=%s -> %RU32 transfers\n",
|
---|
907 | pSrvTx->pTransfer, pSrvTx->pTransfer->State.uID, pSrvTx->szPathVirtual, pSrv->cTransfers));
|
---|
908 |
|
---|
909 | LogRel2(("Shared Clipboard: Destroyed HTTP transfer %RU16, now %RU32 HTTP transfers total\n",
|
---|
910 | pSrvTx->pTransfer->State.uID, pSrv->cTransfers));
|
---|
911 |
|
---|
912 | if (RTCritSectIsInitialized(&pSrvTx->CritSect))
|
---|
913 | {
|
---|
914 | int rc = RTCritSectDelete(&pSrvTx->CritSect);
|
---|
915 | AssertRCReturn(rc, rc);
|
---|
916 | }
|
---|
917 |
|
---|
918 | RTMemFree(pSrvTx);
|
---|
919 | pSrvTx = NULL;
|
---|
920 |
|
---|
921 | return VINF_SUCCESS;
|
---|
922 | }
|
---|
923 |
|
---|
924 |
|
---|
925 | /*********************************************************************************************************************************
|
---|
926 | * Public Shared Clipboard HTTP server functions *
|
---|
927 | *********************************************************************************************************************************/
|
---|
928 |
|
---|
929 | /**
|
---|
930 | * Registers a Shared Clipboard transfer to a HTTP server instance.
|
---|
931 | *
|
---|
932 | * @returns VBox status code.
|
---|
933 | * @param pSrv HTTP server instance to register transfer for.
|
---|
934 | * @param pTransfer Transfer to register. Needs to be on the heap.
|
---|
935 | */
|
---|
936 | int ShClTransferHttpServerRegisterTransfer(PSHCLHTTPSERVER pSrv, PSHCLTRANSFER pTransfer)
|
---|
937 | {
|
---|
938 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
939 | AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
|
---|
940 |
|
---|
941 | AssertMsgReturn(pTransfer->State.uID, ("Transfer needs to be registered with a transfer context first\n"),
|
---|
942 | VERR_INVALID_PARAMETER);
|
---|
943 |
|
---|
944 | uint64_t const cRoots = ShClTransferRootsCount(pTransfer);
|
---|
945 | AssertMsgReturn(cRoots > 0, ("Transfer has no root entries\n"), VERR_INVALID_PARAMETER);
|
---|
946 | /** @todo Check for directories? */
|
---|
947 |
|
---|
948 | shClTransferHttpServerLock(pSrv);
|
---|
949 |
|
---|
950 | PSHCLHTTPSERVERTRANSFER pSrvTx = (PSHCLHTTPSERVERTRANSFER)RTMemAllocZ(sizeof(SHCLHTTPSERVERTRANSFER));
|
---|
951 | AssertPtrReturn(pSrvTx, VERR_NO_MEMORY);
|
---|
952 |
|
---|
953 | RTUUID Uuid;
|
---|
954 | int rc = RTUuidCreate(&Uuid);
|
---|
955 | if (RT_SUCCESS(rc))
|
---|
956 | {
|
---|
957 | char szUuid[64];
|
---|
958 | rc = RTUuidToStr(&Uuid, szUuid, sizeof(szUuid));
|
---|
959 | if (RT_SUCCESS(rc))
|
---|
960 | {
|
---|
961 | rc = RTCritSectInit(&pSrvTx->CritSect);
|
---|
962 | AssertRCReturn(rc, rc);
|
---|
963 |
|
---|
964 | /* Create the virtual HTTP path for the transfer.
|
---|
965 | * Every transfer has a dedicated HTTP path (but live in the same URL namespace). */
|
---|
966 | char *pszPath;
|
---|
967 | #ifdef VBOX_SHCL_DEBUG_HTTPSERVER
|
---|
968 | # ifdef DEBUG_andy /** Too lazy to specify a different transfer ID for debugging. */
|
---|
969 | ssize_t cch = RTStrAPrintf(&pszPath, "/transfer");
|
---|
970 | # else
|
---|
971 | ssize_t cch = RTStrAPrintf(&pszPath, "/transfer%RU16", pTransfer->State.uID);
|
---|
972 | # endif
|
---|
973 | #else /* Release mode */
|
---|
974 | ssize_t cch = RTStrAPrintf(&pszPath, "/%s/%s", SHCL_HTTPT_URL_NAMESPACE, szUuid);
|
---|
975 | #endif
|
---|
976 | AssertReturn(cch, VERR_NO_MEMORY);
|
---|
977 |
|
---|
978 | char *pszURI;
|
---|
979 | size_t cchScheme;
|
---|
980 | rc = shClTransferHttpURLCreateFromPathEx(pszPath, &pszURI, &cchScheme);
|
---|
981 | if (RT_SUCCESS(rc))
|
---|
982 | {
|
---|
983 | /* For the virtual path we only keep everything after the full scheme (e.g. "http://").
|
---|
984 | * The virtual path always has to start with a "/". */
|
---|
985 | if (RTStrPrintf2(pSrvTx->szPathVirtual, sizeof(pSrvTx->szPathVirtual), "%s", pszURI + cchScheme) <= 0)
|
---|
986 | rc = VERR_BUFFER_OVERFLOW;
|
---|
987 |
|
---|
988 | RTStrFree(pszURI);
|
---|
989 | pszURI = NULL;
|
---|
990 | }
|
---|
991 | else
|
---|
992 | rc = VERR_NO_MEMORY;
|
---|
993 |
|
---|
994 | RTStrFree(pszPath);
|
---|
995 | pszPath = NULL;
|
---|
996 |
|
---|
997 | if (RT_SUCCESS(rc))
|
---|
998 | {
|
---|
999 | pSrvTx->pTransfer = pTransfer;
|
---|
1000 | pSrvTx->hObj = NIL_SHCLOBJHANDLE;
|
---|
1001 |
|
---|
1002 | RTListAppend(&pSrv->lstTransfers, &pSrvTx->Node);
|
---|
1003 | pSrv->cTransfers++;
|
---|
1004 |
|
---|
1005 | shclTransferHttpServerSetStatusLocked(pSrv, SHCLHTTPSERVERSTATUS_TRANSFER_REGISTERED);
|
---|
1006 |
|
---|
1007 | LogFunc(("pTransfer=%p, idTransfer=%RU16, szPath=%s -> %RU32 transfers\n",
|
---|
1008 | pSrvTx->pTransfer, pSrvTx->pTransfer->State.uID, pSrvTx->szPathVirtual, pSrv->cTransfers));
|
---|
1009 |
|
---|
1010 | LogRel2(("Shared Clipboard: Registered HTTP transfer %RU16, now %RU32 HTTP transfers total\n",
|
---|
1011 | pTransfer->State.uID, pSrv->cTransfers));
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | if (RT_FAILURE(rc))
|
---|
1018 | RTMemFree(pSrvTx);
|
---|
1019 |
|
---|
1020 | shClTransferHttpServerUnlock(pSrv);
|
---|
1021 |
|
---|
1022 | LogFlowFuncLeaveRC(rc);
|
---|
1023 | return rc;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | /**
|
---|
1027 | * Unregisters a formerly registered Shared Clipboard transfer.
|
---|
1028 | *
|
---|
1029 | * @returns VBox status code.
|
---|
1030 | * @param pSrv HTTP server instance to unregister transfer from.
|
---|
1031 | * @param pTransfer Transfer to unregister.
|
---|
1032 | */
|
---|
1033 | int ShClTransferHttpServerUnregisterTransfer(PSHCLHTTPSERVER pSrv, PSHCLTRANSFER pTransfer)
|
---|
1034 | {
|
---|
1035 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
1036 | AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
|
---|
1037 |
|
---|
1038 | shClTransferHttpServerLock(pSrv);
|
---|
1039 |
|
---|
1040 | int rc = VINF_SUCCESS;
|
---|
1041 |
|
---|
1042 | PSHCLHTTPSERVERTRANSFER pSrvTx, pSrvTxNext;
|
---|
1043 | RTListForEachSafe(&pSrv->lstTransfers, pSrvTx, pSrvTxNext, SHCLHTTPSERVERTRANSFER, Node)
|
---|
1044 | {
|
---|
1045 | AssertPtr(pSrvTx->pTransfer);
|
---|
1046 | if (pSrvTx->pTransfer->State.uID == pTransfer->State.uID)
|
---|
1047 | {
|
---|
1048 | rc = shClTransferHttpServerDestroyTransfer(pSrv, pSrvTx);
|
---|
1049 | if (RT_SUCCESS(rc))
|
---|
1050 | shclTransferHttpServerSetStatusLocked(pSrv, SHCLHTTPSERVERSTATUS_TRANSFER_UNREGISTERED);
|
---|
1051 | break;
|
---|
1052 | }
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | shClTransferHttpServerUnlock(pSrv);
|
---|
1056 |
|
---|
1057 | LogFlowFuncLeaveRC(rc);
|
---|
1058 | return rc;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | /**
|
---|
1062 | * Sets a new status.
|
---|
1063 | *
|
---|
1064 | * @returns New status set.
|
---|
1065 | * @param pSrv HTTP server instance to set status for.
|
---|
1066 | * @param fStatus New status to set.
|
---|
1067 | *
|
---|
1068 | * @note Caller needs to take critical section.
|
---|
1069 | */
|
---|
1070 | static SHCLHTTPSERVERSTATUS shclTransferHttpServerSetStatusLocked(PSHCLHTTPSERVER pSrv, SHCLHTTPSERVERSTATUS enmStatus)
|
---|
1071 | {
|
---|
1072 | Assert(RTCritSectIsOwner(&pSrv->CritSect));
|
---|
1073 |
|
---|
1074 | /* Bogus checks. */
|
---|
1075 | Assert(!(enmStatus & SHCLHTTPSERVERSTATUS_NONE) || enmStatus == SHCLHTTPSERVERSTATUS_NONE);
|
---|
1076 |
|
---|
1077 | pSrv->enmStatus = enmStatus;
|
---|
1078 | LogFlowFunc(("fStatus=%#x\n", pSrv->enmStatus));
|
---|
1079 |
|
---|
1080 | int rc2 = RTSemEventSignal(pSrv->StatusEvent);
|
---|
1081 | AssertRC(rc2);
|
---|
1082 |
|
---|
1083 | return pSrv->enmStatus;
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | /**
|
---|
1087 | * Returns the first transfer in the list.
|
---|
1088 | *
|
---|
1089 | * @returns Pointer to first transfer if found, or NULL if not found.
|
---|
1090 | * @param pSrv HTTP server instance.
|
---|
1091 | */
|
---|
1092 | PSHCLTRANSFER ShClTransferHttpServerGetTransferFirst(PSHCLHTTPSERVER pSrv)
|
---|
1093 | {
|
---|
1094 | shClTransferHttpServerLock(pSrv);
|
---|
1095 |
|
---|
1096 | PSHCLHTTPSERVERTRANSFER pHttpTransfer = RTListGetFirst(&pSrv->lstTransfers, SHCLHTTPSERVERTRANSFER, Node);
|
---|
1097 |
|
---|
1098 | shClTransferHttpServerUnlock(pSrv);
|
---|
1099 |
|
---|
1100 | return pHttpTransfer ? pHttpTransfer->pTransfer : NULL;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | /**
|
---|
1104 | * Returns the last transfer in the list.
|
---|
1105 | *
|
---|
1106 | * @returns Pointer to last transfer if found, or NULL if not found.
|
---|
1107 | * @param pSrv HTTP server instance.
|
---|
1108 | */
|
---|
1109 | PSHCLTRANSFER ShClTransferHttpServerGetTransferLast(PSHCLHTTPSERVER pSrv)
|
---|
1110 | {
|
---|
1111 | shClTransferHttpServerLock(pSrv);
|
---|
1112 |
|
---|
1113 | PSHCLHTTPSERVERTRANSFER pHttpTransfer = RTListGetLast(&pSrv->lstTransfers, SHCLHTTPSERVERTRANSFER, Node);
|
---|
1114 |
|
---|
1115 | shClTransferHttpServerUnlock(pSrv);
|
---|
1116 |
|
---|
1117 | return pHttpTransfer ? pHttpTransfer->pTransfer : NULL;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | /**
|
---|
1121 | * Returns a transfer for a specific ID.
|
---|
1122 | *
|
---|
1123 | * @returns Pointer to the transfer if found, or NULL if not found.
|
---|
1124 | * @param pSrv HTTP server instance.
|
---|
1125 | * @param idTransfer Transfer ID of transfer to return..
|
---|
1126 | */
|
---|
1127 | bool ShClTransferHttpServerGetTransfer(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer)
|
---|
1128 | {
|
---|
1129 | AssertPtrReturn(pSrv, false);
|
---|
1130 |
|
---|
1131 | shClTransferHttpServerLock(pSrv);
|
---|
1132 |
|
---|
1133 | PSHCLHTTPSERVERTRANSFER pTransfer = shClTransferHttpServerGetTransferById(pSrv, idTransfer);
|
---|
1134 |
|
---|
1135 | shClTransferHttpServerUnlock(pSrv);
|
---|
1136 |
|
---|
1137 | return pTransfer;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /**
|
---|
1141 | * Returns the used TCP port number of a HTTP server instance.
|
---|
1142 | *
|
---|
1143 | * @returns TCP port number. 0 if not specified yet.
|
---|
1144 | * @param pSrv HTTP server instance to return port for.
|
---|
1145 | */
|
---|
1146 | uint16_t ShClTransferHttpServerGetPort(PSHCLHTTPSERVER pSrv)
|
---|
1147 | {
|
---|
1148 | AssertPtrReturn(pSrv, 0);
|
---|
1149 |
|
---|
1150 | shClTransferHttpServerLock(pSrv);
|
---|
1151 |
|
---|
1152 | const uint16_t uPort = pSrv->uPort;
|
---|
1153 |
|
---|
1154 | shClTransferHttpServerUnlock(pSrv);
|
---|
1155 |
|
---|
1156 | return uPort;
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | /**
|
---|
1160 | * Returns the number of registered HTTP server transfers of a HTTP server instance.
|
---|
1161 | *
|
---|
1162 | * @returns Number of registered transfers.
|
---|
1163 | * @param pSrv HTTP server instance to return registered transfers for.
|
---|
1164 | */
|
---|
1165 | uint32_t ShClTransferHttpServerGetTransferCount(PSHCLHTTPSERVER pSrv)
|
---|
1166 | {
|
---|
1167 | AssertPtrReturn(pSrv, 0);
|
---|
1168 |
|
---|
1169 | shClTransferHttpServerLock(pSrv);
|
---|
1170 |
|
---|
1171 | const uint32_t cTransfers = pSrv->cTransfers;
|
---|
1172 | LogFlowFunc(("cTransfers=%RU32\n", cTransfers));
|
---|
1173 |
|
---|
1174 | shClTransferHttpServerUnlock(pSrv);
|
---|
1175 |
|
---|
1176 | return cTransfers;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | /**
|
---|
1180 | * Returns an allocated string with a HTTP server instance's address.
|
---|
1181 | *
|
---|
1182 | * @returns Allocated string with a HTTP server instance's address, or NULL on OOM.
|
---|
1183 | * Needs to be free'd by the caller using RTStrFree().
|
---|
1184 | * @param pSrv HTTP server instance to return address for.
|
---|
1185 | */
|
---|
1186 | char *ShClTransferHttpServerGetAddressA(PSHCLHTTPSERVER pSrv)
|
---|
1187 | {
|
---|
1188 | AssertPtrReturn(pSrv, NULL);
|
---|
1189 |
|
---|
1190 | shClTransferHttpServerLock(pSrv);
|
---|
1191 |
|
---|
1192 | char *pszAddress = RTStrAPrintf2("%s:%RU16", shClTransferHttpServerGetHost(pSrv), pSrv->uPort);
|
---|
1193 | AssertPtr(pszAddress);
|
---|
1194 |
|
---|
1195 | shClTransferHttpServerUnlock(pSrv);
|
---|
1196 |
|
---|
1197 | return pszAddress;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | /**
|
---|
1201 | * Returns an allocated string with the URL of a given Shared Clipboard transfer ID.
|
---|
1202 | *
|
---|
1203 | * @returns Allocated string with the URL of a given Shared Clipboard transfer ID, or NULL if not found.
|
---|
1204 | * Needs to be free'd by the caller using RTStrFree().
|
---|
1205 | * @param pSrv HTTP server instance to return URL for.
|
---|
1206 | * @param idTransfer Transfer ID to return the URL for.
|
---|
1207 | * @param idxEntry Index of transfer entry to return URL for.
|
---|
1208 | * Specify UINT64_MAX to only return the base URL.
|
---|
1209 | */
|
---|
1210 | char *ShClTransferHttpServerGetUrlA(PSHCLHTTPSERVER pSrv, SHCLTRANSFERID idTransfer, uint64_t idxEntry)
|
---|
1211 | {
|
---|
1212 | AssertPtrReturn(pSrv, NULL);
|
---|
1213 | AssertReturn(idTransfer != NIL_SHCLTRANSFERID, NULL);
|
---|
1214 |
|
---|
1215 | shClTransferHttpServerLock(pSrv);
|
---|
1216 |
|
---|
1217 | PSHCLHTTPSERVERTRANSFER pSrvTx = shClTransferHttpServerGetTransferById(pSrv, idTransfer);
|
---|
1218 | if (!pSrvTx)
|
---|
1219 | {
|
---|
1220 | AssertFailed();
|
---|
1221 | shClTransferHttpServerUnlock(pSrv);
|
---|
1222 | return NULL;
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | PSHCLTRANSFER pTx = pSrvTx->pTransfer;
|
---|
1226 | AssertPtr(pTx);
|
---|
1227 |
|
---|
1228 | char *pszUrl = NULL;
|
---|
1229 |
|
---|
1230 | if (RT_LIKELY(idxEntry != UINT64_MAX))
|
---|
1231 | {
|
---|
1232 | /* For now this only supports root entries. */
|
---|
1233 | PCSHCLLISTENTRY pEntry = ShClTransferRootsEntryGet(pTx, idxEntry);
|
---|
1234 | if (pEntry)
|
---|
1235 | {
|
---|
1236 | AssertReturn(RTStrNLen(pSrvTx->szPathVirtual, RTPATH_MAX), NULL);
|
---|
1237 | pszUrl = RTStrAPrintf2("%s:%RU16%s/%s", shClTransferHttpServerGetHost(pSrv), pSrv->uPort, pSrvTx->szPathVirtual, pEntry->pszName);
|
---|
1238 | }
|
---|
1239 | }
|
---|
1240 | else /* Only return the base. */
|
---|
1241 | pszUrl = RTStrAPrintf2("%s:%RU16%s", shClTransferHttpServerGetHost(pSrv), pSrv->uPort, pSrvTx->szPathVirtual);
|
---|
1242 |
|
---|
1243 | shClTransferHttpServerUnlock(pSrv);
|
---|
1244 | return pszUrl;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | /**
|
---|
1248 | * Converts a HTTP transfer to a string list.
|
---|
1249 | *
|
---|
1250 | * @returns VBox status code.
|
---|
1251 | * @param pHttpSrv HTTP server that contains the transfer.
|
---|
1252 | * @param pTransfer Transfer to convert data from.
|
---|
1253 | * @param pszSep Separator to use for the transfer entries.
|
---|
1254 | * @param ppszData Where to store the string list on success.
|
---|
1255 | * @param pcbData Where to return the bytes of \a ppszData on success.
|
---|
1256 | * Includes terminator. Optional.
|
---|
1257 | */
|
---|
1258 | static int shClTransferHttpConvertToStringListEx(PSHCLHTTPSERVER pSrv, PSHCLTRANSFER pTransfer, const char *pszSep,
|
---|
1259 | char **ppszData, size_t *pcbData)
|
---|
1260 | {
|
---|
1261 | AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
|
---|
1262 | AssertPtrReturn(pszSep, VERR_INVALID_POINTER);
|
---|
1263 | AssertPtrReturn(ppszData, VERR_INVALID_POINTER);
|
---|
1264 | /* pcbData is optional. */
|
---|
1265 |
|
---|
1266 | int rc = VINF_SUCCESS;
|
---|
1267 | char *pszData = NULL;
|
---|
1268 |
|
---|
1269 | uint64_t const cRoots = ShClTransferRootsCount(pTransfer);
|
---|
1270 | for (uint32_t i = 0; i < cRoots; i++)
|
---|
1271 | {
|
---|
1272 | char *pszEntry = ShClTransferHttpServerGetUrlA(pSrv, ShClTransferGetID(pTransfer), i /* Entry index */);
|
---|
1273 | AssertPtrBreakStmt(pszEntry, rc = VERR_NO_MEMORY);
|
---|
1274 |
|
---|
1275 | if (i > 0)
|
---|
1276 | {
|
---|
1277 | rc = RTStrAAppend(&pszData, pszSep); /* Separate entries with a newline. */
|
---|
1278 | AssertRCBreak(rc);
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | rc = RTStrAAppend(&pszData, pszEntry);
|
---|
1282 | AssertRCBreak(rc);
|
---|
1283 |
|
---|
1284 | RTStrFree(pszEntry);
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | if (RT_FAILURE(rc))
|
---|
1288 | {
|
---|
1289 | RTStrFree(pszData);
|
---|
1290 | return rc;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | *ppszData = pszData;
|
---|
1294 | if (pcbData)
|
---|
1295 | *pcbData = RTStrNLen(pszData, RTSTR_MAX) + 1 /* Terminator. */;
|
---|
1296 |
|
---|
1297 | return VINF_SUCCESS;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | /**
|
---|
1301 | * Converts a HTTP transfer to a string list.
|
---|
1302 | *
|
---|
1303 | * @returns VBox status code.
|
---|
1304 | * @param pHttpSrv HTTP server that contains the transfer.
|
---|
1305 | * @param pTransfer Transfer to convert data from.
|
---|
1306 | * @param ppszData Where to store the string list on success.
|
---|
1307 | * @param pcbData Where to return the bytes of \a ppszData on success.
|
---|
1308 | * Includes terminator. Optional.
|
---|
1309 | *
|
---|
1310 | * @note Uses '\n' as the separator. @sa ShClTransferHttpConvertToStringListEx().
|
---|
1311 | */
|
---|
1312 | int ShClTransferHttpConvertToStringList(PSHCLHTTPSERVER pSrv, PSHCLTRANSFER pTransfer, char **ppszData, size_t *pcbData)
|
---|
1313 | {
|
---|
1314 | return shClTransferHttpConvertToStringListEx(pSrv, pTransfer, "\n", ppszData, pcbData);
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 | /**
|
---|
1318 | * Returns whether a given HTTP server instance is initialized or not.
|
---|
1319 | *
|
---|
1320 | * @returns \c true if running, or \c false if not.
|
---|
1321 | * @param pSrv HTTP server instance to check initialized state for.
|
---|
1322 | */
|
---|
1323 | bool ShClTransferHttpServerIsInitialized(PSHCLHTTPSERVER pSrv)
|
---|
1324 | {
|
---|
1325 | AssertPtrReturn(pSrv, false);
|
---|
1326 |
|
---|
1327 | return ASMAtomicReadBool(&pSrv->fInitialized);
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | /**
|
---|
1331 | * Returns whether a given HTTP server instance is running or not.
|
---|
1332 | *
|
---|
1333 | * @returns \c true if running, or \c false if not.
|
---|
1334 | * @param pSrv HTTP server instance to check running state for.
|
---|
1335 | */
|
---|
1336 | bool ShClTransferHttpServerIsRunning(PSHCLHTTPSERVER pSrv)
|
---|
1337 | {
|
---|
1338 | AssertPtrReturn(pSrv, false);
|
---|
1339 |
|
---|
1340 | return ASMAtomicReadBool(&pSrv->fRunning);
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | /**
|
---|
1344 | * Waits for a status change.
|
---|
1345 | *
|
---|
1346 | * @returns VBox status code.
|
---|
1347 | * @retval VERR_STATE_CHANGED if the HTTP server was uninitialized.
|
---|
1348 | * @param pSrv HTTP server instance to wait for.
|
---|
1349 | * @param fStatus Status to wait for.
|
---|
1350 | * Multiple statuses are possible, @sa SHCLHTTPSERVERSTATUS.
|
---|
1351 | * @param msTimeout Timeout (in ms) to wait.
|
---|
1352 | */
|
---|
1353 | int ShClTransferHttpServerWaitForStatusChange(PSHCLHTTPSERVER pSrv, SHCLHTTPSERVERSTATUS fStatus, RTMSINTERVAL msTimeout)
|
---|
1354 | {
|
---|
1355 | AssertPtrReturn(pSrv, VERR_INVALID_POINTER);
|
---|
1356 | AssertMsgReturn(ASMAtomicReadBool(&pSrv->fInitialized), ("Server not initialized yet\n"), VERR_WRONG_ORDER);
|
---|
1357 |
|
---|
1358 | shClTransferHttpServerLock(pSrv);
|
---|
1359 |
|
---|
1360 | uint64_t const tsStartMs = RTTimeMilliTS();
|
---|
1361 |
|
---|
1362 | int rc = VERR_TIMEOUT;
|
---|
1363 |
|
---|
1364 | LogFlowFunc(("fStatus=%#x, msTimeout=%RU32 -- current is %#x\n", fStatus, msTimeout, pSrv->enmStatus));
|
---|
1365 |
|
---|
1366 | while (RTTimeMilliTS() - tsStartMs <= msTimeout)
|
---|
1367 | {
|
---|
1368 | if (!pSrv->fInitialized)
|
---|
1369 | {
|
---|
1370 | rc = VERR_STATE_CHANGED;
|
---|
1371 | break;
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | shClTransferHttpServerUnlock(pSrv); /* Leave lock before waiting. */
|
---|
1375 |
|
---|
1376 | rc = RTSemEventWait(pSrv->StatusEvent, msTimeout);
|
---|
1377 |
|
---|
1378 | shClTransferHttpServerLock(pSrv);
|
---|
1379 |
|
---|
1380 | if (RT_FAILURE(rc))
|
---|
1381 | break;
|
---|
1382 |
|
---|
1383 | LogFlowFunc(("Current status now is: %#x\n", pSrv->enmStatus));
|
---|
1384 | LogRel2(("Shared Clipboard: HTTP server entered status '%s'\n", shClTransferHttpServerStatusToStr(pSrv->enmStatus)));
|
---|
1385 |
|
---|
1386 | if (pSrv->enmStatus & fStatus)
|
---|
1387 | {
|
---|
1388 | rc = VINF_SUCCESS;
|
---|
1389 | break;
|
---|
1390 | }
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | shClTransferHttpServerUnlock(pSrv);
|
---|
1394 |
|
---|
1395 | LogFlowFuncLeaveRC(rc);
|
---|
1396 | return rc;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 |
|
---|
1400 | /*********************************************************************************************************************************
|
---|
1401 | * Public Shared Clipboard HTTP context functions *
|
---|
1402 | *********************************************************************************************************************************/
|
---|
1403 |
|
---|
1404 | /**
|
---|
1405 | * Starts the HTTP server, if not started already.
|
---|
1406 | *
|
---|
1407 | * @returns VBox status code.
|
---|
1408 | * @param pCtx HTTP context to start HTTP server for.
|
---|
1409 | */
|
---|
1410 | int ShClTransferHttpServerMaybeStart(PSHCLHTTPCONTEXT pCtx)
|
---|
1411 | {
|
---|
1412 | int rc = VINF_SUCCESS;
|
---|
1413 |
|
---|
1414 | LogFlowFuncEnter();
|
---|
1415 |
|
---|
1416 | /* Start the built-in HTTP server to serve file(s). */
|
---|
1417 | if (!ShClTransferHttpServerIsRunning(&pCtx->HttpServer)) /* Only one HTTP server per transfer context. */
|
---|
1418 | rc = ShClTransferHttpServerStart(&pCtx->HttpServer, 32 /* cMaxAttempts */, NULL /* puPort */);
|
---|
1419 |
|
---|
1420 | LogFlowFuncLeaveRC(rc);
|
---|
1421 | return rc;
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | /**
|
---|
1425 | * Stops the HTTP server, if no running transfers are left.
|
---|
1426 | *
|
---|
1427 | * @returns VBox status code.
|
---|
1428 | * @param pCtx HTTP context to stop HTTP server for.
|
---|
1429 | */
|
---|
1430 | int ShClTransferHttpServerMaybeStop(PSHCLHTTPCONTEXT pCtx)
|
---|
1431 | {
|
---|
1432 | int rc = VINF_SUCCESS;
|
---|
1433 |
|
---|
1434 | LogFlowFuncEnter();
|
---|
1435 |
|
---|
1436 | if (ShClTransferHttpServerIsRunning(&pCtx->HttpServer))
|
---|
1437 | {
|
---|
1438 | /* No more registered transfers left? Tear down the HTTP server instance then. */
|
---|
1439 | if (ShClTransferHttpServerGetTransferCount(&pCtx->HttpServer) == 0)
|
---|
1440 | rc = ShClTransferHttpServerStop(&pCtx->HttpServer);
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | LogFlowFuncLeaveRC(rc);
|
---|
1444 | return rc;
|
---|
1445 | }
|
---|
1446 |
|
---|