comparison src/goodjava/webserver/handlers/FileHandler.java @ 1607:fa066aaa068c

nginx caching
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 30 Apr 2021 20:23:28 -0600
parents 8a7519dee55c
children
comparison
equal deleted inserted replaced
1606:7c7f28c724e8 1607:fa066aaa068c
42 Response response = new Response(); 42 Response response = new Response();
43 43
44 DateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); 44 DateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
45 fmt.setTimeZone(TimeZone.getTimeZone("GMT")); 45 fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
46 String lastModified = fmt.format(new Date(file.lastModified())); 46 String lastModified = fmt.format(new Date(file.lastModified()));
47 String ifMod = (String)request.headers.get("if-modified-since"); 47 String ifMod = (String)request.headers.get("If-Modified-Since");
48 if( ifMod != null ) { 48 if( ifMod != null ) {
49 try { 49 try {
50 Date ifModDate = fmt.parse(ifMod); 50 Date ifModDate = fmt.parse(ifMod);
51 if( ifModDate != null && ifModDate.getTime() >= fmt.parse(lastModified).getTime() ) { 51 if( ifModDate != null && ifModDate.getTime() >= fmt.parse(lastModified).getTime() ) {
52 response.status = Status.NOT_MODIFIED; 52 response.status = Status.NOT_MODIFIED;
53 return response; 53 return response;
54 } 54 }
55 55
56 } catch(ParseException e) {} 56 } catch(ParseException e) {}
57 } 57 }
58 response.headers.put("last-modified",lastModified); 58 response.headers.put("Last-Modified",lastModified);
59 59
60 response.body = new Response.Body( file.length(), new FileInputStream(file) ); 60 response.body = new Response.Body( file.length(), new FileInputStream(file) );
61 return response; 61 return response;
62 } 62 }
63 return null; 63 return null;