Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/brazil/SSLConnectionHandler.java

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Patch Set: Created April 11, 2014, 1:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 28 matching lines...) Expand all
39 * <dt>auth 39 * <dt>auth
40 * <dd>The value of the proxy-authenticate header (if any) sent to the upstream 40 * <dd>The value of the proxy-authenticate header (if any) sent to the upstream
41 * proxy 41 * proxy
42 * <dt>proxyHost 42 * <dt>proxyHost
43 * <dd>If specified, the name of the upstream proxy 43 * <dd>If specified, the name of the upstream proxy
44 * <dt>proxyPort 44 * <dt>proxyPort
45 * <dd>The upstream proxy port, if a proxyHost is specified (defaults to 80) 45 * <dd>The upstream proxy port, if a proxyHost is specified (defaults to 80)
46 * 46 *
47 * </dl> 47 * </dl>
48 * 48 *
49 * A sample set of configuration parameters illustrating how to use this 49 * A sample set of configuration parameters illustrating how to use this handler
50 * handler follows: 50 * follows:
51 * 51 *
52 * <pre> 52 * <pre>
53 * handler=https 53 * handler=https
54 * https.class=org.adblockplus.brazil.SSLConnectionHandler 54 * https.class=org.adblockplus.brazil.SSLConnectionHandler
55 * </pre> 55 * </pre>
56 * 56 *
57 * See the description under {@link sunlabs.brazil.server.Handler#respond 57 * See the description under {@link sunlabs.brazil.server.Handler#respond
58 * respond} for a more detailed explanation. 58 * respond} for a more detailed explanation.
59 * 59 *
60 * Original source by Jochen Luell, PAW (http://paw-project.sourceforge.net/) 60 * Original source by Jochen Luell, PAW (http://paw-project.sourceforge.net/)
61 */ 61 */
62 62
63 public class SSLConnectionHandler extends BaseRequestHandler 63 public class SSLConnectionHandler extends BaseRequestHandler
64 { 64 {
65 @Override 65 @Override
66 public boolean respond(Request request) throws IOException 66 public boolean respond(final Request request) throws IOException
67 { 67 {
68 if (!request.method.equals("CONNECT")) 68 if (!request.method.equals("CONNECT"))
69 {
69 return false; 70 return false;
71 }
70 72
71 request.log(Server.LOG_LOG, prefix, "SSL connection to " + request.url); 73 request.log(Server.LOG_LOG, this.prefix, "SSL connection to " + request.url) ;
72 74
73 String host = null; 75 String host = null;
74 int port = 0; 76 int port = 0;
75 77
76 Socket serverSocket; 78 Socket serverSocket;
77 try 79 try
78 { 80 {
79 if (proxyHost != null) 81 if (this.proxyHost != null)
80 { 82 {
81 host = proxyHost; 83 host = this.proxyHost;
82 port = proxyPort; 84 port = this.proxyPort;
83 if (auth != null) 85 if (this.auth != null)
84 { 86 {
85 request.headers.add("Proxy-Authorization", auth); 87 request.headers.add("Proxy-Authorization", this.auth);
86 } 88 }
87 } 89 }
88 else 90 else
89 { 91 {
90 int c = request.url.indexOf(':'); 92 final int c = request.url.indexOf(':');
91 host = request.url.substring(0, c); 93 host = request.url.substring(0, c);
92 port = Integer.parseInt(request.url.substring(c + 1)); 94 port = Integer.parseInt(request.url.substring(c + 1));
93 } 95 }
94 96
95 // Connect to server or upstream proxy 97 // Connect to server or upstream proxy
96 serverSocket = new Socket(); 98 serverSocket = new Socket();
97 serverSocket.setKeepAlive(true); 99 serverSocket.setKeepAlive(true);
98 serverSocket.connect(new InetSocketAddress(host, port)); 100 serverSocket.connect(new InetSocketAddress(host, port));
99 } 101 }
100 catch (Exception e) 102 catch (final Exception e)
101 { 103 {
102 request.sendError(500, "SSL connection failure"); 104 request.sendError(500, "SSL connection failure");
103 return true; 105 return true;
104 } 106 }
105 107
106 try 108 try
107 { 109 {
108 if (proxyHost != null) 110 if (this.proxyHost != null)
109 { 111 {
110 // Forward request to upstream proxy 112 // Forward request to upstream proxy
111 OutputStream out = serverSocket.getOutputStream(); 113 final OutputStream out = serverSocket.getOutputStream();
112 out.write((request.method + " " + request.url + " " + request.protocol + "\r\n").getBytes()); 114 out.write((request.method + " " + request.url + " " + request.protocol + "\r\n").getBytes());
113 request.headers.print(out); 115 request.headers.print(out);
114 out.write("\r\n".getBytes()); 116 out.write("\r\n".getBytes());
115 out.flush(); 117 out.flush();
116 } 118 }
117 else 119 else
118 { 120 {
119 // Send response to client 121 // Send response to client
120 OutputStream out = request.sock.getOutputStream(); 122 final OutputStream out = request.sock.getOutputStream();
121 out.write((request.protocol + " 200 Connection established\r\n\r\n").get Bytes()); 123 out.write((request.protocol + " 200 Connection established\r\n\r\n").get Bytes());
122 out.flush(); 124 out.flush();
123 } 125 }
124 126
125 // Start bi-directional data transfer 127 // Start bi-directional data transfer
126 ConnectionHandler client = new ConnectionHandler(request.sock, serverSocke t); 128 final ConnectionHandler client = new ConnectionHandler(request.sock, serve rSocket);
127 ConnectionHandler server = new ConnectionHandler(serverSocket, request.soc k); 129 final ConnectionHandler server = new ConnectionHandler(serverSocket, reque st.sock);
128 client.start(); 130 client.start();
129 server.start(); 131 server.start();
130 132
131 // Wait for connections to close 133 // Wait for connections to close
132 client.join(); 134 client.join();
133 server.join(); 135 server.join();
134 } 136 }
135 catch (InterruptedException e) 137 catch (final InterruptedException e)
136 { 138 {
137 request.log(Server.LOG_ERROR, prefix, "Data exchange error: " + e.getMessa ge()); 139 request.log(Server.LOG_ERROR, this.prefix, "Data exchange error: " + e.get Message());
138 } 140 }
139 141
140 // Close connection 142 // Close connection
141 serverSocket.close(); 143 serverSocket.close();
142 request.log(Server.LOG_LOG, prefix, "SSL connection closed"); 144 request.log(Server.LOG_LOG, this.prefix, "SSL connection closed");
143 145
144 return true; 146 return true;
145 } 147 }
146 148
147 private class ConnectionHandler extends Thread 149 private class ConnectionHandler extends Thread
148 { 150 {
149 private InputStream in; 151 private final InputStream in;
150 private OutputStream out; 152 private final OutputStream out;
151 153
152 ConnectionHandler(Socket sin, Socket sout) throws IOException 154 ConnectionHandler(final Socket sin, final Socket sout) throws IOException
153 { 155 {
154 in = sin.getInputStream(); 156 this.in = sin.getInputStream();
155 out = sout.getOutputStream(); 157 this.out = sout.getOutputStream();
156 } 158 }
157 159
158 @Override 160 @Override
159 public void run() 161 public void run()
160 { 162 {
161 byte[] buf = new byte[4096]; 163 final byte[] buf = new byte[4096];
162 int count; 164 int count;
163 165
164 try 166 try
165 { 167 {
166 while ((count = in.read(buf, 0, buf.length)) != -1) 168 while ((count = this.in.read(buf, 0, buf.length)) != -1)
167 { 169 {
168 out.write(buf, 0, count); 170 this.out.write(buf, 0, count);
169 } 171 }
170 out.flush(); 172 this.out.flush();
171 } 173 }
172 catch (IOException e) 174 catch (final IOException e)
173 { 175 {
174 e.printStackTrace(); 176 e.printStackTrace();
175 } 177 }
176 } 178 }
177 } 179 }
178 } 180 }
OLDNEW

Powered by Google App Engine
This is Rietveld