AustinL
AustinL

Reputation: 23

Java web server compare if-modified-since with last modified

I am trying to make a Java web server that will compare the if-modified-since with the file's last modified. However, I don't know how to get the if-modified-since header from the client and do the comparison.

Upvotes: 0

Views: 353

Answers (1)

John Kugelman
John Kugelman

Reputation: 362087

I wouldn't jump right into trying to handle a particular header. If you're writing a web server from scratch then you should write a generic HTTP parser that can handle every part of an HTTP request:

  • Request line
    • Request method (GET, POST, etc.)
    • URL
    • HTTP version
  • Zero or more headers of the form Name: Value
  • A blank line
  • Message body

You could, for instance, build up a class like:

class HttpRequest {
    String method;
    URL url;
    String httpVersion;
    Map<String, String> headers;
    byte[] body;
}

Since header names are case insensitive I'd suggest using map with String.CASE_INSENSITIVE_ORDER.

Once you can parse all headers than looking for a particular header will be a simple task. If you had the class above it'd be as easy as looking up headers.get("If-Modified-Since").

Upvotes: 1

Related Questions