J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of Sun Microsystems nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | import java.io.*; |
| 33 | import java.nio.*; |
| 34 | import java.nio.channels.*; |
| 35 | |
| 36 | /** |
| 37 | * Primary driver class used by non-blocking Servers to receive, |
| 38 | * prepare, send, and shutdown requests. |
| 39 | * |
| 40 | * @author Mark Reinhold |
| 41 | * @author Brad R. Wetmore |
| 42 | */ |
| 43 | class RequestHandler implements Handler { |
| 44 | |
| 45 | private ChannelIO cio; |
| 46 | private ByteBuffer rbb = null; |
| 47 | |
| 48 | private boolean requestReceived = false; |
| 49 | private Request request = null; |
| 50 | private Reply reply = null; |
| 51 | |
| 52 | private static int created = 0; |
| 53 | |
| 54 | RequestHandler(ChannelIO cio) { |
| 55 | this.cio = cio; |
| 56 | |
| 57 | // Simple heartbeat to let user know we're alive. |
| 58 | synchronized (RequestHandler.class) { |
| 59 | created++; |
| 60 | if ((created % 50) == 0) { |
| 61 | System.out.println("."); |
| 62 | created = 0; |
| 63 | } else { |
| 64 | System.out.print("."); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Returns true when request is complete |
| 70 | // May expand rbb if more room required |
| 71 | // |
| 72 | private boolean receive(SelectionKey sk) throws IOException { |
| 73 | ByteBuffer tmp = null; |
| 74 | |
| 75 | if (requestReceived) { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | if (!cio.doHandshake(sk)) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | if ((cio.read() < 0) || Request.isComplete(cio.getReadBuf())) { |
| 84 | rbb = cio.getReadBuf(); |
| 85 | return (requestReceived = true); |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | // When parse is successfull, saves request and returns true |
| 91 | // |
| 92 | private boolean parse() throws IOException { |
| 93 | try { |
| 94 | request = Request.parse(rbb); |
| 95 | return true; |
| 96 | } catch (MalformedRequestException x) { |
| 97 | reply = new Reply(Reply.Code.BAD_REQUEST, |
| 98 | new StringContent(x)); |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // Ensures that reply field is non-null |
| 104 | // |
| 105 | private void build() throws IOException { |
| 106 | Request.Action action = request.action(); |
| 107 | if ((action != Request.Action.GET) && |
| 108 | (action != Request.Action.HEAD)) { |
| 109 | reply = new Reply(Reply.Code.METHOD_NOT_ALLOWED, |
| 110 | new StringContent(request.toString())); |
| 111 | } |
| 112 | reply = new Reply(Reply.Code.OK, |
| 113 | new FileContent(request.uri()), action); |
| 114 | } |
| 115 | |
| 116 | public void handle(SelectionKey sk) throws IOException { |
| 117 | try { |
| 118 | |
| 119 | if (request == null) { |
| 120 | if (!receive(sk)) |
| 121 | return; |
| 122 | rbb.flip(); |
| 123 | if (parse()) |
| 124 | build(); |
| 125 | try { |
| 126 | reply.prepare(); |
| 127 | } catch (IOException x) { |
| 128 | reply.release(); |
| 129 | reply = new Reply(Reply.Code.NOT_FOUND, |
| 130 | new StringContent(x)); |
| 131 | reply.prepare(); |
| 132 | } |
| 133 | if (send()) { |
| 134 | // More bytes remain to be written |
| 135 | sk.interestOps(SelectionKey.OP_WRITE); |
| 136 | } else { |
| 137 | // Reply completely written; we're done |
| 138 | if (cio.shutdown()) { |
| 139 | cio.close(); |
| 140 | reply.release(); |
| 141 | } |
| 142 | } |
| 143 | } else { |
| 144 | if (!send()) { // Should be rp.send() |
| 145 | if (cio.shutdown()) { |
| 146 | cio.close(); |
| 147 | reply.release(); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } catch (IOException x) { |
| 152 | String m = x.getMessage(); |
| 153 | if (!m.equals("Broken pipe") && |
| 154 | !m.equals("Connection reset by peer")) { |
| 155 | System.err.println("RequestHandler: " + x.toString()); |
| 156 | } |
| 157 | |
| 158 | try { |
| 159 | /* |
| 160 | * We had a failure here, so we'll try to be nice |
| 161 | * before closing down and send off a close_notify, |
| 162 | * but if we can't get the message off with one try, |
| 163 | * we'll just shutdown. |
| 164 | */ |
| 165 | cio.shutdown(); |
| 166 | } catch (IOException e) { |
| 167 | // ignore |
| 168 | } |
| 169 | |
| 170 | cio.close(); |
| 171 | if (reply != null) { |
| 172 | reply.release(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | private boolean send() throws IOException { |
| 179 | try { |
| 180 | return reply.send(cio); |
| 181 | } catch (IOException x) { |
| 182 | if (x.getMessage().startsWith("Resource temporarily")) { |
| 183 | System.err.println("## RTA"); |
| 184 | return true; |
| 185 | } |
| 186 | throw x; |
| 187 | } |
| 188 | } |
| 189 | } |