Skip to content

Commit 1fb01ef

Browse files
committed
AdHocHttpServer fixed
1 parent 765b8c3 commit 1fb01ef

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/testing/AdHocHttpServer.java

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,81 @@
11
package fi.helsinki.cs.tmc.testing;
22

33
import java.io.IOException;
4-
import java.io.InterruptedIOException;
54
import java.net.InetSocketAddress;
65
import java.net.ServerSocket;
76
import java.net.Socket;
87
import java.net.SocketException;
98
import java.util.concurrent.Semaphore;
109
import org.apache.http.ConnectionClosedException;
1110
import org.apache.http.HttpResponseInterceptor;
12-
import org.apache.http.impl.DefaultConnectionReuseStrategy;
13-
import org.apache.http.impl.DefaultHttpResponseFactory;
14-
import org.apache.http.impl.DefaultHttpServerConnection;
15-
import org.apache.http.params.SyncBasicHttpParams;
11+
import org.apache.http.impl.DefaultBHttpServerConnection;
1612
import org.apache.http.protocol.BasicHttpContext;
1713
import org.apache.http.protocol.HttpContext;
1814
import org.apache.http.protocol.HttpProcessor;
1915
import org.apache.http.protocol.HttpRequestHandler;
20-
import org.apache.http.protocol.HttpRequestHandlerRegistry;
21-
import org.apache.http.protocol.HttpRequestHandlerResolver;
16+
import org.apache.http.protocol.HttpRequestHandlerMapper;
2217
import org.apache.http.protocol.HttpService;
2318
import org.apache.http.protocol.ImmutableHttpProcessor;
2419
import org.apache.http.protocol.ResponseConnControl;
2520
import org.apache.http.protocol.ResponseContent;
2621
import org.apache.http.protocol.ResponseDate;
2722
import org.apache.http.protocol.ResponseServer;
23+
import org.apache.http.protocol.UriHttpRequestHandlerMapper;
2824
import org.openide.util.Exceptions;
2925

30-
/**
31-
* A HTTP server running on a random port in a single background thread.
32-
*
33-
* An exception in a request handler will cause the server to shut down.
34-
* The exception will be propagated when {@link #stop()} is called.
35-
*/
3626
public class AdHocHttpServer {
37-
private HttpRequestHandlerResolver handlers;
38-
27+
28+
/**
29+
* A HTTP server running on a random port in a single background thread.
30+
*
31+
* An exception in a request handler will cause the server to shut down. The
32+
* exception will be propagated when {@link #stop()} is called.
33+
*/
34+
private HttpRequestHandlerMapper handlers;
35+
3936
private ServerSocket serverSocket;
4037
private HttpService httpService;
4138
private Thread thread;
42-
39+
4340
private Exception inThreadException; // Set by thread, read in stop()
4441
private Semaphore requestCounter = new Semaphore(0);
45-
42+
4643
private volatile boolean debugEnabled = false;
4744

4845
public AdHocHttpServer() {
49-
this.handlers = new HttpRequestHandlerRegistry();
46+
this.handlers = new UriHttpRequestHandlerMapper();
5047
}
51-
48+
5249
public void enableDebug() {
5350
debugEnabled = true;
5451
}
55-
52+
5653
public void setHandler(HttpRequestHandler handler) {
57-
HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
54+
UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper();
5855
registry.register("*", handler);
5956
this.handlers = registry;
6057
}
61-
58+
6259
public boolean isStarted() {
6360
return thread != null;
6461
}
65-
62+
6663
public int getPort() {
6764
if (!isStarted()) {
6865
throw new IllegalStateException("Server must be started first");
6966
}
7067
return serverSocket.getLocalPort();
7168
}
72-
69+
7370
public String getBaseUrl() {
7471
return "http://localhost:" + getPort();
7572
}
76-
73+
7774
public synchronized void start() throws IOException {
7875
if (isStarted()) {
7976
throw new IllegalStateException("Already started");
8077
}
81-
78+
8279
setupServerSocket();
8380
setupHttpClientIncantations();
8481
startThread();
@@ -91,22 +88,18 @@ private void setupServerSocket() throws IOException {
9188
serverSocket.setReceiveBufferSize(64);
9289
serverSocket.bind(addr);
9390
}
94-
91+
9592
private void setupHttpClientIncantations() {
96-
HttpProcessor proc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] {
93+
HttpProcessor proc = new ImmutableHttpProcessor(new HttpResponseInterceptor[]{
9794
new ResponseDate(),
9895
new ResponseServer(),
9996
new ResponseContent(),
10097
new ResponseConnControl()
10198
});
102-
99+
103100
httpService = new HttpService(
104101
proc,
105-
new DefaultConnectionReuseStrategy(),
106-
new DefaultHttpResponseFactory(),
107-
handlers,
108-
new SyncBasicHttpParams()
109-
);
102+
handlers);
110103
}
111104

112105
private void startThread() {
@@ -153,8 +146,8 @@ public void run() {
153146
}
154147

155148
debug("Got connection");
156-
DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
157-
conn.bind(socket, httpService.getParams());
149+
DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(5000);
150+
conn.bind(socket);
158151
HttpContext ctx = new BasicHttpContext(null);
159152
while (!Thread.currentThread().isInterrupted() && conn.isOpen()) {
160153
httpService.handleRequest(conn, ctx);
@@ -163,9 +156,6 @@ public void run() {
163156
debug("Connection processed");
164157
} catch (ConnectionClosedException ex) {
165158
// No problem I think
166-
} catch (InterruptedIOException ex) {
167-
debug("InterruptedIOException: " + ex);
168-
break;
169159
} catch (Exception ex) {
170160
inThreadException = ex;
171161
debug("Exception: " + ex);

0 commit comments

Comments
 (0)