~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch/ProxyHTTP.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, with or without
 
5
modification, are permitted provided that the following conditions are met:
 
6
 
 
7
  1. Redistributions of source code must retain the above copyright notice,
 
8
     this list of conditions and the following disclaimer.
 
9
 
 
10
  2. Redistributions in binary form must reproduce the above copyright 
 
11
     notice, this list of conditions and the following disclaimer in 
 
12
     the documentation and/or other materials provided with the distribution.
 
13
 
 
14
  3. The names of the authors may not be used to endorse or promote products
 
15
     derived from this software without specific prior written permission.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
 
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
This code is based on jsch (http://www.jcraft.com/jsch).
 
29
All credit should go to the authors of jsch.
 
30
*/
 
31
 
 
32
using System;
 
33
using System.IO;
 
34
using System.Net.Sockets;
 
35
using System.Text;
 
36
using NSch;
 
37
using Sharpen;
 
38
 
 
39
namespace NSch
 
40
{
 
41
        public class ProxyHTTP : Proxy
 
42
        {
 
43
                private static int DEFAULTPORT = 80;
 
44
 
 
45
                private string proxy_host;
 
46
 
 
47
                private int proxy_port;
 
48
 
 
49
                private InputStream @in;
 
50
 
 
51
                private OutputStream @out;
 
52
 
 
53
                private Socket socket;
 
54
 
 
55
                private string user;
 
56
 
 
57
                private string passwd;
 
58
 
 
59
                public ProxyHTTP(string proxy_host)
 
60
                {
 
61
                        int port = DEFAULTPORT;
 
62
                        string host = proxy_host;
 
63
                        if (proxy_host.IndexOf(':') != -1)
 
64
                        {
 
65
                                try
 
66
                                {
 
67
                                        host = Sharpen.Runtime.Substring(proxy_host, 0, proxy_host.IndexOf(':'));
 
68
                                        port = System.Convert.ToInt32(Sharpen.Runtime.Substring(proxy_host, proxy_host.IndexOf
 
69
                                                (':') + 1));
 
70
                                }
 
71
                                catch (Exception)
 
72
                                {
 
73
                                }
 
74
                        }
 
75
                        this.proxy_host = host;
 
76
                        this.proxy_port = port;
 
77
                }
 
78
 
 
79
                public ProxyHTTP(string proxy_host, int proxy_port)
 
80
                {
 
81
                        this.proxy_host = proxy_host;
 
82
                        this.proxy_port = proxy_port;
 
83
                }
 
84
 
 
85
                public virtual void SetUserPasswd(string user, string passwd)
 
86
                {
 
87
                        this.user = user;
 
88
                        this.passwd = passwd;
 
89
                }
 
90
 
 
91
                /// <exception cref="NSch.JSchException"></exception>
 
92
                public virtual void Connect(SocketFactory socket_factory, string host, int port, 
 
93
                        int timeout)
 
94
                {
 
95
                        try
 
96
                        {
 
97
                                if (socket_factory == null)
 
98
                                {
 
99
                                        socket = Util.CreateSocket(proxy_host, proxy_port, timeout);
 
100
                                        @in = socket.GetInputStream();
 
101
                                        @out = socket.GetOutputStream();
 
102
                                }
 
103
                                else
 
104
                                {
 
105
                                        socket = socket_factory.CreateSocket(proxy_host, proxy_port);
 
106
                                        @in = socket_factory.GetInputStream(socket);
 
107
                                        @out = socket_factory.GetOutputStream(socket);
 
108
                                }
 
109
                                if (timeout > 0)
 
110
                                {
 
111
                                        socket.ReceiveTimeout = timeout;
 
112
                                }
 
113
                                socket.NoDelay = true;
 
114
                                @out.Write(Util.Str2byte("CONNECT " + host + ":" + port + " HTTP/1.0\r\n"));
 
115
                                if (user != null && passwd != null)
 
116
                                {
 
117
                                        byte[] code = Util.Str2byte(user + ":" + passwd);
 
118
                                        code = Util.ToBase64(code, 0, code.Length);
 
119
                                        @out.Write(Util.Str2byte("Proxy-Authorization: Basic "));
 
120
                                        @out.Write(code);
 
121
                                        @out.Write(Util.Str2byte("\r\n"));
 
122
                                }
 
123
                                @out.Write(Util.Str2byte("\r\n"));
 
124
                                @out.Flush();
 
125
                                int foo = 0;
 
126
                                StringBuilder sb = new StringBuilder();
 
127
                                while (foo >= 0)
 
128
                                {
 
129
                                        foo = @in.Read();
 
130
                                        if (foo != 13)
 
131
                                        {
 
132
                                                sb.Append((char)foo);
 
133
                                                continue;
 
134
                                        }
 
135
                                        foo = @in.Read();
 
136
                                        if (foo != 10)
 
137
                                        {
 
138
                                                continue;
 
139
                                        }
 
140
                                        break;
 
141
                                }
 
142
                                if (foo < 0)
 
143
                                {
 
144
                                        throw new IOException();
 
145
                                }
 
146
                                string response = sb.ToString();
 
147
                                string reason = "Unknow reason";
 
148
                                int code_1 = -1;
 
149
                                try
 
150
                                {
 
151
                                        foo = response.IndexOf(' ');
 
152
                                        int bar = response.IndexOf(' ', foo + 1);
 
153
                                        code_1 = System.Convert.ToInt32(Sharpen.Runtime.Substring(response, foo + 1, bar)
 
154
                                                );
 
155
                                        reason = Sharpen.Runtime.Substring(response, bar + 1);
 
156
                                }
 
157
                                catch (Exception)
 
158
                                {
 
159
                                }
 
160
                                if (code_1 != 200)
 
161
                                {
 
162
                                        throw new IOException("proxy error: " + reason);
 
163
                                }
 
164
                                int count = 0;
 
165
                                while (true)
 
166
                                {
 
167
                                        count = 0;
 
168
                                        while (foo >= 0)
 
169
                                        {
 
170
                                                foo = @in.Read();
 
171
                                                if (foo != 13)
 
172
                                                {
 
173
                                                        count++;
 
174
                                                        continue;
 
175
                                                }
 
176
                                                foo = @in.Read();
 
177
                                                if (foo != 10)
 
178
                                                {
 
179
                                                        continue;
 
180
                                                }
 
181
                                                break;
 
182
                                        }
 
183
                                        if (foo < 0)
 
184
                                        {
 
185
                                                throw new IOException();
 
186
                                        }
 
187
                                        if (count == 0)
 
188
                                        {
 
189
                                                break;
 
190
                                        }
 
191
                                }
 
192
                        }
 
193
                        catch (RuntimeException e)
 
194
                        {
 
195
                                throw;
 
196
                        }
 
197
                        catch (Exception e)
 
198
                        {
 
199
                                try
 
200
                                {
 
201
                                        if (socket != null)
 
202
                                        {
 
203
                                                socket.Close();
 
204
                                        }
 
205
                                }
 
206
                                catch (Exception)
 
207
                                {
 
208
                                }
 
209
                                string message = "ProxyHTTP: " + e.ToString();
 
210
                                if (e is Exception)
 
211
                                {
 
212
                                        throw new JSchException(message, (Exception)e);
 
213
                                }
 
214
                                throw new JSchException(message);
 
215
                        }
 
216
                }
 
217
 
 
218
                public virtual InputStream GetInputStream()
 
219
                {
 
220
                        return @in;
 
221
                }
 
222
 
 
223
                public virtual OutputStream GetOutputStream()
 
224
                {
 
225
                        return @out;
 
226
                }
 
227
 
 
228
                public virtual Socket GetSocket()
 
229
                {
 
230
                        return socket;
 
231
                }
 
232
 
 
233
                public virtual void Close()
 
234
                {
 
235
                        try
 
236
                        {
 
237
                                if (@in != null)
 
238
                                {
 
239
                                        @in.Close();
 
240
                                }
 
241
                                if (@out != null)
 
242
                                {
 
243
                                        @out.Close();
 
244
                                }
 
245
                                if (socket != null)
 
246
                                {
 
247
                                        socket.Close();
 
248
                                }
 
249
                        }
 
250
                        catch (Exception)
 
251
                        {
 
252
                        }
 
253
                        @in = null;
 
254
                        @out = null;
 
255
                        socket = null;
 
256
                }
 
257
 
 
258
                public static int GetDefaultPort()
 
259
                {
 
260
                        return DEFAULTPORT;
 
261
                }
 
262
        }
 
263
}