// Path: jflask/src/main/java/net/jflask/sun/WebServer.java
// public class WebServer {
// 
//   private HttpServer srv;
// 
//   private final ExecutorService pool;
// 
//   private InetSocketAddress address;
// 
//   private final Map<String, RequestHandler> handlers = new Hashtable<>();
// 
//   public WebServer(int port, ExecutorService pool) {
//     if (pool == null)
//       pool = Executors.newCachedThreadPool();
//     this.pool = pool;
//     this.address = new InetSocketAddress(port);
//   }
// 
//   public WebServer addHandler(String path, RequestHandler handler) {
//     RequestHandler old = handlers.get(path);
//     if (old != null) {
//       if (old.getApp() != handler.getApp())
//         throw new RuntimeException(String.format(
//             "Path %s already used by app %s",
//             path,
//             old.getApp()));
//       else
//         throw new RuntimeException("Path already used: " + path);
//     }
// 
//     handlers.put(path, handler);
//     if (srv != null)
//       srv.createContext(path, handler.asHttpHandler());
//     return this;
//   }
// 
//   public void setPort(int port) {
//     this.address = new InetSocketAddress(port);
//   }
// 
//   private void checkNotStarted() {
//     if (srv != null)
//       throw new IllegalStateException("Already started");
//   }
// 
//   /**
//    * Shuts down the web sever.
//    * <p/>
//    * WARNING: with JDK6, HttpServer creates a zombie thread (blocked on a
//    * sleep()). No problem with JDK 1.7.0_40.
//    */
//   public void close() {
//     this.srv.stop(0);
//     pool.shutdownNow();
//   }
// 
//   public int getPort() {
//     return srv.getAddress().getPort();
//   }
// 
//   public void start() throws IOException {
//     this.srv = HttpServer.create(address, 0);
//     this.srv.setExecutor(pool);
//     for (Map.Entry<String, RequestHandler> e : handlers.entrySet()) {
//       String path = e.getKey();
//       if (path.isEmpty())
//         path = "/";
//       srv.createContext(path, e.getValue().asHttpHandler());
//     }
// 
//     this.srv.start();
//   }
// 
//   public boolean isStarted() {
//     return srv != null;
//   }
// }
// 
// Path: jflask/src/main/java/net/jflask/util/IO.java
// public class IO {
// 
//   /**
//    * Reads specified input stream until it reaches EOF.
//    */
//   public static byte[] readFully(InputStream in) throws IOException {
//     ByteArrayOutputStream bout = new ByteArrayOutputStream();
//     pipe(in, bout, false);
//     return bout.toByteArray();
//   }
// 
//   /**
//    * Reads input stream until EOF and writes all read data into output stream,
//    * then closes it.
//    */
//   public static void pipe(InputStream in, OutputStream out, boolean closeOutput)
//       throws IOException {
//     byte[] buf = new byte[1024];
//     while (true) {
//       int len = in.read(buf);
//       if (len >= 0) {
//         out.write(buf, 0, len);
//         out.flush();
//       }
//       else {
//         if (closeOutput)
//           out.close();
//         break;
//       }
//     }
//   }
// 
// }


import java.io.IOException;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.xml.ws.http.HTTPException;
import net.jflask.sun.WebServer;
import net.jflask.util.IO;
package net.jflask.test.util;




/**
 * Minimal HTTP client that mimics the behaviour of a browser, eg. by allowing
 * cookies.
 *
 * @author pcdv
 */
public class SimpleClient {

  private final String rootUrl;

  private CookieManager cookies;

  public SimpleClient(String host, int port) {
    this("http://" + host + ":" + port);
  }

