int Http_recvHttpRsp()

in src/source/net/http_helper.c [166:246]


int Http_recvHttpRsp(NetIoHandle xNetIoHandle, unsigned int *puHttpStatus, char **ppRspBody, size_t *puRspBodyLen)
{
    int xRes = KVS_ERRNO_NONE;
    BUFFER_HANDLE xBufRecv = NULL;
    size_t uBytesReceived = 0;
    size_t uBytesTotalReceived = 0;
    unsigned int uHttpStatusCode = 0;
    const char *pBodyLoc = NULL;
    size_t uBodyLen = 0;
    char *pRspBody = NULL;

    if (xNetIoHandle == NULL || puHttpStatus == NULL || ppRspBody == NULL || puRspBodyLen == NULL)
    {
        xRes = KVS_ERRNO_FAIL;
    }
    else if ((xBufRecv = BUFFER_create_with_size(DEFAULT_HTTP_RECV_BUFSIZE)) == NULL)
    {
        xRes = KVS_ERRNO_FAIL;
    }
    else
    {
        do
        {
            /* TODO: Add timeout checking here */

            if (uBytesTotalReceived == BUFFER_length(xBufRecv))
            {
                /* If buffer is full, then we double the size of buffer. */
                if (BUFFER_enlarge(xBufRecv, uBytesTotalReceived) != 0)
                {
                    LogError("OOM: xBufRecv");
                    xRes = KVS_ERRNO_FAIL;
                    break;
                }
            }
            if (NetIo_recv(xNetIoHandle, BUFFER_u_char(xBufRecv) + uBytesTotalReceived, BUFFER_length(xBufRecv) - uBytesTotalReceived, &uBytesReceived) != KVS_ERRNO_NONE ||
                uBytesReceived == 0)
            {
                xRes = KVS_ERRNO_FAIL;
                break;
            }
            else
            {
                uBytesTotalReceived += uBytesReceived;
                if (prvParseHttpResponse((const char *)BUFFER_u_char(xBufRecv), uBytesTotalReceived, &uHttpStatusCode, &pBodyLoc, &uBodyLen) != HPE_OK)
                {
                    xRes = KVS_ERRNO_FAIL;
                }
                /* If it's 100-continue, then we need to discard previous result and do it again. */
                else if (uHttpStatusCode / 100 == 1)
                {
                    LogInfo("100-continue");
                    uBytesTotalReceived = 0;
                    xRes = KVS_ERRNO_FAIL;
                }
                else
                {
                    xRes = KVS_ERRNO_NONE;
                    *puHttpStatus = uHttpStatusCode;
                    if (uHttpStatusCode == 200)
                    {
                        if ((pRspBody = (char *)kvsMalloc(uBodyLen + 1)) == NULL)
                        {
                            LogError("OOM: ppBodyLoc");
                            xRes = KVS_ERRNO_NONE;
                            break;
                        }
                        memcpy(pRspBody, pBodyLoc, uBodyLen);
                        pRspBody[uBodyLen] = '\0';
                        *ppRspBody = pRspBody;
                        *puRspBodyLen = uBodyLen;
                    }
                }
            }
        } while (xRes != KVS_ERRNO_NONE);
    }

    BUFFER_delete(xBufRecv);

    return xRes;
}