Changeset 46 in cheroxy for trunk/src/CRXHttpResponse.cpp


Ignore:
Timestamp:
11/26/12 15:23:31 (11 years ago)
Author:
cheese
Message:

#1 rename http message to http header and change way to process response content as binary

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/CRXHttpResponse.cpp

    r45 r46  
    3636{
    3737    /*----------------------------------------------------------------*/
    38     ResetMessage ();
    39     ResetContent ();
     38    Get ().clear ();
     39    mContent.clear ();
    4040    /*----------------------------------------------------------------*/
    4141}
    4242
    4343void
    44 CRXHttpResponse::ResetContent (void)
     44CRXHttpResponse::SetHeader (const char * aHeader)
    4545{
    4646    /*----------------------------------------------------------------*/
    47     if (mContent.mBody)
    48         free (mContent.mBody);
    49 
    50     memset (&mContent, 0x00, sizeof (mContent));
    51     /*----------------------------------------------------------------*/
    52 }
    53 
    54 int
    55 CRXHttpResponse::SetResponseAll (const char * aHttpResponse,
    56                                  const int  aResponseLength)
    57 {
    58     int             aResult = 0;
    59 
    60     const char      * aEndOfHeader = NULL;
    61     std::string     aHeader;
    62 
    63     int             aHeaderLength = 0;
    64 
    65     /*----------------------------------------------------------------*/
    66     if (aResponseLength <= 0)
    67         return ERROR_HTTP_RESPONSE_INVALID_LENGTH;
    68 
    69     if (GetHeader ().rfind (CRLF2) == std::string::npos)
    70     {
    71         aEndOfHeader = strstr (aHttpResponse, CRLF2);
    72         if (aEndOfHeader == NULL)
    73             return ERROR_HTTP_RESPONSE_INVALID_FORMAT;
    74 
    75         aEndOfHeader += strlen (CRLF2);
    76         aHeaderLength = aEndOfHeader - aHttpResponse;
    77         aHeader.assign (aHttpResponse, aHeaderLength);
    78 
    79         SetHeader (aHeader.c_str ());
    80     }
    81 
    82     aResult = SetContent (aHttpResponse + aHeaderLength, aResponseLength - aHeaderLength);
    83     if (aResult)
    84     {
    85         aResult = ERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT;
    86     }
    87     /*----------------------------------------------------------------*/
    88 
    89     return aResult;
    90 }
    91 
    92 void
    93 CRXHttpResponse::SetHeader (const char * aHttpMessage)
    94 {
    95     /*----------------------------------------------------------------*/
    96     SetMessage (aHttpMessage);
     47    Set (aHeader);
    9748    /*----------------------------------------------------------------*/
    9849}
     
    10455    /*----------------------------------------------------------------*/
    10556
    106     return GetMessage ();
     57    return Get ();
    10758}
    10859
     
    164115                             const int  aLength)
    165116{
    166     int     aResult = 0;
    167 
    168     char    * aReallocPtr = NULL;
     117    int     aResult = 0;
     118    int     aSize   = 0;
     119    char    * aChunkedPtr = NULL;
    169120
    170121    /*----------------------------------------------------------------*/
    171     if (!mIsChunked)
     122    /*----------------------------------------------------------------
     123     * 1. check parameter
     124     *----------------------------------------------------------------*/
     125    if (aLength < 0)
     126        return ERROR_HTTP_RESPONSE_INVALID_LENGTH;
     127
     128    /*----------------------------------------------------------------
     129     * 2. check content size when is NOT chunked-encoding
     130     *----------------------------------------------------------------*/
     131    if (!mIsChunked && mContent.size () >= mContentLength)
     132        return 0;
     133
     134    /*----------------------------------------------------------------
     135     * 3. resize memory and copy
     136     *----------------------------------------------------------------*/
     137    aSize = mContent.size ();
     138    mContent.resize (aSize + aLength);
     139    memcpy (mContent.begin ()._Myptr + aSize, aContent, aLength);
     140
     141    /*----------------------------------------------------------------
     142     * 4.
     143     *----------------------------------------------------------------*/
     144    aResult = mContent.size ();
     145    if (mIsChunked)
    172146    {
    173         if (mContentLength <= mContent.mLength)
    174             return aResult;
    175 
    176         if (mContent.mBody == NULL)
     147        mContentLength += aLength;
     148        aSize = mContent.size ();
     149        if ((unsigned int)aSize > strlen (CRLF) + strlen (CRLF2))
    177150        {
    178             mContent.mBody = static_cast<char *> (calloc (mContentLength, 1));
    179             if (mContent.mBody == NULL)
     151            aChunkedPtr = mContent.begin ()._Myptr + aSize - strlen (CRLF2);
     152            if (!strncmp (aChunkedPtr, CRLF2, strlen (CRLF2)))
    180153            {
    181                 return ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION;
     154                for ( ; strncmp (aChunkedPtr--, CRLF, strlen (CRLF)) ; );
     155                aResult = strtol (aChunkedPtr, NULL, 16);
    182156            }
    183157        }
    184158    }
    185     else
    186     {
    187         mContentLength += aLength;
    188         aReallocPtr = static_cast<char *> (realloc (mContent.mBody, mContentLength));
    189         if (aReallocPtr == NULL)
    190         {
    191             return ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION;
    192         }
    193         mContent.mBody = aReallocPtr;
    194     }
    195 
    196     memcpy (mContent.mBody + mContent.mLength, aContent, aLength);
    197     mContent.mLength += aLength;
    198159    /*----------------------------------------------------------------*/
    199160
     
    201162}
    202163
    203 int             CRXHttpResponse::GetStatusCode      (void) const { return mStatusCode; }
    204 bool            CRXHttpResponse::IsChunked          (void) const { return mIsChunked; }
    205 int             CRXHttpResponse::GetContentLength   (void) const { return mContentLength; }
    206 const char *    CRXHttpResponse::GetContentBody     (void) const { return mContent.mBody; }
     164int
     165CRXHttpResponse::GetStatusCode (void) const
     166{
     167    /*----------------------------------------------------------------*/
     168    /*----------------------------------------------------------------*/
     169
     170    return mStatusCode;
     171}
     172
     173bool
     174CRXHttpResponse::IsChunked (void) const
     175{
     176    /*----------------------------------------------------------------*/
     177    /*----------------------------------------------------------------*/
     178
     179    return mIsChunked;
     180}
     181
     182int
     183CRXHttpResponse::GetContentLength (void) const
     184{
     185    /*----------------------------------------------------------------*/
     186    /*----------------------------------------------------------------*/
     187
     188    return mContentLength;
     189}
     190
     191const char *
     192CRXHttpResponse::GetContentBody (void) const
     193{
     194    /*----------------------------------------------------------------*/
     195    /*----------------------------------------------------------------*/
     196
     197    return mContent.empty () ? "" : &mContent[0];
     198}
Note: See TracChangeset for help on using the changeset viewer.