VirtualBox

Changeset 87042 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Dec 4, 2020 12:10:36 PM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
141732
Message:

Shared Clipboard/Transfers: More cleanup / documentation work. bugref:9874

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/http-server.cpp

    r87041 r87042  
    328328
    329329/**
     330 * Initializes a HTTP body.
     331 *
     332 * @param   pBody               Body to initialize.
     333 * @param   cbSize              Size of body (in bytes) to allocate. Optional and can be 0.
     334 */
     335static int rtHttpServerBodyInit(PRTHTTPBODY pBody, size_t cbSize)
     336{
     337    if (cbSize)
     338    {
     339        pBody->pvBody      = RTMemAlloc(cbSize);
     340        AssertPtrReturn(pBody->pvBody, VERR_NO_MEMORY);
     341        pBody->cbBodyAlloc = cbSize;
     342    }
     343    else
     344    {
     345        pBody->pvBody      = NULL;
     346        pBody->cbBodyAlloc = 0;
     347    }
     348
     349    pBody->cbBodyUsed = 0;
     350    pBody->offBody    = 0;
     351
     352    return VINF_SUCCESS;
     353}
     354
     355/**
     356 * Destroys a HTTP body.
     357 *
     358 * @param   pBody               Body to destroy.
     359 */
     360static void rtHttpServerBodyDestroy(PRTHTTPBODY pBody)
     361{
     362    if (!pBody)
     363        return;
     364
     365    if (pBody->pvBody)
     366    {
     367        Assert(pBody->cbBodyAlloc);
     368
     369        RTMemFree(pBody->pvBody);
     370        pBody->pvBody = NULL;
     371    }
     372
     373    pBody->cbBodyAlloc  = 0;
     374    pBody->cbBodyUsed   = 0;
     375    pBody->offBody      = 0;
     376}
     377
     378/**
    330379 * Allocates and initializes a new client request.
    331380 *
     
    341390    AssertRC(rc2);
    342391
     392    rc2 = rtHttpServerBodyInit(&pReq->Body, 0 /* cbSize */);
     393    AssertRC(rc2);
     394
    343395    return pReq;
    344396}
     
    356408    RTHttpHeaderListDestroy(pReq->hHdrLst);
    357409
    358     RTMemFree(pReq->pvBody);
    359     pReq->pvBody = NULL;
     410    rtHttpServerBodyDestroy(&pReq->Body);
    360411
    361412    RTMemFree(pReq);
     
    376427    AssertRCReturn(rc, rc);
    377428
    378     if (cbBody)
    379     {
    380         pResp->pvBody      = RTMemAlloc(cbBody);
    381         AssertPtrReturn(pResp->pvBody, VERR_NO_MEMORY);
    382         pResp->cbBodyAlloc = cbBody;
    383     }
    384     else
    385     {
    386         pResp->cbBodyAlloc = 0;
    387     }
    388 
    389     pResp->cbBodyUsed = 0;
     429    rc = rtHttpServerBodyInit(&pResp->Body, cbBody);
    390430
    391431    return rc;
     
    417457    RTHttpHeaderListDestroy(pResp->hHdrLst);
    418458
    419     if (pResp->pvBody)
    420     {
    421         Assert(pResp->cbBodyAlloc);
    422 
    423         RTMemFree(pResp->pvBody);
    424         pResp->pvBody = NULL;
    425     }
    426 
    427     pResp->cbBodyAlloc = 0;
    428     pResp->cbBodyUsed  = 0;
     459    rtHttpServerBodyDestroy(&pResp->Body);
    429460}
    430461
  • trunk/src/VBox/Runtime/tools/RTHttpServer.cpp

    r87037 r87042  
    328328     * !!! HACK ALERT !!!
    329329     ** @todo Build up and use a real XML DOM here. Works with Gnome / Gvfs-compatible apps though.
     330     * !!! HACK ALERT !!!
    330331     */
    331332    ssize_t cch = RTStrPrintf(pszBuf, cbBuf,
     
    354355    return rc;
    355356}
     357
     358static int writeHeaderDAV(PRTHTTPSERVERREQ pReq, PRTFSOBJINFO pObjInfo, char *pszBuf, size_t cbBuf, size_t *pcbWritten)
     359{
     360    /**
     361     * !!! HACK ALERT !!!
     362     ** @todo Build up and use a real XML DOM here. Works with Gnome / Gvfs-compatible apps though.
     363     * !!! HACK ALERT !!!
     364     */
     365
     366    size_t cbWritten = 0;
     367
     368    ssize_t cch = RTStrPrintf2(pszBuf, cbBuf - cbWritten, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
     369    AssertReturn(cch, VERR_BUFFER_UNDERFLOW);
     370    pszBuf    += cch;
     371    cbWritten += cch;
     372
     373    cch = RTStrPrintf2(pszBuf, cbBuf - cbWritten, "<d:multistatus xmlns:d=\"DAV:\">\r\n");
     374    AssertReturn(cch, VERR_BUFFER_UNDERFLOW);
     375    pszBuf    += cch;
     376    cbWritten += cch;
     377
     378    int rc = dirEntryWriteDAV(pszBuf, cbBuf - cbWritten, pReq->pszUrl, pObjInfo, (size_t *)&cch);
     379    AssertRC(rc);
     380    pszBuf    += cch;
     381    cbWritten += cch;
     382
     383    *pcbWritten += cbWritten;
     384
     385    return rc;
     386}
     387
     388static int writeFooterDAV(PRTHTTPSERVERREQ pReq, char *pszBuf, size_t cbBuf, size_t *pcbWritten)
     389{
     390    RT_NOREF(pReq, pcbWritten);
     391
     392    /**
     393     * !!! HACK ALERT !!!
     394     ** @todo Build up and use a real XML DOM here. Works with Gnome / Gvfs-compatible apps though.
     395     * !!! HACK ALERT !!!
     396     */
     397    ssize_t cch = RTStrPrintf2(pszBuf, cbBuf, "</d:multistatus>");
     398    AssertReturn(cch, VERR_BUFFER_UNDERFLOW);
     399    RT_NOREF(cch);
     400
     401    return VINF_SUCCESS;
     402}
    356403#endif /* IPRT_HTTP_WITH_WEBDAV */
    357404
     
    477524        PRTHTTPSERVERRESP pResp = &pThis->Resp;
    478525
    479         const size_t cbToCopy = RT_MIN(cbBuf, pResp->cbBodyUsed);
    480         memcpy(pvBuf, pResp->pvBody, cbToCopy);
    481         Assert(pResp->cbBodyUsed >= cbToCopy);
    482         pResp->cbBodyUsed -= cbToCopy;
     526        const size_t cbToCopy = RT_MIN(cbBuf, pResp->Body.cbBodyUsed - pResp->Body.offBody);
     527        memcpy(pvBuf, (uint8_t *)pResp->Body.pvBody + pResp->Body.offBody, cbToCopy);
     528        Assert(pResp->Body.cbBodyUsed >= cbToCopy);
     529        pResp->Body.offBody += cbToCopy;
    483530
    484531        *pcbRead = cbToCopy;
     
    545592            if (RTFS_IS_DIRECTORY(objInfo.Attr.fMode))
    546593            {
    547                 PRTHTTPSERVERRESP pResp = &pThis->Resp;
     594                PRTHTTPSERVERRESP pResp = &pThis->Resp; /* Only one request a time for now. */
    548595
    549596                RTVFSDIR hVfsDir;
     
    554601                    RTHttpServerResponseInitEx(pResp, _64K); /** @todo Make this more dynamic. */
    555602
    556                     char  *pszBody    = (char *)pResp->pvBody;
    557                     size_t cbBodyLeft = pResp->cbBodyAlloc;
     603                    char  *pszBody    = (char *)pResp->Body.pvBody;
     604                    size_t cbBodyLeft = pResp->Body.cbBodyAlloc;
    558605
    559606                    /*
     
    573620                    else if (pReq->enmMethod == RTHTTPMETHOD_PROPFIND)
    574621                    {
    575                         ssize_t cch = RTStrPrintf2(pszBody, cbBodyLeft, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
    576                         Assert(cch);
    577                         pszBody    += cch;
    578                         cbBodyLeft -= cch;
    579 
    580                         cch = RTStrPrintf2(pszBody, cbBodyLeft, "<d:multistatus xmlns:d=\"DAV:\">\r\n");
    581                         Assert(cch);
    582                         pszBody    += cch;
    583                         cbBodyLeft -= cch;
    584 
    585                         rc = dirEntryWriteDAV(pszBody, cbBodyLeft, "/", &objInfo, (size_t *)&cch);
    586                         AssertRC(rc);
    587                         pszBody    += cch;
    588                         cbBodyLeft -= cch;
     622                        size_t cbWritten = 0;
     623                        rc = writeHeaderDAV(pReq, &objInfo, pszBody, cbBodyLeft, &cbWritten);
     624                        if (RT_SUCCESS(rc))
     625                        {
     626                            Assert(cbBodyLeft >= cbWritten);
     627                            cbBodyLeft -= cbWritten;
     628                        }
     629
    589630                    }
    590631#endif /* IPRT_HTTP_WITH_WEBDAV */
     
    596637                    while (RT_SUCCESS(rc = dirRead(hVfsDir, &pszEntry, &fsObjInfo)))
    597638                    {
     639                        LogFlowFunc(("Entry '%s'\n", pszEntry));
     640
    598641                        size_t cbWritten = 0;
    599642                        rc = dirEntryWrite(pReq->enmMethod, pszBody, cbBodyLeft, pszEntry, &fsObjInfo, &cbWritten);
    600643                        if (rc == VERR_BUFFER_OVERFLOW)
    601644                        {
    602                             pResp->cbBodyAlloc += _4K; /** @todo Improve this. */
    603                             pResp->pvBody       = RTMemRealloc(pResp->pvBody, pResp->cbBodyAlloc);
    604                             AssertPtrBreakStmt(pResp->pvBody, rc = VERR_NO_MEMORY);
    605 
    606                             pszBody = (char *)pResp->pvBody;
     645                            pResp->Body.cbBodyAlloc += _4K; /** @todo Improve this. */
     646                            pResp->Body.pvBody       = RTMemRealloc(pResp->Body.pvBody, pResp->Body.cbBodyAlloc);
     647                            AssertPtrBreakStmt(pResp->Body.pvBody, rc = VERR_NO_MEMORY);
     648
     649                            pszBody = (char *)pResp->Body.pvBody;
    607650                            cbBodyLeft += _4K; /** @todo Ditto. */
    608651
     
    642685                        else if (pReq->enmMethod == RTHTTPMETHOD_PROPFIND)
    643686                        {
    644                             /**
    645                              * !!! HACK ALERT !!!
    646                              ** @todo Build up and use a real XML DOM here. Works with Gnome / Gvfs-compatible apps though.
    647                              */
    648                             ssize_t cch = RTStrPrintf2(pszBody, cbBodyLeft, "</d:multistatus>\r\n");
    649                             Assert(cch);
    650                             RT_NOREF(cch);
     687                            rc  = writeFooterDAV(pReq, pszBody, cbBodyLeft, NULL);
    651688                        }
    652689#endif /* IPRT_HTTP_WITH_WEBDAV */
    653690
    654                         pResp->cbBodyUsed = strlen((char *)pResp->pvBody);
    655 
    656                         pObjInfo->cbObject = pResp->cbBodyUsed;
     691                        pResp->Body.cbBodyUsed = strlen((char *)pResp->Body.pvBody);
     692
     693                        pObjInfo->cbObject = pResp->Body.cbBodyUsed;
    657694                    }
    658695                }
Note: See TracChangeset for help on using the changeset viewer.

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