VirtualBox

Ignore:
Timestamp:
Sep 1, 2018 8:57:46 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
124766
Message:

IPRT/rest: Improvements to request parameter handling, implementing optional parameters and form parameters as well as convering it all to table based. Also assert on non-optional (required) parameters that aren't set. bugref:9167

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/rest/RTCRestClientRequestBase.cpp

    r73978 r74020  
    7777
    7878int RTCRestClientRequestBase::doPathParameters(RTCString *a_pStrPath, const char *a_pszPathTemplate, size_t a_cchPathTemplate,
    79                                                PATHREPLACEENTRY *a_paPathParams, size_t a_cPathParams) const
     79                                               PATHPARAMDESC const *a_paPathParams, PATHPARAMSTATE *a_paPathParamStates,
     80                                               size_t a_cPathParams) const
    8081{
    8182    int rc = a_pStrPath->assignNoThrow(a_pszPathTemplate, a_cchPathTemplate);
     
    8788        char const *psz = strstr(a_pszPathTemplate, a_paPathParams[i].pszName);
    8889        AssertReturn(psz, VERR_INTERNAL_ERROR_5);
    89         a_paPathParams[i].offName = psz - a_pszPathTemplate;
     90        a_paPathParamStates[i].offName = psz - a_pszPathTemplate;
    9091    }
    9192
     
    9495    for (size_t i = 0; i < a_cPathParams; i++)
    9596    {
    96         rc = a_paPathParams[i].pObj->toString(&strTmpVal, a_paPathParams[i].fFlags);
     97        AssertReturn(   (a_paPathParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
     98                     != RTCRestObjectBase::kCollectionFormat_multi,
     99                     VERR_INTERNAL_ERROR_3);
     100        AssertMsg(m_fIsSet & RT_BIT_64(a_paPathParams[i].iBitNo),
     101                  ("Path parameter '%s' is not set!\n", a_paPathParams[i].pszName));
     102
     103        rc = a_paPathParamStates[i].pObj->toString(&strTmpVal, a_paPathParams[i].fFlags);
    97104        AssertRCReturn(rc, rc);
    98105
    99106        /* Replace. */
    100107        ssize_t cchAdjust = strTmpVal.length() - a_paPathParams[i].cchName;
    101         rc = a_pStrPath->replaceNoThrow(a_paPathParams[i].offName, a_paPathParams[i].cchName, strTmpVal);
     108        rc = a_pStrPath->replaceNoThrow(a_paPathParamStates[i].offName, a_paPathParams[i].cchName, strTmpVal);
    102109        AssertRCReturn(rc, rc);
    103110
     
    105112        if (cchAdjust != 0)
    106113            for (size_t j = i + 1; j < a_cPathParams; j++)
    107                 if (a_paPathParams[j].offName > a_paPathParams[i].offName)
    108                     a_paPathParams[j].offName += cchAdjust;
     114                if (a_paPathParamStates[j].offName > a_paPathParamStates[i].offName)
     115                    a_paPathParamStates[j].offName += cchAdjust;
    109116    }
    110117
     
    120127    for (size_t i = 0; i < a_cQueryParams; i++)
    121128    {
    122         if ((a_paQueryParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask) != RTCRestObjectBase::kCollectionFormat_multi)
     129        if (   a_paQueryParams[i].fRequired
     130            || (m_fIsSet & RT_BIT_64(a_paQueryParams[i].iBitNo)) )
    123131        {
    124             int rc = a_papQueryParamObjs[i]->toString(&strTmpVal, a_paQueryParams[i].fFlags);
    125             AssertRCReturn(rc, rc);
     132            AssertMsg(m_fIsSet & RT_BIT_64(a_paQueryParams[i].iBitNo),
     133                      ("Required query parameter '%s' is not set!\n", a_paQueryParams[i].pszName));
    126134
    127             rc = a_pStrQuery->appendPrintfNoThrow("%c%RMpq=%RMpq", chSep, a_paQueryParams[i].pszName, strTmpVal.c_str());
    128             AssertRCReturn(rc, rc);
     135            if (   (a_paQueryParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
     136                != RTCRestObjectBase::kCollectionFormat_multi)
     137            {
     138                int rc = a_papQueryParamObjs[i]->toString(&strTmpVal, a_paQueryParams[i].fFlags);
     139                AssertRCReturn(rc, rc);
    129140
    130             chSep = '&';
    131         }
    132         else
    133         {
    134             AssertFailedReturn(VERR_NOT_IMPLEMENTED);
     141                rc = a_pStrQuery->appendPrintfNoThrow("%c%RMpq=%RMpq", chSep, a_paQueryParams[i].pszName, strTmpVal.c_str());
     142                AssertRCReturn(rc, rc);
     143
     144                chSep = '&';
     145            }
     146            else
     147            {
     148                AssertFailedReturn(VERR_NOT_IMPLEMENTED);
     149            }
    135150        }
    136151    }
     
    138153}
    139154
     155
     156int RTCRestClientRequestBase::doHeaderParameters(RTHTTP a_hHttp, HEADERPARAMDESC const *a_paHeaderParams,
     157                                                 RTCRestObjectBase const **a_papHeaderParamObjs, size_t a_cHeaderParams) const
     158{
     159    RTCString strTmpVal;
     160    for (size_t i = 0; i < a_cHeaderParams; i++)
     161    {
     162        AssertReturn(   (a_paHeaderParams[i].fFlags & RTCRestObjectBase::kCollectionFormat_Mask)
     163                     != RTCRestObjectBase::kCollectionFormat_multi,
     164                     VERR_INTERNAL_ERROR_3);
     165
     166        if (   a_paHeaderParams[i].fRequired
     167            || (m_fIsSet & RT_BIT_64(a_paHeaderParams[i].iBitNo)) )
     168        {
     169            AssertMsg(m_fIsSet & RT_BIT_64(a_paHeaderParams[i].iBitNo),
     170                      ("Required header parameter '%s' is not set!\n", a_paHeaderParams[i].pszName));
     171
     172            int rc = a_papHeaderParamObjs[i]->toString(&strTmpVal, a_paHeaderParams[i].fFlags);
     173            AssertRCReturn(rc, rc);
     174
     175            rc = RTHttpAppendHeader(a_hHttp, a_paHeaderParams[i].pszName, strTmpVal.c_str(), 0);
     176            AssertRCReturn(rc, rc);
     177        }
     178    }
     179    return VINF_SUCCESS;
     180}
     181
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