78 port = s.getLocalPort(); |
80 port = s.getLocalPort(); |
79 } |
81 } |
80 |
82 |
81 public void run () { |
83 public void run () { |
82 try { |
84 try { |
|
85 System.out.println("Server started"); |
83 Socket s1 = s.accept (); |
86 Socket s1 = s.accept (); |
84 is = s1.getInputStream (); |
87 is = s1.getInputStream (); |
85 os = s1.getOutputStream (); |
88 os = s1.getOutputStream (); |
86 is.read (); |
89 is.read (); |
87 os.write (reply1.getBytes()); |
90 os.write (reply1.getBytes()); |
|
91 System.out.println("First response sent"); |
88 Thread.sleep (2000); |
92 Thread.sleep (2000); |
89 s1.close (); |
93 s1.close (); |
|
94 System.out.println("First connection closed"); |
90 |
95 |
91 s1 = s.accept (); |
96 s1 = s.accept (); |
92 is = s1.getInputStream (); |
97 is = s1.getInputStream (); |
93 os = s1.getOutputStream (); |
98 os = s1.getOutputStream (); |
94 is.read (); |
99 // is.read (); |
95 // need to get the cnonce out of the response |
100 // need to get the cnonce out of the response |
96 MessageHeader header = new MessageHeader (is); |
101 MessageHeader header = new MessageHeader (is); |
97 String raw = header.findValue ("Proxy-Authorization"); |
102 String raw = header.findValue ("Proxy-Authorization"); |
98 HeaderParser parser = new HeaderParser (raw); |
103 HeaderParser parser = new HeaderParser (raw); |
99 String cnonce = parser.findValue ("cnonce"); |
104 String cnonce = parser.findValue ("cnonce"); |
113 String reply = reply2 + getAuthorization ( |
118 String reply = reply2 + getAuthorization ( |
114 realm, false, uri, "GET", cnonce, |
119 realm, false, uri, "GET", cnonce, |
115 cnstring, passwd, username |
120 cnstring, passwd, username |
116 ) +"\r\n"; |
121 ) +"\r\n"; |
117 os.write (reply.getBytes()); |
122 os.write (reply.getBytes()); |
|
123 System.out.println("Second response sent"); |
118 Thread.sleep (2000); |
124 Thread.sleep (2000); |
119 s1.close (); |
125 s1.close (); |
|
126 System.out.println("Second connection closed"); |
120 } |
127 } |
121 catch (Exception e) { |
128 catch (Exception e) { |
122 System.out.println (e); |
129 System.out.println (e); |
123 e.printStackTrace(); |
130 e.printStackTrace(); |
|
131 } finally { |
|
132 System.out.println("Server finished"); |
124 } |
133 } |
125 } |
134 } |
126 |
135 |
127 private String getAuthorization (String realm, boolean isRequest, String uri, String method, String cnonce, String cnstring, char[] password, String username) { |
136 private String getAuthorization (String realm, boolean isRequest, String uri, String method, String cnonce, String cnstring, char[] password, String username) { |
128 String response; |
137 String response; |
223 int port, n =0; |
232 int port, n =0; |
224 byte b[] = new byte[nSize]; |
233 byte b[] = new byte[nSize]; |
225 DigestServer server; |
234 DigestServer server; |
226 ServerSocket sock; |
235 ServerSocket sock; |
227 |
236 |
|
237 InetAddress address = InetAddress.getLoopbackAddress(); |
|
238 InetAddress resolved = InetAddress.getByName(address.getHostName()); |
|
239 System.out.println("Lookup: " |
|
240 + address + " -> \"" + address.getHostName() + "\" -> " |
|
241 + resolved); |
|
242 String proxyHost = address.equals(resolved) |
|
243 ? address.getHostName() |
|
244 : address.getHostAddress(); |
228 try { |
245 try { |
229 sock = new ServerSocket (0); |
246 sock = new ServerSocket(); |
|
247 sock.bind(new InetSocketAddress(address, 0)); |
230 port = sock.getLocalPort (); |
248 port = sock.getLocalPort (); |
231 } |
249 } |
232 catch (Exception e) { |
250 catch (Exception e) { |
233 System.out.println ("Exception: " + e); |
251 System.out.println ("Exception: " + e); |
234 return; |
252 return; |
236 |
254 |
237 server = new DigestServer(sock); |
255 server = new DigestServer(sock); |
238 server.start (); |
256 server.start (); |
239 |
257 |
240 try { |
258 try { |
241 |
|
242 Authenticator.setDefault (new MyAuthenticator ()); |
259 Authenticator.setDefault (new MyAuthenticator ()); |
243 SocketAddress addr = new InetSocketAddress (InetAddress.getLoopbackAddress(), port); |
260 SocketAddress addr = InetSocketAddress.createUnresolved(proxyHost, port); |
244 Proxy proxy = new Proxy (Proxy.Type.HTTP, addr); |
261 Proxy proxy = new Proxy (Proxy.Type.HTTP, addr); |
245 String s = "http://www.ibm.com"; |
262 String s = "http://www.ibm.com"; |
246 URL url = new URL(s); |
263 URL url = new URL(s); |
|
264 System.out.println("opening connection through proxy: " + addr); |
247 java.net.URLConnection conURL = url.openConnection(proxy); |
265 java.net.URLConnection conURL = url.openConnection(proxy); |
248 |
266 |
249 InputStream in = conURL.getInputStream(); |
267 InputStream in = conURL.getInputStream(); |
250 int c; |
268 int c; |
251 while ((c = in.read ()) != -1) { |
269 while ((c = in.read ()) != -1) { |