⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpsurlconnection.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.maverick.ssl.https;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.Vector;

import com.maverick.ssl.SSLException;
import com.maverick.ssl.SSLIOException;
import com.maverick.ssl.SSLSocket;
import com.maverick.http.*;

import org.apache.commons.logging.*;

public class HttpsURLConnection
    extends HttpURLConnection {

  boolean debugging;
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  Vector requestKeys, requestValues;
  Vector headers = new Vector(), headerKeys = new Vector();
  Hashtable headerValues = new Hashtable();
  Socket socket;
  PushbackInputStream input;
  int responseCode = -1;
  String responseMessage;

  /*public static final String
      httpsProxyHostPropertyName = "ssl.https.proxy.host";

  public static final String
      httpsProxyPortPropertyName = "ssl.https.proxy.port";*/

  /* DEBUG */private static Log log = LogFactory.getLog(HttpsURLConnection.class);

  public static final String
      httpProxyHostProperty = "com.maverick.ssl.https.HTTPProxyHostname";
  public static final String
      httpProxyPortProperty = "com.maverick.ssl.https.HTTPProxyPort";
  public static final String
      httpProxyUsernameProperty = "com.maverick.ssl.https.HTTPProxyUsername";
  public static final String
      httpProxyPasswordProperty = "com.maverick.ssl.https.HTTPProxyPassword";
  public static final String
      httpProxySecureProperty = "com.maverick.ssl.https.HTTPProxySecure";
  public static final String
      httpProxyNonProxyHostsProperty = "com.maverick.ssl.https.HTTPProxyNonProxyHosts";

  static Vector defaultRequestKeys = new Vector(),
      defaultRequestValues = new Vector();

  static {
    defaultRequestKeys.addElement("User-agent");
    defaultRequestValues.addElement( HttpClient.USER_AGENT);
  }

  public HttpsURLConnection(URL url) {
    super(url);
    synchronized (defaultRequestKeys) {
      requestKeys = (Vector) defaultRequestKeys.clone();
      requestValues = (Vector) defaultRequestValues.clone();
    }
  }

  public void addRequestProperty(String name, String value) {
    setRequestProperty(name, value);
  }

  public static void setDefaultRequestProperty(String key, String value) {
    synchronized (defaultRequestKeys) {
      int i = 0;
      while ( (i < defaultRequestKeys.size()) &&
             ! (key.equalsIgnoreCase
                ( (String) defaultRequestKeys.elementAt(i)))) {
        ++i;
      }
      if (i < defaultRequestKeys.size()) {
        defaultRequestValues.removeElementAt(i);
        defaultRequestKeys.removeElementAt(i);
      }
      if (value != null) {
        defaultRequestValues.addElement(value);
        defaultRequestKeys.addElement(key);
      }
    }
  }

  public static String getDefaultRequestProperty(String key) {
    synchronized (defaultRequestKeys) {
      int i = 0;
      while ( (i < defaultRequestKeys.size()) &&
             ! (key.equalsIgnoreCase
                ( (String) defaultRequestKeys.elementAt(i)))) {
        ++i;
      }
      if (i < defaultRequestKeys.size()) {
        return (String) defaultRequestValues.elementAt(i);
      }
      else {
        return null;
      }
    }
  }

  public synchronized void setRequestProperty(String key, String value) {
    if (connected) {
      throw new IllegalStateException("Already connected");
    }
    int i = 0;
    while ( (i < requestKeys.size()) &&
           ! (key.equalsIgnoreCase( (String) requestKeys.elementAt(i)))) {
      ++i;
    }
    if (i < requestKeys.size()) {
      requestValues.removeElementAt(i);
      requestKeys.removeElementAt(i);
    }
    if (value != null) {
      requestValues.addElement(value);
      requestKeys.addElement(key);
    }
  }

  public String getRequestProperty(String key) {
    int i = 0;
    while ( (i < requestKeys.size()) &&
           ! (key.equalsIgnoreCase( (String) requestKeys.elementAt(i)))) {
      ++i;
    }
    if (i < requestKeys.size()) {
      return (String) requestValues.elementAt(i);
    }
    else {
      return null;
    }
  }



  public OutputStream getOutputStream() throws IOException {
    if (!doOutput) {
      throw new IOException("Protocol output not configured.");
    }
    if (connected) {
      throw new IOException("Already connected");
    }
    return output;
  }

  private boolean isNonProxiedHost(String host) {
    String nonProxiedHosts = System.getProperty(httpProxyNonProxyHostsProperty);
    if(nonProxiedHosts == null || nonProxiedHosts.equals("")) {
      return false;
    }
    StringTokenizer t = new StringTokenizer(nonProxiedHosts, "|");
    // TODO add wildcard logic like the sun implementation
    while(t.hasMoreTokens()) {
      if(host.equalsIgnoreCase(t.nextToken())) {
        return true;
      }
    }
    return false;
  }

  public synchronized void connect() throws IOException {


    if (!connected) {

      /* DEBUG */log.info("Connecting HTTPS URL to "
      /* DEBUG */ + url.getHost() + ":" + (url.getPort() == -1 ? 443 : url.getPort()));

      String proxyHost = System.getProperty(httpProxyHostProperty);
      if (proxyHost != null && !isNonProxiedHost(url.getHost())) {

        boolean isSecure = Boolean.valueOf(System.getProperty(httpProxySecureProperty, "true")).booleanValue();
        String proxyPort = System.getProperty(httpProxyPortProperty);
        String proxyUsername = System.getProperty(httpProxyUsernameProperty);
        String proxyPassword = System.getProperty(httpProxyPasswordProperty);

        /* DEBUG */log.info("Requires proxy connection through " + (isSecure ? "https" : "http://" ) + proxyHost + ":" + proxyPort);
        /* DEBUG */log.info("Proxy username is " + (proxyUsername == null || proxyUsername.equals("") ? "not set" : proxyUsername));
        if (proxyPort == null) {
          throw new IOException("No proxy port configured.");
        }

        try {
          int port = Integer.parseInt(proxyPort);

          HttpClient client = new HttpClient(proxyHost, port, isSecure);
          HttpMethod method = new ConnectMethod(url.getHost(),
                                                url.getPort() == -1 ? 443 : url.getPort(),
                                                true);

          PasswordCredentials credentials = new PasswordCredentials();
          credentials.setUsername(proxyUsername);
          credentials.setPassword(proxyPassword);

          client.setCredentials(credentials);

          HttpResponse response = client.execute(method);
          socket = response.getConnection().getSocket();
        } catch(HttpException ex) {
          /* DEBUG */log.info("Proxy connection failed: "+ ex.getMessage() + " [" + ex.getStatus() + "]");
          throw new IOException("Proxy connection failed: "+ ex.getMessage() + " [" + ex.getStatus() + "]");
        } catch(UnsupportedAuthenticationException ex) {
          /* DEBUG */log.info("Proxy authentication failed", ex);
          throw new IOException(ex.getMessage());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -