ProxyHTTP.cs :  » Network-Clients » SharpSSH » Tamir » SharpSsh » jsch » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » SharpSSH 
SharpSSH » Tamir » SharpSsh » jsch » ProxyHTTP.cs
using Tamir.SharpSsh.java;
using Tamir.SharpSsh.java.io;
using Tamir.SharpSsh.java.util;
using Tamir.SharpSsh.java.net;
using Tamir.SharpSsh.java.lang;
using Tamir.SharpSsh.jsch;
using System.IO;

namespace Tamir.SharpSsh.jsch{
  public class ProxyHTTP : Proxy
  {
    private static int DEFAULTPORT=80;
    private String proxy_host;
    private int proxy_port;
    private JStream ins;
    private JStream outs;
    private Socket socket;

    private String user;
    private String passwd;

    public ProxyHTTP(String proxy_host)
    {
      int port=DEFAULTPORT;
      String host=proxy_host;
      if(proxy_host.indexOf(':')!=-1)
      {
        try
        {
          host=proxy_host.substring(0, proxy_host.indexOf(':'));
          port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
        }
        catch(Exception e)
        {
        }
      }
      this.proxy_host=host;
      this.proxy_port=port;
    }
    public ProxyHTTP(String proxy_host, int proxy_port)
    {
      this.proxy_host=proxy_host;
      this.proxy_port=proxy_port;
    }
    public void setUserPasswd(String user, String passwd)
    {
      this.user=user;
      this.passwd=passwd;
    }
    public void connect(SocketFactory socket_factory, String host, int port, int timeout)
    {
      try
      {
        if(socket_factory==null)
        {
          socket=Util.createSocket(proxy_host, proxy_port, timeout);    
          ins= new JStream(socket.getInputStream());
          outs=new JStream(socket.getOutputStream());
        }
        else
        {
          socket=socket_factory.createSocket(proxy_host, proxy_port);
          ins=new JStream(socket_factory.getInputStream(socket));
          outs=new JStream(socket_factory.getOutputStream(socket));
        }
        if(timeout>0)
        {
          socket.setSoTimeout(timeout);
        }
        socket.setTcpNoDelay(true);

        outs.write(new String("CONNECT "+host+":"+port+" HTTP/1.0\r\n").getBytes());

        if(user!=null && passwd!=null)
        {
          byte[] _code=(user+":"+passwd).getBytes();
          _code=Util.toBase64(_code, 0, _code.Length);
          outs.write(new String("Proxy-Authorization: Basic ").getBytes());
          outs.write(_code);
          outs.write(new String("\r\n").getBytes());
        }

        outs.write(new String("\r\n").getBytes());
        outs.flush();

        int foo=0;

        StringBuffer sb=new StringBuffer();
        while(foo>=0)
        {
          foo=ins.read(); if(foo!=13){sb.append((char)foo);  continue;}
          foo=ins.read(); if(foo!=10){continue;}
          break;
        }
        if(foo<0)
        {
          throw new System.IO.IOException(); 
        }

        String response=sb.toString(); 
        String reason="Unknow reason";
        int code=-1;
        try
        {
          foo=response.indexOf(' ');
          int bar=response.indexOf(' ', foo+1);
          code=Integer.parseInt(response.substring(foo+1, bar));
          reason=response.substring(bar+1);
        }
        catch(Exception e)
        {
        }
        if(code!=200)
        {
          throw new System.IO.IOException("proxy error: "+reason);
        }

        /*
        while(foo>=0){
          foo=in.read(); if(foo!=13) continue;
          foo=in.read(); if(foo!=10) continue;
          foo=in.read(); if(foo!=13) continue;      
          foo=in.read(); if(foo!=10) continue;
          break;
        }
        */

        int count=0;
        while(true)
        {
          count=0;
          while(foo>=0)
          {
            foo=ins.read(); if(foo!=13){count++;  continue;}
            foo=ins.read(); if(foo!=10){continue;}
            break;
          }
          if(foo<0)
          {
            throw new System.IO.IOException();
          }
          if(count==0)break;
        }
      }
      catch(RuntimeException e)
      {
        throw e;
      }
      catch(Exception e)
      {
        try{ if(socket!=null)socket.close(); }
        catch(Exception eee)
        {
        }
        String message="ProxyHTTP: "+e.toString();
        throw e;
      }
    }
    public Stream getInputStream(){ return ins.s; }
    public Stream getOutputStream(){ return outs.s; }
    public Socket getSocket(){ return socket; }
    public void close()
    {
      try
      {
        if(ins!=null)ins.close();
        if(outs!=null)outs.close();
        if(socket!=null)socket.close();
      }
      catch(Exception e)
      {
      }
      ins=null;
      outs=null;
      socket=null;
    }
    public static int getDefaultPort()
    {
      return DEFAULTPORT;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.