51 class RequestServicer
implements Runnable {
53 private ChannelIO cio;
55 private static int created = 0;
57 RequestServicer(ChannelIO cio) {
61 synchronized (RequestServicer.class) {
63 if ((created % 50) == 0) {
64 System.out.println(
".");
67 System.out.print(
".");
72 private void service() throws IOException {
75 ByteBuffer rbb = receive();
78 rq = Request.parse(rbb);
79 }
catch (MalformedRequestException x) {
80 rp =
new Reply(Reply.Code.BAD_REQUEST,
81 new StringContent(x));
83 if (rp ==
null) rp = build(rq);
84 do {}
while (rp.send(cio));
85 do {}
while (!cio.shutdown());
88 }
catch (IOException x) {
89 String m = x.getMessage();
90 if (!m.equals(
"Broken pipe") &&
91 !m.equals(
"Connection reset by peer")) {
92 System.err.println(
"RequestHandler: " + x.toString());
103 }
catch (IOException e) {
117 }
catch (IOException x) {
122 ByteBuffer receive() throws IOException {
124 do {}
while (!cio.doHandshake());
127 int read = cio.read();
128 ByteBuffer bb = cio.getReadBuf();
129 if ((read < 0) || (Request.isComplete(bb))) {
136 Reply build(Request rq)
throws IOException {
139 Request.Action action = rq.action();
140 if ((action != Request.Action.GET) &&
141 (action != Request.Action.HEAD))
142 rp =
new Reply(Reply.Code.METHOD_NOT_ALLOWED,
143 new StringContent(rq.toString()));
145 rp =
new Reply(Reply.Code.OK,
146 new FileContent(rq.uri()), action);
149 }
catch (IOException x) {
151 rp =
new Reply(Reply.Code.NOT_FOUND,
152 new StringContent(x));