ProxyHTTP.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00032 import java.io.*;
00033 import java.net.*;
00034 
00035 
00049 public class ProxyHTTP implements Proxy{
00050   private static int DEFAULTPORT=80;
00051   private String proxy_host;
00052   private int proxy_port;
00053   private InputStream in;
00054   private OutputStream out;
00055   private Socket socket;
00056 
00057   private String user;
00058   private String passwd;
00059 
00065   public ProxyHTTP(String proxy_host){
00066     int port=DEFAULTPORT;
00067     String host=proxy_host;
00068     // do we really need to call  indexOf(':')  three times? -- P.E.
00069     if(proxy_host.indexOf(':')!=-1){
00070       try{
00071     host=proxy_host.substring(0, proxy_host.indexOf(':'));
00072     port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
00073       }
00074       catch(Exception e){
00075       }
00076     }
00077     this.proxy_host=host;
00078     this.proxy_port=port;
00079   }
00080 
00086   public ProxyHTTP(String proxy_host, int proxy_port){
00087     this.proxy_host=proxy_host;
00088     this.proxy_port=proxy_port;
00089   }
00090 
00108   public void setUserPasswd(String user, String passwd){
00109     this.user=user;
00110     this.passwd=passwd;
00111   }
00112 
00113   // javadoc will be copied from interface. -- P.E.
00114   public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws JSchException{
00115     try{
00116       if(socket_factory==null){
00117         socket=Util.createSocket(proxy_host, proxy_port, timeout);    
00118         in=socket.getInputStream();
00119         out=socket.getOutputStream();
00120       }
00121       else{
00122         socket=socket_factory.createSocket(proxy_host, proxy_port);
00123         in=socket_factory.getInputStream(socket);
00124         out=socket_factory.getOutputStream(socket);
00125       }
00126       if(timeout>0){
00127         socket.setSoTimeout(timeout);
00128       }
00129       socket.setTcpNoDelay(true);
00130 
00131       out.write(Util.str2byte("CONNECT "+host+":"+port+" HTTP/1.0\r\n"));
00132 
00133       if(user!=null && passwd!=null){
00134     byte[] code=Util.str2byte(user+":"+passwd);
00135     code=Util.toBase64(code, 0, code.length);
00136     out.write(Util.str2byte("Proxy-Authorization: Basic "));
00137     out.write(code);
00138     out.write(Util.str2byte("\r\n"));
00139       }
00140 
00141       out.write(Util.str2byte("\r\n"));
00142       out.flush();
00143 
00144       int foo=0;
00145 
00146       StringBuffer sb=new StringBuffer();
00147       while(foo>=0){
00148         foo=in.read(); if(foo!=13){sb.append((char)foo);  continue;}
00149         foo=in.read(); if(foo!=10){continue;}
00150         break;
00151       }
00152       if(foo<0){
00153         throw new IOException();
00154       }
00155 
00156       String response=sb.toString(); 
00157       String reason="Unknow reason";
00158       int code=-1;
00159       try{
00160         foo=response.indexOf(' ');
00161         int bar=response.indexOf(' ', foo+1);
00162         code=Integer.parseInt(response.substring(foo+1, bar));
00163         reason=response.substring(bar+1);
00164       }
00165       catch(Exception e){
00166       }
00167       if(code!=200){
00168         throw new IOException("proxy error: "+reason);
00169       }
00170 
00171       /*
00172       while(foo>=0){
00173         foo=in.read(); if(foo!=13) continue;
00174         foo=in.read(); if(foo!=10) continue;
00175         foo=in.read(); if(foo!=13) continue;      
00176         foo=in.read(); if(foo!=10) continue;
00177         break;
00178       }
00179       */
00180 
00181       int count=0;
00182       while(true){
00183         count=0;
00184         while(foo>=0){
00185           foo=in.read(); if(foo!=13){count++;  continue;}
00186           foo=in.read(); if(foo!=10){continue;}
00187           break;
00188         }
00189         if(foo<0){
00190           throw new IOException();
00191         }
00192         if(count==0)break;
00193       }
00194     }
00195     catch(RuntimeException e){
00196       throw e;
00197     }
00198     catch(Exception e){
00199       try{ if(socket!=null)socket.close(); }
00200       catch(Exception eee){
00201       }
00202       String message="ProxyHTTP: "+e.toString();
00203       if(e instanceof Throwable)
00204         throw new JSchException(message, (Throwable)e);
00205       throw new JSchException(message);
00206     }
00207   }
00208 
00209   // javadoc will be copied from interface. -- P.E.
00210   public InputStream getInputStream(){ return in; }
00211 
00212   // javadoc will be copied from interface. -- P.E.
00213   public OutputStream getOutputStream(){ return out; }
00214 
00215   // javadoc will be copied from interface. -- P.E.
00216   public Socket getSocket(){ return socket; }
00217 
00218   // javadoc will be copied from interface. -- P.E.
00219   public void close(){
00220     try{
00221       if(in!=null)in.close();
00222       if(out!=null)out.close();
00223       if(socket!=null)socket.close();
00224     }
00225     catch(Exception e){
00226     }
00227     in=null;
00228     out=null;
00229     socket=null;
00230   }
00231 
00232 
00236   public static int getDefaultPort(){
00237     return DEFAULTPORT;
00238   }
00239 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1