Example #1
0
    /*
     * Processes the CONNECT request
     */
    private void processRequest(Socket clientSocket) throws Exception {
      MessageHeader mheader = new MessageHeader(clientSocket.getInputStream());
      String statusLine = mheader.getValue(0);

      if (!statusLine.startsWith("CONNECT")) {
        out.println(
            "proxy server: processes only " + "CONNECT method requests, recieved: " + statusLine);
        return;
      }

      // retrieve the host and port info from the status-line
      InetSocketAddress serverAddr = getConnectInfo(statusLine);

      // open socket to the server
      try (Socket serverSocket = new Socket(serverAddr.getAddress(), serverAddr.getPort())) {
        Forwarder clientFW =
            new Forwarder(clientSocket.getInputStream(), serverSocket.getOutputStream());
        Thread clientForwarderThread = new Thread(clientFW, "ClientForwarder");
        clientForwarderThread.start();
        send200(clientSocket);
        Forwarder serverFW =
            new Forwarder(serverSocket.getInputStream(), clientSocket.getOutputStream());
        serverFW.run();
        clientForwarderThread.join();
      }
    }
Example #2
0
  /**
   * Test the http protocol handler with the given authentication schemes in the WWW-Authenticate
   * header.
   */
  static void test(String... schemes) throws IOException {

    // the authentication scheme that the client is expected to choose
    String expected = null;
    for (String s : schemes) {
      if (expected == null) {
        expected = s;
      } else if (s.equals("Digest")) {
        expected = s;
      }
    }

    // server reply
    String reply = authReplyFor(schemes);

    System.out.println("====================================");
    System.out.println("Expect client to choose: " + expected);
    System.out.println(reply);

    try (ServerSocket ss = new ServerSocket(0)) {
      Client.start(ss.getLocalPort());

      // client ---- GET ---> server
      // client <--- 401 ---- server
      try (Socket s = ss.accept()) {
        new MessageHeader().parseHeader(s.getInputStream());
        s.getOutputStream().write(reply.getBytes("US-ASCII"));
      }

      // client ---- GET ---> server
      // client <--- 200 ---- server
      String auth;
      try (Socket s = ss.accept()) {
        MessageHeader mh = new MessageHeader();
        mh.parseHeader(s.getInputStream());
        s.getOutputStream().write(OKAY.getBytes("US-ASCII"));
        auth = mh.findValue("Authorization");
      }

      // check Authorization header
      if (auth == null) throw new RuntimeException("Authorization header not found");
      System.out.println("Server received Authorization header: " + auth);
      String[] values = auth.split(" ");
      if (!values[0].equals(expected)) throw new RuntimeException("Unexpected value");
    }
  }
  /* Iterate through each header line, and then within each line.
   * If multiple entries exist for a particular scheme (unlikely)
   * then the last one will be used. The
   * preferred scheme that we support will be used.
   */
  private void parse() {
    Iterator iter = rsp.multiValueIterator(hdrname);
    while (iter.hasNext()) {
      String raw = (String) iter.next();
      HeaderParser hp = new HeaderParser(raw);
      Iterator keys = hp.keys();
      int i, lastSchemeIndex;
      for (i = 0, lastSchemeIndex = -1; keys.hasNext(); i++) {
        keys.next();
        if (hp.findValue(i) == null) {
            /* found a scheme name */
          if (lastSchemeIndex != -1) {
            HeaderParser hpn = hp.subsequence(lastSchemeIndex, i);
            String scheme = hpn.findKey(0);
            schemes.put(scheme, new SchemeMapValue(hpn, raw));
          }
          lastSchemeIndex = i;
        }
      }
      if (i > lastSchemeIndex) {
        HeaderParser hpn = hp.subsequence(lastSchemeIndex, i);
        String scheme = hpn.findKey(0);
        schemes.put(scheme, new SchemeMapValue(hpn, raw));
      }
    }

    /* choose the best of them */
    SchemeMapValue v;
    if ((v = (SchemeMapValue) schemes.get("digest")) == null) {
      if (!NTLMAuthentication.isSupported()
          || ((v = (SchemeMapValue) schemes.get("ntlm")) == null)) {
        v = (SchemeMapValue) schemes.get("basic");
      }
    }
    if (v != null) {
      preferred = v.parser;
      preferred_r = v.raw;
      ;
    }
  }