|
25 | 25 | package de.bluecolored.bluemap.common.web.http; |
26 | 26 |
|
27 | 27 | import de.bluecolored.bluemap.core.logger.Logger; |
| 28 | +import lombok.RequiredArgsConstructor; |
28 | 29 |
|
| 30 | +import java.io.BufferedInputStream; |
| 31 | +import java.io.BufferedOutputStream; |
| 32 | +import java.io.EOFException; |
29 | 33 | import java.io.IOException; |
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; |
| 34 | +import java.net.Socket; |
| 35 | +import java.net.SocketTimeoutException; |
39 | 36 |
|
40 | | -public class HttpConnection implements SelectionConsumer { |
| 37 | +public class HttpConnection implements Runnable { |
41 | 38 |
|
| 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; |
47 | 43 |
|
48 | | - public HttpConnection(HttpRequestHandler requestHandler) { |
49 | | - this(requestHandler, Runnable::run); //run synchronously |
50 | | - } |
51 | | - |
52 | | - public HttpConnection(HttpRequestHandler requestHandler, Executor responseHandlerExecutor) { |
| 44 | + public HttpConnection(Socket socket, HttpRequestHandler requestHandler) throws IOException { |
| 45 | + this.socket = socket; |
53 | 46 | this.requestHandler = requestHandler; |
54 | | - this.responseHandlerExecutor = responseHandlerExecutor; |
55 | | - } |
56 | | - |
57 | | - @Override |
58 | | - public void accept(SelectionKey selectionKey) { |
59 | | - if (!selectionKey.isValid()) return; |
60 | 47 |
|
61 | | - SelectableChannel selChannel = selectionKey.channel(); |
62 | | - |
63 | | - if (!(selChannel instanceof SocketChannel)) return; |
64 | | - SocketChannel channel = (SocketChannel) selChannel; |
| 48 | + this.requestIn = new HttpRequestInputStream(new BufferedInputStream(socket.getInputStream()), socket.getInetAddress()); |
| 49 | + this.responseOut = new HttpResponseOutputStream(new BufferedOutputStream(socket.getOutputStream())); |
| 50 | + } |
65 | 51 |
|
| 52 | + public void run() { |
66 | 53 | try { |
| 54 | + while (socket.isConnected() && !socket.isClosed() && !socket.isInputShutdown() && !socket.isOutputShutdown()) { |
| 55 | + HttpRequest request = requestIn.read(); |
| 56 | + if (request == null) continue; |
67 | 57 |
|
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); |
75 | | - } |
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; |
| 58 | + try (HttpResponse response = requestHandler.handle(request)) { |
| 59 | + responseOut.write(response); |
| 60 | + } |
114 | 61 | } |
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 | | - |
| 62 | + } catch (EOFException | SocketTimeoutException ignore) { |
| 63 | + // ignore known exceptions that happen when browsers or us close the connection |
123 | 64 | } 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) { |
| 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); |
| 70 | + } |
| 71 | + } catch (Exception e) { |
| 72 | + Logger.global.logDebug("Exception in HttpConnection: " + e); |
| 73 | + } finally { |
132 | 74 | try { |
133 | | - response.close(); |
134 | | - } catch (IOException e2) { |
135 | | - Logger.global.logWarning("Failed to close response: " + e2); |
| 75 | + socket.close(); |
| 76 | + } catch (IOException e) { |
| 77 | + Logger.global.logDebug("Exception closing HttpConnection: " + e); |
136 | 78 | } |
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); |
156 | 79 | } |
157 | 80 | } |
158 | 81 |
|
|
0 commit comments