comparison src/ch/ethz/ssh2/transport/HTTPProxyClientTransportManager.java @ 273:91a31873c42a ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:21:46 -0700
parents
children 071eccdff8ea
comparison
equal deleted inserted replaced
272:ce2f4e397703 273:91a31873c42a
1 package ch.ethz.ssh2.transport;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.LineNumberReader;
7 import java.io.OutputStream;
8 import java.net.InetSocketAddress;
9 import java.net.Socket;
10
11 import ch.ethz.ssh2.HTTPProxyData;
12 import ch.ethz.ssh2.HTTPProxyException;
13 import ch.ethz.ssh2.crypto.Base64;
14 import ch.ethz.ssh2.util.StringEncoder;
15
16 /**
17 * @version $Id: HTTPProxyClientTransportManager.java 155 2014-04-28 12:01:19Z dkocher@sudo.ch $
18 */
19 public class HTTPProxyClientTransportManager extends ClientTransportManager {
20
21 /**
22 * Used to tell the library that the connection shall be established through a proxy server.
23 */
24
25 private HTTPProxyData pd;
26
27 private final Socket sock;
28
29 public HTTPProxyClientTransportManager(final Socket socket, final HTTPProxyData pd) {
30 super(socket);
31 this.sock = socket;
32 this.pd = pd;
33 }
34
35 @Override
36 protected void connect(final String hostname, final int port, final int connectTimeout) throws IOException {
37
38 sock.connect(new InetSocketAddress(pd.proxyHost, pd.proxyPort), connectTimeout);
39
40 // Tell the proxy where we actually want to connect to
41 StringBuilder sb = new StringBuilder();
42
43 sb.append("CONNECT ");
44 sb.append(hostname);
45 sb.append(':');
46 sb.append(port);
47 sb.append(" HTTP/1.0\r\n");
48
49 if((pd.proxyUser != null) && (pd.proxyPass != null)) {
50 String credentials = pd.proxyUser + ":" + pd.proxyPass;
51 char[] encoded = Base64.encode(StringEncoder.GetBytes(credentials));
52 sb.append("Proxy-Authorization: Basic ");
53 sb.append(encoded);
54 sb.append("\r\n");
55 }
56
57 if(pd.requestHeaderLines != null) {
58 for(int i = 0; i < pd.requestHeaderLines.length; i++) {
59 if(pd.requestHeaderLines[i] != null) {
60 sb.append(pd.requestHeaderLines[i]);
61 sb.append("\r\n");
62 }
63 }
64 }
65
66 sb.append("\r\n");
67
68 OutputStream out = sock.getOutputStream();
69
70 out.write(StringEncoder.GetBytes(sb.toString()));
71 out.flush();
72
73 // Parse the HTTP response
74
75 InputStream in = sock.getInputStream();
76
77 final LineNumberReader reader = new LineNumberReader(new InputStreamReader(in));
78 String httpReponse = reader.readLine();
79
80 if(!httpReponse.startsWith("HTTP/")) {
81 throw new IOException("The proxy did not send back a valid HTTP response.");
82 }
83
84 // "HTTP/1.X XYZ X" => 14 characters minimum
85
86 if((httpReponse.length() < 14) || (httpReponse.charAt(8) != ' ') || (httpReponse.charAt(12) != ' ')) {
87 throw new IOException("The proxy did not send back a valid HTTP response.");
88 }
89
90 int errorCode;
91
92 try {
93 errorCode = Integer.parseInt(httpReponse.substring(9, 12));
94 }
95 catch(NumberFormatException ignore) {
96 throw new IOException("The proxy did not send back a valid HTTP response.");
97 }
98
99 if((errorCode < 0) || (errorCode > 999)) {
100 throw new IOException("The proxy did not send back a valid HTTP response.");
101 }
102
103 if(errorCode != 200) {
104 throw new HTTPProxyException(httpReponse.substring(13), errorCode);
105 }
106
107 while(reader.readLine() != null) {
108 // Read until empty line
109 }
110 }
111 }