| 227 | |
| 228 | |
| 229 | void handleClient() throws IOException { |
| 230 | InputStream is = new BufferedInputStream(s.getInputStream()); |
| 231 | PrintStream ps = new PrintStream(s.getOutputStream()); |
| 232 | // we will only block in read for this many milliseconds |
| 233 | // before we fail with java.io.InterruptedIOException, |
| 234 | // at which point we will abandon the connection. |
| 235 | s.setSoTimeout(WebServer.timeout); |
| 236 | s.setTcpNoDelay(true); |
| 237 | // zero out the buffer from last time |
| 238 | for (int i = 0; i < BUF_SIZE; i++) { |
| 239 | buf[i] = 0; |
| 240 | } |
| 241 | try { |
| 242 | // We only support HTTP GET/HEAD, and don't support any fancy HTTP |
| 243 | // options, so we're only interested really in the first line. |
| 244 | int nread = 0, r = 0; |
| 245 | |
| 246 | outerloop: |
| 247 | while (nread < BUF_SIZE) { |
| 248 | r = is.read(buf, nread, BUF_SIZE - nread); |
| 249 | if (r == -1) { |
| 250 | return; // EOF |
| 251 | } |
| 252 | int i = nread; |
| 253 | nread += r; |
| 254 | for (; i < nread; i++) { |
| 255 | if (buf[i] == (byte)'\n' || buf[i] == (byte)'\r') { |
| 256 | break outerloop; // read one line |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* are we doing a GET or just a HEAD */ |
| 262 | boolean doingGet; |
| 263 | /* beginning of file name */ |
| 264 | int index; |
| 265 | if (buf[0] == (byte)'G' && |
| 266 | buf[1] == (byte)'E' && |
| 267 | buf[2] == (byte)'T' && |
| 268 | buf[3] == (byte)' ') { |
| 269 | doingGet = true; |
| 270 | index = 4; |
| 271 | } else if (buf[0] == (byte)'H' && |
| 272 | buf[1] == (byte)'E' && |
| 273 | buf[2] == (byte)'A' && |
| 274 | buf[3] == (byte)'D' && |
| 275 | buf[4] == (byte)' ') { |
| 276 | doingGet = false; |
| 277 | index = 5; |
| 278 | } else { |
| 279 | /* we don't support this method */ |
| 280 | ps.print("HTTP/1.0 " + HTTP_BAD_METHOD + |
| 281 | " unsupported method type: "); |
| 282 | ps.write(buf, 0, 5); |
| 283 | ps.write(EOL); |
| 284 | ps.flush(); |
| 285 | s.close(); |
| 286 | return; |