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

Last change on this file since 14 was 14, checked in by cheese, 12 years ago

#1 add timeout to socket connection and fix test label in makefile

File size: 5.1 KB
Line 
1// LocalProxy.cpp : ÄÜ¼Ö ÀÀ¿ë ÇÁ·Î±×·¥¿¡ ´ëÇÑ ÁøÀÔÁ¡À» Á¤ÀÇÇÕ´Ï´Ù.
2//
3
4#ifdef _WIN32
5# define _CRT_SECURE_NO_WARNINGS
6# include <process.h>
7#else
8# include <errno.h>
9#endif
10
11#include "CRXSocket.h"
12#include "CRXHttpRequest.h"
13#include "CRXHttpResponse.h"
14
15#include <iostream>
16#include <iomanip>
17#include <sstream>
18
19#include <stdio.h>
20#include <string.h>
21
22using namespace std;
23
24/*-----------------------------------------------------------------*/
25int CRXCreateThread (int (* aThreadFunc) (void *), void * aThreadArg);
26int CRXProxyThread (void * aThreadArg);
27
28/* error */
29#ifdef _WIN32
30# define CRX_ERROR() GetLastError ()
31# ifndef __func__
32# define __func__ __FUNCTION__
33# endif
34#else
35# define CRX_ERROR() errno
36#endif
37
38#define CRX_PRINT_ERROR(__code, __message) \
39 do { \
40 char __error[1024] = {0x00, }; \
41 ostringstream __stream; \
42 __stream << __message; \
43 string __string = __stream.str (); \
44 sprintf (__error, "[%s][%d] (%d) %s\n", __func__, __LINE__, __code, __string.c_str ()); \
45 cout << __error; \
46 } while (0)
47
48typedef struct __crx_proxy_thread_argument__
49{
50 int mSocket;
51} CRXProxyThreadArgs;
52/*----------------------------------------------------------------*/
53
54int main (int argc, char* argv[])
55{
56 int aResult = 0;
57 const unsigned short aPort = 8080;
58
59 CRXSocket aSocket;
60 CRXProxyThreadArgs * aThreadArg;
61
62 /*----------------------------------------------------------------*/
63 /*----------------------------------------------------------------
64 * Initialize
65 *----------------------------------------------------------------*/
66 aResult = aSocket.CreateServer (aPort);
67 if (aResult < 0)
68 {
69 CRX_PRINT_ERROR (aResult, "failed to create server");
70 return aResult;
71 }
72
73 for (;;)
74 {
75 aResult = aSocket.Accept ();
76 if (aResult < 0)
77 {
78 CRX_PRINT_ERROR (aResult, "failed to accept");
79 break;
80 }
81
82 if ((aThreadArg = new(std::nothrow) CRXProxyThreadArgs) == NULL)
83 {
84 aResult = -1;
85 break;
86 }
87
88 memset (aThreadArg, 0x0, sizeof (CRXProxyThreadArgs));
89 aThreadArg->mSocket = aResult;
90
91 aResult = CRXCreateThread (CRXProxyThread, aThreadArg);
92 if (aResult < 0)
93 {
94 aResult = CRX_ERROR ();
95 CRX_PRINT_ERROR (aResult, "failed to create thread");
96 break;
97 }
98 }
99
100 /*----------------------------------------------------------------*/
101 aSocket.Close ();
102
103 return aResult;
104}
105
106int
107CRXCreateThread (int (* aThreadFunc) (void *), void * aThreadArg)
108{
109#ifdef _WIN32
110 return _beginthread ((void (*) (void *))aThreadFunc, 0, aThreadArg);
111#else
112 pthread_t aThreadID = 0;
113
114 return pthread_create (&aThreadID, NULL, (void * (*) (void *))aThreadFunc, aThreadArg);
115#endif
116}
117
118int
119CRXProxyThread (void * aThreadArg)
120{
121 int aResult = 0;
122 int aReceivedSize = 0;
123 int aSentSize = 0;
124
125 const unsigned int aBufferSize = 1024 * 64;
126 char aBuffer[aBufferSize] = {0x00, };
127
128 CRXHttpRequest aHttpRequest;
129 CRXHttpResponse aHttpResponse;
130 string aUrl = "";
131 string aHost = "";
132 int aPort = 0;
133
134 CRXSocket aWebServer;
135 CRXSocket aWebBrowser = ((CRXProxyThreadArgs *)aThreadArg)->mSocket;
136
137 bool aIsMoreRequest = false;
138 /*----------------------------------------------------------------*/
139 delete (CRXProxyThreadArgs *)aThreadArg;
140
141 do
142 {
143 /* BROWSER --- request ---> [PROXY] SERVER */
144 aReceivedSize = aWebBrowser.Receive (aBuffer, aBufferSize);
145 if (aReceivedSize < 0)
146 {
147 CRX_PRINT_ERROR (aReceivedSize, "failed to receive from client (" << CRX_ERROR () << ")");
148 return aResult;
149 }
150
151 /* BROWSER ?--- waiting --- [PROXY] --- connect ---> SERVER */
152 if (!aWebServer)
153 {
154 /* parse http request */
155 aHttpRequest = aBuffer;
156
157 /* connect */
158 aResult = aWebServer.Connect (aHttpRequest.GetHost (), aHttpRequest.GetPort (), 1);
159 if (aResult < 0)
160 {
161 CRX_PRINT_ERROR (aResult, "failed to connect to server <" << aHost << ":" << aPort << "> (" << CRX_ERROR () << ")");
162 return aResult;
163 }
164 }
165
166 /* BROWSER ?--- waiting --- [PROXY] --- request ---> SERVER */
167 aSentSize = aWebServer.Send (aBuffer, aReceivedSize);
168 if (aSentSize != aReceivedSize)
169 {
170 CRX_PRINT_ERROR (aResult, "failed to send to server (" << CRX_ERROR () << ")");
171 return aResult;
172 }
173
174 /* BROWSER <--- response--- [PROXY] <--- response--- SERVER */
175 for (;;)
176 {
177 memset (aBuffer, 0x00, aBufferSize);
178
179 aReceivedSize = aWebServer.Receive (aBuffer, aBufferSize);
180 if (aReceivedSize < 0)
181 {
182 CRX_PRINT_ERROR (aReceivedSize, "failed to receive (" << CRX_ERROR () << ")");
183 break;
184 }
185 else if (aReceivedSize == 0)
186 {
187 aResult = 0;
188 break;
189 }
190
191 aSentSize = aWebBrowser.Send (aBuffer, aReceivedSize);
192 if (aSentSize != aReceivedSize)
193 {
194 CRX_PRINT_ERROR (aSentSize, "failed to send (" << CRX_ERROR () << ")");
195 break;
196 }
197 }
198 } while (aIsMoreRequest);
199
200 /*----------------------------------------------------------------*/
201 aWebBrowser.Close ();
202 aWebServer.Close ();
203
204 return aResult;
205}
Note: See TracBrowser for help on using the repository browser.