|
25 | 25 | package de.bluecolored.bluemap.common.web.http; |
26 | 26 |
|
27 | 27 | import de.bluecolored.bluemap.core.logger.Logger; |
28 | | -import lombok.RequiredArgsConstructor; |
29 | 28 |
|
30 | | -import java.io.BufferedInputStream; |
31 | | -import java.io.BufferedOutputStream; |
32 | | -import java.io.EOFException; |
33 | 29 | import java.io.IOException; |
34 | | -import java.net.Socket; |
35 | | -import java.net.SocketTimeoutException; |
| 30 | +import java.net.InetAddress; |
| 31 | +import java.net.InetSocketAddress; |
| 32 | +import java.net.SocketAddress; |
| 33 | +import java.nio.channels.Channel; |
| 34 | +import java.nio.channels.SelectableChannel; |
| 35 | +import java.nio.channels.SelectionKey; |
| 36 | +import java.nio.channels.SocketChannel; |
| 37 | +import java.util.concurrent.CompletableFuture; |
| 38 | +import java.util.concurrent.Executor; |
36 | 39 |
|
37 | | -public class HttpConnection implements Runnable { |
| 40 | +public class HttpConnection implements SelectionConsumer { |
38 | 41 |
|
39 | | - private final Socket socket; |
40 | | - private final HttpRequestInputStream requestIn; |
41 | | - private final HttpResponseOutputStream responseOut; |
42 | 42 | private final HttpRequestHandler requestHandler; |
| 43 | + private final Executor responseHandlerExecutor; |
| 44 | + private HttpRequest request; |
| 45 | + private CompletableFuture<HttpResponse> futureResponse; |
| 46 | + private HttpResponse response; |
43 | 47 |
|
44 | | - public HttpConnection(Socket socket, HttpRequestHandler requestHandler) throws IOException { |
45 | | - this.socket = socket; |
46 | | - this.requestHandler = requestHandler; |
| 48 | + public HttpConnection(HttpRequestHandler requestHandler) { |
| 49 | + this(requestHandler, Runnable::run); //run synchronously |
| 50 | + } |
47 | 51 |
|
48 | | - this.requestIn = new HttpRequestInputStream(new BufferedInputStream(socket.getInputStream()), socket.getInetAddress()); |
49 | | - this.responseOut = new HttpResponseOutputStream(new BufferedOutputStream(socket.getOutputStream())); |
| 52 | + public HttpConnection(HttpRequestHandler requestHandler, Executor responseHandlerExecutor) { |
| 53 | + this.requestHandler = requestHandler; |
| 54 | + this.responseHandlerExecutor = responseHandlerExecutor; |
50 | 55 | } |
51 | 56 |
|
52 | | - public void run() { |
| 57 | + @Override |
| 58 | + public void accept(SelectionKey selectionKey) { |
| 59 | + if (!selectionKey.isValid()) return; |
| 60 | + |
| 61 | + SelectableChannel selChannel = selectionKey.channel(); |
| 62 | + |
| 63 | + if (!(selChannel instanceof SocketChannel)) return; |
| 64 | + SocketChannel channel = (SocketChannel) selChannel; |
| 65 | + |
53 | 66 | try { |
54 | | - while (socket.isConnected() && !socket.isClosed() && !socket.isInputShutdown() && !socket.isOutputShutdown()) { |
55 | | - HttpRequest request = requestIn.read(); |
56 | | - if (request == null) continue; |
57 | 67 |
|
58 | | - try (HttpResponse response = requestHandler.handle(request)) { |
59 | | - responseOut.write(response); |
60 | | - } |
| 68 | + if (request == null) { |
| 69 | + SocketAddress remote = channel.getRemoteAddress(); |
| 70 | + InetAddress remoteInet = null; |
| 71 | + if (remote instanceof InetSocketAddress) |
| 72 | + remoteInet = ((InetSocketAddress) remote).getAddress(); |
| 73 | + |
| 74 | + request = new HttpRequest(remoteInet); |
61 | 75 | } |
62 | | - } catch (EOFException | SocketTimeoutException ignore) { |
63 | | - // ignore known exceptions that happen when browsers or us close the connection |
64 | | - } catch (IOException e) { |
65 | | - if ( // ignore known exceptions that happen when browsers close the connection |
66 | | - e.getMessage() == null || |
67 | | - !e.getMessage().equals("Broken pipe") |
68 | | - ) { |
69 | | - Logger.global.logDebug("Exception in HttpConnection: " + e); |
| 76 | + |
| 77 | + // receive request |
| 78 | + if (!request.write(channel)) { |
| 79 | + if (!selectionKey.isValid()) return; |
| 80 | + selectionKey.interestOps(SelectionKey.OP_READ); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // process request |
| 85 | + if (futureResponse == null) { |
| 86 | + futureResponse = CompletableFuture.supplyAsync( |
| 87 | + () -> requestHandler.handle(request), |
| 88 | + responseHandlerExecutor |
| 89 | + ); |
| 90 | + futureResponse.handle((response, error) -> { |
| 91 | + if (error != null) { |
| 92 | + Logger.global.logError("Unexpected error handling request", error); |
| 93 | + response = new HttpResponse(HttpStatusCode.INTERNAL_SERVER_ERROR); |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + response.read(channel); // do an initial read to trigger response sending intent |
| 98 | + this.response = response; |
| 99 | + } catch (IOException e) { |
| 100 | + handleIOException(channel, e); |
| 101 | + } |
| 102 | + |
| 103 | + return null; |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + if (response == null) return; |
| 108 | + if (!selectionKey.isValid()) return; |
| 109 | + |
| 110 | + // send response |
| 111 | + if (!response.read(channel)){ |
| 112 | + selectionKey.interestOps(SelectionKey.OP_WRITE); |
| 113 | + return; |
70 | 114 | } |
71 | | - } catch (Exception e) { |
72 | | - Logger.global.logDebug("Exception in HttpConnection: " + e); |
73 | | - } finally { |
| 115 | + |
| 116 | + // reset to accept new request |
| 117 | + request.clear(); |
| 118 | + response.close(); |
| 119 | + futureResponse = null; |
| 120 | + response = null; |
| 121 | + selectionKey.interestOps(SelectionKey.OP_READ); |
| 122 | + |
| 123 | + } catch (IOException e) { |
| 124 | + handleIOException(channel, e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void handleIOException(Channel channel, IOException e) { |
| 129 | + request.clear(); |
| 130 | + |
| 131 | + if (response != null) { |
74 | 132 | try { |
75 | | - socket.close(); |
76 | | - } catch (IOException e) { |
77 | | - Logger.global.logDebug("Exception closing HttpConnection: " + e); |
| 133 | + response.close(); |
| 134 | + } catch (IOException e2) { |
| 135 | + Logger.global.logWarning("Failed to close response: " + e2); |
78 | 136 | } |
| 137 | + response = null; |
| 138 | + } |
| 139 | + |
| 140 | + if (futureResponse != null) { |
| 141 | + futureResponse.thenAccept(response -> { |
| 142 | + try { |
| 143 | + response.close(); |
| 144 | + } catch (IOException e2) { |
| 145 | + Logger.global.logWarning("Failed to close response: " + e2); |
| 146 | + } |
| 147 | + }); |
| 148 | + futureResponse = null; |
| 149 | + } |
| 150 | + |
| 151 | + Logger.global.logDebug("Failed to process selection: " + e); |
| 152 | + try { |
| 153 | + channel.close(); |
| 154 | + } catch (IOException e2) { |
| 155 | + Logger.global.logWarning("Failed to close channel" + e2); |
79 | 156 | } |
80 | 157 | } |
81 | 158 |
|
|
0 commit comments