public void run() {
    try {
      Socket s = serverSock.accept();
      InputStream in = s.getInputStream();
      byte b[] = new byte[4096];

      // assume we read the entire http request
      // (bad assumption but okay for test case)
      int nread = in.read(b);

      // check the date format by the position of the comma
      String request = new String(b, 0, nread);
      int pos = request.indexOf("If-Modified-Since:");
      int respCode = 200;
      if (pos != -1) {
        pos += "If-Modified-Since:".length() + 4;
        if (pos < nread) {
          if (request.charAt(pos) == (char) ',') {
            respCode = 304;
          }
        }
      }

      OutputStream o = s.getOutputStream();
      if (respCode == 304) {
        o.write("HTTP/1.1 304 Not Modified".getBytes());
      } else {
        o.write("HTTP/1.1 200 OK".getBytes());
      }
      o.write((byte) '\r');
      o.write((byte) '\n');
      o.write((byte) '\r');
      o.write((byte) '\n');
      o.flush();

    } catch (Exception e) {
    }
  }
示例#2
0
 /**
  * Copies all data from in to out
  *
  * @param in the input stream
  * @param out the output stream
  * @param buffer copy buffer
  */
 public static void copyStreamsWithoutClose(InputStream in, OutputStream out, byte[] buffer)
     throws IOException {
   int b;
   while ((b = in.read(buffer)) != -1) out.write(buffer, 0, b);
 }