Changeset 46 in cheroxy


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

Location:
trunk
Files:
8 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/include/CRXHttpHeader.h

    r45 r46  
    11/**
    2  * CRXHttpMessage.h
     2 * CRXHttpHeader.h
    33 */
    44#ifndef __CRXHTTPMESSAGE_H__
     
    1010#define CRLF2               "\r\n\r\n" 
    1111
    12 class CRXHttpMessage
     12class CRXHttpHeader
    1313{
    1414protected:
    15     std::string mHttpMessage;
     15    std::string mHttpHeader;
    1616    std::string mHttpVersion;
    1717
    1818public:
    19     CRXHttpMessage              (void);
    20     virtual ~CRXHttpMessage     (void);
     19    CRXHttpHeader           (void);
     20    virtual ~CRXHttpHeader  (void);
    2121
    2222protected:
    23     virtual void    Parse           (void) = 0;
    24     virtual void    Reset           (void) = 0;
     23    virtual void    Parse   (void) = 0;
    2524
    2625public:
    27     void            SetMessage      (const char * aHttpMessage);
    28     std::string     GetMessage      (void) const;
    29 
    30     void            ResetMessage    (void);
     26    void            Set     (const char * aHeader);
     27    std::string     Get     (void) const;
    3128};
    3229
  • trunk/include/CRXHttpRequest.h

    r44 r46  
    55#define __CRXHTTPREQUEST_H__
    66
    7 #include "CRXHttpMessage.h"
     7#include "CRXHttpHeader.h"
    88
    9 class CRXHttpRequest : public CRXHttpMessage
     9class CRXHttpRequest : public CRXHttpHeader
    1010{
    1111private:
     
    2525    void                Parse               (void);
    2626    void                ParseFileExtension  (void);
    27     void                Reset               (void);
    2827
    2928public:
  • trunk/include/CRXHttpResponse.h

    r44 r46  
    55#define __CRXHTTPRESPONSE_H__
    66
    7 #include "CRXHttpMessage.h"
     7#include "CRXHttpHeader.h"
     8
     9#include <vector>
    810
    911#define ERROR_HTTP_RESPONSE                                 -4000
     
    1315#define ERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT         ERROR_HTTP_RESPONSE - 4
    1416
    15 class CRXHttpResponse : public CRXHttpMessage
     17class CRXHttpResponse : public CRXHttpHeader
    1618{
    1719private:
    18     int         mStatusCode;
    19     std::string mStatusString;
    20     bool        mIsChunked;     /* Transfer-Encoding */
    21     int         mContentLength;
    22 
    23     struct __content_body__ {
    24         char    * mBody;
    25         int     mLength;
    26     } mContent;
     20    int                 mStatusCode;
     21    std::string         mStatusString;
     22    bool                mIsChunked;     /* Transfer-Encoding */
     23    unsigned int        mContentLength;
     24    std::vector <char>  mContent;
    2725
    2826public:
     
    3331    void                Parse               (void);
    3432    void                Reset               (void);
    35     void                ResetContent        (void);
    3633
    3734public:
     
    3936
    4037    bool                IsChunked           (void) const;
    41     int                 SetResponseAll      (const char * aHttpResponse,
    42                                              const int  aResponseLength);
    43     void                SetHeader           (const char * aHttpResponse);
     38    void                SetHeader           (const char * aHeader);
    4439    std::string         GetHeader           (void) const;
    4540    int                 SetContent          (const char * aContent,
  • trunk/include/CRXProxy.h

    r45 r46  
    77#include "CRXException.h"
    88#include "CRXSocket.h"
    9 #include "CRXHttpMessage.h"
     9#include "CRXHttpHeader.h"
    1010#include "CRXHttpRequest.h"
    1111#include "CRXHttpResponse.h"
  • trunk/src/CRXHttpHeader.cpp

    r45 r46  
    11/**
    2  * CRXHttpMessage.cpp
     2 * CRXHttpHeader.cpp
    33 */
    44#ifdef _WIN32
     
    66#endif
    77
    8 #include "CRXHttpMessage.h"
     8#include "CRXHttpHeader.h"
    99
    10 CRXHttpMessage::CRXHttpMessage (void)
     10CRXHttpHeader::CRXHttpHeader (void)
    1111{
    1212    /*----------------------------------------------------------------*/
     
    1414}
    1515
    16 CRXHttpMessage::~CRXHttpMessage (void)
     16CRXHttpHeader::~CRXHttpHeader (void)
    1717{
    1818    /*----------------------------------------------------------------*/
     
    2121
    2222void
    23 CRXHttpMessage::SetMessage (const char * aHttpMessage)
     23CRXHttpHeader::Set (const char * aHeader)
    2424{
    2525    /*----------------------------------------------------------------*/
    26     if (aHttpMessage == NULL)
     26    if (aHeader == NULL)
    2727        return ;
    2828
    29     mHttpMessage.assign (aHttpMessage);
     29    mHttpHeader.assign (aHeader);
    3030
    3131    this->Parse ();
     
    3434
    3535std::string
    36 CRXHttpMessage::GetMessage (void) const
     36CRXHttpHeader::Get (void) const
    3737{
    3838    /*----------------------------------------------------------------*/
    3939    /*----------------------------------------------------------------*/
    4040
    41     return mHttpMessage;
     41    return mHttpHeader;
    4242}
    43 
    44 void
    45 CRXHttpMessage::ResetMessage (void)
    46 {
    47     /*----------------------------------------------------------------*/
    48     mHttpMessage.clear ();
    49     /*----------------------------------------------------------------*/
    50 }
  • trunk/src/CRXHttpRequest.cpp

    r44 r46  
    2121{
    2222    /*----------------------------------------------------------------*/
    23     Reset ();
    2423    /*----------------------------------------------------------------*/
    2524}
    2625
    2726void
    28 CRXHttpRequest::Reset (void)
     27CRXHttpRequest::SetHeader (const char * aHeader)
    2928{
    3029    /*----------------------------------------------------------------*/
    31     ResetMessage ();
    32     /*----------------------------------------------------------------*/
    33 }
    34 
    35 void
    36 CRXHttpRequest::SetHeader (const char * aHttpRequest)
    37 {
    38     /*----------------------------------------------------------------*/
    39     SetMessage (aHttpRequest);
     30    Set (aHeader);
    4031    /*----------------------------------------------------------------*/
    4132}
     
    4738    /*----------------------------------------------------------------*/
    4839
    49     return GetMessage ();
     40    return Get ();
    5041}
    5142
     
    6657
    6758    /*----------------------------------------------------------------*/
    68     aHttpRequestUrl = GetHeader ().substr (0, mHttpMessage.find ('\r'));
     59    aHttpRequestUrl = GetHeader ().substr (0, mHttpHeader.find ('\r'));
    6960
    7061    /*----------------------------------------------------------------
     
    129120}
    130121
    131 std::string CRXHttpRequest::GetURL              (void) const { return mUrl; }
    132 std::string CRXHttpRequest::GetHost             (void) const { return mHost; }
    133 int         CRXHttpRequest::GetPort             (void) const { return mPort; }
    134 std::string CRXHttpRequest::GetFileExtension    (void) const { return mFileExtension; }
     122std::string
     123CRXHttpRequest::GetURL (void) const
     124{
     125    /*----------------------------------------------------------------*/
     126    /*----------------------------------------------------------------*/
     127
     128    return mUrl;
     129}
     130
     131std::string
     132CRXHttpRequest::GetHost (void) const
     133{
     134    /*----------------------------------------------------------------*/
     135    /*----------------------------------------------------------------*/
     136
     137    return mHost;
     138}
     139
     140int
     141CRXHttpRequest::GetPort (void) const
     142{
     143    /*----------------------------------------------------------------*/
     144    /*----------------------------------------------------------------*/
     145
     146    return mPort;
     147}
     148
     149std::string
     150CRXHttpRequest::GetFileExtension (void) const
     151{
     152    /*----------------------------------------------------------------*/
     153    /*----------------------------------------------------------------*/
     154
     155    return mFileExtension;
     156}
  • 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}
  • trunk/src/CRXProxy.cpp

    r45 r46  
    9191
    9292    std::string aHttpHeader;
     93
    9394    /*----------------------------------------------------------------*/
    9495    for (;;)
     
    116117CRXProxy::SendRequest (void)
    117118{
    118     int     aResult = 0;
    119     int     aSize   = mHttpRequest.GetHeader ().length ();
    120 
    121     struct sockaddr_in  aSockName;
    122     socklen_t           aSockLen = sizeof (aSockName);
    123 
    124     /*----------------------------------------------------------------*/
    125     if (!mServer)
    126     {
    127         /* connect */
    128         aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
    129         if (aResult < 0)
    130         {
    131             aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
    132             CRX_ADD_SUBCLASS_ERROR (mServer);
    133             CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
    134             return aResult;
    135         }
    136     }
    137 
    138     aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize);
    139     if (aResult != aSize)
     119    int aResult = 0;
     120    int aLength = mHttpRequest.GetHeader ().length ();
     121
     122    /*----------------------------------------------------------------*/
     123    aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout);
     124    if (aResult < 0)
     125    {
     126        aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER;
     127        CRX_ADD_SUBCLASS_ERROR (mServer);
     128        CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ());
     129        return aResult;
     130    }
     131
     132    aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aLength);
     133    if (aResult != aLength)
    140134    {
    141135        CRX_ADD_SUBCLASS_ERROR (mServer);
     
    150144CRXProxy::ReceiveResponse (void)
    151145{
    152     int                 aResult         = 0;
    153 
    154     const unsigned int  aBufferSize     = 1024 * 64;
    155     char                aBuffer[aBufferSize + 1] = {0x00, };
     146    int         aResult = 0;
     147    char        aBuffer;
     148
     149    std::string aHttpHeader;
     150    bool        aRemainHeader = true;
    156151
    157152    /*----------------------------------------------------------------*/
    158153    for (;;)
    159154    {
    160         memset (aBuffer, 0x00, sizeof (aBuffer));
    161 
    162         aResult = mServer.Receive (aBuffer, aBufferSize);
     155        aResult = mServer.Receive (&aBuffer, 1);
    163156        if (aResult < 0)
    164157        {
     
    169162        }
    170163
    171         aResult = mHttpResponse.SetResponseAll (aBuffer, aResult);
    172         if (aResult < 0)
    173         {
    174             aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
    175             CRX_ERROR_SET (aResult, "Failed to set response.");
    176             return aResult;
     164        if (aRemainHeader)
     165        {
     166            aHttpHeader += aBuffer;
     167            if (aHttpHeader.rfind (CRLF2) != std::string::npos)
     168            {
     169                mHttpResponse.SetHeader (aHttpHeader.c_str ());
     170                aRemainHeader = false;
     171            }
     172        }
     173        else
     174        {
     175            aResult = mHttpResponse.SetContent (&aBuffer, aResult);
     176            if (aResult < 0)
     177            {
     178                aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
     179                CRX_ERROR_SET (aResult, "Failed to set response.");
     180                return aResult;
     181            }
     182            else if (aResult == 0)
     183                break;
    177184        }
    178185    }
     
    185192CRXProxy::SendResponse (void)
    186193{
    187     int     aResult = 0;
    188     int     aSize   = 0;
    189 
    190     /*----------------------------------------------------------------*/
    191     aSize = mHttpResponse.GetHeader ().length ();;
    192     aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize);
    193     if (aResult != aSize)
     194    int aResult = 0;
     195    int aLength = 0;
     196    /*----------------------------------------------------------------*/
     197    aLength = mHttpResponse.GetHeader ().length ();
     198    aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aLength);
     199    if (aResult != aLength)
    194200    {
    195201        CRX_ADD_SUBCLASS_ERROR (mClient);
     
    198204    }
    199205
    200     aSize = mHttpResponse.GetContentLength ();
    201     aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize);
    202     if (aResult != aSize)
     206    aLength = mHttpResponse.GetContentLength ();
     207    aResult = mClient.Send (mHttpResponse.GetContentBody (), aLength);
     208    if (aResult != aLength)
    203209    {
    204210        CRX_ADD_SUBCLASS_ERROR (mClient);
  • trunk/src/makefile

    r33 r46  
    1515FILES           =   \
    1616                    CRXException        \
    17                     CRXHttpMessage      \
     17                    CRXHttpHeader       \
    1818                    CRXHttpRequest      \
    1919                    CRXHttpResponse     \
  • trunk/windows/cheroxy/cheroxy.vcproj

    r32 r46  
    160160            </File>
    161161            <File
    162                 RelativePath="..\..\src\CRXHttpMessage.cpp"
     162                RelativePath="..\..\src\CRXHttpHeader.cpp"
    163163                >
    164164            </File>
     
    198198            </File>
    199199            <File
    200                 RelativePath="..\..\include\CRXHttpMessage.h"
     200                RelativePath="..\..\include\CRXHttpHeader.h"
    201201                >
    202202            </File>
Note: See TracChangeset for help on using the changeset viewer.