Changeset 45 in cheroxy for trunk


Ignore:
Timestamp:
11/22/12 18:38:07 (11 years ago)
Author:
cheese
Message:

#1 change access modifier of filter and http messages

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/CRXProxy.h

    r44 r45  
    1919#define ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER ERROR_PROXY - 5
    2020#define ERROR_PROXY_FAILED_TO_SET_RESPONSE      ERROR_PROXY - 6
     21#define ERROR_PROXY_FAILED_TO_ACCEPT_CLIENT     ERROR_PROXY - 7
    2122
    2223
     
    3536
    3637public:
    37     CRXProxy            (void);
     38    CRXProxy            (const int aSocket);
    3839    ~CRXProxy           (void);
    3940
    4041public:
    4142    /* common utilities */
    42     void    SetClientSocket             (const int  aSocket);
    4343    void    SetServerTimeout            (const int  aTimeout = TCPSOCKET_NO_TIMEOUT);
    4444    void    Close                       (void);
    45 
    46 public:
    47     /* interface to filter */
    48     void    SetRequestFilter            (const E_CRX_FILTER_REQUEST aType,
    49                                          const bool                 aIsMatched,
    50                                          const std::string          aValue);
    51     void    RemoveRequestFilter         (const E_CRX_FILTER_REQUEST aType);
    52     bool    CheckRequestFilter          (const E_CRX_FILTER_REQUEST aType);
    5345
    5446public:
     
    6456public:
    6557    /* HTTP data */
    66     void    SetHttpRequest              (const char * aHttpRequest);
    67     char *  GetHttpRequest              (char       * aBuffer,
    68                                          const int  aBufferSize) const;
    69     int     GetHttpRequestLength        (void) const;
    70 
    71     void    SetHttpResponse             (const char * aHttpResponse);
    72     char *  GetHttpResponseHeader       (char       * aBuffer,
    73                                          const int  aBufferSize) const;
    74     int     GetHttpResponseHeaderLength (void) const;
    75     char *  GetHttpResponseBody         (char       * aBuffer,
    76                                          const int  aBufferSize) const;
    77     int     GetHttpResponseBodyLength   (void) const;
    78    
     58    CRXFilter &         GetFilter       (void) const;
     59    CRXHttpRequest &    GetHttpRequest  (void);
     60    CRXHttpResponse &   GetHttpResponse (void);
    7961};
    8062
  • trunk/src/CRXHttpResponse.cpp

    r44 r45  
    162162int
    163163CRXHttpResponse::SetContent (const char * aContent,
    164                                const int    aLength)
     164                             const int  aLength)
    165165{
    166166    int     aResult = 0;
  • trunk/src/CRXProxy.cpp

    r44 r45  
    99CRXFilter   CRXProxy::mFilter;
    1010
    11 CRXProxy::CRXProxy (void)
    12     : mClient (0),
    13       mServer (0),
    14       mServerTimeout (0)
    15 {
    16     /*----------------------------------------------------------------*/
     11CRXProxy::CRXProxy (const int aSocket)
     12    : mServerTimeout (0)
     13{
     14    /*----------------------------------------------------------------*/
     15    if (aSocket < 0)
     16        return ;
     17
     18    mClient = aSocket;
    1719    /*----------------------------------------------------------------*/
    1820}
     
    2224    /*----------------------------------------------------------------*/
    2325    Close ();
    24     /*----------------------------------------------------------------*/
    25 }
    26 
    27 void
    28 CRXProxy::SetClientSocket (const int aSocket)
    29 {
    30     /*----------------------------------------------------------------*/
    31     if (aSocket <= 0)
    32         return ;
    33 
    34     mClient = aSocket;
    3526    /*----------------------------------------------------------------*/
    3627}
     
    5142    mClient.Close ();
    5243    /*----------------------------------------------------------------*/
    53 }
    54 
    55 void
    56 CRXProxy::SetRequestFilter (const E_CRX_FILTER_REQUEST  aType,
    57                             const bool                  aIsMatched,
    58                             const std::string           aValue)
    59 {
    60     /*----------------------------------------------------------------*/
    61     mFilter.SetRequestFilter (aType, aIsMatched, aValue);
    62     /*----------------------------------------------------------------*/
    63 }
    64 
    65 void
    66 CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)
    67 {
    68     /*----------------------------------------------------------------*/
    69     mFilter.RemoveRequestFilter (aType);
    70     /*----------------------------------------------------------------*/
    71 }
    72 
    73 bool
    74 CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)
    75 {
    76     /*----------------------------------------------------------------*/
    77     /*----------------------------------------------------------------*/
    78 
    79     return mFilter.CheckRequestFilter (aType, this->mHttpRequest);
    8044}
    8145
     
    12084}
    12185
    122 void
    123 CRXProxy::SetHttpRequest (const char * aHttpRequest)
    124 {
    125     /*----------------------------------------------------------------*/
    126     mHttpRequest.SetHeader (aHttpRequest);
    127     /*----------------------------------------------------------------*/
    128 }
    129 
    130 char *
    131 CRXProxy::GetHttpRequest (char      * aBuffer,
    132                           const int aBufferSize) const
    133 {
    134     void        * aPtr = static_cast<void *> (aBuffer);
    135 
    136     int         aCopyLength     = 0;
    137     int         aRemainLength   = aBufferSize;
    138     const int   aHeaderLength   = mHttpRequest.GetHeader ().length ();
    139 
    140     /*----------------------------------------------------------------*/
    141     if (aPtr == NULL)
    142         return NULL;
    143 
    144     aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
    145     if (aCopyLength > 0)
    146         memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aCopyLength);
    147     /*----------------------------------------------------------------*/
    148 
    149     return aBuffer;
    150 }
    151 
    152 int
    153 CRXProxy::GetHttpRequestLength (void) const
    154 {
    155     /*----------------------------------------------------------------*/
    156     /*----------------------------------------------------------------*/
    157 
    158     return mHttpRequest.GetHeader().length ();
    159 }
    160 
    161 void
    162 CRXProxy::SetHttpResponse (const char * aHttpResponse)
    163 {
    164     /*----------------------------------------------------------------*/
    165     mHttpResponse.SetHeader (aHttpResponse);
    166     /*----------------------------------------------------------------*/
    167 }
    168 
    169 char *
    170 CRXProxy::GetHttpResponseHeader (char       * aBuffer,
    171                                  const int  aBufferSize) const
    172 {
    173     void        * aPtr = static_cast<void *> (aBuffer);
    174 
    175     int         aCopyLength     = 0;
    176     int         aRemainLength   = aBufferSize;
    177     const int   aHeaderLength   = mHttpResponse.GetHeader ().length ();
    178 
    179     /*----------------------------------------------------------------*/
    180     if (aPtr == NULL)
    181         return NULL;
    182 
    183     aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength;
    184     if (aCopyLength > 0)
    185         memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aCopyLength);
    186     /*----------------------------------------------------------------*/
    187 
    188     return aBuffer;
    189 }
    190 
    191 int
    192 CRXProxy::GetHttpResponseHeaderLength (void) const
    193 {
    194     /*----------------------------------------------------------------*/
    195     /*----------------------------------------------------------------*/
    196 
    197     return mHttpResponse.GetHeader ().length ();
    198 }
    199 
    200 char *
    201 CRXProxy::GetHttpResponseBody (char         * aBuffer,
    202                                const int    aBufferSize) const
    203 {
    204     void        * aPtr = static_cast<void *> (aBuffer);
    205 
    206     int         aCopyLength     = 0;
    207     int         aRemainLength   = aBufferSize;
    208     const int   aContentLength  = mHttpResponse.GetContentLength ();
    209 
    210     /*----------------------------------------------------------------*/
    211     if (aPtr == NULL)
    212         return NULL;
    213 
    214     aCopyLength = aRemainLength < aContentLength ? aRemainLength : aContentLength;
    215     if (aCopyLength > 0)
    216         memcpy (aPtr, mHttpResponse.GetContentBody (), aCopyLength);
    217     /*----------------------------------------------------------------*/
    218 
    219     return aBuffer;
    220 }
    221 
    222 int
    223 CRXProxy::GetHttpResponseBodyLength (void) const
    224 {
    225     /*----------------------------------------------------------------*/
    226     /*----------------------------------------------------------------*/
    227 
    228     return mHttpResponse.GetContentLength ();
    229 }
    230 
    23186int
    23287CRXProxy::ReceiveRequest (void)
     
    315170
    316171        aResult = mHttpResponse.SetResponseAll (aBuffer, aResult);
    317         if (aResult)
     172        if (aResult < 0)
    318173        {
    319174            aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE;
     
    354209    return aResult < 0 ? aResult : 0;
    355210}
     211
     212CRXFilter &
     213CRXProxy::GetFilter (void) const
     214{
     215    /*----------------------------------------------------------------*/
     216    /*----------------------------------------------------------------*/
     217
     218    return mFilter;
     219}
     220
     221CRXHttpRequest &
     222CRXProxy::GetHttpRequest (void)
     223{
     224    /*----------------------------------------------------------------*/
     225    /*----------------------------------------------------------------*/
     226
     227    return mHttpRequest;
     228}
     229
     230CRXHttpResponse &
     231CRXProxy::GetHttpResponse (void)
     232{
     233    /*----------------------------------------------------------------*/
     234    /*----------------------------------------------------------------*/
     235
     236    return mHttpResponse;
     237}
  • trunk/src/main.cpp

    r44 r45  
    6868}
    6969
    70 THREAD_FUNCTION_RETURN
    71 THREAD_FUNCTION_CALLING_CONVENTION
    72 CRXProxyMTWrapper (void * aThreadArg)
    73 {
    74     int         aResult = 0;
    75 
    76     CRXSocket   * aProxySocket = (CRXSocket *)aThreadArg;
    77     CRXProxy    * aProxy = NULL;
    78 
    79     char        aFilterFileExtension[] = "exe|gif|jpg|png|css|js|ico|swf|";
    80     char        * aHttpMessage = NULL;
    81     int         aHttpMessageLength = 0;
    82 
    83     /*----------------------------------------------------------------*/
    84     for (;;)
    85     {
    86         aResult = aProxySocket->Accept ();
    87         if (aResult < 0)
    88         {
    89             cerr << "Failed to accept." << endl;
    90             break;
    91         }
    92 
    93         if ((aProxy = new(std::nothrow) CRXProxy ()) == NULL)
    94         {
    95             cerr << "Failed to get proxy." << endl;
    96             aResult = -1;
    97             break;
    98         }
    99 
    100         aProxy->SetClientSocket (aResult);
    101         aProxy->SetServerTimeout (2);
    102         aProxy->SetRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION,
    103                                   CRX_FILTER_MATCHED,
    104                                   aFilterFileExtension);
    105 
    106         aResult = aProxy->Forward ();
    107         if (aResult < 0)
    108         {
    109             cout << aProxy->GetErrorMessage () << endl;
    110         }
    111 
    112         if (!aProxy->CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION))
    113         {
    114             aHttpMessageLength = aProxy->GetHttpRequestLength ();
    115             aHttpMessage = (char *) calloc (aHttpMessageLength + 1, 1);
    116             aProxy->GetHttpRequest (aHttpMessage, aHttpMessageLength);
    117             CRX_DEBUG_TRACE ("== Request: \n%s\n", aHttpMessage);
    118             free (aHttpMessage);
    119 
    120             aHttpMessageLength = aProxy->GetHttpResponseHeaderLength ();
    121             aHttpMessage = (char *) calloc (aHttpMessageLength + 1, 1);
    122             aProxy->GetHttpResponseHeader (aHttpMessage, aHttpMessageLength);
    123             CRX_DEBUG_TRACE ("== Response: \n%s\n", aHttpMessage);
    124             free (aHttpMessage);
    125 
    126             aHttpMessageLength = aProxy->GetHttpResponseBodyLength ();
    127             aHttpMessage = (char *) calloc (aHttpMessageLength + 1, 1);
    128             aProxy->GetHttpResponseBody (aHttpMessage, aHttpMessageLength);
    129             CRX_DEBUG_TRACE_BIN (aHttpMessage, aHttpMessageLength, "== Content-Body: \n");
    130             free (aHttpMessage);
    131         }
    132 
    133         delete aProxy;
    134     }
    135 
    136     cout << " thread exit." << endl;
    137     /*----------------------------------------------------------------*/
    138 
    139     return (THREAD_FUNCTION_RETURN)0;
    140 }
    141 
    14270int ThreadPool (CRXSocket   * aProxySocket,
    14371                const int   aThreadPoolCount)
     
    17199    return aResult;
    172100}
     101
     102THREAD_FUNCTION_RETURN
     103THREAD_FUNCTION_CALLING_CONVENTION
     104CRXProxyMTWrapper (void * aThreadArg)
     105{
     106    int         aResult = 0;
     107
     108    CRXSocket   & aProxySocket = *((CRXSocket *)aThreadArg);
     109    CRXProxy    * aProxy = NULL;
     110
     111    char        aFilterFileExtension[] = "exe|gif|jpg|png|css|js|ico|swf|";
     112
     113    /*----------------------------------------------------------------*/
     114    for (;;)
     115    {
     116        aResult = aProxySocket.Accept ();
     117        if (aResult < 0)
     118        {
     119            cerr << aProxySocket.GetErrorMessage () << endl;
     120            aResult = -1;
     121            break;
     122        }
     123
     124        aProxy = new(std::nothrow) CRXProxy (aResult);
     125        if (aProxy == NULL)
     126        {
     127            cerr << "Failed to get proxy." << endl;
     128            aResult = -1;
     129            break;
     130        }
     131        aProxy->SetServerTimeout (2);
     132
     133        CRXFilter       & aFilter   = aProxy->GetFilter ();
     134        CRXHttpRequest  & aRequest  = aProxy->GetHttpRequest ();
     135        CRXHttpResponse & aResponse = aProxy->GetHttpResponse ();
     136
     137        aFilter.SetRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION,
     138                                  CRX_FILTER_MATCHED,
     139                                  aFilterFileExtension);
     140
     141        aResult = aProxy->Forward ();
     142        if (aResult < 0)
     143        {
     144            cerr << aProxy->GetErrorMessage () << endl;
     145        }
     146
     147        if (!aFilter.CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION, aRequest))
     148        {
     149            CRX_DEBUG_TRACE ("== Request:  \n%s\n", aRequest.GetHeader ().c_str ());
     150            CRX_DEBUG_TRACE ("== Response: \n%s\n", aResponse.GetHeader ().c_str ());
     151            CRX_DEBUG_TRACE_BIN (aResponse.GetContentBody (),
     152                                 aResponse.GetContentLength (),
     153                                 "== Content-Body: \n");
     154        }
     155
     156        delete aProxy;
     157    }
     158
     159    cout << " thread exit." << endl;
     160    /*----------------------------------------------------------------*/
     161
     162    return (THREAD_FUNCTION_RETURN)0;
     163}
Note: See TracChangeset for help on using the changeset viewer.