Skip to main content
summaryrefslogtreecommitdiffstats
blob: d446f69b435e1d60f42cf1b7e6cc69fca5ee7194 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
//  ========================================================================
//  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.rhttp.gateway;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.client.Address;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.rhttp.client.JettyClient;
import org.eclipse.jetty.rhttp.client.RHTTPClient;
import org.eclipse.jetty.rhttp.client.RHTTPListener;
import org.eclipse.jetty.rhttp.client.RHTTPRequest;
import org.eclipse.jetty.rhttp.client.RHTTPResponse;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
 * <p>This class combines a gateway server and a gateway client to obtain the functionality of a simple proxy server.</p>
 * <p>This gateway proxy server starts on port 8080 and can be set as http proxy in browsers such as Firefox, and used
 * to browse the internet.</p>
 * <p>Its functionality is limited (for example, it only supports http, and not https).</p>
 * @version $Revision$ $Date$
 */
public class GatewayProxyServer
{
    private static final Logger logger = Log.getLogger(GatewayProxyServer.class.toString());

    public static void main(String[] args) throws Exception
    {
        GatewayServer server = new GatewayServer();

        Connector plainConnector = new SelectChannelConnector();
        plainConnector.setPort(8080);
        server.addConnector(plainConnector);

        ((StandardGateway)server.getGateway()).setExternalTimeout(180000);
        ((StandardGateway)server.getGateway()).setGatewayTimeout(20000);
        server.setTargetIdRetriever(new ProxyTargetIdRetriever());
        server.start();

        HttpClient httpClient = new HttpClient();
        httpClient.setConnectorType(HttpClient.CONNECTOR_SOCKET);
        httpClient.start();

        RHTTPClient client = new JettyClient(httpClient, new Address("localhost", plainConnector.getPort()), server.getContext().getContextPath() + "/gw", "proxy");
        client.addListener(new ProxyListener(httpClient, client));
        client.connect();

        Runtime.getRuntime().addShutdownHook(new Shutdown(server, httpClient, client));
        logger.info("{} started", GatewayProxyServer.class.getSimpleName());
    }

    private static class Shutdown extends Thread
    {
        private final GatewayServer server;
        private final HttpClient httpClient;
        private final RHTTPClient client;

        public Shutdown(GatewayServer server, HttpClient httpClient, RHTTPClient client)
        {
            this.server = server;
            this.httpClient = httpClient;
            this.client = client;
        }

        @Override
        public void run()
        {
            try
            {
                client.disconnect();
                httpClient.stop();
                server.stop();
                logger.info("{} stopped", GatewayProxyServer.class.getSimpleName());
            }
            catch (Exception x)
            {
                logger.debug("Exception while stopping " + GatewayProxyServer.class.getSimpleName(), x);
            }
        }
    }

    private static class ProxyListener implements RHTTPListener
    {
        private final HttpClient httpClient;
        private final RHTTPClient client;

        private ProxyListener(HttpClient httpClient, RHTTPClient client)
        {
            this.httpClient = httpClient;
            this.client = client;
        }

        public void onRequest(RHTTPRequest request) throws Exception
        {
            ProxyExchange exchange = new ProxyExchange();
            Address address = Address.from(request.getHeaders().get("Host"));
            if (address.getPort() == 0) address = new Address(address.getHost(), 80);
            exchange.setAddress(address);
            exchange.setMethod(request.getMethod());
            exchange.setURI(request.getURI());
            for (Map.Entry<String, String> header : request.getHeaders().entrySet())
                exchange.setRequestHeader(header.getKey(), header.getValue());
            exchange.setRequestContent(new ByteArrayBuffer(request.getBody()));
            int status = syncSend(exchange);
            if (status == HttpExchange.STATUS_COMPLETED)
            {
                int statusCode = exchange.getResponseStatus();
                String statusMessage = exchange.getResponseMessage();
                Map<String, String> responseHeaders = exchange.getResponseHeaders();
                byte[] responseBody = exchange.getResponseBody();
                RHTTPResponse response = new RHTTPResponse(request.getId(), statusCode, statusMessage, responseHeaders, responseBody);
                client.deliver(response);
            }
            else
            {
                int statusCode = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
                String statusMessage = "Gateway error";
                HashMap<String, String> responseHeaders = new HashMap<String, String>();
                responseHeaders.put("Connection", "close");
                byte[] responseBody = new byte[0];
                RHTTPResponse response = new RHTTPResponse(request.getId(), statusCode, statusMessage, responseHeaders, responseBody);
                client.deliver(response);
            }
        }

        private int syncSend(ProxyExchange exchange) throws Exception
        {
            long start = System.nanoTime();
            httpClient.send(exchange);
            int status = exchange.waitForDone();
            long end = System.nanoTime();
            long millis = TimeUnit.NANOSECONDS.toMillis(end - start);
            long micros = TimeUnit.NANOSECONDS.toMicros(end - start - TimeUnit.MILLISECONDS.toNanos(millis));
            logger.debug("Proxied request took {}.{} ms", millis, micros);
            return status;
        }
    }

    private static class ProxyExchange extends ContentExchange
    {
        private String responseMessage;
        private Map<String, String> responseHeaders = new HashMap<String, String>();
        private ByteArrayOutputStream responseBody = new ByteArrayOutputStream();

        private ProxyExchange()
        {
            super(true);
        }

        public String getResponseMessage()
        {
            return responseMessage;
        }

        public Map<String, String> getResponseHeaders()
        {
            return responseHeaders;
        }

        public byte[] getResponseBody()
        {
            return responseBody.toByteArray();
        }

        @Override
        protected void onResponseStatus(Buffer version, int code, Buffer message) throws IOException
        {
            super.onResponseStatus(version, code, message);
            this.responseMessage = message.toString("UTF-8");
        }

        @Override
        protected void onResponseHeader(Buffer nameBuffer, Buffer valueBuffer) throws IOException
        {
            super.onResponseHeader(nameBuffer, valueBuffer);
            String name = nameBuffer.toString("UTF-8");
            String value = valueBuffer.toString("UTF-8");
            // Skip chunked header, since we read the whole body and will not re-chunk it
            if (!name.equalsIgnoreCase("Transfer-Encoding") || !value.equalsIgnoreCase("chunked"))
                responseHeaders.put(name, value);
        }

        @Override
        protected void onResponseContent(Buffer buffer) throws IOException
        {
            responseBody.write(buffer.asArray());
            super.onResponseContent(buffer);
        }
    }

    public static class ProxyTargetIdRetriever implements TargetIdRetriever
    {
        public String retrieveTargetId(HttpServletRequest httpRequest)
        {
            return "proxy";
        }
    }
}

Back to the top