public override void Write(byte[] d, int s, int len)
        {
            if (init)
            {
                startid = sftp.seq;
                _ackid  = sftp.seq;
                init    = false;
            }

            try {
                int _len = len;
                while (_len > 0)
                {
                    int sent = sftp.sendWRITE(handle, _offset[0], d, s, _len);
                    _offset[0] += sent;
                    s          += sent;
                    _len       -= sent;
                    if ((sftp.seq - 1) == startid ||
                        StreamAux.available(sftp.io.ins) >= 1024)
                    {
                        while (StreamAux.available(sftp.io.ins) > 0)
                        {
                            if (sftp.checkStatus(ackid, header))
                            {
                                _ackid = ackid[0];
                                if (startid > _ackid || _ackid > sftp.seq - 1)
                                {
                                    throw new SftpException(ChannelSftp.SSH_FX_FAILURE, "");
                                }
                                ackcount++;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                if (monitor != null && !monitor.count(len))
                {
                    close();
                    throw new IOException("canceled");
                }
            }
            catch (IOException e) {
                throw e;
            }
            catch (Exception e) {
                throw new IOException(e.ToString());
            }
        }
Exemplo n.º 2
0
        public void connect(ISocketFactory socket_factory, String host, int port, int timeout)
        {
            try {
                if (socket_factory == null)
                {
                    socket = TcpSocketCreator.CreateSocket(proxy_host, proxy_port, timeout);
                    ins    = socket.getInputStream();
                    outs   = socket.getOutputStream();
                }
                else
                {
                    socket = socket_factory.createSocket(proxy_host, proxy_port);
                    ins    = socket_factory.getInputStream(socket);
                    outs   = socket_factory.getOutputStream(socket);
                }
                if (timeout > 0)
                {
                    socket.setSoTimeout(timeout);
                }
                socket.setTcpNoDelay(true);

                StreamAux.write(outs, StringAux.getBytesUTF8("CONNECT " + host + ":" + port + " HTTP/1.0\r\n"));

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

                StreamAux.write(outs, StringAux.getBytesUTF8("\r\n"));
                StreamAux.flush(outs);

                int foo = 0;

                StringBuilder sb = new StringBuilder();
                while (foo >= 0)
                {
                    foo = ins.ReadByte();
                    if (foo != 13)
                    {
                        sb.Append((char)foo);
                        continue;
                    }
                    foo = ins.ReadByte();
                    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   = int.Parse(response.Substring(foo + 1, bar - (foo + 1)));
                    reason = response.Substring(bar + 1);
                }
                catch                //(JException 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.ReadByte();
                        if (foo != 13)
                        {
                            count++;
                            continue;
                        }
                        foo = ins.ReadByte();
                        if (foo != 10)
                        {
                            continue;
                        }
                        break;
                    }
                    if (foo < 0)
                    {
                        throw new System.IO.IOException();
                    }
                    if (count == 0)
                    {
                        break;
                    }
                }
            }
            catch (RuntimeException e) {
                try {
                    if (socket != null)
                    {
                        socket.close();
                    }
                }
                catch                //(JException eee)
                {
                }
                String message = "ProxyHTTP: " + e.ToString();
                throw e;
            }
        }