0

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
AustinL
  • 23
  • 1
  • 6
  • Tell us more about your web server. Are you using an existing framework / platform / library? Are you writing networking code by hand? Do you have any code written so far? – John Kugelman Apr 21 '20 at 17:26
  • I am going to build a web server from scratch. I am thinking about how to get the if-modified-since header from the client. No existing framework I am using. I am new to java. I don't know much about those library/ platform/ framework. Thanks. – AustinL Apr 21 '20 at 17:36

1 Answers1

1

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").

John Kugelman
  • 349,597
  • 67
  • 533
  • 578