01: package pygmy.core;
02:
03: import java.io.OutputStream;
04: import java.io.InputStream;
05: import java.io.IOException;
06: import java.io.EOFException;
07: import java.util.logging.Level;
08: import java.util.logging.Logger;
09: import java.net.HttpURLConnection;
10: import java.net.Socket;
11:
12: public class NonBlockingRunnable implements Runnable {
13: private static final Logger log = Logger
14: .getLogger(NonBlockingRunnable.class.getName());
15:
16: Server server;
17: Socket socket;
18: InputStream is;
19: OutputStream os;
20:
21: public NonBlockingRunnable(Server server, Socket aSocket,
22: InputStream anIn, OutputStream anOut) {
23: this .server = server;
24: socket = aSocket;
25: is = anIn;
26: os = anOut;
27: }
28:
29: public void run() {
30: try {
31: boolean next = false;
32: do {
33: HttpRequest request = new HttpRequest("http", socket,
34: server.getConfig());
35: next = request.readRequest(is);
36: if (next) {
37: HttpResponse response = new HttpResponse(request,
38: os, server.getResponseListeners());
39: if (!server.post(request, response)) {
40: response.sendError(
41: HttpURLConnection.HTTP_NOT_FOUND,
42: " was not found on this server.");
43: }
44: next = response.isKeepAlive();
45: if (!next) {
46: response.addHeader("Connection", "close");
47: }
48: response.commitResponse();
49: }
50: } while (next);
51: } catch (EOFException eof) {
52: log.finer("Closing connection");
53: // do nothing
54: } catch (IOException e) {
55: log.log(Level.SEVERE, "IOException", e);
56: } catch (Exception e) {
57: log.log(Level.WARNING, "Handler threw an exception.", e);
58: } finally {
59: try {
60: is.close();
61: } catch (IOException e) {
62: }
63:
64: try {
65: os.close();
66: } catch (IOException e) {
67: }
68: }
69: }
70: }
|