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.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());
      client.request(req.method(), 8282, "localhost", req.uri())
        .onSuccess(c_req -> {
          c_req.setChunked(true);
          c_req.headers().setAll(req.headers());
          c_req.send(req).onSuccess(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());
          }).onFailure(err -> {
            System.out.println("Back end failure");
            req.response().setStatusCode(500).end();
          });
      }).onFailure(err -> {
        System.out.println("Could not connect to localhost");
        req.response().setStatusCode(500).end();
      });
    }).listen(8080);
  }
}

@@ -158,16 +158,62 @@
 ptions;%0A
+import io.vertx.core.http.HttpServerResponse;%0A
 import i
@@ -241,16 +241,16 @@
 Runner;%0A
-
 %0A/*%0A * @
@@ -653,19 +653,29 @@
 Handler(
-req
+serverRequest
  -%3E %7B%0A  
@@ -720,19 +720,29 @@
 st: %22 + 
-req
+serverRequest
 .uri());
@@ -738,24 +738,121 @@
 est.uri());%0A
+      serverRequest.pause();%0A      HttpServerResponse serverResponse = serverRequest.response();%0A
       client
@@ -860,19 +860,29 @@
 request(
-req
+serverRequest
 .method(
@@ -903,19 +903,29 @@
 lhost%22, 
-req
+serverRequest
 .uri())%0A
@@ -944,20 +944,28 @@
 uccess(c
-_req
+lientRequest
  -%3E %7B%0A  
@@ -973,20 +973,28 @@
        c
-_req
+lientRequest
 .setChun
@@ -1015,20 +1015,28 @@
        c
-_req
+lientRequest
 .headers
@@ -1045,19 +1045,29 @@
 .setAll(
-req
+serverRequest
 .headers
@@ -1086,38 +1086,65 @@
    c
-_req.send(req).onSuccess(c_res
+lientRequest.send(serverRequest).onSuccess(clientResponse
  -%3E 
@@ -1201,20 +1201,29 @@
 e: %22 + c
-_res
+lientResponse
 .statusC
@@ -1238,38 +1238,38 @@
             
-req.r
+serverR
 esponse
-()
 .setChunked(
@@ -1283,38 +1283,38 @@
             
-req.r
+serverR
 esponse
-()
 .setStatusCo
@@ -1317,20 +1317,29 @@
 usCode(c
-_res
+lientResponse
 .statusC
@@ -1358,30 +1358,30 @@
         
-req.r
+serverR
 esponse
-()
 .headers
@@ -1395,267 +1395,80 @@
 ll(c
-_res.headers());%0A            c_res.handler(data -%3E %7B%0A              System.out.println(%22Proxying response body: %22 + data.toString(%22ISO-8859-1%22));%0A              req.response().write(data);%0A            %7D);%0A            c_res.endHandler((v) -%3E req.response().end()
+lientResponse.headers());%0A            serverResponse.send(clientResponse
 );%0A 
@@ -1558,38 +1558,38 @@
             
-req.r
+serverR
 esponse
-()
 .setStatusCo
@@ -1719,22 +1719,22 @@
     
-req.r
+serverR
 esponse
-()
 .set

Update proxy example