public Result authenticate(HttpExchange t) {
    Headers rmap = t.getRequestHeaders();
    /*
     * look for auth token
     */
    String auth = rmap.getFirst("Authorization");
    if (auth == null) {
      Headers map = t.getResponseHeaders();
      map.set("WWW-Authenticate", "Basic realm=" + "\"" + realm + "\"");
      return new Authenticator.Retry(401);
    }
    int sp = auth.indexOf(' ');
    if (sp == -1 || !auth.substring(0, sp).equals("Basic")) {
      return new Authenticator.Failure(401);
    }
    byte[] b = Base64.getDecoder().decode(auth.substring(sp + 1));
    String userpass = new String(b);
    int colon = userpass.indexOf(':');
    String uname = userpass.substring(0, colon);
    String pass = userpass.substring(colon + 1);

    if (checkCredentials(uname, pass)) {
      return new Authenticator.Success(new HttpPrincipal(uname, realm));
    } else {
      /* reject the request again with 401 */

      Headers map = t.getResponseHeaders();
      map.set("WWW-Authenticate", "Basic realm=" + "\"" + realm + "\"");
      return new Authenticator.Failure(401);
    }
  }
 public void sendResponseHeaders(int rCode, long contentLen) throws IOException {
   if (sentHeaders) {
     throw new IOException("headers already sent");
   }
   this.rcode = rCode;
   String statusLine = "HTTP/1.1 " + rCode + Code.msg(rCode) + "\r\n";
   OutputStream tmpout = new BufferedOutputStream(ros);
   PlaceholderOutputStream o = getPlaceholderResponseBody();
   tmpout.write(bytes(statusLine, 0), 0, statusLine.length());
   boolean noContentToSend = false; // assume there is content
   rspHdrs.set("Date", df.format(new Date()));
   if (contentLen == 0) {
     if (http10) {
       o.setWrappedStream(new UndefLengthOutputStream(this, ros));
       close = true;
     } else {
       rspHdrs.set("Transfer-encoding", "chunked");
       o.setWrappedStream(new ChunkedOutputStream(this, ros));
     }
   } else {
     if (contentLen == -1) {
       noContentToSend = true;
       contentLen = 0;
     }
     /* content len might already be set, eg to implement HEAD resp */
     if (rspHdrs.getFirst("Content-length") == null) {
       rspHdrs.set("Content-length", Long.toString(contentLen));
     }
     o.setWrappedStream(new FixedLengthOutputStream(this, ros, contentLen));
   }
   write(rspHdrs, tmpout);
   this.rspContentLen = contentLen;
   tmpout.flush();
   tmpout = null;
   sentHeaders = true;
   if (noContentToSend) {
     WriteFinishedEvent e = new WriteFinishedEvent(this);
     server.addEvent(e);
     closed = true;
   }
   server.logReply(rCode, req.requestLine(), null);
 }