ProxyHTTP.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
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
00173
00174
00175
00176
00177
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
00210 public InputStream getInputStream(){ return in; }
00211
00212
00213 public OutputStream getOutputStream(){ return out; }
00214
00215
00216 public Socket getSocket(){ return socket; }
00217
00218
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 }