Skip to content

Commit c4dcd98

Browse files
author
Robert Engels
committed
improved logging. simplify websocket stream handling. avoid spurious exception logging.
1 parent 7016ed1 commit c4dcd98

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

src/main/java/robaho/net/httpserver/ExchangeImpl.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ public InputStream getRequestBody() {
162162
return uis;
163163
}
164164
if (websocket) {
165-
uis_orig = new UndefLengthInputStream(this, ris);
166-
uis = uis_orig;
165+
// websocket connection cannot be re-used
166+
uis = ris;
167167
} else if (reqContentLen == -1L) {
168168
uis_orig = new ChunkedInputStream(this, ris);
169169
uis = uis_orig;
@@ -266,7 +266,11 @@ public void sendResponseHeaders(int rCode, long contentLen)
266266
o.setWrappedStream(new FixedLengthOutputStream(this, ros, contentLen));
267267
} else { /* not a HEAD request or 304 response */
268268
if (contentLen == 0) {
269-
if (http10 || websocket) {
269+
if (websocket) {
270+
o.setWrappedStream(ros);
271+
close = true;
272+
}
273+
else if (http10) {
270274
o.setWrappedStream(new UndefLengthOutputStream(this, ros));
271275
close = true;
272276
} else {

src/main/java/robaho/net/httpserver/LeftOverInputStream.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.FilterInputStream;
2929
import java.io.IOException;
3030
import java.io.InputStream;
31+
import java.io.EOFException;
3132

3233
/**
3334
* a (filter) input stream which can tell us if bytes are "left over"
@@ -71,7 +72,7 @@ public boolean isClosed() {
7172

7273
public synchronized int read() throws IOException {
7374
if (closed) {
74-
throw new IOException("Stream is closed");
75+
throw new EOFException("Stream is closed");
7576
}
7677
int c = readImpl(one, 0, 1);
7778
if (c == -1 || c == 0) {
@@ -83,7 +84,7 @@ public synchronized int read() throws IOException {
8384

8485
public synchronized int read(byte[] b, int off, int len) throws IOException {
8586
if (closed) {
86-
throw new IOException("Stream is closed");
87+
throw new EOFException("Stream is closed");
8788
}
8889
return readImpl(b, off, len);
8990
}

src/main/java/robaho/net/httpserver/ServerImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public void run() {
366366
this.rawin = connection.getInputStream();
367367
this.rawout = connection.getOutputStream();
368368

369-
logger.log(Level.TRACE, "exchange started");
369+
logger.log(Level.TRACE, "exchange started "+connection.toString());
370370

371371
while (true) {
372372
try {
@@ -399,7 +399,7 @@ public void run() {
399399
throw t;
400400
}
401401
}
402-
logger.log(Level.TRACE, "exchange finished");
402+
logger.log(Level.TRACE, "exchange finished "+connection.toString());
403403
}
404404

405405
private void runPerRequest() throws IOException {
@@ -634,9 +634,8 @@ void logReply(int code, String requestStr, String text) {
634634
} else {
635635
r = requestStr;
636636
}
637-
String message = r + " [" + code + " "
638-
+ Code.msg(code) + "] (" + text + ")";
639-
logger.log(Level.DEBUG, message);
637+
logger.log(Level.DEBUG, "reply "+ r + " [" + code + " "
638+
+ Code.msg(code) + "] (" + text + ")");
640639
}
641640

642641
void delay() {

src/main/java/robaho/net/httpserver/UndefLengthOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void close() throws IOException {
7171
closed = true;
7272
flush();
7373
LeftOverInputStream is = t.getOriginalInputStream();
74-
if (!is.isClosed()) {
74+
if (is!=null &&!is.isClosed()) {
7575
try {
7676
is.close();
7777
} catch (IOException e) {

src/main/java/robaho/net/httpserver/websockets/WebSocket.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
*/
3535

3636
import java.io.IOException;
37+
import java.io.EOFException;
3738
import java.io.InputStream;
3839
import java.io.OutputStream;
40+
import java.net.URI;
3941
import java.nio.charset.CharacterCodingException;
4042
import java.util.LinkedList;
4143
import java.util.List;
@@ -57,11 +59,17 @@ public abstract class WebSocket {
5759

5860
private final List<WebSocketFrame> continuousFrames = new LinkedList<WebSocketFrame>();
5961

60-
private State state = State.UNCONNECTED;
62+
private volatile State state = State.UNCONNECTED;
6163

6264
private Lock lock = new ReentrantLock();
6365

66+
private final URI uri;
67+
6468
protected WebSocket(HttpExchange exchange) {
69+
this.uri = exchange.getRequestURI();
70+
71+
logger.info("connecting websocket "+uri);
72+
6573
this.state = State.CONNECTING;
6674
this.in = exchange.getRequestBody();
6775
this.out = exchange.getResponseBody();
@@ -81,7 +89,9 @@ protected void onOpen() throws WebSocketException {
8189
protected abstract void onPong(WebSocketFrame pong) throws WebSocketException;
8290

8391
protected void onException(IOException exception) {
84-
logger.log(Level.FINER, "exception on websocket", exception);
92+
if(state!=State.CLOSING && state!=State.CLOSED) {
93+
logger.log(Level.FINER, "exception on websocket", exception);
94+
}
8595
}
8696

8797
protected void onFrameReceived(WebSocketFrame frame) {
@@ -100,6 +110,8 @@ protected void onFrameSent(WebSocketFrame frame) {
100110
}
101111

102112
public void close(CloseCode code, String reason, boolean initiatedByRemote) throws IOException {
113+
logger.info("closing websocket "+uri);
114+
103115
State oldState = this.state;
104116
this.state = State.CLOSING;
105117
if (oldState == State.OPEN) {
@@ -116,15 +128,13 @@ void doClose(CloseCode code, String reason, boolean initiatedByRemote) {
116128
if (this.in != null) {
117129
try {
118130
this.in.close();
119-
} catch (IOException e) {
120-
logger.log(Level.FINE, "close failed", e);
131+
} catch (IOException expected) {
121132
}
122133
}
123134
if (this.out != null) {
124135
try {
125136
this.out.close();
126-
} catch (IOException e) {
127-
logger.log(Level.FINE, "close failed", e);
137+
} catch (IOException expected) {
128138
}
129139
}
130140
this.state = State.CLOSED;
@@ -140,6 +150,7 @@ private void handleCloseFrame(WebSocketFrame frame) throws IOException {
140150
code = ((CloseFrame) frame).getCloseCode();
141151
reason = ((CloseFrame) frame).getCloseReason();
142152
}
153+
logger.finest("handleCloseFrame: "+uri+", code="+code+", reason="+reason+", state "+this.state);
143154
if (this.state == State.CLOSING) {
144155
// Answer for my requested close
145156
doClose(code, reason, false);
@@ -204,21 +215,27 @@ public void ping(byte[] payload) throws IOException {
204215
void readWebsocket() {
205216
try {
206217
state = State.OPEN;
218+
logger.fine("websocket open "+uri);
207219
onOpen();
208220
while (this.state == State.OPEN) {
209221
handleWebsocketFrame(WebSocketFrame.read(in));
210222
}
223+
} catch (EOFException e) {
224+
onException(e);
225+
doClose(CloseCode.AbnormalClosure, e.toString(), false);
211226
} catch (CharacterCodingException e) {
212227
onException(e);
213228
doClose(CloseCode.InvalidFramePayloadData, e.toString(), false);
214229
} catch (IOException e) {
215230
onException(e);
216231
if (e instanceof WebSocketException wse) {
217232
doClose(wse.getCode(), wse.getReason(), false);
233+
} else {
234+
doClose(CloseCode.AbnormalClosure, e.toString(), false);
218235
}
219236
} finally {
220237
doClose(CloseCode.InternalServerError, "Handler terminated without closing the connection.", false);
221-
logger.info("readWebsocket() exiting");
238+
logger.finest("readWebsocket() exiting "+uri);
222239
}
223240
}
224241

0 commit comments

Comments
 (0)