  public SimpleClient(String rootUrl) {
    this.rootUrl = rootUrl;
    this.cookies = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    // WTF: there is no non-static way to bind a CookieHandler to an
    // URLConnection!
    CookieHandler.setDefault(cookies);
  }

// Path: jflask/src/main/java/net/jflask/sun/WebServer.java
// public class WebServer {
// 
//   private HttpServer srv;
// 
//   private final ExecutorService pool;
// 
//   private InetSocketAddress address;
// 
//   private final Map<String, RequestHandler> handlers = new Hashtable<>();
// 
//   public WebServer(int port, ExecutorService pool) {
//     if (pool == null)
//       pool = Executors.newCachedThreadPool();
//     this.pool = pool;
//     this.address = new InetSocketAddress(port);
//   }
// 
//   public WebServer addHandler(String path, RequestHandler handler) {
//     RequestHandler old = handlers.get(path);
//     if (old != null) {
//       if (old.getApp() != handler.getApp())
//         throw new RuntimeException(String.format(
//             "Path %s already used by app %s",
//             path,
//             old.getApp()));
//       else
//         throw new RuntimeException("Path already used: " + path);
//     }
// 
//     handlers.put(path, handler);
//     if (srv != null)
//       srv.createContext(path, handler.asHttpHandler());
//     return this;
//   }
// 
//   public void setPort(int port) {
//     this.address = new InetSocketAddress(port);
//   }
// 
//   private void checkNotStarted() {
//     if (srv != null)
//       throw new IllegalStateException("Already started");
//   }
// 
//   /**
//    * Shuts down the web sever.
//    * <p/>
//    * WARNING: with JDK6, HttpServer creates a zombie thread (blocked on a
//    * sleep()). No problem with JDK 1.7.0_40.
//    */
//   public void close() {
//     this.srv.stop(0);
//     pool.shutdownNow();
//   }
// 
//   public int getPort() {
//     return srv.getAddress().getPort();
//   }
// 
//   public void start() throws IOException {
//     this.srv = HttpServer.create(address, 0);
//     this.srv.setExecutor(pool);
//     for (Map.Entry<String, RequestHandler> e : handlers.entrySet()) {
//       String path = e.getKey();
//       if (path.isEmpty())
//         path = "/";
//       srv.createContext(path, e.getValue().asHttpHandler());
//     }
// 
//     this.srv.start();
//   }
// 
//   public boolean isStarted() {
//     return srv != null;
//   }
// }
// 
// Path: jflask/src/main/java/net/jflask/util/IO.java
// public class IO {
// 
//   /**
//    * Reads specified input stream until it reaches EOF.
//    */
//   public static byte[] readFully(InputStream in) throws IOException {
//     ByteArrayOutputStream bout = new ByteArrayOutputStream();
//     pipe(in, bout, false);
//     return bout.toByteArray();
//   }
// 
//   /**
//    * Reads input stream until EOF and writes all read data into output stream,
//    * then closes it.
//    */
//   public static void pipe(InputStream in, OutputStream out, boolean closeOutput)
//       throws IOException {
//     byte[] buf = new byte[1024];
//     while (true) {
//       int len = in.read(buf);
//       if (len >= 0) {
//         out.write(buf, 0, len);
//         out.flush();
//       }
//       else {
//         if (closeOutput)
//           out.close();
//         break;
//       }
//     }
//   }
// 
// }


// Path: jflask/src/test/java/net/jflask/test/util/SimpleClient.java
import java.io.IOException;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.xml.ws.http.HTTPException;
import net.jflask.sun.WebServer;
import net.jflask.util.IO;

package net.jflask.test.util;




/**
 * Minimal HTTP client that mimics the behaviour of a browser, eg. by allowing
 * cookies.
 *
 * @author pcdv
 */
public class SimpleClient {

  private final String rootUrl;

  private CookieManager cookies;

  public SimpleClient(String host, int port) {
    this("http://" + host + ":" + port);
  }

  public SimpleClient(String rootUrl) {
    this.rootUrl = rootUrl;
    this.cookies = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
    // WTF: there is no non-static way to bind a CookieHandler to an
    // URLConnection!
    CookieHandler.setDefault(cookies);
  }

  public SimpleClient(WebServer ws) {