Changeset 45 in cheroxy
- Timestamp:
- 11/22/12 18:38:07 (11 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/CRXProxy.h
r44 r45 19 19 #define ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER ERROR_PROXY - 5 20 20 #define ERROR_PROXY_FAILED_TO_SET_RESPONSE ERROR_PROXY - 6 21 #define ERROR_PROXY_FAILED_TO_ACCEPT_CLIENT ERROR_PROXY - 7 21 22 22 23 … … 35 36 36 37 public: 37 CRXProxy ( void);38 CRXProxy (const int aSocket); 38 39 ~CRXProxy (void); 39 40 40 41 public: 41 42 /* common utilities */ 42 void SetClientSocket (const int aSocket);43 43 void SetServerTimeout (const int aTimeout = TCPSOCKET_NO_TIMEOUT); 44 44 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);53 45 54 46 public: … … 64 56 public: 65 57 /* 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); 79 61 }; 80 62 -
trunk/src/CRXHttpResponse.cpp
r44 r45 162 162 int 163 163 CRXHttpResponse::SetContent (const char * aContent, 164 164 const int aLength) 165 165 { 166 166 int aResult = 0; -
trunk/src/CRXProxy.cpp
r44 r45 9 9 CRXFilter CRXProxy::mFilter; 10 10 11 CRXProxy::CRXProxy (void) 12 : mClient (0), 13 mServer (0), 14 mServerTimeout (0) 15 { 16 /*----------------------------------------------------------------*/ 11 CRXProxy::CRXProxy (const int aSocket) 12 : mServerTimeout (0) 13 { 14 /*----------------------------------------------------------------*/ 15 if (aSocket < 0) 16 return ; 17 18 mClient = aSocket; 17 19 /*----------------------------------------------------------------*/ 18 20 } … … 22 24 /*----------------------------------------------------------------*/ 23 25 Close (); 24 /*----------------------------------------------------------------*/25 }26 27 void28 CRXProxy::SetClientSocket (const int aSocket)29 {30 /*----------------------------------------------------------------*/31 if (aSocket <= 0)32 return ;33 34 mClient = aSocket;35 26 /*----------------------------------------------------------------*/ 36 27 } … … 51 42 mClient.Close (); 52 43 /*----------------------------------------------------------------*/ 53 }54 55 void56 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 void66 CRXProxy::RemoveRequestFilter (const E_CRX_FILTER_REQUEST aType)67 {68 /*----------------------------------------------------------------*/69 mFilter.RemoveRequestFilter (aType);70 /*----------------------------------------------------------------*/71 }72 73 bool74 CRXProxy::CheckRequestFilter (const E_CRX_FILTER_REQUEST aType)75 {76 /*----------------------------------------------------------------*/77 /*----------------------------------------------------------------*/78 79 return mFilter.CheckRequestFilter (aType, this->mHttpRequest);80 44 } 81 45 … … 120 84 } 121 85 122 void123 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) const133 {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 int153 CRXProxy::GetHttpRequestLength (void) const154 {155 /*----------------------------------------------------------------*/156 /*----------------------------------------------------------------*/157 158 return mHttpRequest.GetHeader().length ();159 }160 161 void162 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) const172 {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 int192 CRXProxy::GetHttpResponseHeaderLength (void) const193 {194 /*----------------------------------------------------------------*/195 /*----------------------------------------------------------------*/196 197 return mHttpResponse.GetHeader ().length ();198 }199 200 char *201 CRXProxy::GetHttpResponseBody (char * aBuffer,202 const int aBufferSize) const203 {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 int223 CRXProxy::GetHttpResponseBodyLength (void) const224 {225 /*----------------------------------------------------------------*/226 /*----------------------------------------------------------------*/227 228 return mHttpResponse.GetContentLength ();229 }230 231 86 int 232 87 CRXProxy::ReceiveRequest (void) … … 315 170 316 171 aResult = mHttpResponse.SetResponseAll (aBuffer, aResult); 317 if (aResult )172 if (aResult < 0) 318 173 { 319 174 aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE; … … 354 209 return aResult < 0 ? aResult : 0; 355 210 } 211 212 CRXFilter & 213 CRXProxy::GetFilter (void) const 214 { 215 /*----------------------------------------------------------------*/ 216 /*----------------------------------------------------------------*/ 217 218 return mFilter; 219 } 220 221 CRXHttpRequest & 222 CRXProxy::GetHttpRequest (void) 223 { 224 /*----------------------------------------------------------------*/ 225 /*----------------------------------------------------------------*/ 226 227 return mHttpRequest; 228 } 229 230 CRXHttpResponse & 231 CRXProxy::GetHttpResponse (void) 232 { 233 /*----------------------------------------------------------------*/ 234 /*----------------------------------------------------------------*/ 235 236 return mHttpResponse; 237 } -
trunk/src/main.cpp
r44 r45 68 68 } 69 69 70 THREAD_FUNCTION_RETURN71 THREAD_FUNCTION_CALLING_CONVENTION72 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 142 70 int ThreadPool (CRXSocket * aProxySocket, 143 71 const int aThreadPoolCount) … … 171 99 return aResult; 172 100 } 101 102 THREAD_FUNCTION_RETURN 103 THREAD_FUNCTION_CALLING_CONVENTION 104 CRXProxyMTWrapper (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.