forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proxy.java
44 lines (40 loc) · 1.55 KB
/
Proxy.java
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
package io.vertx.example.core.http.proxy;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.example.util.Runner;
/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class Proxy extends AbstractVerticle {
// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(Proxy.class);
}
@Override
public void start() throws Exception {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
vertx.createHttpServer().requestHandler(req -> {
System.out.println("Proxying request: " + req.uri());
HttpClientRequest c_req = client.request(req.method(), 8282, "localhost", req.uri(), c_res -> {
System.out.println("Proxying response: " + c_res.statusCode());
req.response().setChunked(true);
req.response().setStatusCode(c_res.statusCode());
req.response().headers().setAll(c_res.headers());
c_res.handler(data -> {
System.out.println("Proxying response body: " + data.toString("ISO-8859-1"));
req.response().write(data);
});
c_res.endHandler((v) -> req.response().end());
});
c_req.setChunked(true);
c_req.headers().setAll(req.headers());
req.handler(data -> {
System.out.println("Proxying request body " + data.toString("ISO-8859-1"));
c_req.write(data);
});
req.endHandler((v) -> c_req.end());
}).listen(8080);
}
}