VirtualBox

Changeset 87044 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Dec 4, 2020 5:42:55 PM (4 years ago)
Author:
vboxsync
Message:

Shared Clipboard/Transfers: A bit of cleanup rtHttpServerParseRequest(). bugref:9874

File:
1 edited

Legend:

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

    r87042 r87044  
    768768
    769769            rc = rtHttpServerSendResponseEx(pClient, RTHTTPSTATUS_OK, &HdrLst);
     770
     771            RTHttpHeaderListDestroy(HdrLst);
     772
     773            if (rc == VERR_BROKEN_PIPE) /* Could happen on fast reloads. */
     774                break;
    770775            AssertRCReturn(rc, rc);
    771 
    772             RTHttpHeaderListDestroy(HdrLst);
    773776
    774777            size_t cbToRead  = fsObj.cbObject;
     
    10531056 *                              Needs to be free'd via rtHttpServerReqFree().
    10541057 */
    1055 static int rtHttpServerParseRequest(PRTHTTPSERVERCLIENT pClient, char *pszReq, size_t cbReq,
     1058static int rtHttpServerParseRequest(PRTHTTPSERVERCLIENT pClient, const char *pszReq, size_t cbReq,
    10561059                                    PRTHTTPSERVERREQ *ppReq)
    10571060{
     
    10641067    AssertReturn(RTStrIsValidEncoding(pszReq), VERR_INVALID_PARAMETER);
    10651068
    1066     char **ppapszStrings = NULL;
    1067     size_t cStrings      = 0;
    1068     int rc = RTStrSplit(pszReq, cbReq, RTHTTPSERVER_HTTP11_EOL_STR, &ppapszStrings, &cStrings);
     1069    char **ppapszReq = NULL;
     1070    size_t cReq      = 0;
     1071    int rc = RTStrSplit(pszReq, cbReq, RTHTTPSERVER_HTTP11_EOL_STR, &ppapszReq, &cReq);
    10691072    if (RT_FAILURE(rc))
    10701073        return rc;
    10711074
    1072     if (!cStrings)
     1075    if (!cReq)
    10731076        return VERR_INVALID_PARAMETER;
    10741077
    1075     /** Advances pszReq to the next string in the current line.
    1076      ** @todo Can we do better here? */
    1077 #define REQ_GET_NEXT_STRING \
    1078     pszReq = psz; \
    1079     while (psz && !RT_C_IS_SPACE(*psz)) \
    1080         psz++; \
    1081     if (psz) \
    1082     { \
    1083         *psz = '\0'; \
    1084         psz++; \
    1085     } \
    1086     else /* Be strict for now. */ \
    1087         AssertFailedBreakStmt(rc = VERR_INVALID_PARAMETER);
     1078#ifdef LOG_ENABLED
     1079    for (size_t i = 0; i < cReq; i++)
     1080        LogFlowFunc(("%s\n", ppapszReq[i]));
     1081#endif
    10881082
    10891083    PRTHTTPSERVERREQ pReq = NULL;
    10901084
    1091     for (;;) /* To use break. */
    1092     {
    1093         /* Start with the first line. */
    1094         char *psz = ppapszStrings[0];
    1095         AssertPtrBreakStmt(psz, rc = VERR_INVALID_PARAMETER);
    1096 
    1097         /* A tiny bit of sanitation. */
    1098         RTStrStrip(psz);
    1099 
     1085    char **ppapszFirstLine = NULL;
     1086    size_t cFirstLine = 0;
     1087    rc = RTStrSplit(ppapszReq[0], strlen(ppapszReq[0]), " ", &ppapszFirstLine, &cFirstLine);
     1088    if (RT_SUCCESS(rc))
     1089    {
     1090        if (cFirstLine < 3) /* At leat the method, path and version has to be present. */
     1091            rc = VERR_INVALID_PARAMETER;
     1092    }
     1093
     1094    while (RT_SUCCESS(rc)) /* To use break. */
     1095    {
    11001096        pReq = rtHttpServerReqAlloc();
    11011097        AssertPtrBreakStmt(pReq, rc = VERR_NO_MEMORY);
    1102 
    1103         REQ_GET_NEXT_STRING
    11041098
    11051099        /*
    11061100         * Parse method to use. Method names are case sensitive.
    11071101         */
    1108         if      (!RTStrCmp(pszReq, "GET"))      pReq->enmMethod = RTHTTPMETHOD_GET;
    1109         else if (!RTStrCmp(pszReq, "HEAD"))     pReq->enmMethod = RTHTTPMETHOD_HEAD;
     1102        const char *pszMethod = ppapszFirstLine[0];
     1103        if      (!RTStrCmp(pszMethod, "GET"))      pReq->enmMethod = RTHTTPMETHOD_GET;
     1104        else if (!RTStrCmp(pszMethod, "HEAD"))     pReq->enmMethod = RTHTTPMETHOD_HEAD;
    11101105#ifdef IPRT_HTTP_WITH_WEBDAV
    1111         else if (!RTStrCmp(pszReq, "OPTIONS"))  pReq->enmMethod = RTHTTPMETHOD_OPTIONS;
    1112         else if (!RTStrCmp(pszReq, "PROPFIND")) pReq->enmMethod = RTHTTPMETHOD_PROPFIND;
     1106        else if (!RTStrCmp(pszMethod, "OPTIONS"))  pReq->enmMethod = RTHTTPMETHOD_OPTIONS;
     1107        else if (!RTStrCmp(pszMethod, "PROPFIND")) pReq->enmMethod = RTHTTPMETHOD_PROPFIND;
    11131108#endif
    11141109        else
     
    11181113        }
    11191114
    1120         REQ_GET_NEXT_STRING
    1121 
     1115        /*
     1116         * Parse requested path.
     1117         */
    11221118        /** @todo Do URL unescaping here. */
    1123 
    1124         if (!rtHttpServerPathIsValid(pszReq, false /* fIsAbsolute */))
     1119        const char *pszPath = ppapszFirstLine[1];
     1120        if (!rtHttpServerPathIsValid(pszPath, false /* fIsAbsolute */))
    11251121        {
    11261122            rc = VERR_PATH_NOT_FOUND;
     
    11281124        }
    11291125
    1130         pReq->pszUrl = RTStrDup(pszReq);
    1131 
    1132         REQ_GET_NEXT_STRING
    1133 
    1134         /* We're picky heree: Only HTTP 1.1 is supported by now. */
    1135         if (RTStrCmp(pszReq, RTHTTPVER_1_1_STR)) /** @todo Use RTStrVersionCompare. Later. */
     1126        pReq->pszUrl = RTStrDup(pszPath);
     1127
     1128        /*
     1129         * Parse HTTP version to use.
     1130         * We're picky heree: Only HTTP 1.1 is supported by now.
     1131         */
     1132        const char *pszVer = ppapszFirstLine[2];
     1133        if (RTStrCmp(pszVer, RTHTTPVER_1_1_STR)) /** @todo Use RTStrVersionCompare. Later. */
    11361134        {
    11371135            rc = VERR_NOT_SUPPORTED;
     
    11441142         * Process headers, if any.
    11451143         */
    1146         if (cStrings > 1)
    1147         {
    1148             rc = rtHttpServerParseHeaders(pReq->hHdrLst, cStrings - 1, &ppapszStrings[1]);
     1144        if (cReq > 1)
     1145        {
     1146            rc = rtHttpServerParseHeaders(pReq->hHdrLst, cReq - 1, &ppapszReq[1]);
    11491147            if (RT_SUCCESS(rc))
    11501148            {
     
    11591157     * Cleanup.
    11601158     */
    1161     for (size_t i = 0; i < cStrings; i++)
    1162         RTStrFree(ppapszStrings[i]);
    1163     RTMemFree(ppapszStrings);
     1159
     1160    for (size_t i = 0; i < cFirstLine; i++)
     1161        RTStrFree(ppapszFirstLine[i]);
     1162    RTMemFree(ppapszFirstLine);
     1163
     1164    for (size_t i = 0; i < cReq; i++)
     1165        RTStrFree(ppapszReq[i]);
     1166    RTMemFree(ppapszReq);
    11641167
    11651168    if (RT_FAILURE(rc))
Note: See TracChangeset for help on using the changeset viewer.

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