source: cheroxy/trunk/src/main.cpp@ 56

Last change on this file since 56 was 56, checked in by cheese, 11 years ago

#1 fix to return from CRXProxy::ReceiveResponse if receive zero-byte(infinite-loop bug).

File size: 5.0 KB
RevLine 
[6]1#ifdef _WIN32
2# define _CRT_SECURE_NO_WARNINGS
3#endif
4
5#include "CRXSocket.h"
[18]6#include "CRXProxy.h"
[6]7
8#include <iostream>
9#include <iomanip>
10#include <sstream>
[55]11#include <string>
[6]12
13#include <stdio.h>
[29]14#include <stdlib.h>
[6]15#include <string.h>
16
17using namespace std;
18
[24]19#ifdef _WIN32
20# include <process.h>
21# define THREAD_TYPE void *
22# define THREAD_FUNCTION_RETURN unsigned int
23# define THREAD_FUNCTION_CALLING_CONVENTION WINAPI
24#else
[51]25# include <pthread.h>
[24]26# define THREAD_TYPE pthread_t
27# define THREAD_FUNCTION_RETURN void *
28# define THREAD_FUNCTION_CALLING_CONVENTION
29#endif
30
[55]31#define PROXY_CONNECTION "Proxy-Connection"
32#define CONNECTION "Connection"
33
[24]34THREAD_FUNCTION_RETURN
35THREAD_FUNCTION_CALLING_CONVENTION
36CRXProxyMTWrapper (void * aThreadArg);
37
38int ThreadPool (CRXSocket * aProxySocket,
39 const int aThreadPoolCount);
40
[55]41bool gViewRequest = true;
42bool gViewResponse = false;
43
[6]44int main (int argc, char* argv[])
45{
[24]46 int aResult = 0;
47 const short aPort = 8080;
[52]48 const int aThreadPoolCount = 50;
[6]49
[24]50 CRXSocket aProxySocket;
[6]51
52 /*----------------------------------------------------------------*/
[24]53 /* 1. create proxy server */
54 aResult = aProxySocket.CreateServer (aPort);
[6]55 if (aResult < 0)
56 {
[22]57 cout << "Failed to create server." << endl;
[6]58 return aResult;
59 }
60
[24]61 /* 2. ready proxy servers */
62 aResult = ThreadPool (&aProxySocket, aThreadPoolCount);
63 if (aResult)
64 {
65 cout << "Failed to create thread pool." << endl;
66 return aResult;
67 }
68
69 /* 3. keep process */
70 while (true);
71
72 /*----------------------------------------------------------------*/
73 aProxySocket.Close ();
74
75 return aResult;
76}
77
[45]78int ThreadPool (CRXSocket * aProxySocket,
79 const int aThreadPoolCount)
80{
81 int aResult = 0;
82 int aIter = 0;
83 THREAD_TYPE aThreadID = (THREAD_TYPE)0;
84
85 /*----------------------------------------------------------------*/
86 for (aIter = 0 ; aIter < aThreadPoolCount ; aIter++)
87 {
88#ifdef _WIN32
89 aThreadID = (void *)_beginthreadex (0, 0, CRXProxyMTWrapper, aProxySocket, 0, 0);
90 if (aThreadID == NULL)
91 {
92 aResult = -1;
93 break;
94 }
95 CloseHandle (aThreadID);
96#else
97 aResult = pthread_create (&aThreadID, NULL, CRXProxyMTWrapper, aProxySocket);
98 if (aResult < 0)
99 {
100 break;
101 }
102#endif
103 cout << "Thread(" << aIter + 1 << ") is created successfully." << endl;
104 }
105 /*----------------------------------------------------------------*/
106
107 return aResult;
108}
109
[24]110THREAD_FUNCTION_RETURN
111THREAD_FUNCTION_CALLING_CONVENTION
112CRXProxyMTWrapper (void * aThreadArg)
113{
114 int aResult = 0;
115
[45]116 CRXSocket & aProxySocket = *((CRXSocket *)aThreadArg);
[24]117 CRXProxy * aProxy = NULL;
118
[55]119 const char aFilterFileExtension[] = "exe|gif|jpg|png|css|js|ico|swf|";
[28]120
[24]121 /*----------------------------------------------------------------*/
[6]122 for (;;)
123 {
[45]124 aResult = aProxySocket.Accept ();
[6]125 if (aResult < 0)
126 {
[52]127 cout << aProxySocket.GetErrorMessage () << endl;
[45]128 aResult = -1;
[6]129 break;
130 }
131
[45]132 aProxy = new(std::nothrow) CRXProxy (aResult);
133 if (aProxy == NULL)
[6]134 {
[52]135 cout << "Failed to get proxy." << endl;
[6]136 aResult = -1;
137 break;
138 }
[45]139 aProxy->SetServerTimeout (2);
[6]140
[45]141 CRXFilter & aFilter = aProxy->GetFilter ();
142 CRXHttpRequest & aRequest = aProxy->GetHttpRequest ();
143 CRXHttpResponse & aResponse = aProxy->GetHttpResponse ();
144
145 aFilter.SetRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION,
[28]146 CRX_FILTER_MATCHED,
147 aFilterFileExtension);
[24]148
[56]149 /* 1. receive request from client */
[55]150 aResult = aProxy->ReceiveRequest ();
[6]151 if (aResult < 0)
152 {
[52]153 cout << aProxy->GetErrorMessage () << endl;
[6]154 }
[24]155
[56]156 /* 1.1. modify and print request data */
[55]157 if (gViewRequest && !aFilter.CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION, aRequest))
[28]158 {
[55]159 string aRequestHeader = aRequest.GetHeader ();
160 aRequestHeader.replace (aRequest.GetHeader ().find (PROXY_CONNECTION), strlen (PROXY_CONNECTION), CONNECTION);
161 aRequest.SetHeader (aRequestHeader.c_str ());
162
[45]163 CRX_DEBUG_TRACE ("== Request: \n%s\n", aRequest.GetHeader ().c_str ());
[55]164 }
165
[56]166 /* 2. send request to web server */
[55]167 aResult = aProxy->SendRequest ();
168 if (aResult < 0)
169 {
170 cout << aProxy->GetErrorMessage () << endl;
171 }
172
[56]173 /* 3. receive response from web server */
[55]174 aResult = aProxy->ReceiveResponse ();
175 if (aResult < 0)
176 {
177 cout << aProxy->GetErrorMessage () << endl;
178 }
179
[56]180 /* 3.1. if request url is matched, print response */
[55]181 if (gViewResponse && !aFilter.CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION, aRequest))
182 {
[45]183 CRX_DEBUG_TRACE ("== Response: \n%s\n", aResponse.GetHeader ().c_str ());
184 CRX_DEBUG_TRACE_BIN (aResponse.GetContentBody (),
185 aResponse.GetContentLength (),
186 "== Content-Body: \n");
[28]187 }
188
[56]189 /* 4. send response to client */
[55]190 aResult = aProxy->SendResponse ();
191 if (aResult < 0)
192 {
193 cout << aProxy->GetErrorMessage () << endl;
194 }
195
[44]196 delete aProxy;
[6]197 }
198
[35]199 cout << " thread exit." << endl;
[6]200 /*----------------------------------------------------------------*/
201
[24]202 return (THREAD_FUNCTION_RETURN)0;
[49]203}
Note: See TracBrowser for help on using the repository browser.