/*
 * Copyright 2018 Google LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.cloud.tools.jib.http;

import com.google.common.io.Resources;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

/** Simple local web server for testing. */
public class TestWebServer implements Closeable {

  private final boolean https;
  private final ServerSocket serverSocket;
  private final ExecutorService executorService = Executors.newSingleThreadExecutor();
  private final Semaphore threadStarted = new Semaphore(0);
  private final StringBuffer inputRead = new StringBuffer();

  public TestWebServer(boolean https)
      throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException {
    this.https = https;
    serverSocket = createServerSocket(https);
    ignoreReturn(executorService.submit(this::serve200));
    threadStarted.acquire();
  }

  public String getEndpoint() {
    String host = serverSocket.getInetAddress().getHostAddress();
    return (https ? "https" : "http") + "://" + host + ":" + serverSocket.getLocalPort();
  }

  @Override
  public void close() throws IOException {
    serverSocket.close();
    executorService.shutdown();
  }

  private ServerSocket createServerSocket(boolean https)
      throws IOException, GeneralSecurityException, URISyntaxException {
    if (https) {
      KeyStore keyStore = KeyStore.getInstance("JKS");
      // generated with: keytool -genkey -keyalg RSA -keystore ./TestWebServer-keystore
      Path keyStoreFile = Paths.get(Resources.getResource("core/TestWebServer-keystore").toURI());
      try (InputStream in = Files.newInputStream(keyStoreFile)) {
        keyStore.load(in, "password".toCharArray());
      }

      KeyManagerFactory keyManagerFactory =
          KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
      keyManagerFactory.init(keyStore, "password".toCharArray());

      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
      return sslContext.getServerSocketFactory().createServerSocket(0);
    } else {
      return new ServerSocket(0);
    }
  }

  private Void serve200() throws IOException {
    threadStarted.release();
    try (Socket socket = serverSocket.accept()) {
      String response = "HTTP/1.1 200 OK\nContent-Length:12\n\nHello World!";
      socket.getOutputStream().write(response.getBytes(StandardCharsets.UTF_8));
      socket.getOutputStream().flush();

      InputStream in = socket.getInputStream();
      for (int ch = in.read(); ch != -1; ch = in.read()) {
        inputRead.append((char) ch);
      }
    }
    return null;
  }

  private void ignoreReturn(Future<Void> future) {
    // do nothing; to make Error Prone happy
  }

  public String getInputRead() {
    return inputRead.toString();
  }
}

@@ -667,24 +667,55 @@
 .Resources;%0A
+import java.io.BufferedReader;%0A
 import java.
@@ -784,16 +784,50 @@
 Stream;%0A
+import java.io.InputStreamReader;%0A
 import j
@@ -1684,18 +1684,19 @@
 StringBu
-ff
+ild
 er input
@@ -1714,18 +1714,19 @@
 StringBu
-ff
+ild
 er();%0A%0A 
@@ -3424,24 +3424,398 @@
 accept()) %7B%0A
+%0A      InputStream in = socket.getInputStream();%0A      BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));%0A      for (String line = reader.readLine();%0A          line != null && !line.isEmpty(); // An empty line marks the end of an HTTP request.%0A          line = reader.readLine()) %7B%0A        inputRead.append(line + %22%5Cn%22);%0A      %7D%0A%0A
       String
@@ -4004,161 +4004,8 @@
 h();
-%0A%0A      InputStream in = socket.getInputStream();%0A      for (int ch = in.read(); ch != -1; ch = in.read()) %7B%0A        inputRead.append((char) ch);%0A      %7D
 %0A   

Write after reading input (#1624)