Changeset 24 in cheroxy
- Timestamp:
- 11/14/12 14:00:40 (12 years ago)
- Location:
- trunk
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/CRXException.h
r19 r24 30 30 31 31 public: 32 CRXException (void); 32 CRXException (void); 33 virtual ~CRXException (void); 33 34 34 35 public: -
trunk/include/CRXHttpMessage.h
r18 r24 17 17 public: 18 18 CRXHttpMessage (void); 19 virtual ~CRXHttpMessage (void); 19 20 20 21 protected: … … 25 26 std::string GetMessage (void) const; 26 27 27 virtual CRXHttpMessage & operator = (const char * aHttpMessage); 28 void Reset (void); 29 30 virtual CRXHttpMessage & operator = (const char * aHttpMessage); 28 31 }; 29 32 -
trunk/include/CRXHttpRequest.h
r17 r24 24 24 25 25 public: 26 void Set Request(const char * aHttpRequest);27 std::string Get Request(void) const;26 void SetHeader (const char * aHttpRequest); 27 std::string GetHeader (void) const; 28 28 29 29 CRXHttpRequest & operator = (const char * aHttpRequest); -
trunk/include/CRXHttpResponse.h
r5 r24 7 7 #include "CRXHttpMessage.h" 8 8 9 #define ERROR_HTTP_RESPONSE -4000 10 #define ERROR_HTTP_RESPONSE_INVALID_LENGTH ERROR_HTTP_RESPONSE - 1 11 #define ERROR_HTTP_RESPONSE_INVALID_FORMAT ERROR_HTTP_RESPONSE - 2 12 #define ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION ERROR_HTTP_RESPONSE - 3 13 #define ERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT ERROR_HTTP_RESPONSE - 4 14 9 15 class CRXHttpResponse : public CRXHttpMessage 10 16 { … … 12 18 int mStatusCode; 13 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; 14 27 15 28 public: 16 CRXHttpResponse (void); 29 CRXHttpResponse (void); 30 ~CRXHttpResponse (void); 17 31 18 32 private: 19 void Parse (void); 33 void Parse (void); 34 int ParseContent (const char * aContent, 35 const int aLength); 36 void ResetContent (void); 20 37 21 38 public: 22 void SetResponse (const char * aHttpResponse); 23 std::string GetResponse (void) const; 39 int GetStatusCode (void) const; 24 40 25 CRXHttpResponse & operator = (const char * aHttpResponse); 41 int SetResponse (const char * aHttpResponse, 42 const int aResponseLength); 43 std::string GetHeader (void) const; 44 const char * GetContentBody (void) const; 45 int GetContentLength (void) const; 26 46 27 public: 28 int GetStatusCode (void) const; 47 CRXHttpResponse & operator = (const char * aHttpResponse); 29 48 }; 30 49 -
trunk/include/CRXProxy.h
r20 r24 6 6 7 7 #include "CRXException.h" 8 #include "CRXSocket.h" 9 #include "CRXHttpMessage.h" 10 #include "CRXHttpRequest.h" 11 #include "CRXHttpResponse.h" 8 12 9 class CRXSocket; 13 #define ERROR_PROXY -10000 14 #define ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST ERROR_PROXY - 1 15 #define ERROR_PROXY_FAILED_TO_SEND_REQUEST ERROR_PROXY - 2 16 #define ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE ERROR_PROXY - 3 17 #define ERROR_PROXY_FAILED_TO_SEND_RESPONSE ERROR_PROXY - 4 18 #define ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER ERROR_PROXY - 5 19 #define ERROR_PROXY_FAILED_TO_SET_RESPONSE ERROR_PROXY - 5 20 21 typedef enum { 22 CRX_PROXY_STATUS_ERROR = -1, 23 CRX_PROXY_STATUS_IDLE, 24 CRX_PROXY_STATUS_BUSY 25 } E_CRX_PROXY_STATUS; 10 26 11 27 class CRXProxy : public CRXException … … 13 29 private: 14 30 CRXSocket mClient; 31 CRXSocket mServer; 32 33 CRXHttpRequest mHttpRequest; 34 CRXHttpResponse mHttpResponse; 35 36 E_CRX_PROXY_STATUS mProxyStatus; 15 37 16 38 public: 17 CRXProxy (const int aSocket = 0); 39 CRXProxy (void); 40 ~CRXProxy (void); 41 42 static CRXProxy * GetNewInstance (void); 18 43 19 44 public: 20 static CRXProxy * GetNewInstance (const int aSocket = 0); 45 void ReleaseInstance (void); 46 47 void SetClientSocket (const int aSocket); 48 void Close (void); 49 50 int GetStatus (void); 21 51 22 52 public: 23 int Forward (void); 24 int ForwardMT (void); 53 int Forward (void); 54 55 void SetHttpRequest (const char * aHttpRequest); 56 char * GetHttpRequest (char * aBuffer, 57 const int aBufferSize) const; 58 int GetHttpRequestLength (void) const; 59 60 void SetHttpResponse (const char * aHttpResponse); 61 char * GetHttpResponse (char * aBuffer, 62 const int aBufferSize) const; 63 int GetHttpResponseLength (void) const; 64 65 int ReceiveRequest (void); 66 int SendRequest (void); 67 68 int ReceiveResponse (void); 69 int SendResponse (void); 25 70 }; 26 71 -
trunk/include/CRXSocket.h
r18 r24 55 55 * @return available : true, else : false. 56 56 */ 57 inline boolIsCreated (void) const;57 bool IsCreated (void) const; 58 58 59 inline boolIsReady (void) const;59 bool IsReady (void) const; 60 60 61 61 /** … … 87 87 CRXSocket & operator = (int aSocket); 88 88 89 int SetTimeout (const int aTimeout); 90 89 91 /** 90 92 * for server … … 107 109 * for communication 108 110 */ 109 int Send (c har * aBuffer,110 int aSize);111 int Send (const char * aBuffer, 112 int aSize); 111 113 112 114 int Receive (char * aBuffer, -
trunk/src/CRXException.cpp
r19 r24 15 15 CRXException::CRXException (void) 16 16 : mCode (0) 17 { 18 /*----------------------------------------------------------------*/ 19 /*----------------------------------------------------------------*/ 20 } 21 22 CRXException::~CRXException (void) 17 23 { 18 24 /*----------------------------------------------------------------*/ -
trunk/src/CRXHttpMessage.cpp
r18 r24 9 9 10 10 CRXHttpMessage::CRXHttpMessage (void) 11 { 12 } 13 14 CRXHttpMessage::~CRXHttpMessage (void) 11 15 { 12 16 } … … 30 34 31 35 void 36 CRXHttpMessage::Reset (void) 37 { 38 mHttpMessage.clear (); 39 } 40 41 void 32 42 CRXHttpMessage::Parse (void) 33 43 { -
trunk/src/CRXHttpRequest.cpp
r18 r24 17 17 18 18 void 19 CRXHttpRequest::Set Request(const char * aHttpRequest)19 CRXHttpRequest::SetHeader (const char * aHttpRequest) 20 20 { 21 21 SetMessage (aHttpRequest); … … 23 23 24 24 std::string 25 CRXHttpRequest::Get Request(void) const25 CRXHttpRequest::GetHeader (void) const 26 26 { 27 27 return GetMessage (); … … 49 49 CRXHttpRequest::operator = (const char * aHttpMessage) 50 50 { 51 Set Request(aHttpMessage);51 SetHeader (aHttpMessage); 52 52 return *this; 53 53 } … … 69 69 70 70 /*----------------------------------------------------------------*/ 71 aHttpRequestUri = Get Request().substr (0, mHttpMessage.find ('\r'));71 aHttpRequestUri = GetHeader ().substr (0, mHttpMessage.find ('\r')); 72 72 73 73 /*---------------------------------------------------------------- -
trunk/src/CRXHttpResponse.cpp
r18 r24 11 11 #include <stdlib.h> 12 12 13 #define CRLF "\r\n" 14 #define CRLF2 "\r\n\r\n" 15 16 #define CONTENT_LENGTH "Content-Length: " 17 #define TRANSFER_ENCODING "Transfer-Encoding: " 18 13 19 CRXHttpResponse::CRXHttpResponse (void) 14 : mStatusCode (0) 15 { 20 : mStatusCode (0), 21 mIsChunked (false), 22 mContentLength (0) 23 { 24 memset (&mContent, 0x00, sizeof (mContent)); 25 } 26 27 CRXHttpResponse::~CRXHttpResponse (void) 28 { 29 ResetContent (); 16 30 } 17 31 18 32 void 19 CRXHttpResponse::SetResponse (const char * aHttpResponse) 20 { 21 SetMessage (aHttpResponse); 22 } 23 24 std::string 25 CRXHttpResponse::GetResponse (void) const 26 { 27 return GetMessage (); 33 CRXHttpResponse::ResetContent (void) 34 { 35 if (mContent.mBody) 36 free (mContent.mBody); 37 38 memset (&mContent, 0x00, sizeof (mContent)); 39 } 40 41 int 42 CRXHttpResponse::SetResponse (const char * aHttpResponse, 43 const int aResponseLength) 44 { 45 int aResult = 0; 46 47 const char * aEndOfHeader = NULL; 48 std::string aHeader; 49 50 int aHeaderLength = 0; 51 int aBodyLength = 0; 52 53 /*----------------------------------------------------------------*/ 54 if (aResponseLength <= 0) 55 return ERROR_HTTP_RESPONSE_INVALID_LENGTH; 56 57 if (GetHeader ().length () == 0) 58 { 59 aEndOfHeader = strstr (aHttpResponse, CRLF2); 60 if (aEndOfHeader == NULL) 61 return ERROR_HTTP_RESPONSE_INVALID_FORMAT; 62 63 aEndOfHeader += strlen (CRLF2); 64 aHeaderLength = aEndOfHeader - aHttpResponse; 65 aHeader.assign (aHttpResponse, aHeaderLength); 66 67 SetMessage (aHeader.c_str ()); 68 } 69 70 aResult = ParseContent (aHttpResponse + aHeaderLength, aResponseLength - aHeaderLength); 71 if (aResult) 72 { 73 return ERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT; 74 } 75 /*----------------------------------------------------------------*/ 76 77 return aResult; 28 78 } 29 79 … … 35 85 } 36 86 87 std::string 88 CRXHttpResponse::GetHeader (void) const 89 { 90 return GetMessage (); 91 } 92 93 int 94 CRXHttpResponse::GetStatusCode (void) const 95 { 96 return mStatusCode; 97 } 98 99 int 100 CRXHttpResponse::GetContentLength (void) const 101 { 102 return mContentLength; 103 } 104 105 const char * 106 CRXHttpResponse::GetContentBody (void) const 107 { 108 return mContent.mBody; 109 } 110 37 111 void 38 112 CRXHttpResponse::Parse (void) 39 113 { 40 std::string a HttpStatus = "";114 std::string aValue; 41 115 42 116 char aStatusCode[64] = {0x00, }; 43 char aStatus String[64]= {0x00, };117 char aStatusName[64] = {0x00, }; 44 118 char aHttpVersion[64] = {0x00, }; 45 119 46 /*----------------------------------------------------------------*/ 47 aHttpStatus = GetResponse ().substr (0, mHttpMessage.find ('\r')); 120 size_t aBegin = 0; 121 size_t aOffset = 0; 122 char aBuffer[12] = {0x00, }; 123 124 /*----------------------------------------------------------------*/ 125 aValue = GetHeader ().substr (0, GetHeader ().find (CRLF, 0)); 48 126 49 127 /*---------------------------------------------------------------- 50 128 * 1. separate first line to <METHOD>, <URL> and <VERSION> 51 129 *----------------------------------------------------------------*/ 52 sscanf (a HttpStatus.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aHttpVersion, aStatusCode, aStatusString);130 sscanf (aValue.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aHttpVersion, aStatusCode, aStatusName); 53 131 mHttpVersion.assign (aHttpVersion); 54 mStatusString.assign (aStatus String);132 mStatusString.assign (aStatusName); 55 133 mStatusCode = atoi (aStatusCode); 56 /*----------------------------------------------------------------*/ 57 } 58 59 int 60 CRXHttpResponse::GetStatusCode (void) const 61 { 62 return mStatusCode; 63 } 134 135 /*---------------------------------------------------------------- 136 * 2. get Content-Length or check is chunked 137 *----------------------------------------------------------------*/ 138 aBegin = GetHeader ().find (CONTENT_LENGTH); 139 if (aBegin == std::string::npos) 140 { 141 /* is really chunked encoding ? */ 142 aBegin = GetHeader ().find (TRANSFER_ENCODING); 143 if (aBegin == std::string::npos) 144 return ; 145 146 aBegin += strlen (TRANSFER_ENCODING); 147 aOffset = GetHeader ().find (CRLF, aBegin) - aBegin; 148 149 aValue = GetHeader ().substr (aBegin, aOffset); 150 if (aValue == "chunked") 151 { 152 mIsChunked = true; 153 return ; 154 } 155 } 156 157 aBegin += strlen (CONTENT_LENGTH); 158 aOffset = GetHeader ().find (CRLF, aBegin) - aBegin; 159 160 aValue = GetHeader ().substr (aBegin, aOffset); 161 mContentLength = atoi (aValue.c_str ()); 162 /*----------------------------------------------------------------*/ 163 } 164 165 int 166 CRXHttpResponse::ParseContent (const char * aContent, 167 const int aLength) 168 { 169 int aResult = 0; 170 171 char * aReallocPtr = NULL; 172 173 /*----------------------------------------------------------------*/ 174 175 if (!mIsChunked) 176 { 177 if (mContentLength <= mContent.mLength) 178 return aResult; 179 180 if (mContent.mBody == NULL) 181 { 182 mContent.mBody = static_cast<char *> (calloc (mContentLength, 1)); 183 if (mContent.mBody == NULL) 184 { 185 return ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION; 186 } 187 } 188 } 189 else 190 { 191 mContentLength += aLength; 192 aReallocPtr = static_cast<char *> (realloc (mContent.mBody, mContentLength)); 193 if (aReallocPtr == NULL) 194 { 195 return ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION; 196 } 197 mContent.mBody = aReallocPtr; 198 } 199 200 memcpy (mContent.mBody + mContent.mLength, aContent, aLength); 201 mContent.mLength += aLength; 202 /*----------------------------------------------------------------*/ 203 204 return aResult; 205 } -
trunk/src/CRXProxy.cpp
r23 r24 3 3 */ 4 4 5 #include "CRXSocket.h"6 #include "CRXHttpRequest.h"7 #include "CRXHttpResponse.h"8 9 5 #include "CRXProxy.h" 10 6 11 7 #include <string.h> 12 8 13 #ifdef _DEBUG 14 # include <iostream> 15 using namespace std; 16 # define PRINT_ERROR cerr 17 #else 18 # define PRINT_ERROR 19 #endif 20 21 #ifdef _WIN32 22 # include <process.h> 23 # define THREAD_TYPE void * 24 # define THREAD_FUNCTION_RETURN unsigned int 25 # define THREAD_FUNCTION_CALLING_CONVENTION WINAPI 26 #else 27 # define THREAD_TYPE pthread_t 28 # define THREAD_FUNCTION_RETURN void * 29 # define THREAD_FUNCTION_CALLING_CONVENTION 30 #endif 31 32 THREAD_FUNCTION_RETURN 33 THREAD_FUNCTION_CALLING_CONVENTION 34 CRXProxyWrapper (void * aThreadArg) 35 { 36 int aResult = 0; 37 CRXProxy * aProxy = (CRXProxy *) aThreadArg; 9 CRXProxy::CRXProxy (void) 10 : mClient (0), 11 mServer (0) 12 { 13 /*----------------------------------------------------------------*/ 14 /*----------------------------------------------------------------*/ 15 } 16 17 CRXProxy::~CRXProxy (void) 18 { 19 /*----------------------------------------------------------------*/ 20 Close (); 21 /*----------------------------------------------------------------*/ 22 } 23 24 CRXProxy * 25 CRXProxy::GetNewInstance(void) 26 { 27 /*----------------------------------------------------------------*/ 28 /*----------------------------------------------------------------*/ 29 30 return new(std::nothrow) CRXProxy (); 31 } 32 33 void 34 CRXProxy::ReleaseInstance (void) 35 { 36 /*----------------------------------------------------------------*/ 37 delete this; 38 /*----------------------------------------------------------------*/ 39 } 40 41 void 42 CRXProxy::SetClientSocket (const int aSocket) 43 { 44 /*----------------------------------------------------------------*/ 45 if (aSocket <= 0) 46 return ; 47 48 mClient = aSocket; 49 /*----------------------------------------------------------------*/ 50 } 51 52 void 53 CRXProxy::Close (void) 54 { 55 /*----------------------------------------------------------------*/ 56 mServer.Close (); 57 mClient.Close (); 58 /*----------------------------------------------------------------*/ 59 } 60 61 int 62 CRXProxy::GetStatus (void) 63 { 64 /*----------------------------------------------------------------*/ 65 /*----------------------------------------------------------------*/ 66 67 return mProxyStatus; 68 } 69 70 int 71 CRXProxy::Forward (void) 72 { 73 int aResult = 0; 74 75 /*----------------------------------------------------------------*/ 76 aResult = ReceiveRequest (); 77 if (aResult < 0) 78 { 79 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST; 80 CRX_ERROR (aResult, "Failed to receive from client"); 81 } 82 83 aResult = SendRequest (); 84 if (aResult < 0) 85 { 86 aResult = ERROR_PROXY_FAILED_TO_SEND_REQUEST; 87 CRX_ERROR (aResult, "Failed to send to server"); 88 return aResult; 89 } 90 91 aResult = ReceiveResponse (); 92 if (aResult < 0) 93 { 94 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_RESPONSE; 95 CRX_ERROR (aResult, "Failed to receive from server"); 96 } 97 98 aResult = SendResponse (); 99 if (aResult < 0) 100 { 101 aResult = ERROR_PROXY_FAILED_TO_SEND_RESPONSE; 102 CRX_ERROR (aResult, "Failed to send to client"); 103 return aResult; 104 } 105 /*----------------------------------------------------------------*/ 106 107 return aResult; 108 } 109 110 void 111 CRXProxy::SetHttpRequest (const char * aHttpRequest) 112 { 113 /*----------------------------------------------------------------*/ 114 mHttpRequest = aHttpRequest; 115 /*----------------------------------------------------------------*/ 116 } 117 118 char * 119 CRXProxy::GetHttpRequest (char * aBuffer, 120 const int aBufferSize) const 121 { 122 void * aPtr = static_cast<void *> (aBuffer); 123 124 /*----------------------------------------------------------------*/ 125 if (aPtr == NULL) 126 return NULL; 38 127 39 aResult = aProxy->Forward (); 40 if (aResult) 41 PRINT_ERROR << aProxy->GetErrorMessage (); 42 43 delete aProxy; 44 return (THREAD_FUNCTION_RETURN)0; 45 } 46 47 CRXProxy::CRXProxy (const int aSocket) 48 { 49 /*----------------------------------------------------------------*/ 50 mClient = aSocket; 51 /*----------------------------------------------------------------*/ 52 } 53 54 CRXProxy * 55 CRXProxy::GetNewInstance(const int aSocket) 56 { 57 return new(std::nothrow) CRXProxy (aSocket); 58 } 59 60 int 61 CRXProxy::ForwardMT (void) 62 { 63 THREAD_TYPE aThreadID = (THREAD_TYPE)0; 64 /*----------------------------------------------------------------*/ 65 #ifdef _WIN32 66 aThreadID = reinterpret_cast<void *> (_beginthreadex (0, 0, CRXProxyWrapper, this, 0, 0)); 67 if (aThreadID == NULL) 68 { 69 return -1; 70 } 71 72 CloseHandle (aThreadID); 73 74 return 0; 75 #else 76 return pthread_create (&aThreadID, NULL, CRXProxyWrapper, this); 77 #endif 78 /*----------------------------------------------------------------*/ 79 } 80 81 int 82 CRXProxy::Forward (void) 83 { 84 int aResult = 0; 85 int aReceivedSize = 0; 86 87 const unsigned int aBufferSize = 1024 * 16; 88 char aBuffer[aBufferSize] = {0x00, }; 89 90 CRXHttpRequest aHttpRequest; 91 92 CRXSocket aWebServer; 93 94 bool aIsMoreRequest = false; 95 96 /*----------------------------------------------------------------*/ 97 do 98 { 99 /* BROWSER --- request ---> [PROXY] SERVER */ 100 aResult = mClient.Receive (aBuffer, aBufferSize); 128 if (aBufferSize > 0) 129 memcpy (aPtr, mHttpRequest.GetHeader ().c_str (), aBufferSize); 130 /*----------------------------------------------------------------*/ 131 132 return aBuffer; 133 } 134 135 int 136 CRXProxy::GetHttpRequestLength (void) const 137 { 138 /*----------------------------------------------------------------*/ 139 /*----------------------------------------------------------------*/ 140 141 return mHttpRequest.GetHeader().length (); 142 } 143 144 void 145 CRXProxy::SetHttpResponse (const char * aHttpResponse) 146 { 147 /*----------------------------------------------------------------*/ 148 mHttpResponse = aHttpResponse; 149 /*----------------------------------------------------------------*/ 150 } 151 152 char * 153 CRXProxy::GetHttpResponse (char * aBuffer, 154 const int aBufferSize) const 155 { 156 void * aPtr = static_cast<void *> (aBuffer); 157 158 int aRemainLength = aBufferSize; 159 160 /*----------------------------------------------------------------*/ 161 if (aPtr == NULL) 162 return NULL; 163 164 if (aRemainLength > 0) 165 memcpy (aPtr, mHttpResponse.GetHeader ().c_str (), aRemainLength); 166 167 aRemainLength -= mHttpResponse.GetHeader ().length (); 168 169 if (aRemainLength > 0) 170 memcpy (aPtr, mHttpResponse.GetContentBody (), aRemainLength); 171 /*----------------------------------------------------------------*/ 172 173 return aBuffer; 174 } 175 176 int 177 CRXProxy::GetHttpResponseLength (void) const 178 { 179 /*----------------------------------------------------------------*/ 180 /*----------------------------------------------------------------*/ 181 182 return mHttpResponse.GetHeader ().length () 183 + mHttpResponse.GetContentLength (); 184 } 185 186 int 187 CRXProxy::ReceiveRequest (void) 188 { 189 int aResult = 0; 190 191 const unsigned int aBufferSize = 1024 * 56; 192 char aBuffer[aBufferSize + 1] = {0x00, }; 193 194 /*----------------------------------------------------------------*/ 195 aResult = mClient.Receive (aBuffer, aBufferSize); 196 if (aResult < 0) 197 { 198 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST; 199 CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ()); 200 CRX_ERROR (aResult, "Failed to receive from client"); 201 return aResult; 202 } 203 204 /* parse http request */ 205 mHttpRequest = aBuffer; 206 /*----------------------------------------------------------------*/ 207 208 return aResult < 0 ? aResult : 0; 209 } 210 211 int 212 CRXProxy::SendRequest (void) 213 { 214 int aResult = 0; 215 int aSize = mHttpRequest.GetHeader ().length (); 216 217 /*----------------------------------------------------------------*/ 218 if (!mServer) 219 { 220 /* connect */ 221 aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), 1); 101 222 if (aResult < 0) 102 223 { 103 CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ()); 104 CRX_ERROR (aResult, "Failed to receive from browser"); 224 aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER; 225 CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ()); 226 CRX_ERROR (aResult, "Failed to connect to server <%s>", mHttpRequest.GetURL ().c_str ()); 227 return aResult; 228 } 229 } 230 231 aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize); 232 if (aResult != aSize) 233 { 234 CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ()); 235 CRX_ERROR (aResult, "Failed to send to server <%s>", mHttpRequest.GetURL ().c_str ()); 236 } 237 /*----------------------------------------------------------------*/ 238 239 return aResult < 0 ? aResult : 0; 240 } 241 242 int 243 CRXProxy::ReceiveResponse (void) 244 { 245 int aResult = 0; 246 247 const unsigned int aBufferSize = 1024 * 64; 248 char aBuffer[aBufferSize + 1] = {0x00, }; 249 250 std::string aHttpResponse; 251 252 /*----------------------------------------------------------------*/ 253 for (;;) 254 { 255 memset (aBuffer, 0x00, sizeof (aBuffer)); 256 257 aResult = mServer.Receive (aBuffer, aBufferSize); 258 if (aResult < 0) 259 { 260 aResult = ERROR_PROXY_FAILED_TO_RECEIVE_REQUEST; 261 CRX_ERROR_ADD (mServer.GetErrorMessage ().c_str ()); 262 CRX_ERROR (aResult, "Failed to receive from server"); 263 return aResult; 264 } 265 else if (aResult == 0) 266 { 105 267 break; 106 268 } 107 aReceivedSize = aResult; 108 109 /* BROWSER ?--- waiting --- [PROXY] --- connect ---> SERVER */ 110 if (!aWebServer) 269 270 aResult = mHttpResponse.SetResponse (aBuffer, aResult); 271 if (aResult) 111 272 { 112 /* parse http request */ 113 aHttpRequest = aBuffer; 114 115 /* connect */ 116 aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort (), 1); 117 if (aResult < 0) 118 { 119 CRX_ERROR_ADD (aWebServer.GetErrorMessage ().c_str ()); 120 CRX_ERROR (aResult, "Failed to connect to server <%s>", aHttpRequest.GetURL ().c_str ()); 121 return aResult; 122 } 123 } 124 125 /* BROWSER ?--- waiting --- [PROXY] --- request ---> SERVER */ 126 aResult = aWebServer.Send (aBuffer, aReceivedSize); 127 if (aResult != aReceivedSize) 128 { 129 CRX_ERROR_ADD (aWebServer.GetErrorMessage ().c_str ()); 130 CRX_ERROR (aResult, "Failed to send to server <%s>", aHttpRequest.GetURL ().c_str ()); 273 aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE; 274 CRX_ERROR (aResult, "Failed to set response"); 131 275 return aResult; 132 276 } 133 134 /* BROWSER <--- response--- [PROXY] <--- response--- SERVER */ 135 for (;;) 136 { 137 memset (aBuffer, 0x00, aBufferSize); 138 139 aResult = aWebServer.Receive (aBuffer, aBufferSize); 140 if (aResult < 0) 141 { 142 CRX_ERROR_ADD (aWebServer.GetErrorMessage ().c_str ()); 143 CRX_ERROR (aResult, "Failed to receive <%s>", aHttpRequest.GetURL ().c_str ()); 144 break; 145 } 146 else if (aResult == 0) 147 { 148 break; 149 } 150 aReceivedSize = aResult; 151 152 aResult = mClient.Send (aBuffer, aReceivedSize); 153 if (aResult != aReceivedSize) 154 { 155 CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ()); 156 CRX_ERROR (aResult, "Failed to send <%s>", aHttpRequest.GetURL ().c_str ()); 157 break; 158 } 159 } 160 } while (aIsMoreRequest); 161 162 /*----------------------------------------------------------------*/ 163 mClient.Close (); 164 aWebServer.Close (); 165 166 return aResult; 167 } 277 } 278 /*----------------------------------------------------------------*/ 279 280 return aResult < 0 ? aResult : 0; 281 } 282 283 int 284 CRXProxy::SendResponse (void) 285 { 286 int aResult = 0; 287 int aSize = 0; 288 289 /*----------------------------------------------------------------*/ 290 aSize = mHttpResponse.GetHeader ().length ();; 291 aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize); 292 if (aResult != aSize) 293 { 294 CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ()); 295 CRX_ERROR (aResult, "Failed to send to client"); 296 return aResult; 297 } 298 299 aSize = mHttpResponse.GetContentLength (); 300 aResult = mClient.Send (mHttpResponse.GetContentBody (), aSize); 301 if (aResult != aSize) 302 { 303 CRX_ERROR_ADD (mClient.GetErrorMessage ().c_str ()); 304 CRX_ERROR (aResult, "Failed to send to client"); 305 } 306 /*----------------------------------------------------------------*/ 307 308 return aResult < 0 ? aResult : 0; 309 } -
trunk/src/CRXSocket.cpp
r18 r24 18 18 { 19 19 /*----------------------------------------------------------------*/ 20 CRXSocket::Initialize (); 21 20 22 memset ((void *)&mAddress, 0x0, sizeof (struct sockaddr_in)); 21 22 if (!CRXSocket::IsInitialized ())23 CRXSocket::Initialize ();24 23 /*----------------------------------------------------------------*/ 25 24 } … … 81 80 82 81 int 83 CRXSocket::Initialize (void) 84 { 85 int aResult = 0; 86 87 /*----------------------------------------------------------------*/ 88 if (CRXSocket::IsInitialized ()) 89 return aResult; 90 91 #ifdef WIN32 92 WSADATA aWinSockData; 93 94 aResult = WSAStartup (MAKEWORD (2, 0), &aWinSockData); 95 if (aResult != 0) 96 { 97 aResult = ERROR_TCPSOCKET_FAILED_TO_INITIALIZE; 98 return aResult; 99 } 100 #endif 101 102 mInitialized = true; 103 104 /*----------------------------------------------------------------*/ 105 return aResult; 106 } 107 108 void 109 CRXSocket::Finalize (void) 110 { 111 /*----------------------------------------------------------------*/ 112 #ifdef WIN32 113 WSACleanup (); 114 #endif 115 /*----------------------------------------------------------------*/ 116 } 117 118 bool 119 CRXSocket::IsInitialized (void) 120 { 121 /*----------------------------------------------------------------*/ 122 /*----------------------------------------------------------------*/ 123 return mInitialized; 124 } 125 126 bool 127 CRXSocket::IsReady (void) const 128 { 129 /*----------------------------------------------------------------*/ 130 /*----------------------------------------------------------------*/ 131 return IsInitialized () && IsCreated (); 132 } 133 134 bool 135 CRXSocket::IsCreated (void) const 136 { 137 /*----------------------------------------------------------------*/ 138 /*----------------------------------------------------------------*/ 139 return mSocket > 0 ? true : false; 140 } 141 142 int 143 CRXSocket::CreateSocket (void) 144 { 145 int aResult = -1; 146 147 /*----------------------------------------------------------------*/ 148 if (!IsInitialized () || IsCreated ()) 149 { 150 aResult = ERROR_TCPSOCKET_ALREADY_IN_USE; 151 CRX_ERROR (aResult, "Already in use."); 152 return aResult; 153 } 154 155 aResult = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 156 if (aResult < 0) 157 { 158 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET; 159 CRX_ERROR (aResult, "Failed to create socket."); 160 return aResult; 161 } 162 163 mSocket = aResult; 164 165 /*----------------------------------------------------------------*/ 166 return aResult; 167 } 168 169 int 170 CRXSocket::Connect (const std::string aUrl, 171 const unsigned short aPort, 172 const int aTimeout) 173 { 174 int aResult = -1; 175 struct hostent * aHostEnt; 82 CRXSocket::SetTimeout (const int aTimeout) 83 { 84 int aResult = 0; 176 85 177 86 #ifndef _WIN32 … … 184 93 185 94 /*----------------------------------------------------------------*/ 95 if (!IsReady ()) 96 return ERROR_TCPSOCKET_NOT_READY; 97 98 if (aTimeout == TCPSOCKET_NO_TIMEOUT) 99 return 0; 100 101 aResult = setsockopt (mSocket, 102 SOL_SOCKET, 103 SO_RCVTIMEO, 104 #ifdef _WIN32 105 (char *) &aTimeMilliSec, 106 sizeof (aTimeMilliSec)); 107 #else 108 &aTimeVal, 109 (socklen_t) sizeof (aTimeVal)); 110 #endif 111 if (aResult < 0) 112 { 113 aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT; 114 CRX_ERROR (aResult, "Failed to set socket option."); 115 } 116 /*----------------------------------------------------------------*/ 117 118 return aResult; 119 } 120 121 int 122 CRXSocket::Initialize (void) 123 { 124 int aResult = 0; 125 126 /*----------------------------------------------------------------*/ 127 if (CRXSocket::IsInitialized ()) 128 return aResult; 129 130 #ifdef WIN32 131 WSADATA aWinSockData; 132 133 aResult = WSAStartup (MAKEWORD (2, 0), &aWinSockData); 134 if (aResult != 0) 135 { 136 aResult = ERROR_TCPSOCKET_FAILED_TO_INITIALIZE; 137 return aResult; 138 } 139 #endif 140 141 mInitialized = true; 142 143 /*----------------------------------------------------------------*/ 144 return aResult; 145 } 146 147 void 148 CRXSocket::Finalize (void) 149 { 150 /*----------------------------------------------------------------*/ 151 #ifdef WIN32 152 WSACleanup (); 153 #endif 154 /*----------------------------------------------------------------*/ 155 } 156 157 bool 158 CRXSocket::IsInitialized (void) 159 { 160 /*----------------------------------------------------------------*/ 161 /*----------------------------------------------------------------*/ 162 return mInitialized; 163 } 164 165 bool 166 CRXSocket::IsReady (void) const 167 { 168 /*----------------------------------------------------------------*/ 169 /*----------------------------------------------------------------*/ 170 return IsInitialized () && IsCreated (); 171 } 172 173 bool 174 CRXSocket::IsCreated (void) const 175 { 176 /*----------------------------------------------------------------*/ 177 /*----------------------------------------------------------------*/ 178 return mSocket > 0 ? true : false; 179 } 180 181 int 182 CRXSocket::CreateSocket (void) 183 { 184 int aResult = -1; 185 186 /*----------------------------------------------------------------*/ 187 if (!IsInitialized () || IsCreated ()) 188 { 189 aResult = ERROR_TCPSOCKET_ALREADY_IN_USE; 190 CRX_ERROR (aResult, "Already in use."); 191 return aResult; 192 } 193 194 aResult = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 195 if (aResult < 0) 196 { 197 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET; 198 CRX_ERROR (aResult, "Failed to create socket(%d).", GetSystemErrorCode ()); 199 return aResult; 200 } 201 202 mSocket = aResult; 203 204 /*----------------------------------------------------------------*/ 205 return aResult; 206 } 207 208 int 209 CRXSocket::Connect (const std::string aUrl, 210 const unsigned short aPort, 211 const int aTimeout) 212 { 213 int aResult = -1; 214 struct hostent * aHostEnt; 215 216 /*----------------------------------------------------------------*/ 186 217 aResult = CreateSocket (); 187 218 if (aResult < 0) 188 219 { 220 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET; 221 CRX_ERROR (aResult, "Failed to create socket(%d).", GetSystemErrorCode ()); 189 222 return aResult; 190 223 } … … 207 240 } 208 241 209 if (aTimeout > TCPSOCKET_NO_TIMEOUT) 210 { 211 aResult = setsockopt (mSocket, 212 SOL_SOCKET, 213 SO_RCVTIMEO, 214 #ifdef _WIN32 215 (char *) &aTimeMilliSec, 216 sizeof (aTimeMilliSec)); 217 #else 218 &aTimeVal, 219 (socklen_t) sizeof (aTimeVal)); 220 #endif 221 if (aResult < 0) 222 { 223 aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT; 224 CRX_ERROR (aResult, "Failed to set socket option."); 225 return aResult; 226 } 242 aResult = SetTimeout (aTimeout); 243 if (aResult < 0) 244 { 245 aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT; 246 CRX_ERROR (aResult, "Failed to set timeout (%d).", GetSystemErrorCode ()); 247 return aResult; 227 248 } 228 249 … … 231 252 { 232 253 aResult = ERROR_TCPSOCKET_FAILED_TO_CONNECT; 233 CRX_ERROR (aResult, "Failed to connect .");254 CRX_ERROR (aResult, "Failed to connect (%d).", GetSystemErrorCode ()); 234 255 } 235 256 … … 249 270 if (aResult < 0) 250 271 { 272 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET; 273 CRX_ERROR (aResult, "Failed to create socket(%d).", GetSystemErrorCode ()); 251 274 return aResult; 252 275 } … … 260 283 { 261 284 aResult = ERROR_TCPSOCKET_FAILED_TO_BIND; 262 CRX_ERROR (aResult, "Failed to bind .");285 CRX_ERROR (aResult, "Failed to bind(%d).", GetSystemErrorCode ()); 263 286 return aResult; 264 287 } … … 268 291 { 269 292 aResult = ERROR_TCPSOCKET_FAILED_TO_LISTEN; 270 CRX_ERROR (aResult, "Failed to listen .");293 CRX_ERROR (aResult, "Failed to listen(%d).", GetSystemErrorCode ()); 271 294 return aResult; 272 295 } … … 292 315 293 316 /*----------------------------------------------------------------*/ 294 if (!IsReady ()) return ERROR_TCPSOCKET_NOT_READY; 317 if (!IsReady ()) 318 return ERROR_TCPSOCKET_NOT_READY; 295 319 296 320 aResult = accept (mSocket, (struct sockaddr *) &aAddress, &aLength); … … 298 322 { 299 323 aResult = ERROR_TCPSOCKET_FAILED_TO_ACCEPT; 300 CRX_ERROR (aResult, "Failed to accept .");324 CRX_ERROR (aResult, "Failed to accept(%d).", GetSystemErrorCode ()); 301 325 return aResult; 302 326 } … … 316 340 317 341 int 318 CRXSocket::Send (c har * aBuffer,319 int aSize)342 CRXSocket::Send (const char * aBuffer, 343 int aSize) 320 344 { 321 345 int aResult = -1; 322 346 323 347 /*----------------------------------------------------------------*/ 324 if (!IsReady ()) return ERROR_TCPSOCKET_NOT_READY; 348 if (!IsReady ()) 349 return ERROR_TCPSOCKET_NOT_READY; 325 350 326 351 aResult = send (mSocket, aBuffer, aSize, 0); … … 328 353 { 329 354 aResult = ERROR_TCPSOCKET_FAILED_TO_SEND; 330 CRX_ERROR (aResult, "Failed to send .");355 CRX_ERROR (aResult, "Failed to send(%d).", GetSystemErrorCode ()); 331 356 return aResult; 332 357 } … … 343 368 344 369 /*----------------------------------------------------------------*/ 345 if (!IsReady ()) return ERROR_TCPSOCKET_NOT_READY; 370 if (!IsReady ()) 371 return ERROR_TCPSOCKET_NOT_READY; 346 372 347 373 aResult = recv (mSocket, aBuffer, aSize, 0); … … 349 375 { 350 376 aResult = ERROR_TCPSOCKET_FAILED_TO_RECEIVE; 351 CRX_ERROR (aResult, "Failed to receive .");352 return aResult; 353 } 354 355 /*----------------------------------------------------------------*/ 356 return aResult; 357 } 377 CRX_ERROR (aResult, "Failed to receive(%d).", GetSystemErrorCode ()); 378 return aResult; 379 } 380 381 /*----------------------------------------------------------------*/ 382 return aResult; 383 } -
trunk/src/main.cpp
r22 r24 1 // LocalProxy.cpp : ÄÜ¼Ö ÀÀ¿ë ÇÁ·Î±×·¥¿¡ ´ëÇÑ ÁøÀÔÁ¡À» Á¤ÀÇÇÕ´Ï´Ù.2 //3 4 1 #ifdef _WIN32 5 2 # define _CRT_SECURE_NO_WARNINGS … … 18 15 using namespace std; 19 16 17 #ifdef _WIN32 18 # include <process.h> 19 # define THREAD_TYPE void * 20 # define THREAD_FUNCTION_RETURN unsigned int 21 # define THREAD_FUNCTION_CALLING_CONVENTION WINAPI 22 #else 23 # define THREAD_TYPE pthread_t 24 # define THREAD_FUNCTION_RETURN void * 25 # define THREAD_FUNCTION_CALLING_CONVENTION 26 #endif 27 28 THREAD_FUNCTION_RETURN 29 THREAD_FUNCTION_CALLING_CONVENTION 30 CRXProxyMTWrapper (void * aThreadArg); 31 32 int ThreadPool (CRXSocket * aProxySocket, 33 const int aThreadPoolCount); 34 20 35 int main (int argc, char* argv[]) 21 36 { 22 int aResult = 0; 23 const unsigned short aPort = 8080; 37 int aResult = 0; 38 const short aPort = 8080; 39 const int aThreadPoolCount = 30; 24 40 25 CRXSocket aSocket; 26 CRXProxy * aProxy = NULL; 41 CRXSocket aProxySocket; 27 42 28 43 /*----------------------------------------------------------------*/ 29 /*---------------------------------------------------------------- 30 * Initialize 31 *----------------------------------------------------------------*/ 32 aResult = aSocket.CreateServer (aPort); 44 /* 1. create proxy server */ 45 aResult = aProxySocket.CreateServer (aPort); 33 46 if (aResult < 0) 34 47 { … … 37 50 } 38 51 52 /* 2. ready proxy servers */ 53 aResult = ThreadPool (&aProxySocket, aThreadPoolCount); 54 if (aResult) 55 { 56 cout << "Failed to create thread pool." << endl; 57 return aResult; 58 } 59 60 /* 3. keep process */ 61 while (true); 62 63 /*----------------------------------------------------------------*/ 64 aProxySocket.Close (); 65 66 return aResult; 67 } 68 69 THREAD_FUNCTION_RETURN 70 THREAD_FUNCTION_CALLING_CONVENTION 71 CRXProxyMTWrapper (void * aThreadArg) 72 { 73 int aResult = 0; 74 75 CRXSocket * aProxySocket = (CRXSocket *)aThreadArg; 76 CRXProxy * aProxy = NULL; 77 78 /*----------------------------------------------------------------*/ 39 79 for (;;) 40 80 { 41 aResult = a Socket.Accept ();81 aResult = aProxySocket->Accept (); 42 82 if (aResult < 0) 43 83 { 44 c out<< "Failed to accept." << endl;84 cerr << "Failed to accept." << endl; 45 85 break; 46 86 } 47 87 48 if ((aProxy = CRXProxy::GetNewInstance ( aResult)) == NULL)88 if ((aProxy = CRXProxy::GetNewInstance ()) == NULL) 49 89 { 50 c out<< "Failed to get proxy." << endl;90 cerr << "Failed to get proxy." << endl; 51 91 aResult = -1; 52 92 break; 53 93 } 54 94 55 aResult = aProxy->ForwardMT (); 95 aProxy->SetClientSocket (aResult); 96 97 aResult = aProxy->Forward (); 56 98 if (aResult < 0) 57 99 { 58 cout << "Failed to forward." << endl; 100 cerr << aProxy->GetErrorMessage () << endl; 101 } 102 103 aProxy->ReleaseInstance (); 104 } 105 106 cerr << " thread exit." << endl; 107 /*----------------------------------------------------------------*/ 108 109 return (THREAD_FUNCTION_RETURN)0; 110 } 111 112 int ThreadPool (CRXSocket * aProxySocket, 113 const int aThreadPoolCount) 114 { 115 int aResult = 0; 116 int aIter = 0; 117 THREAD_TYPE aThreadID = (THREAD_TYPE)0; 118 119 /*----------------------------------------------------------------*/ 120 for (aIter = 0 ; aIter < aThreadPoolCount ; aIter++) 121 { 122 #ifdef _WIN32 123 aThreadID = (void *)_beginthreadex (0, 0, CRXProxyMTWrapper, aProxySocket, 0, 0); 124 if (aThreadID == NULL) 125 { 126 aResult = -1; 59 127 break; 60 128 } 129 CloseHandle (aThreadID); 130 #else 131 aResult = pthread_create (&aThreadID, NULL, CRXProxyMTWrapper, aProxySocket); 132 if (aResult < 0) 133 { 134 break; 135 } 136 #endif 137 cout << "Thread(" << aIter + 1 << ") is created successfully." << endl; 61 138 } 62 63 139 /*----------------------------------------------------------------*/ 64 aSocket.Close ();65 140 66 141 return aResult;
Note:
See TracChangeset
for help on using the changeset viewer.