2525import java .security .GeneralSecurityException ;
2626import java .security .MessageDigest ;
2727import java .util .*;
28+ import java .util .concurrent .ExecutorService ;
29+ import java .util .concurrent .Executors ;
2830
2931/**
3032 * @author Gianlu
@@ -78,7 +80,7 @@ public class ZeroconfServer implements Closeable {
7880 private final Session .Inner inner ;
7981 private final DiffieHellman keys ;
8082 private final JmDNS [] instances ;
81- private Session session ;
83+ private volatile Session session ;
8284
8385 private ZeroconfServer (Session .Inner inner , Configuration conf ) throws IOException {
8486 this .inner = inner ;
@@ -119,7 +121,7 @@ private ZeroconfServer(Session.Inner inner, Configuration conf) throws IOExcepti
119121 instances [i ].registerService (serviceInfo );
120122 atLeastOne = true ;
121123 } catch (SocketException ex ) {
122- LOGGER .warn ("Failed creating socket for " + bound [i ]);
124+ LOGGER .warn ("Failed creating socket for " + bound [i ], ex );
123125 }
124126 }
125127
@@ -274,6 +276,21 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
274276 aes .init (Cipher .DECRYPT_MODE , new SecretKeySpec (Arrays .copyOfRange (encryptionKey , 0 , 16 ), "AES" ), new IvParameterSpec (iv ));
275277 byte [] decrypted = aes .doFinal (encrypted );
276278
279+
280+ String resp = DEFAULT_SUCCESSFUL_ADD_USER .toString ();
281+ out .write (httpVersion .getBytes ());
282+ out .write (" 200 OK" .getBytes ());
283+ out .write (EOL );
284+ out .write ("Content-Length: " .getBytes ());
285+ out .write (String .valueOf (resp .length ()).getBytes ());
286+ out .write (EOL );
287+ out .flush ();
288+
289+ out .write (EOL );
290+ out .write (resp .getBytes ());
291+ out .flush ();
292+
293+
277294 try {
278295 Authentication .LoginCredentials credentials = inner .decryptBlob (username , decrypted );
279296 if (hasValidSession ()) {
@@ -289,22 +306,7 @@ private void handleAddUser(OutputStream out, Map<String, String> params, String
289306 } catch (Session .SpotifyAuthenticationException | SpotifyIrc .IrcException ex ) {
290307 LOGGER .fatal ("Failed handling connection! Going away." , ex );
291308 close ();
292- return ;
293309 }
294-
295- String resp = DEFAULT_SUCCESSFUL_ADD_USER .toString ();
296-
297- out .write (httpVersion .getBytes ());
298- out .write (" 200 OK" .getBytes ());
299- out .write (EOL );
300- out .write ("Content-Length: " .getBytes ());
301- out .write (String .valueOf (resp .length ()).getBytes ());
302- out .write (EOL );
303- out .flush ();
304-
305- out .write (EOL );
306- out .write (resp .getBytes ());
307- out .flush ();
308310 }
309311
310312 public interface Configuration {
@@ -318,6 +320,7 @@ public interface Configuration {
318320
319321 private class HttpRunner implements Runnable , Closeable {
320322 private final ServerSocket serverSocket ;
323+ private final ExecutorService executorService = Executors .newCachedThreadPool ();
321324 private volatile boolean shouldStop = false ;
322325
323326 HttpRunner (int port ) throws IOException {
@@ -328,10 +331,18 @@ private class HttpRunner implements Runnable, Closeable {
328331 @ Override
329332 public void run () {
330333 while (!shouldStop ) {
331- try (Socket socket = serverSocket .accept ()) { // We don't need this to be async
332- handle (socket );
334+ try {
335+ Socket socket = serverSocket .accept ();
336+ executorService .execute (() -> {
337+ try {
338+ handle (socket );
339+ socket .close ();
340+ } catch (IOException ex ) {
341+ LOGGER .fatal ("Failed handling request!" , ex );
342+ }
343+ });
333344 } catch (IOException ex ) {
334- LOGGER .fatal ("Failed handling request !" , ex );
345+ LOGGER .fatal ("Failed handling connection !" , ex );
335346 }
336347 }
337348 }
0 commit comments