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

Delta Between Two Patch Sets: src/org/adblockplus/brazil/SSLConnectionHandler.java

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Left Patch Set: Created April 11, 2014, 1:31 p.m.
Right Patch Set: Even more review issues fixed. Created April 28, 2014, 10:18 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/org/adblockplus/brazil/RequestHandler.java ('k') | src/org/adblockplus/brazil/TransparentProxyHandler.java » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 handler 49 * A sample set of configuration parameters illustrating how to use this
50 * follows: 50 * handler 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(final 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 {
70 return false; 69 return false;
71 }
72 70
73 request.log(Server.LOG_LOG, this.prefix, "SSL connection to " + request.url) ; 71 request.log(Server.LOG_LOG, prefix, "SSL connection to " + request.url);
74 72
75 String host = null; 73 String host = null;
76 int port = 0; 74 int port = 0;
77 75
78 Socket serverSocket; 76 Socket serverSocket;
79 try 77 try
80 { 78 {
81 if (this.proxyHost != null) 79 if (proxyHost != null)
82 { 80 {
83 host = this.proxyHost; 81 host = proxyHost;
84 port = this.proxyPort; 82 port = proxyPort;
85 if (this.auth != null) 83 if (auth != null)
86 { 84 {
87 request.headers.add("Proxy-Authorization", this.auth); 85 request.headers.add("Proxy-Authorization", auth);
88 } 86 }
89 } 87 }
90 else 88 else
91 { 89 {
92 final int c = request.url.indexOf(':'); 90 final int c = request.url.indexOf(':');
93 host = request.url.substring(0, c); 91 host = request.url.substring(0, c);
94 port = Integer.parseInt(request.url.substring(c + 1)); 92 port = Integer.parseInt(request.url.substring(c + 1));
95 } 93 }
96 94
97 // Connect to server or upstream proxy 95 // Connect to server or upstream proxy
98 serverSocket = new Socket(); 96 serverSocket = new Socket();
99 serverSocket.setKeepAlive(true); 97 serverSocket.setKeepAlive(true);
100 serverSocket.connect(new InetSocketAddress(host, port)); 98 serverSocket.connect(new InetSocketAddress(host, port));
101 } 99 }
102 catch (final Exception e) 100 catch (final Exception e)
103 { 101 {
104 request.sendError(500, "SSL connection failure"); 102 request.sendError(500, "SSL connection failure");
105 return true; 103 return true;
106 } 104 }
107 105
108 try 106 try
109 { 107 {
110 if (this.proxyHost != null) 108 if (proxyHost != null)
111 { 109 {
112 // Forward request to upstream proxy 110 // Forward request to upstream proxy
113 final OutputStream out = serverSocket.getOutputStream(); 111 final OutputStream out = serverSocket.getOutputStream();
114 out.write((request.method + " " + request.url + " " + request.protocol + "\r\n").getBytes()); 112 out.write((request.method + " " + request.url + " " + request.protocol + "\r\n").getBytes());
115 request.headers.print(out); 113 request.headers.print(out);
116 out.write("\r\n".getBytes()); 114 out.write("\r\n".getBytes());
117 out.flush(); 115 out.flush();
118 } 116 }
119 else 117 else
120 { 118 {
121 // Send response to client 119 // Send response to client
122 final OutputStream out = request.sock.getOutputStream(); 120 final OutputStream out = request.sock.getOutputStream();
123 out.write((request.protocol + " 200 Connection established\r\n\r\n").get Bytes()); 121 out.write((request.protocol + " 200 Connection established\r\n\r\n").get Bytes());
124 out.flush(); 122 out.flush();
125 } 123 }
126 124
127 // Start bi-directional data transfer 125 // Start bi-directional data transfer
128 final ConnectionHandler client = new ConnectionHandler(request.sock, serve rSocket); 126 final ConnectionHandler client = new ConnectionHandler(request.sock, serve rSocket);
129 final ConnectionHandler server = new ConnectionHandler(serverSocket, reque st.sock); 127 final ConnectionHandler server = new ConnectionHandler(serverSocket, reque st.sock);
130 client.start(); 128 client.start();
131 server.start(); 129 server.start();
132 130
133 // Wait for connections to close 131 // Wait for connections to close
134 client.join(); 132 client.join();
135 server.join(); 133 server.join();
136 } 134 }
137 catch (final InterruptedException e) 135 catch (final InterruptedException e)
138 { 136 {
139 request.log(Server.LOG_ERROR, this.prefix, "Data exchange error: " + e.get Message()); 137 request.log(Server.LOG_ERROR, prefix, "Data exchange error: " + e.getMessa ge());
140 } 138 }
141 139
142 // Close connection 140 // Close connection
143 serverSocket.close(); 141 serverSocket.close();
144 request.log(Server.LOG_LOG, this.prefix, "SSL connection closed"); 142 request.log(Server.LOG_LOG, prefix, "SSL connection closed");
145 143
146 return true; 144 return true;
147 } 145 }
148 146
149 private class ConnectionHandler extends Thread 147 private class ConnectionHandler extends Thread
150 { 148 {
151 private final InputStream in; 149 private final InputStream in;
152 private final OutputStream out; 150 private final OutputStream out;
153 151
154 ConnectionHandler(final Socket sin, final Socket sout) throws IOException 152 ConnectionHandler(final Socket sin, final Socket sout) throws IOException
155 { 153 {
156 this.in = sin.getInputStream(); 154 in = sin.getInputStream();
157 this.out = sout.getOutputStream(); 155 out = sout.getOutputStream();
158 } 156 }
159 157
160 @Override 158 @Override
161 public void run() 159 public void run()
162 { 160 {
163 final byte[] buf = new byte[4096]; 161 final byte[] buf = new byte[4096];
164 int count; 162 int count;
165 163
166 try 164 try
167 { 165 {
168 while ((count = this.in.read(buf, 0, buf.length)) != -1) 166 while ((count = in.read(buf, 0, buf.length)) != -1)
169 { 167 {
170 this.out.write(buf, 0, count); 168 out.write(buf, 0, count);
171 } 169 }
172 this.out.flush(); 170 out.flush();
173 } 171 }
174 catch (final IOException e) 172 catch (final IOException e)
175 { 173 {
176 e.printStackTrace(); 174 e.printStackTrace();
177 } 175 }
178 } 176 }
179 } 177 }
180 } 178 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld