ANSC_STATUS
HttpBmoParseHeaders
    (
        ANSC_HANDLE                 hThisObject
    )
{
    ANSC_STATUS                     returnStatus  = ANSC_STATUS_SUCCESS;
    PHTTP_BASIC_MESSAGE_OBJECT      pMyObject     = (PHTTP_BASIC_MESSAGE_OBJECT)hThisObject;
    PHTTP_HFP_INTERFACE             pHfpIf        = (PHTTP_HFP_INTERFACE       )pMyObject->hHfpIf;
    PANSC_BUFFER_DESCRIPTOR         pHeaderBdo    = (PANSC_BUFFER_DESCRIPTOR   )pMyObject->hHeaderBdo;
    PHTTP_HEADER_FIELD              pHttpHfo      = NULL;
    PVOID                           pHeaderBuffer = NULL;
    ULONG                           ulBufferSize  = 0;
    char*                           pHfStart      = NULL;
    ULONG                           ulSkipSize    = 0;
    char*                           pRawHfLine    = NULL;
    char*                           pStdHfLine    = (char*)pMyObject->ScratchPad1;
    ULONG                           ulRawLineSize = 0;
    ULONG                           ulStdLineSize = 0;

    pMyObject->DelStartLine((ANSC_HANDLE)pMyObject);
    pMyObject->DelAllHfos  ((ANSC_HANDLE)pMyObject);

    returnStatus = pMyObject->ParseStartLine((ANSC_HANDLE)pMyObject);

    if ( returnStatus != ANSC_STATUS_SUCCESS )
    {
        return  returnStatus;
    }
    else if ( !pHeaderBdo )
    {
        return  ANSC_STATUS_UNAPPLICABLE;
    }
    else
    {
        pHeaderBuffer = AnscBdoGetBlock    (pHeaderBdo);
        ulBufferSize  = AnscBdoGetBlockSize(pHeaderBdo);
    }

    AnscHttpFindHfStart(pHeaderBuffer, ulBufferSize, pHfStart);

    if ( !pHfStart )
    {
        return  ANSC_STATUS_UNAPPLICABLE;
    }
    else
    {
        ulSkipSize    = (ULONG)pHfStart - (ULONG)pHeaderBuffer;
        ulBufferSize -= ulSkipSize;
        pRawHfLine    = pHfStart;
    }

    /*
     * Skip the first line, which is the start line: request-line in client message and status-line
     * in server message.
     */
    AnscHttpGetHfLineSize(pRawHfLine, ulBufferSize, ulRawLineSize);

    pRawHfLine   += ulRawLineSize;
    ulBufferSize -= ulRawLineSize;

    /*
     * We don't have to verify the completeness of the header fields, since the caller SHOULD have
     * done so already. We create a separate HTTP Header Field Object for each header line and add
     * it into the distributed hash table. The end of header fields is signalled by the presece of
     * a CRLF pair.
     */
    while ( (ulBufferSize > 0) && pRawHfLine && !AnscHttpIsCr(*pRawHfLine) && !AnscHttpIsLf(*pRawHfLine) )
    {
        AnscHttpGetHfLineSize(pRawHfLine, ulBufferSize, ulRawLineSize);

        if ( ulRawLineSize <= pMyObject->PadSize1 )
        {
            AnscHttpPrepareHeader(pRawHfLine, ulRawLineSize, pStdHfLine, ulStdLineSize);

            pStdHfLine[ulStdLineSize + 0] = HTTP_CARRIAGE_RETURN;
            pStdHfLine[ulStdLineSize + 1] = HTTP_LINE_FEED;

            pHttpHfo =
                (PHTTP_HEADER_FIELD)pHfpIf->ParseHeader
                    (
                        pHfpIf->hOwnerContext,
                        pStdHfLine,
                        ulStdLineSize
                    );

            if ( pHttpHfo )
            {
                returnStatus =
                    pMyObject->AddHeaderField
                        (
                            (ANSC_HANDLE)pMyObject,
                            (ANSC_HANDLE)pHttpHfo
                        );
            }
        }

        pRawHfLine   += ulRawLineSize;
        ulBufferSize -= ulRawLineSize;
    }

    return  ANSC_STATUS_SUCCESS;
}
ANSC_STATUS
HttpBmoSetHeaderValueByName
    (
        ANSC_HANDLE                 hThisObject,
        char*                       name,
        char*                       value
    )
{
    ANSC_STATUS                     returnStatus = ANSC_STATUS_SUCCESS;
    PHTTP_BASIC_MESSAGE_OBJECT      pMyObject    = (PHTTP_BASIC_MESSAGE_OBJECT   )hThisObject;
    PHTTP_HFP_INTERFACE             pHfpIf       = (PHTTP_HFP_INTERFACE          )pMyObject->hHfpIf;
#ifndef _CCSP_CWMP_TCP_CONNREQ_HANDLER
    PHTTP_HELPER_CONTAINER_OBJECT   pHttpHco     = (PHTTP_HELPER_CONTAINER_OBJECT)pMyObject->hContainerContext;
#endif
    PHTTP_HEADER_FIELD              pHttpHfo     = NULL;
    char*                           pHttpHfValue = NULL;
    char*                           pHfLine      = (char*)pMyObject->ScratchPad1;
    ULONG                           ulLineSize   = 0;

    if ( (AnscSizeOfString(name) + AnscSizeOfString(value) + 4) >= pMyObject->PadSize1 )
    {
        return  ANSC_STATUS_RESOURCES;
    }
    else
    {
        AnscCopyString(pHfLine, name);

        ulLineSize  = AnscSizeOfString(name);
        pHfLine[ulLineSize + 0] = HTTP_COLON;
        pHfLine[ulLineSize + 1] = HTTP_SPACE;
        ulLineSize += 2;

        AnscCopyString(pHfLine + ulLineSize, value);

        ulLineSize += AnscSizeOfString(value);
        pHfLine[ulLineSize + 0] = HTTP_CARRIAGE_RETURN;
        pHfLine[ulLineSize + 1] = HTTP_LINE_FEED;
    }

    pHttpHfo =
        (PHTTP_HEADER_FIELD)pHfpIf->ParseHeader
            (
                pHfpIf->hOwnerContext,
                pHfLine,
                ulLineSize
            );

    if ( !pHttpHfo )
    {
        return  ANSC_STATUS_UNAPPLICABLE;
    }
    else
    {
        returnStatus =
            pMyObject->AddHeaderField
                (
                    (ANSC_HANDLE)pMyObject,
                    (ANSC_HANDLE)pHttpHfo
                );
    }

    return  returnStatus;
}
Ejemplo n.º 3
0
static void
HttpScoSetCookie
    (
        ANSC_HANDLE                 hHfpIf,
        ANSC_HANDLE                 hResponse,
        ANSC_HANDLE                 hRequest
    )
{
    PHTTP_HFP_INTERFACE             pHttpHfpIf  = (PHTTP_HFP_INTERFACE)hHfpIf;
    PHTTP_BMO_REP_OBJECT            pResponse   = (PHTTP_BMO_REP_OBJECT)hResponse;
    PHTTP_BMO_REQ_OBJECT            pRequest    = (PHTTP_BMO_REQ_OBJECT)hRequest;
    char*                           pSetCookie  = NULL;
    BOOL                            bSetCookie2 = FALSE;
    ULONG                           ulCount1    = 0;
    ULONG                           ulCount2    = 0;
    PHTTP_HEADER_FIELD              pHttpHfo     = NULL;

    pRequest->DelHeaderField((ANSC_HANDLE)pRequest, HTTP_HEADER_ID_COOKIE);

    while ( TRUE )
    {
        pSetCookie = pResponse->GetHeaderValueByName2((ANSC_HANDLE)pResponse, "Set-Cookie2", ulCount1++);
        if ( !pSetCookie )
        {
            pSetCookie = pResponse->GetHeaderValueByName2((ANSC_HANDLE)pResponse, "Set-Cookie", ulCount2++);
            bSetCookie2 = TRUE;
        }

        if ( !pSetCookie )
        {
            break;
        }
        else
        {
            char*                       pCookie = NULL;
            ULONG                       ulCookieSize    = AnscSizeOfString(pSetCookie) + 64;

            pCookie = (char *)AnscAllocateMemory(ulCookieSize);

            if ( pCookie )
            {
                PHTTP_HFO_SET_COOKIE    pHfoSetCookie = NULL;

                if ( bSetCookie2 )
                {
                    _ansc_sprintf(pCookie, "Set-Cookie2: %s", pSetCookie);
                }
                else
                {
                    _ansc_sprintf(pCookie, "Set-Cookie: %s", pSetCookie);
                }

                pHfoSetCookie = (PHTTP_HFO_SET_COOKIE)pHttpHfpIf->ParseHeader(pHttpHfpIf->hOwnerContext, pCookie, AnscSizeOfString(pCookie));

                if ( pHfoSetCookie )
                {
                    char*               pCookieValue = NULL;

                    pHfoSetCookie->HeaderId = HTTP_HEADER_ID_COOKIE;
                    pHfoSetCookie->Flags &= ~HTTP_FIELD_FLAG_VALUE_PRESENT;
                    pHfoSetCookie->Flags &= ~HTTP_FIELD_FLAG_LINE_PRESENT;

                    ulCookieSize = pHttpHfpIf->GetHeaderSize(pHttpHfpIf->hOwnerContext, (ANSC_HANDLE)pHfoSetCookie);
                    pHttpHfpIf->BuildHeader(pHttpHfpIf->hOwnerContext, (ANSC_HANDLE)pHfoSetCookie, pCookie, ulCookieSize);
                    pCookie[ulCookieSize] = 0;

                    if ( pCookie )
                    {
                        pRequest->SetHeaderValueByName((ANSC_HANDLE)pRequest, "Cookie", pCookie + 8);
                    }

                    AnscFreeMemory(pHfoSetCookie);
                }

                AnscFreeMemory(pCookie);
            }
        }
    }
}
ANSC_STATUS
CcspCwmpAcscoHttpAddCookie
    (
        ANSC_HANDLE                 hThisObject,
        PCHAR                       pCookie
    )
{
    PCCSP_CWMP_ACS_CONNECTION_OBJECT pMyObject         = (PCCSP_CWMP_ACS_CONNECTION_OBJECT)hThisObject;
    ANSC_STATUS                     returnStatus      = ANSC_STATUS_SUCCESS;
    PHTTP_SIMPLE_CLIENT_OBJECT      pHttpSimpleClient = (PHTTP_SIMPLE_CLIENT_OBJECT)pMyObject->hHttpSimpleClient;
    PHTTP_HFP_INTERFACE             pHttpHfpIf        = (PHTTP_HFP_INTERFACE)pHttpSimpleClient->GetHfpIf((ANSC_HANDLE)pHttpSimpleClient);
    char*                           pCookieValue      = NULL;
    PHTTP_HFO_SET_COOKIE            pHfoSetCookie     = NULL;

    if ( !pCookie || AnscSizeOfString(pCookie) <= 5)
    {
        CcspTr069PaTraceDebug(("!!!Empty Cookie, ignored.\n"));

        return ANSC_STATUS_SUCCESS;
    }

    if ( pMyObject->NumCookies >= CCSP_CWMP_ACSCO_MAX_COOKIE )
    {
        CcspTr069PaTraceDebug(("!!!Too many cookies, over the limit %d.\n", CCSP_CWMP_ACSCO_MAX_COOKIE));

        return ANSC_STATUS_DISCARD;
    }

    pCookieValue = (PCHAR)CcspTr069PaAllocateMemory(AnscSizeOfString(pCookie) + 64);

    if( NULL == pCookieValue )
    {
        return ANSC_STATUS_RESOURCES;
    }

    pHfoSetCookie = (PHTTP_HFO_SET_COOKIE)pHttpHfpIf->ParseHeader(pHttpHfpIf->hOwnerContext, pCookie, AnscSizeOfString(pCookie));

    if ( pHfoSetCookie )
    {
        ULONG                       ulCookieSize = 0;
        int                         nIndex;
        ULONG                       i;

        /* play a trick here - suppose the definitions of Cookie and Set-Cookie/2 are the same */
        pHfoSetCookie->HeaderId = HTTP_HEADER_ID_COOKIE;
        pHfoSetCookie->Flags &= ~HTTP_FIELD_FLAG_VALUE_PRESENT;
        pHfoSetCookie->Flags &= ~HTTP_FIELD_FLAG_LINE_PRESENT;

        ulCookieSize = pHttpHfpIf->GetHeaderSize(pHttpHfpIf->hOwnerContext, (ANSC_HANDLE)pHfoSetCookie);
        pHttpHfpIf->BuildHeader(pHttpHfpIf->hOwnerContext, (ANSC_HANDLE)pHfoSetCookie, pCookieValue, ulCookieSize);
        pCookieValue[ulCookieSize] = 0;

        /* remove old cookies */
        for ( i = 0; i < pHfoSetCookie->CookieCount; i ++ )
        {
            nIndex = pMyObject->FindCookie((ANSC_HANDLE)pMyObject, pHfoSetCookie->CookieArray[i].Name);
            if ( nIndex >= 0 )
            {
                pMyObject->DelCookie((ANSC_HANDLE)pMyObject, (ULONG)nIndex);
            }
        }

        pMyObject->Cookies[pMyObject->NumCookies++] = CcspTr069PaCloneString(pCookieValue + 8);

        CcspTr069PaFreeMemory(pHfoSetCookie);
    }

    CcspTr069PaFreeMemory(pCookieValue);

    return returnStatus;
}
ANSC_STATUS
HttpMboChkParseTrailer
    (
        ANSC_HANDLE                 hThisObject,
        char*                       pRawTrailer,
        ULONG                       ulTrailerSize
    )
{
    ANSC_STATUS                     returnStatus  = ANSC_STATUS_SUCCESS;
    PHTTP_MBO_CHUNKED_OBJECT        pMyObject     = (PHTTP_MBO_CHUNKED_OBJECT)hThisObject;
    PHTTP_HFP_INTERFACE             pHfpIf        = (PHTTP_HFP_INTERFACE     )pMyObject->hHfpIf;
    PHTTP_BCC_INTERFACE             pBccIf        = (PHTTP_BCC_INTERFACE     )pMyObject->hBccIf;
    PHTTP_HEADER_FIELD              pHttpHfo      = NULL;
    char*                           pHfStart      = (char*)pRawTrailer;
    char*                           pRawHfLine    = (char*)pRawTrailer;
    char*                           pStdHfLine    = (char*)pMyObject->ScratchPad2;
    ULONG                           ulRawLineSize = 0;
    ULONG                           ulStdLineSize = 0;
    ULONG                           ulBufferSize  = ulTrailerSize;

    if ( ulTrailerSize == 2 )
    {
        return  ANSC_STATUS_SUCCESS;
    }
    else
    {
        pHfStart   = pRawTrailer;
        pRawHfLine = pHfStart;
    }

    /*
     * Skip the first line, which is the start line: request-line in client message and status-line
     * in server message.
     */
    AnscHttpGetHfLineSize(pRawHfLine, ulBufferSize, ulRawLineSize);

    pRawHfLine   += ulRawLineSize;
    ulBufferSize -= ulRawLineSize;

    /*
     * We don't have to verify the completeness of the header fields, since the caller SHOULD have
     * done so already. We create a separate HTTP Header Field Object for each header line and add
     * it into the distributed hash table. The end of header fields is signalled by the presece of
     * a CRLF pair.
     */
    while ( (ulBufferSize > 0) && pRawHfLine && !AnscHttpIsCr(*pRawHfLine) && !AnscHttpIsLf(*pRawHfLine) )
    {
        AnscHttpGetHfLineSize(pRawHfLine, ulBufferSize, ulRawLineSize);

        if ( ulRawLineSize <= pMyObject->PadSize1 )
        {
            AnscHttpPrepareHeader(pRawHfLine, ulRawLineSize, pStdHfLine, ulStdLineSize);

            pStdHfLine[ulStdLineSize + 0] = HTTP_CARRIAGE_RETURN;
            pStdHfLine[ulStdLineSize + 1] = HTTP_LINE_FEED;

            pHttpHfo =
                (PHTTP_HEADER_FIELD)pHfpIf->ParseHeader
                    (
                        pHfpIf->hOwnerContext,
                        pStdHfLine,
                        ulStdLineSize
                    );

            if ( !pHttpHfo )
            {
                return  ANSC_STATUS_BAD_PAYLOAD;
            }
            else
            {
                returnStatus =
                    pBccIf->AddHeaderField
                        (
                            pBccIf->hOwnerContext,
                            (ANSC_HANDLE)pHttpHfo
                        );
            }
        }

        pRawHfLine   += ulRawLineSize;
        ulBufferSize -= ulRawLineSize;
    }

    return  ANSC_STATUS_SUCCESS;
}