Changeset 44 in cheroxy
- Timestamp:
- 11/22/12 15:21:02 (11 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/CRXHttpMessage.h
r31 r44 6 6 7 7 #include <string> 8 9 #define CRLF "\r\n" 10 #define CRLF2 "\r\n\r\n" 8 11 9 12 class CRXHttpMessage … … 26 29 27 30 void ResetMessage (void); 28 29 virtual CRXHttpMessage & operator = (const char * aHttpMessage) = 0;30 31 }; 31 32 -
trunk/include/CRXHttpRequest.h
r28 r44 31 31 std::string GetHeader (void) const; 32 32 33 CRXHttpRequest & operator = (const char * aHttpRequest);34 35 33 public: 36 34 std::string GetURL (void) const; -
trunk/include/CRXHttpResponse.h
r26 r44 32 32 private: 33 33 void Parse (void); 34 int ParseContent (const char * aContent,35 const int aLength);36 34 void Reset (void); 37 35 void ResetContent (void); … … 40 38 int GetStatusCode (void) const; 41 39 42 int SetResponse (const char * aHttpResponse, 40 bool IsChunked (void) const; 41 int SetResponseAll (const char * aHttpResponse, 43 42 const int aResponseLength); 43 void SetHeader (const char * aHttpResponse); 44 44 std::string GetHeader (void) const; 45 int SetContent (const char * aContent, 46 const int aLength); 45 47 const char * GetContentBody (void) const; 46 48 int GetContentLength (void) const; 47 48 CRXHttpResponse & operator = (const char * aHttpResponse);49 49 }; 50 50 -
trunk/include/CRXProxy.h
r36 r44 38 38 ~CRXProxy (void); 39 39 40 static CRXProxy * GetNewInstance (void);41 42 40 public: 43 41 /* common utilities */ 44 void ReleaseInstance (void);45 46 42 void SetClientSocket (const int aSocket); 47 43 void SetServerTimeout (const int aTimeout = TCPSOCKET_NO_TIMEOUT); -
trunk/src/CRXHttpRequest.cpp
r28 r44 48 48 49 49 return GetMessage (); 50 }51 52 CRXHttpRequest &53 CRXHttpRequest::operator = (const char * aHttpMessage)54 {55 /*----------------------------------------------------------------*/56 SetHeader (aHttpMessage);57 /*----------------------------------------------------------------*/58 59 return *this;60 50 } 61 51 -
trunk/src/CRXHttpResponse.cpp
r28 r44 12 12 #include <string.h> 13 13 14 #define CRLF "\r\n"15 #define CRLF2 "\r\n\r\n"16 17 14 #define CONTENT_LENGTH "Content-Length: " 18 15 #define TRANSFER_ENCODING "Transfer-Encoding: " … … 56 53 57 54 int 58 CRXHttpResponse::SetResponse (const char * aHttpResponse,59 const intaResponseLength)55 CRXHttpResponse::SetResponseAll (const char * aHttpResponse, 56 const int aResponseLength) 60 57 { 61 58 int aResult = 0; … … 70 67 return ERROR_HTTP_RESPONSE_INVALID_LENGTH; 71 68 72 if (GetHeader (). length () == 0)69 if (GetHeader ().rfind (CRLF2) == std::string::npos) 73 70 { 74 71 aEndOfHeader = strstr (aHttpResponse, CRLF2); … … 80 77 aHeader.assign (aHttpResponse, aHeaderLength); 81 78 82 Set Message(aHeader.c_str ());83 } 84 85 aResult = ParseContent (aHttpResponse + aHeaderLength, aResponseLength - aHeaderLength);79 SetHeader (aHeader.c_str ()); 80 } 81 82 aResult = SetContent (aHttpResponse + aHeaderLength, aResponseLength - aHeaderLength); 86 83 if (aResult) 87 84 { 88 returnERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT;85 aResult = ERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT; 89 86 } 90 87 /*----------------------------------------------------------------*/ … … 93 90 } 94 91 95 CRXHttpResponse & 96 CRXHttpResponse:: operator =(const char * aHttpMessage)92 void 93 CRXHttpResponse::SetHeader (const char * aHttpMessage) 97 94 { 98 95 /*----------------------------------------------------------------*/ 99 96 SetMessage (aHttpMessage); 100 97 /*----------------------------------------------------------------*/ 101 102 return *this;103 98 } 104 99 … … 166 161 167 162 int 168 CRXHttpResponse:: ParseContent (const char * aContent,163 CRXHttpResponse::SetContent (const char * aContent, 169 164 const int aLength) 170 165 { … … 207 202 208 203 int CRXHttpResponse::GetStatusCode (void) const { return mStatusCode; } 204 bool CRXHttpResponse::IsChunked (void) const { return mIsChunked; } 209 205 int CRXHttpResponse::GetContentLength (void) const { return mContentLength; } 210 206 const char * CRXHttpResponse::GetContentBody (void) const { return mContent.mBody; } -
trunk/src/CRXProxy.cpp
r40 r44 25 25 } 26 26 27 CRXProxy *28 CRXProxy::GetNewInstance (void)29 {30 /*----------------------------------------------------------------*/31 /*----------------------------------------------------------------*/32 33 return new(std::nothrow) CRXProxy ();34 }35 36 void37 CRXProxy::ReleaseInstance (void)38 {39 /*----------------------------------------------------------------*/40 delete this;41 /*----------------------------------------------------------------*/42 }43 44 27 void 45 28 CRXProxy::SetClientSocket (const int aSocket) … … 141 124 { 142 125 /*----------------------------------------------------------------*/ 143 mHttpRequest = aHttpRequest;126 mHttpRequest.SetHeader (aHttpRequest); 144 127 /*----------------------------------------------------------------*/ 145 128 } … … 158 141 if (aPtr == NULL) 159 142 return NULL; 160 143 161 144 aCopyLength = aRemainLength < aHeaderLength ? aBufferSize : aHeaderLength; 162 145 if (aCopyLength > 0) … … 180 163 { 181 164 /*----------------------------------------------------------------*/ 182 mHttpResponse = aHttpResponse;165 mHttpResponse.SetHeader (aHttpResponse); 183 166 /*----------------------------------------------------------------*/ 184 167 } … … 249 232 CRXProxy::ReceiveRequest (void) 250 233 { 251 int aResult = 0; 252 253 const unsigned int aBufferSize = 1024 * 56; 254 char aBuffer[aBufferSize + 1] = {0x00, }; 255 256 /*----------------------------------------------------------------*/ 257 aResult = mClient.Receive (aBuffer, aBufferSize); 258 if (aResult < 0) 259 { 260 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST; 261 CRX_ADD_SUBCLASS_ERROR (mClient); 262 CRX_ERROR_SET (aResult, "Failed to receive from client."); 263 return aResult; 264 } 265 266 /* parse http request */ 267 mHttpRequest = aBuffer; 234 int aResult = 0; 235 char aBuffer; 236 237 std::string aHttpHeader; 238 /*----------------------------------------------------------------*/ 239 for (;;) 240 { 241 aResult = mClient.Receive (&aBuffer, 1); 242 if (aResult < 0) 243 { 244 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST; 245 CRX_ADD_SUBCLASS_ERROR (mClient); 246 CRX_ERROR_SET (aResult, "Failed to receive from client."); 247 return aResult; 248 } 249 aHttpHeader += aBuffer; 250 if (aHttpHeader.rfind (CRLF2) != std::string::npos) 251 break; 252 } 253 254 mHttpRequest.SetHeader (aHttpHeader.c_str ()); 268 255 /*----------------------------------------------------------------*/ 269 256 … … 292 279 return aResult; 293 280 } 294 295 if (getsockname (mServer, (struct sockaddr *) &aSockName, &aSockLen) == 0)296 CRX_DEBUG_TRACE ("Connected on port number (%d)\n", ntohs (aSockName.sin_port));297 281 } 298 282 … … 315 299 const unsigned int aBufferSize = 1024 * 64; 316 300 char aBuffer[aBufferSize + 1] = {0x00, }; 317 318 std::string aHttpResponse;319 301 320 302 /*----------------------------------------------------------------*/ … … 331 313 return aResult; 332 314 } 333 else if (aResult == 0) 334 { 335 break; 336 } 337 338 aResult = mHttpResponse.SetResponse (aBuffer, aResult); 315 316 aResult = mHttpResponse.SetResponseAll (aBuffer, aResult); 339 317 if (aResult) 340 318 { … … 343 321 return aResult; 344 322 } 345 346 if (!CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION) &&347 mHttpResponse.GetContentLength () > 0)348 {349 CRX_DEBUG_TRACE_BIN (mHttpResponse.GetContentBody (),350 mHttpResponse.GetContentLength (),351 "");352 }353 323 } 354 324 /*----------------------------------------------------------------*/ -
trunk/src/main.cpp
r36 r44 91 91 } 92 92 93 if ((aProxy = CRXProxy::GetNewInstance()) == NULL)93 if ((aProxy = new(std::nothrow) CRXProxy ()) == NULL) 94 94 { 95 95 cerr << "Failed to get proxy." << endl; … … 114 114 aHttpMessageLength = aProxy->GetHttpRequestLength (); 115 115 aHttpMessage = (char *) calloc (aHttpMessageLength + 1, 1); 116 cout << aProxy->GetHttpRequest (aHttpMessage, aHttpMessageLength) << endl; 116 aProxy->GetHttpRequest (aHttpMessage, aHttpMessageLength); 117 CRX_DEBUG_TRACE ("== Request: \n%s\n", aHttpMessage); 117 118 free (aHttpMessage); 118 119 119 120 aHttpMessageLength = aProxy->GetHttpResponseHeaderLength (); 120 121 aHttpMessage = (char *) calloc (aHttpMessageLength + 1, 1); 121 cout << aProxy->GetHttpResponseHeader (aHttpMessage, aHttpMessageLength) << endl; 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"); 122 130 free (aHttpMessage); 123 131 } 124 132 125 aProxy->ReleaseInstance ();133 delete aProxy; 126 134 } 127 135
Note:
See TracChangeset
for help on using the changeset viewer.