Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1d09edc80ad1154178e33827b139bcac77d308f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package org.eclipse.jetty.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import org.eclipse.jetty.toolchain.test.IO;


public class ProxyFakeTunnelTest extends ProxyTunnellingTest
{
    ServerSocket _proxySocket;
    Thread _proxyThread;

    protected int proxyPort()
    {
        return _proxySocket.getLocalPort();
    }
    

    protected void startProxy() throws Exception
    {
        _proxySocket = new ServerSocket(0);
        
        _proxyThread = new Thread()
        {
            @Override
            public void run()
            {
                while (!_proxySocket.isClosed())
                {
                    try
                    {
                        Socket socket=_proxySocket.accept();
                        System.err.println("accepted "+socket);
                        new FakeProxy(socket).start();
                    }
                    catch (IOException e)
                    {
                    }
                }
            }
        };
        _proxyThread.setDaemon(true);
        _proxyThread.start();
        
    }

    protected void stopProxy() throws Exception
    {
        _proxySocket.close();
        _proxyThread.interrupt();
    }

    static class FakeProxy extends Thread
    {
        Socket _socket;
        
        public FakeProxy(Socket socket)
        {
            _socket=socket;
        }

        public void run()
        {

            Socket toserver=null;
            final InputStream in;
            final OutputStream out; 
            try
            {
                 in = _socket.getInputStream();
                 out = _socket.getOutputStream();
                
                String address="";
                int state=0;
                
                for (int b=in.read();b>=0;b=in.read())
                {   
                    switch(state)
                    {
                        case 0:
                            if (' '==b)
                                state=1;
                            break;
                            
                        case 1:
                            if (' '==b)
                                state=2;
                            else
                                address+=(char)b;
                            break;
                            
                        case 2:
                            if ('\r'==b)
                                state=3;
                            break;
                            
                        case 3:
                            if ('\n'==b)
                                state=4;
                            else
                                state=2;
                            break;
                            
                        case 4:
                            if ('\r'==b)
                                state=5;
                            else
                                state=2;
                            break;
                            
                        case 5:
                            if ('\n'==b)
                            {
                                state=6;
                                System.err.println("address="+address);
                                String[] parts=address.split(":");
                                try
                                {
                                    toserver = new Socket(parts[0],Integer.parseInt(parts[1]));
                                    out.write((
                                            "HTTP/1.1 200 OK\r\n"+
                                            "Server: fake\r\n"+
                                            // "Content-Length: 0\r\n"+ 
                                            "\r\n"
                                            ).getBytes());
                                }
                                catch(IOException e)
                                {
                                    out.write((
                                            "HTTP/1.1 503 Unavailable\r\n"+
                                            "Server: fake\r\n"+
                                            "Content-Length: 0\r\n"+ 
                                            "\r\n"
                                            ).getBytes());
                                }
                                out.flush();
           
                                if (toserver!=null)
                                {
                                    final InputStream from = toserver.getInputStream();

                                    Thread copy = new Thread()
                                    {
                                        public void run()
                                        {
                                            try
                                            {
                                                IO.copy(from,out);
                                                out.close();
                                            }
                                            catch (IOException e)
                                            {
                                            }
                                            finally
                                            {
                                                try
                                                {
                                                    out.close();
                                                }
                                                catch (IOException e)
                                                {
                                                }
                                            }
                                        }
                                    };
                                    copy.setDaemon(true);
                                    copy.start();
                                }
                                
                            }
                            else
                                state=2;
                            break;

                        case 6:
                            toserver.getOutputStream().write((byte)b);
                            
                    }
                }
                
                
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (toserver!=null)
                {
                    try
                    {
                        toserver.close();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
        
    }
    
    
}

Back to the top