Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: don't swallow exceptions in LocalServerReceiver (#595)
* use modern I/O

* PrintWriter is bug prone

* declare guava dependency

* format
  • Loading branch information
elharo committed Jan 12, 2021
1 parent 3fbd4d5 commit f39faec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
5 changes: 5 additions & 0 deletions google-oauth-client-jetty/pom.xml
Expand Up @@ -92,6 +92,11 @@
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Expand Up @@ -26,9 +26,10 @@
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
Expand Down Expand Up @@ -138,9 +139,8 @@ public String getRedirectUri() throws IOException {
}

/*
*Copied from Jetty findFreePort() as referenced by: https://gist.github.com/vorburger/3429822
* Copied from Jetty findFreePort() as referenced by: https://gist.github.com/vorburger/3429822
*/

private int findOpenPort() {
try (ServerSocket socket = new ServerSocket(0)) {
socket.setReuseAddress(true);
Expand Down Expand Up @@ -315,19 +315,19 @@ private Map<String, String> queryToMap(String query) {
}

private void writeLandingHtml(HttpExchange exchange, Headers headers) throws IOException {
OutputStream os = exchange.getResponseBody();
exchange.sendResponseHeaders(HTTP_OK, 0);
headers.add("ContentType", "text/html");

PrintWriter doc = new PrintWriter(os);
doc.println("<html>");
doc.println("<head><title>OAuth 2.0 Authentication Token Received</title></head>");
doc.println("<body>");
doc.println("Received verification code. You may now close this window.");
doc.println("</body>");
doc.println("</html>");
doc.flush();
os.close();
try (OutputStream os = exchange.getResponseBody()) {
exchange.sendResponseHeaders(HTTP_OK, 0);
headers.add("ContentType", "text/html");

OutputStreamWriter doc = new OutputStreamWriter(os, StandardCharsets.UTF_8);
doc.write("<html>");
doc.write("<head><title>OAuth 2.0 Authentication Token Received</title></head>");
doc.write("<body>");
doc.write("Received verification code. You may now close this window.");
doc.write("</body>");
doc.write("</html>\n");
doc.flush();
}
}
}
}
Expand Up @@ -16,13 +16,17 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.common.io.CharStreams;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.junit.Test;

public class LocalServerReceiverTest {
Expand All @@ -33,8 +37,8 @@ public void testActualPort() throws IOException {

try {
receiver.getRedirectUri();
assertTrue(receiver.getPort() != 0);
assertTrue(receiver.getPort() != -1);
assertNotEquals(0, receiver.getPort());
assertNotEquals(-1, receiver.getPort());
} finally {
receiver.stop();
}
Expand Down Expand Up @@ -268,12 +272,12 @@ private void verifyLoginSuccess() {
}

private void verifyLoginFailure() {
assertEquals(authCode, null);
assertNull(authCode);
assertTrue(error.contains("some-error"));
}

private int responseCode;
private StringBuilder responseOutput = new StringBuilder();
private String responseOutput;
private String redirectedLandingPageUrl;

@Test
Expand Down Expand Up @@ -339,8 +343,8 @@ private void verifyRedirectedLandingPageUrl(String landingPageUrlMatch) {
private void verifyDefaultLandingPage() {
assertEquals(200, responseCode);
assertNull(redirectedLandingPageUrl);
assertTrue(responseOutput.toString().contains("<html>"));
assertTrue(responseOutput.toString().contains("</html>"));
assertTrue(responseOutput.contains("<html>"));
assertTrue(responseOutput.contains("</html>"));
}

private void sendSuccessLoginResult(String serverEndpoint) throws IOException {
Expand All @@ -362,11 +366,8 @@ private void sendLoginResult(final String serverEndpoint, final String parameter
connection.setReadTimeout(2000 /* ms */);
responseCode = connection.getResponseCode();
redirectedLandingPageUrl = connection.getHeaderField("Location");

InputStreamReader reader = new InputStreamReader(connection.getInputStream(), "UTF-8");
for (int ch = reader.read(); ch != -1; ch = reader.read()) {
responseOutput.append((char) ch);
}
Reader reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
responseOutput = CharStreams.toString(reader);
} finally {
if (connection != null) {
connection.disconnect();
Expand Down

0 comments on commit f39faec

Please sign in to comment.