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

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

#1 요청 메시지 변조하는 예제로 main 구성

File size: 4.8 KB
Line 
1#ifdef _WIN32
2# define _CRT_SECURE_NO_WARNINGS
3#endif
4
5#include "CRXSocket.h"
6#include "CRXProxy.h"
7
8#include <iostream>
9#include <iomanip>
10#include <sstream>
11#include <string>
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17using namespace std;
18
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
25# include <pthread.h>
26# define THREAD_TYPE pthread_t
27# define THREAD_FUNCTION_RETURN void *
28# define THREAD_FUNCTION_CALLING_CONVENTION
29#endif
30
31#define PROXY_CONNECTION "Proxy-Connection"
32#define CONNECTION "Connection"
33
34THREAD_FUNCTION_RETURN
35THREAD_FUNCTION_CALLING_CONVENTION
36CRXProxyMTWrapper (void * aThreadArg);
37
38int ThreadPool (CRXSocket * aProxySocket,
39 const int aThreadPoolCount);
40
41bool gViewRequest = true;
42bool gViewResponse = false;
43
44int main (int argc, char* argv[])
45{
46 int aResult = 0;
47 const short aPort = 8080;
48 const int aThreadPoolCount = 50;
49
50 CRXSocket aProxySocket;
51
52 /*----------------------------------------------------------------*/
53 /* 1. create proxy server */
54 aResult = aProxySocket.CreateServer (aPort);
55 if (aResult < 0)
56 {
57 cout << "Failed to create server." << endl;
58 return aResult;
59 }
60
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
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
110THREAD_FUNCTION_RETURN
111THREAD_FUNCTION_CALLING_CONVENTION
112CRXProxyMTWrapper (void * aThreadArg)
113{
114 int aResult = 0;
115
116 CRXSocket & aProxySocket = *((CRXSocket *)aThreadArg);
117 CRXProxy * aProxy = NULL;
118
119 const char aFilterFileExtension[] = "exe|gif|jpg|png|css|js|ico|swf|";
120
121 /*----------------------------------------------------------------*/
122 for (;;)
123 {
124 aResult = aProxySocket.Accept ();
125 if (aResult < 0)
126 {
127 cout << aProxySocket.GetErrorMessage () << endl;
128 aResult = -1;
129 break;
130 }
131
132 aProxy = new(std::nothrow) CRXProxy (aResult);
133 if (aProxy == NULL)
134 {
135 cout << "Failed to get proxy." << endl;
136 aResult = -1;
137 break;
138 }
139 aProxy->SetServerTimeout (2);
140
141 CRXFilter & aFilter = aProxy->GetFilter ();
142 CRXHttpRequest & aRequest = aProxy->GetHttpRequest ();
143 CRXHttpResponse & aResponse = aProxy->GetHttpResponse ();
144
145 aFilter.SetRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION,
146 CRX_FILTER_MATCHED,
147 aFilterFileExtension);
148
149 aResult = aProxy->ReceiveRequest ();
150 if (aResult < 0)
151 {
152 cout << aProxy->GetErrorMessage () << endl;
153 }
154
155 if (gViewRequest && !aFilter.CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION, aRequest))
156 {
157 string aRequestHeader = aRequest.GetHeader ();
158 aRequestHeader.replace (aRequest.GetHeader ().find (PROXY_CONNECTION), strlen (PROXY_CONNECTION), CONNECTION);
159 aRequest.SetHeader (aRequestHeader.c_str ());
160
161 CRX_DEBUG_TRACE ("== Request: \n%s\n", aRequest.GetHeader ().c_str ());
162 }
163
164 aResult = aProxy->SendRequest ();
165 if (aResult < 0)
166 {
167 cout << aProxy->GetErrorMessage () << endl;
168 }
169
170 aResult = aProxy->ReceiveResponse ();
171 if (aResult < 0)
172 {
173 cout << aProxy->GetErrorMessage () << endl;
174 }
175
176 if (gViewResponse && !aFilter.CheckRequestFilter (CRX_FILTER_REQUEST_FILE_EXTENSION, aRequest))
177 {
178 CRX_DEBUG_TRACE ("== Response: \n%s\n", aResponse.GetHeader ().c_str ());
179 CRX_DEBUG_TRACE_BIN (aResponse.GetContentBody (),
180 aResponse.GetContentLength (),
181 "== Content-Body: \n");
182 }
183
184 aResult = aProxy->SendResponse ();
185 if (aResult < 0)
186 {
187 cout << aProxy->GetErrorMessage () << endl;
188 }
189
190 delete aProxy;
191 }
192
193 cout << " thread exit." << endl;
194 /*----------------------------------------------------------------*/
195
196 return (THREAD_FUNCTION_RETURN)0;
197}
Note: See TracBrowser for help on using the repository browser.