Changeset 18 in cheroxy
- Timestamp:
- 11/08/12 16:39:42 (12 years ago)
- Location:
- trunk
- Files:
-
- 4 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/_win/cheroxy/cheroxy.vcproj
r12 r18 22 22 IntermediateDirectory="$(ConfigurationName)" 23 23 ConfigurationType="1" 24 CharacterSet=" 1"24 CharacterSet="0" 25 25 > 26 26 <Tool … … 174 174 > 175 175 <File 176 RelativePath="..\..\src\CRXException.cpp" 177 > 178 </File> 179 <File 176 180 RelativePath="..\..\src\CRXHttpMessage.cpp" 177 181 > … … 187 191 <File 188 192 RelativePath="..\..\src\CRXMutex.cpp" 193 > 194 </File> 195 <File 196 RelativePath="..\..\src\CRXProxy.cpp" 189 197 > 190 198 </File> … … 204 212 > 205 213 <File 214 RelativePath="..\..\include\CRXException.h" 215 > 216 </File> 217 <File 206 218 RelativePath="..\..\include\CRXHttpMessage.h" 207 219 > … … 217 229 <File 218 230 RelativePath="..\..\include\CRXMutex.h" 231 > 232 </File> 233 <File 234 RelativePath="..\..\include\CRXProxy.h" 219 235 > 220 236 </File> -
trunk/include/CRXHttpMessage.h
r5 r18 25 25 std::string GetMessage (void) const; 26 26 27 operator const char * (void) const;28 27 virtual CRXHttpMessage & operator = (const char * aHttpMessage); 29 28 }; -
trunk/include/CRXSocket.h
r14 r18 4 4 #ifndef __CRXSOCKET_H__ 5 5 #define __CRXSOCKET_H__ 6 7 #include "CRXException.h" 6 8 7 9 #ifdef WIN32 … … 35 37 #define ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT ERROR_TCPSOCKET - 12 36 38 37 class CRXSocket 39 class CRXSocket : public CRXException 38 40 { 39 41 private: … … 92 94 struct sockaddr_in * aAddress = NULL); 93 95 94 int Accept (struct sockaddr_in * aRemoteAddress = NULL,95 int * aAddressLength = NULL);96 int Accept (struct sockaddr_in * aRemoteAddress = NULL, 97 int * aAddressLength = NULL); 96 98 97 99 /** -
trunk/src/CRXHttpMessage.cpp
r17 r18 34 34 } 35 35 36 CRXHttpMessage::operator const char * (void) const37 {38 return mHttpMessage.c_str ();39 }40 41 36 CRXHttpMessage & 42 37 CRXHttpMessage::operator = (const char * aHttpMessage) -
trunk/src/CRXHttpRequest.cpp
r17 r18 12 12 13 13 CRXHttpRequest::CRXHttpRequest (void) 14 : 15 mPort (0) 14 : mPort (0) 16 15 { 17 16 } -
trunk/src/CRXHttpResponse.cpp
r17 r18 54 54 mStatusString.assign (aStatusString); 55 55 mStatusCode = atoi (aStatusCode); 56 57 56 /*----------------------------------------------------------------*/ 58 57 } -
trunk/src/CRXSocket.cpp
r14 r18 95 95 if (aResult != 0) 96 96 { 97 return ERROR_TCPSOCKET_FAILED_TO_INITIALIZE; 97 aResult = ERROR_TCPSOCKET_FAILED_TO_INITIALIZE; 98 return aResult; 98 99 } 99 100 #endif … … 146 147 /*----------------------------------------------------------------*/ 147 148 if (!IsInitialized () || IsCreated ()) 148 return ERROR_TCPSOCKET_ALREADY_IN_USE; 149 { 150 aResult = ERROR_TCPSOCKET_ALREADY_IN_USE; 151 CRX_ERROR (aResult, "Already in use."); 152 return aResult; 153 } 149 154 150 155 aResult = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); 151 156 if (aResult < 0) 152 157 { 158 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET; 159 CRX_ERROR (aResult, "Failed to create socket."); 160 return aResult; 153 161 } 154 162 … … 191 199 if (aHostEnt == NULL) 192 200 { 193 return ERROR_TCPSOCKET_FAILED_TO_GET_HOSTNAME; 201 aResult = ERROR_TCPSOCKET_FAILED_TO_GET_HOSTNAME; 202 CRX_ERROR (aResult, "Failed to get hostname."); 203 return aResult; 194 204 } 195 205 mAddress.sin_family = aHostEnt->h_addrtype; … … 199 209 if (aTimeout > TCPSOCKET_NO_TIMEOUT) 200 210 { 201 #ifdef _WIN32202 211 aResult = setsockopt (mSocket, 203 212 SOL_SOCKET, 204 213 SO_RCVTIMEO, 214 #ifdef _WIN32 205 215 (char *) &aTimeMilliSec, 206 216 sizeof (aTimeMilliSec)); 207 217 #else 208 aResult = setsockopt (mSocket,209 SOL_SOCKET,210 SO_RCVTIMEO,211 218 &aTimeVal, 212 219 (socklen_t) sizeof (aTimeVal)); … … 214 221 if (aResult < 0) 215 222 { 216 return ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT; 223 aResult = ERROR_TCPSOCKET_FAILED_TO_SETSOCKOPT; 224 CRX_ERROR (aResult, "Failed to set socket option."); 225 return aResult; 217 226 } 218 227 } … … 221 230 if (aResult < 0) 222 231 { 223 return ERROR_TCPSOCKET_FAILED_TO_CONNECT; 232 aResult = ERROR_TCPSOCKET_FAILED_TO_CONNECT; 233 CRX_ERROR (aResult, "Failed to connect."); 224 234 } 225 235 … … 249 259 if (aResult < 0) 250 260 { 251 return ERROR_TCPSOCKET_FAILED_TO_BIND; 261 aResult = ERROR_TCPSOCKET_FAILED_TO_BIND; 262 CRX_ERROR (aResult, "Failed to bind."); 263 return aResult; 252 264 } 253 265 … … 255 267 if (aResult < 0) 256 268 { 257 return ERROR_TCPSOCKET_FAILED_TO_LISTEN; 269 aResult = ERROR_TCPSOCKET_FAILED_TO_LISTEN; 270 CRX_ERROR (aResult, "Failed to listen."); 271 return aResult; 258 272 } 259 273 … … 283 297 if (aResult < 0) 284 298 { 299 aResult = ERROR_TCPSOCKET_FAILED_TO_ACCEPT; 300 CRX_ERROR (aResult, "Failed to accept."); 285 301 return aResult; 286 302 } … … 311 327 if (aResult != aSize) 312 328 { 313 return ERROR_TCPSOCKET_FAILED_TO_SEND; 329 aResult = ERROR_TCPSOCKET_FAILED_TO_SEND; 330 CRX_ERROR (aResult, "Failed to send."); 331 return aResult; 314 332 } 315 333 … … 330 348 if (aResult < 0) 331 349 { 332 return ERROR_TCPSOCKET_FAILED_TO_RECEIVE; 333 } 334 335 /*----------------------------------------------------------------*/ 336 return aResult; 337 } 350 aResult = ERROR_TCPSOCKET_FAILED_TO_RECEIVE; 351 CRX_ERROR (aResult, "Failed to receive."); 352 return aResult; 353 } 354 355 /*----------------------------------------------------------------*/ 356 return aResult; 357 } -
trunk/src/main.cpp
r17 r18 4 4 #ifdef _WIN32 5 5 # define _CRT_SECURE_NO_WARNINGS 6 # include <process.h>7 #else8 # include <errno.h>9 6 #endif 10 7 11 8 #include "CRXSocket.h" 12 #include "CRXHttpRequest.h" 13 #include "CRXHttpResponse.h" 9 #include "CRXProxy.h" 14 10 15 11 #include <iostream> … … 22 18 using namespace std; 23 19 24 /*-----------------------------------------------------------------*/25 int CRXCreateThread (int (* aThreadFunc) (void *), void * aThreadArg);26 int CRXProxyThread (void * aThreadArg);27 28 /* error */29 20 #ifdef _WIN32 30 # define CRX_ERROR() GetLastError () 31 # ifndef __func__ 32 # define __func__ __FUNCTION__ 33 # endif 34 #else 35 # define CRX_ERROR() errno 21 # define __func__ __FUNCTION__ 36 22 #endif 37 23 … … 46 32 } while (0) 47 33 48 typedef struct __crx_proxy_thread_argument__49 {50 int mSocket;51 } CRXProxyThreadArgs;52 /*----------------------------------------------------------------*/53 54 34 int main (int argc, char* argv[]) 55 35 { … … 57 37 const unsigned short aPort = 8080; 58 38 59 CRXSocket aSocket;60 CRXProxy ThreadArgs * aThreadArg;39 CRXSocket aSocket; 40 CRXProxy * aProxy = NULL; 61 41 62 42 /*----------------------------------------------------------------*/ … … 80 60 } 81 61 82 if ((a ThreadArg = new(std::nothrow) CRXProxyThreadArgs) == NULL)62 if ((aProxy = new(std::nothrow) CRXProxy (aResult)) == NULL) 83 63 { 84 64 aResult = -1; … … 86 66 } 87 67 88 memset (aThreadArg, 0x0, sizeof (CRXProxyThreadArgs)); 89 aThreadArg->mSocket = aResult; 90 91 aResult = CRXCreateThread (CRXProxyThread, aThreadArg); 68 aResult = aProxy->ForwardMT (); 92 69 if (aResult < 0) 93 70 { 94 aResult = CRX_ERROR ();95 71 CRX_PRINT_ERROR (aResult, "failed to create thread"); 96 72 break; … … 103 79 return aResult; 104 80 } 105 106 int107 CRXCreateThread (int (* aThreadFunc) (void *), void * aThreadArg)108 {109 #ifdef _WIN32110 return _beginthread ((void (*) (void *))aThreadFunc, 0, aThreadArg);111 #else112 pthread_t aThreadID = 0;113 114 return pthread_create (&aThreadID, NULL, (void * (*) (void *))aThreadFunc, aThreadArg);115 #endif116 }117 118 int119 CRXProxyThread (void * aThreadArg)120 {121 int aResult = 0;122 int aReceivedSize = 0;123 124 const unsigned int aBufferSize = 1024 * 16;125 char aBuffer[aBufferSize] = {0x00, };126 127 CRXHttpRequest aHttpRequest;128 CRXHttpResponse aHttpResponse;129 130 CRXSocket aWebServer;131 CRXSocket aWebBrowser = ((CRXProxyThreadArgs *)aThreadArg)->mSocket;132 133 bool aIsMoreRequest = false;134 135 /*----------------------------------------------------------------*/136 delete (CRXProxyThreadArgs *)aThreadArg;137 138 do139 {140 /* BROWSER --- request ---> [PROXY] SERVER */141 aReceivedSize = aWebBrowser.Receive (aBuffer, aBufferSize);142 if (aReceivedSize < 0)143 {144 CRX_PRINT_ERROR (aReceivedSize, "failed to receive from client (" << CRX_ERROR () << ")");145 return aReceivedSize;146 }147 148 /* BROWSER ?--- waiting --- [PROXY] --- connect ---> SERVER */149 if (!aWebServer)150 {151 /* parse http request */152 aHttpRequest = aBuffer;153 154 /* connect */155 aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort (), 1);156 if (aResult < 0)157 {158 CRX_PRINT_ERROR (aResult, "failed to connect to server ("159 << CRX_ERROR ()160 << ") <"161 << aHttpRequest.GetHost ()162 << ":"163 << aHttpRequest.GetPort ()164 << "><"165 << aHttpRequest.GetURL ()166 << ">");167 return aResult;168 }169 }170 171 /* BROWSER ?--- waiting --- [PROXY] --- request ---> SERVER */172 aResult = aWebServer.Send (aBuffer, aReceivedSize);173 if (aResult != aReceivedSize)174 {175 CRX_PRINT_ERROR (aResult, "failed to send to server <"176 << aHttpRequest.GetHost ()177 << ":"178 << aHttpRequest.GetPort ()179 << CRX_ERROR ()180 << ")");181 return aResult;182 }183 184 /* BROWSER <--- response--- [PROXY] <--- response--- SERVER */185 for (;;)186 {187 memset (aBuffer, 0x00, aBufferSize);188 189 aResult = aWebServer.Receive (aBuffer, aBufferSize);190 if (aResult < 0)191 {192 CRX_PRINT_ERROR (aResult, "failed to receive ("193 << CRX_ERROR ()194 << ") <"195 << aHttpRequest.GetURL ()196 << ">");197 break;198 }199 else if (aResult == 0)200 {201 break;202 }203 aReceivedSize = aResult;204 205 aResult = aWebBrowser.Send (aBuffer, aReceivedSize);206 if (aResult != aReceivedSize)207 {208 CRX_PRINT_ERROR (aResult, "failed to send ("209 << CRX_ERROR ()210 << ") <"211 << aHttpRequest.GetURL ()212 << ">");213 break;214 }215 }216 } while (aIsMoreRequest);217 218 /*----------------------------------------------------------------*/219 aWebBrowser.Close ();220 aWebServer.Close ();221 222 return aResult;223 }
Note:
See TracChangeset
for help on using the changeset viewer.